- How to Change PHP Version in Apache on Ubuntu
- Change PHP version in Apache on Ubuntu
- Install Desired PHP Version
- Update PHP version in Apache config
- Other Cases
- 4 Responses to “How to Change PHP Version in Apache on Ubuntu”
- Upgrading or Downgrading PHP Versions
- 1. Make sure packages for your target version of PHP are installed
- 2. Make sure PHP configuration for your target version is updated
- 3. Update your web server configuration to point to the correct PHP version
- 4. Make sure PHP-CLI points to the right PHP version
- 5. Restart PHP-FPM and your webserver
- 6. Mask the old PHP-FPM daemon
- Get Started For
How to Change PHP Version in Apache on Ubuntu
Sometimes you need to change the PHP version on your server. Changing here means upgrading or downgrading. It’s up to you whether you want to upgrade the PHP version or downgrade. If you are using Apache with PHP-FPM, you can follow this short tutorial to update the PHP version on your Ubuntu server.
If you are using PHP module in Apache, you just have to disable the PHP module you are using right now and enable the PHP module with a different version. However, if you are using PHP-FPM, you might want to follow the article to change the PHP version.
There are some prerequisites to follow this tutorial. You must have Apache with PHP-FPM installed on your server. And you must have sudo privileges on the server. We need sudo privileges in this case because we will update Apache configuration files.
Change PHP version in Apache on Ubuntu
The first thing we have to do is to install the desired PHP version on our server.
Install Desired PHP Version
To install PHP, we have to add the PHP repository on our server so that we can directly install different PHP versions using apt. If you are using Ubuntu 16.04 or 18.04, you might have this repository already added to the system. However, If you are not sure, just run these commands as it will get overwritten!
Execute the following command to add the repository to the source list.
$ sudo add-apt-repository ppa:ondrej/php -y $ sudo apt-get update
Once done, we are ready to install the desired PHP version on our server. Execute the command in the following format to install the newer or older version of PHP on your server.
$ sudo apt-get install php7.X-fpm php7.X-curl php7.X-mbstring php7.X-mysql -y
You have to replace X with the desired PHP version number. For example, if you want to install PHP7.2 on your server, execute the following command.
$ sudo apt-get install php7.2-fpm php7.2-curl php7.2-mbstring php7.2-mysql -y
Note that we have included only a limited number of PHP extensions in this installation process. However, feel free to install as many PHP extensions as you need.
Once the newer version of PHP is installed on your server, It’s time to update the Apache configuration file to change the PHP version.
Update PHP version in Apache config
If you followed our guide to set up Apache with PHP-FPM on your server, execute the following command to open the PHP-FPM and FastCGI configuration file in edit mode.
$ sudo nano /etc/apache2/conf-available/php-fpm.conf
The contents of the configuration file should look like the following.
AddHandler php7-fcgi .php Action php7-fcgi /php7-fcgi Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /run/php/php7.1-fpm.sock -pass-header Authorization Require all granted
Now that we have already defined the PHP version we wanted to use in this configuration file. Now, you just have to update the PHP version in this file. If you haven’t created this file in the past, you can just paste the above-given code in the file. Once done, press CTRL+X followed by Y followed by the Enter key to save the configuration file.
To apply these changes, we have to restart the Apache server. Execute the following command to restart the Apache server.
$ sudo service apache2 restart
After the restart, the PHP version on your server should be changed. This is how easy it is to change the PHP version if you followed our guide to set up your Apache server with PHP-FPM.
Other Cases
If you are using Apache’s PHP module to run PHP with your Apache server, you can simply turn off the Apache module for PHP along with mpm_prefork and enable the configuration file we just created above. To disable the Apache module for PHP and mpm_prefork, execute the following commands.
$ sudo a2dismod php7. X
$ sudo a2dismod mpm_prefork
Now, we have to enable mpm_event and php-fpm configuration file. To perform these tasks, execute the following commands.
$ sudo a2enmod mpm_event
$ sudo a2enconf php-fpm
Once done, restart the Apache service using the command given in the previous section of this tutorial and you are good to go.
Conclusion: So, this is how you can change PHP version in your Apache server on Ubuntu operating system. We recommend you to use mpm_event with PHP-FPM on Apache because that configuration is very fast and provides the best performance you can get on the Apache server. There is one more case. If you are using the code to pass requests to PHP FPM directly in your virtual host configuration, you just have to update the PHP version in your virtual host configuration and restart the webserver, that’s all!
Let us know if you need help changing the version of PHP on your server. If you are an InterServer customer, you can contact our support staff for help, we will help you as soon as possible.
4 Responses to “How to Change PHP Version in Apache on Ubuntu”
Please include the date the article was authored, otherwise it’s hard to have any context. Things change so fast these days!
Upgrading or Downgrading PHP Versions
Upgrading or Downgrading PHP versions on a Webdock stack is essentially the same procedure. In short what you need to do is:
- Ensure the php packages for the version you want are installed
- Update any configuration for that PHP version
- Update your web server configuration to point to the correct PHP version
- Point PHP CLI to the correct PHP version
- Restart your web server and php-fpm
- Mask the old php-fpm daemon
Before performing this operation, it is always a good idea to take a snapshot of your server in case something goes wrong!
1. Make sure packages for your target version of PHP are installed
You need to make sure the correct packages are available on your system for the PHP version you want. First make sure your system is up to date:
sudo apt update sudo apt upgrade
If it prompts you what to do with config files, usually you should just go with the default option and leave the current config as-is.
Next, install the packages — the packages you want are listed on our Perfect Server stack pages. For example say you want to use PHP7.4 you would go to the 7.4 guide and find the section titled Install PHP7.4 and common PHP packages — the command you are looking for would look something like:
sudo apt install -y php7.4-cli php7.4-dev php7.4-pgsql php7.4-sqlite3 php7.4-gd php7.4-curl php7.4-memcached php7.4-imap php7.4-mysql php7.4-mbstring php7.4-xml php7.4-imagick php7.4-zip php7.4-bcmath php7.4-soap php7.4-intl php7.4-readline php7.4-common php7.4-pspell php7.4-tidy php7.4-xmlrpc php7.4-xsl php7.4-opcache php7.4-apcu
If you get an error claiming the appropriate PHP packages cannot be found, then you are on a really old version of our stacks which does not have the ondrej/php PPA added. In which case, execute:
sudo apt-add-repository ppa:ondrej/php -y
In general, please copy/paste commands from our Perfect Server guides as these are always kept up to date, and commands shown here are for example purposes only!
Finally, you should install php-fpm in the target version you want:
sudo apt install php7.4-fpm
2. Make sure PHP configuration for your target version is updated
Again, here you copy/paste the commands from the Perfect Server guide in the sections titled
3. Update your web server configuration to point to the correct PHP version
For Nginx you would edit /etc/nginx/sites-available/webdock. Look for this line and replace the PHP version number with the one you want:
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
For Apache you would edit /etc/apache2/sites-available/webdock.conf and /etc/apache2/sites-available/webdock-le-ssl.conf. Look for this line and replace the PHP version number with the one you want:
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
4. Make sure PHP-CLI points to the right PHP version
Especially if you are downgrading, you need to check that php-cli is pointing to the correct version. First check what version the cli is using right now by executing:
If the version number reported is different than the one you want, execute the following command replacing the version number with the one you want:
sudo rm /etc/alternatives/php; sudo ln -s /usr/bin/php5.6 /etc/alternatives/php;
Confirm by running php -v once more.
5. Restart PHP-FPM and your webserver
For example if your web server is nginx, you would execute:
sudo systemctl restart php7.4-fpm; sudo systemctl restart nginx;
Again, look in the Perfect Server guides for your version and webserver for the correct command here.
Congratulations, you should now be running on the PHP version you want. You can check it by creating a small php file in your web root with the following contents and visiting it in your web browser:
6. Mask the old PHP-FPM daemon
Here you essentially just tell the system to not start the old php-fpm daemon — replace the version number below with the previous version you are moving away from, say if you are going from php7.2 to php7.4 you would do:
sudo systemctl mask php7.2-fpm
If you need any help regarding this article or if you have any questions regarding hosting in general, please be in touch.
Webdock is a world-class hosting provider aimed at professionals and semi-professionals with the goal of providing an absolutely awesome and rock-solid hosting experience.
We use cookies. Please see our Privacy Policy. OK
Get Started For
- 24 hour free trial
- Free Control Panel
- Epic Support
- Free Snapshots
- Free Bot Protection
- Free SSL
Rated Excellent on Trustpilot
- Need Help
- Become An Affiliate
- Sign Up To Newsletter