Ubuntu apache2 change php version

How To Switch PHP Version on Ubuntu 20.04 LTS

This tutorial provides the steps required to switch among the multiple versions of PHP installed on Ubuntu 20.04 LTS for CLI, Apache Web Server, and NGINX.

The previous tutorial provided the steps required to install multiple PHP versions on Ubuntu 20.04 LTS. You can follow How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS.

Switch PHP versions — CLI

Verify the currently active PHP as shown below.

# Verify PHP
php --version

# Output
PHP 8.0.13 (cli) (built: Nov 22 2021 09:50:43) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.13, Copyright (c) Zend Technologies
with Zend OPcache v8.0.13, Copyright (c), by Zend Technologies

We can configure multiple versions of PHP installed on Ubuntu 20.04 LTS using the commands as shown below.

# PHP 7.0 
sudo update-alternatives --set php /usr/bin/php7.0
sudo update-alternatives --set phar /usr/bin/phar7.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.0

# PHP 8.0
sudo update-alternatives --set php /usr/bin/php8.0
sudo update-alternatives --set phar /usr/bin/phar8.0
sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.0

Now we can switch among PHP 7.0 or PHP 8.0 using the commands as shown below.

# Switch PHP
sudo update-alternatives --config php

# Output
There are 2 choices for the alternative php (providing /usr/bin/php).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/php8.0 80 auto mode
1 /usr/bin/php7.0 70 manual mode
* 2 /usr/bin/php8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode

# Switch
sudo update-alternatives --config phar

# Output
There are 2 choices for the alternative phar (providing /usr/bin/phar).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/phar8.0 80 auto mode
1 /usr/bin/phar7.0 70 manual mode
* 2 /usr/bin/phar8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/phar7.0 to provide /usr/bin/phar (phar) in manual mode

# Switch
sudo update-alternatives --config phar.phar

# Output
There are 2 choices for the alternative phar.phar (providing /usr/bin/phar.phar).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/phar.phar8.0 80 auto mode
1 /usr/bin/phar.phar7.0 70 manual mode
* 2 /usr/bin/phar.phar8.0 80 manual mode

Press to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/phar.phar7.0 to provide /usr/bin/phar.phar (phar.phar) in manual mode

Now, again verify the currently active PHP as shown below.

# Verify PHP
php --version

# Output
PHP 7.0.33-57+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Nov 19 2021 06:39:53) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.33-57+ubuntu20.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

This is helpful for the applications reliable on the PHP in CLI mode to run console based programs. We can switch among the multiple versions of PHP installed on Ubuntu systems.

Switch PHP versions — Apache Web Server

Similar to CLI mode, we can also switch among multiple versions of PHP for the Apache Web Server. In the previous tutorial — How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS, we saw that the PHP version remains same i.e. PHP 7.0 even after installing the recent version of PHP i.e. PHP 8.0 as shown in Fig 1.

Читайте также:  Http bt bashtel ru viewtopic php

We can switch among the PHP versions for the Apache Web server using the commands as shown below.

# Disable PHP 7.0
sudo a2dismod php7.0

# Output
Module php7.0 disabled.
To activate the new configuration, you need to run:
systemctl restart apache2

# Enable PHP 8.0
sudo a2enmod php8.0

# Output
Considering dependency mpm_prefork for php8.0:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php8.0:
Enabling module php8.0.
To activate the new configuration, you need to run:
systemctl restart apache2

# Restart Apache Web Server
sudo service apache2 restart

Now, again refresh the info.php using the Web Browser. It should be similar to Fig 2.

We can also switch back to PHP 7 using the same commands.

Switch PHP versions — Apache Web Server — PHP FPM

Instead of switch PHP version for Apache Web Server which impacts all the Virtual Hosts, we can also configure the selected Virtual Host to use the PHP version specified by us. We can do so using PHP FPM installed by us as shown in How To Install Multiple Versions Of PHP On Ubuntu 20.04 LTS.

# Check Status - PHP 7.0 FPM
systemctl status php7.0-fpm

# Output
php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 20:35:28 PST; 1h 14min ago Docs: man:php-fpm7.0(8) Main PID: 42546 (php-fpm7.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 18.1M CGroup: /system.slice/php7.0-fpm.service ├─42546 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf) ├─42548 php-fpm: pool www └─42549 php-fpm: pool www Dec 04 20:35:28 ubuntu systemd[1]: Starting The PHP 7.0 FastCGI Process Manager. Dec 04 20:35:28 ubuntu systemd[1]: Started The PHP 7.0 FastCGI Process Manager.

# Check Status - PHP 8.0 FPM
systemctl status php8.0-fpm

# Output
php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-12-04 21:15:28 PST; 35min ago Docs: man:php-fpm8.0(8) Process: 61492 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/8.0/fpm/pool.d/www.co> Main PID: 61489 (php-fpm8.0) Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 3 (limit: 9432) Memory: 10.7M CGroup: /system.slice/php8.0-fpm.service ├─61489 php-fpm: master process (/etc/php/8.0/fpm/php-fpm.conf) ├─61490 php-fpm: pool www └─61491 php-fpm: pool www Dec 04 21:15:28 ubuntu systemd[1]: Starting The PHP 8.0 FastCGI Process Manager. Dec 04 21:15:28 ubuntu systemd[1]: Started The PHP 8.0 FastCGI Process Manager.

We can see that both PHP 7.0 FPM and PHP 8.0 FPM are in running state. Now, enable Apache2 to use multiple versions of PHP using the command as shown below.

# Install FCGID
sudo apt install libapache2-mod-fcgid

# Enable FCGID
sudo a2enmod actions fcgid alias proxy_fcgi

# Restart Apache
sudo service apache2 restart

Also, update the Virtual Host as shown below.


----
----


# For Apache version 2.4.10 and above
SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost"


----
----

Also, reload Apache as shown below.

# Reload Apache
sudo service apache2 reload

Now, check the output of info.php using the Browser. It should show the configurations specific to PHP 7. Similarly, we can change PHP 7 to PHP 8 for the selected virtual host, without impacting the other virtual hosts.

Читайте также:  My matrix class java

Switch PHP versions — NGINX

Switching PHP version for NGINX is straight-forward, since it uses PHP FPM to execute the PHP scripts. We can simply specify the PHP version while configuring the Server Block as shown below. You can also follow How To Install PHP For Nginx On Ubuntu 20.04 LTS for more details.

# Server Block with PHP FPM
sudo nano /etc/nginx/sites-available/example.com

server <

.
.

# pass the PHP scripts to FastCGI
location ~ \.php$ root /var/www/example.com/html;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
>

.
.
>

Summary

This tutorial provided the steps required to switch among the multiple PHP versions installed on Ubuntu 20.04 LTS for CLI, Apache Web Server, and NGINX.

Источник

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.

Читайте также:  Get file by extensions php

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!

Источник

Оцените статью