How to Change PHP Version in Apache on Ubuntu
Posted on July 31st, 2019
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.
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.