New versions of PHP usually bring new features, vulnerability fixes, or bug corrections. However, migrating from one version to another may cause incompatibilities. Therefore, you should first check whether your software is compatible with the version you want to install.
Currently, the latest stable version is PHP 8.0, but it is not yet compatible with most applications. It is recommended that updated software uses at least PHP version 7.4. For example, WordPress in versions 5.6 and 5.7 offers "beta support" for the latest stable version.
In this article, we will explain how to change the PHP version, for example, from 7.2 to 7.4 for a website using Apache or NGINX as the web server. In other words, this is the method to modify it if you are not using a Webadmin panel with multiPHP, such as HestiaCP, Plesk, CyberPanel, etc.
If you are using OpenLiteSpeed, the procedure is different, skip the "Installing another version" section and go directly to the "Web Service Configuration" tab.
Version Check:
To view installed versions on the server, you can run this command:
# update-alternatives --display php
You can view the PHP version currently used by default with the following command:
# php -v
Installing Another Version:
To install other versions, you need to add the PHP repository and update:
# apt install software-properties-common -y && add-apt-repository ppa:ondrej/php && apt update
For example, to install PHP version 7.4 with all required packages, in this case for a preconfigured WordPress image, you can use the following command:
# apt install php7.4 php7.4-zip php7.4-xmlrpc php7.4-xml php7.4-readline php7.4-opcache php7.4-mysql php7.4-mbstring php7.4-json php7.4-intl php7.4-gd php7.4-fpm php7.4-curl php7.4-common php7.4-cli php7.4-bcmath
You can change the installation command to indicate another version you need. If you’re unsure which packages you are currently using, you can check with:
# apt list --installed | grep "php*"
Once a new version is installed, you can set which one to use by default:
# update-alternatives --config php
You’ll need to enter the version number and then press the Enter key. Example:
There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/php7.4 74 auto mode
1 /usr/bin/php7.2 72 manual mode
2 /usr/bin/php7.4 74 manual mode
Press to keep the current choice[*], or type selection number: 0
Web Service Configuration:
For NGINX web service, you will need to update the socket of the PHP version you want to use in the site’s configuration file, for example by editing it with vim:
# vim /etc/nginx/sites-enabled/default
This is an example of socket configuration in the site file to use PHP 7.4:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets)
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# With php-cgi (or other tcp sockets)
#fastcgi_pass 127.0.0.1:9000;
}
Afterwards, you can verify that the new configuration is valid:
# nginx -t
If there are no errors, restart the service to apply the changes:
# systemctl restart nginx.service
Finally, you can verify that your website is using the correct version and required modules by creating a PHP info file. You can follow the steps to create the phpInfo file and review it.
Remember, if you have any questions about this or other topics related to your servers on Clouding, don’t hesitate to write to support@clouding.io — we’re here to help you with whatever you need!