如何在Vagrant中修改php.ini?需对所有虚拟机生效
Hey there! Let's sort out how to update those PHP.ini settings across all your Vagrant VMs once and for all. I'll break this down into simple, actionable steps, and also fix the issues with the commands you tried earlier.
1. First: Fix the Single VM Configuration (Why Your Previous Commands Failed)
Your earlier attempts had two main issues:
- Using
exportin the echo commands is unnecessary—PHP.ini uses direct key-value assignments, not shell environment variable syntax. Appending these lines would just add duplicate, non-functional entries to the file. - Your
sedcommand was incomplete, which prevented it from working as expected.
Here's the correct non-interactive way to modify the three parameters in a single VM:
Update the PHP.ini Parameters
Run these commands via SSH in your VM (or via vagrant ssh):
# Modify max_execution_time (handles both commented and uncommented lines) sudo sed -i.bak 's/^;*max_execution_time = .*/max_execution_time = 60/' /etc/php/7.2/cli/php.ini # Modify post_max_size sudo sed -i.bak 's/^;*post_max_size = .*/post_max_size = 80M/' /etc/php/7.2/cli/php.ini # Modify upload_max_filesize sudo sed -i.bak 's/^;*upload_max_filesize = .*/upload_max_filesize = 80M/' /etc/php/7.2/cli/php.ini
- The
;*in the regex matches 0 or more comment semicolons, so it works whether the original line was commented out or active. -i.bakcreates a backup of your original PHP.ini file (namedphp.ini.bak) just in case you need to revert.
Apply the Changes
After modifying the file, restart your PHP service to make the changes take effect:
# If using PHP-FPM (common with Nginx) sudo systemctl restart php7.2-fpm # If using Apache with mod_php sudo systemctl restart apache2
2. Batch Update All Running Vagrant VMs
To update every running VM at once, use a bash loop with Vagrant's built-in commands. This avoids manually logging into each VM:
# Loop through all running Vagrant VMs for vm_id in $(vagrant global-status --prune | grep running | awk '{print $1}'); do echo "Updating PHP settings on VM: $vm_id" # Run the sed commands remotely vagrant ssh $vm_id -c "sudo sed -i.bak 's/^;*max_execution_time = .*/max_execution_time = 60/' /etc/php/7.2/cli/php.ini" vagrant ssh $vm_id -c "sudo sed -i.bak 's/^;*post_max_size = .*/post_max_size = 80M/' /etc/php/7.2/cli/php.ini" vagrant ssh $vm_id -c "sudo sed -i.bak 's/^;*upload_max_filesize = .*/upload_max_filesize = 80M/' /etc/php/7.2/cli/php.ini" # Restart PHP service vagrant ssh $vm_id -c "sudo systemctl restart php7.2-fpm" done
vagrant global-status --prunelists all active VMs, and we filter for running ones withgrep running.vagrant ssh $vm_id -c "command"executes the command directly on the remote VM without interactive login.
3. Pro Tip: Automate This for Future VMs (Vagrant Provisioning)
If you want these PHP settings to apply automatically to every new VM (or when you rebuild existing ones), add a provision script to your Vagrantfile. This way you won't have to manually run commands again:
Edit your Vagrantfile to include this block:
Vagrant.configure("2") do |config| # Your existing VM configuration (boxes, networks, etc.) goes here... # Auto-configure PHP.ini settings on provision config.vm.provision "shell", inline: <<-SHELL # Update PHP parameters sed -i.bak 's/^;*max_execution_time = .*/max_execution_time = 60/' /etc/php/7.2/cli/php.ini sed -i.bak 's/^;*post_max_size = .*/post_max_size = 80M/' /etc/php/7.2/cli/php.ini sed -i.bak 's/^;*upload_max_filesize = .*/upload_max_filesize = 80M/' /etc/php/7.2/cli/php.ini # Restart PHP service to apply changes systemctl restart php7.2-fpm SHELL end
Now, whenever you run vagrant up or vagrant provision, these settings will be applied automatically.
内容的提问来源于stack exchange,提问作者Ksenia Subbotina

