Notice
This article is old, and some of the information may be outdated or no longer valid.
If you have any questions or need to confirm any details, we recommend contacting our support team for up-to-date information and personalized assistance.
New versions of PHP usually bring new features and fixes for vulnerabilities or bugs. However, migrating from one version to another can cause incompatibilities, so you should first check whether your software is compatible with the version you are going to install.
The latest stable version is currently PHP 8.4, but it is not yet compatible with most applications. Up-to-date software should ideally use at least PHP 7.4. For example, WordPress versions 5.6 and 5.7 offer "beta support" for the latest stable version.
In this article, we explain how to change the PHP version in use, for example from 7.2 to 7.4, for a website running Apache or NGINX as the web server. In other words, this is how to change 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 its tab under "Web service configuration".
Checking versions:
To see the versions installed on the server, run this command:
# update-alternatives --display phpYou can see the default PHP version by running the following command:
# php -vInstalling another version:
To install other versions, you need to add the PHP repository and run an update:
# apt install software-properties-common -y && add-apt-repository ppa:ondrej/php && apt updateFor example, to install PHP 7.4 with all the required packages, in this case for the 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-bcmathYou can modify the installation command to specify any other version you need. If you don't know which packages you are currently using, you can check them with the following command:
# apt list --installed | grep "php*"Once a new version is installed, you can set which one to use by default:
# update-alternatives --config phpEnter the number of the version and then press the Enter key. Example of the selection:
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 <enter> to keep the current choice[*], or type selection number: 0Web service configuration:
For the NGINX web service, you need to change the socket to the version you want to use in the site configuration, for example by editing it with the vim editor:
# vim /etc/nginx/sites-enabled/default
This is an example of the 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;
}
Then check whether the new configuration is correct:
# nginx -t
If the previous command returns no errors, restart the service to apply the changes:
# systemctl restart nginx.service
To disable the PHP versions you won't be using (php7.2 in this example), run the following:
# a2dismod php7.2
Then, to enable the newly installed PHP version, run this command:
# a2enmod php7.4
If you want to specify whether or not to use the default version, you need to set it in the site configuration file by adding the version's socket inside the virtualhost:
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
Check that the configuration change contains no errors:
# apache2 -t
If there are no errors, restart the Apache web service to apply the changes:
# systemctl restart apache2.service
If you don't have CyberPanel installed, you can follow the procedure below. LiteSpeed uses its own PHP packages, and only long-term support (LTS) versions are included in the apt repository. If the package is not available or you want an EOL version, you need to add its repository:
# wget -O - http://rpms.litespeedtech.com/debian/enable_lst_debian_repo.sh | bash
To install the php7.4 package, use the following command:
# apt update && apt install lsphp74
You can also install additional packages such as the mysql, opcache, memcached and redis modules, among others:
# apt install lsphp74-mysql lsphp74-memcached lsphp74-opcache
Click here for the official link where you can review all available PHP versions and packages for LiteSpeed.
Once the package is installed, go to the WebAdmin Console and set it to be used. By default, the service does not check the directory where the PHP package was installed (/usr/local/lsws/lsphp74).
To do this, open the WebAdmin console, go to "Server Configuration" > "External App" and click "Edit" next to the "LSAPI App" type.
Change the path in the lsphp command field to /usr/local/lsws/lsphp74/bin/lsphp and click "Save" in the top right corner.
LiteSpeed will then use the newly installed lsphp binary. If you need to use multiple versions, instead of modifying it you can add a new one and then assign it to the virtualhost.
If you installed the opcache module, it will not be enabled by default. You need to edit the php.ini of the installed version; in this case, add the following to /usr/local/lsws/lsphp74/etc/php.ini:
zend_extension = opcache.so
opcache.enable = 1
Finally, you can check that the website is using the required version and modules by creating a PHP info file. Follow this procedure to create the phpInfo file and review it.
Remember, if you have any questions about this or any other issue related to your servers at Clouding, don't hesitate to write to soporte@clouding.io. We're here for whatever you need, just ask!