How to change my php version

How to change PHP version on the command line (CLI) using alternatives

Thanks to version-named paths of PHP configurations, libraries and binaries, different PHP versions can be installed in parallel on the same server. This is a great help when switching a PHP web application to a new PHP version. By using different PHP-FPM versions running in parallel, the Apache (or Nginx) config can simply be adjusted for the PHP-FPM listener or socket.

While switching the PHP versions of web-applications is a simple one line changer in the web server (vhost) config, there are other scripts on the command line, where the switch isn’t so simple.

Looking at the WordPress command line ( wp cli ) for example. The wp command is a PHP script, which is executed on the command line:

ck@server:~$ head -n 1 /usr/local/bin/wp
#!/usr/bin/env php

The script uses the php command from the system. wp can tell us which PHP version this is:

Читайте также:  Python packages sublime text

ck@server:~$ wp cli info
OS: Linux 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php8.1
PHP version: 8.1.2

php.ini used: /etc/php/8.1/cli/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
SQL modes:
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/ckadm
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.6.0

But if wp (or any other PHP script) should be used on a web application running on a different PHP version, this might cause problems.

Workaround 1: Launch script with specific PHP cli

Instead of running wp directly (which will lookup the «default» PHP version on the system), launch the script with a specific PHP CLI instead:

ck@server:~$ /usr/bin/php7.4 /usr/local/bin/wp cli info
OS: Linux 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php7.4
PHP version: 7.4.27

php.ini used: /etc/php/7.4/cli/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
SQL modes:
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/ckadm
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.6.0

On the plus side, this gives you a flexible way to use different PHP versions on PHP scripts. But the chances that you forget the specific PHP version prior to the PHP script are pretty high.

Workaround 2: Using alternatives

Alternatives is a system setting available in most Linux distributions and allow to change the path to a binary or script. This also allows to manually (compile and) install newer software and run the new version alongside the version from the package repositories (see Install/upgrade cmake on Ubuntu using alternatives for an example).

On this system, the current path of /usr/bin/php points to /etc/alternatives/php:

ck@server:~$ ls -la /usr/bin/php
lrwxrwxrwx 1 root root 21 Dec 5 2018 /usr/bin/php -> /etc/alternatives/php

This is typically proof that alternatives are configured. By using the update-alternatives command (on Debian-based distributions), the current configuration can be checked:

ck@server:~$ sudo update-alternatives —get-selections | grep php
php auto /usr/bin/php8.1

The current configuration lets /usr/bin/php point to /usr/bin/php8.1.

To change the PHP version in alternatives, we can install a new alternative with a higher priority (99):

ck@server:~$ sudo update-alternatives —install /usr/bin/php php /usr/bin/php7.4 99 —force
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in auto mode

ck@server:~$ sudo update-alternatives —get-selections | grep php
php auto /usr/bin/php7.4

This change is immediately working and can be tested on a PHP script, such as wp :

ck@server:~$ /usr/local/bin/wp cli info
OS: Linux 4.19.0-17-amd64 #1 SMP Debian 4.19.194-2 (2021-06-21) x86_64
Shell: /bin/bash
PHP binary: /usr/bin/php7.4
PHP version: 7.4.27

php.ini used: /etc/php/7.4/cli/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
SQL modes:
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/ckadm
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.6.0

Источник

How to Change PHP Version

Sometimes it is necessary to change the PHP version used by one or many web applications on a server. This could be due to a whole myriad of reasons, though it is likely because either the code in the app is too old for newer versions of PHP, or it features utilities not available in older PHP versions.

In this tutorial, we will learn how to change PHP versions and configure them to be used on different web applications.

Check Which Version Your Site is Using

You are going to need to know what version of PHP your site is currently using and validate it has changed. Please read about how to do this here.

List PHP Versions Available

Now let’s list the versions of PHP available on your system. We can do this using the update-alternatives command-line utility with the —list flag. It will return a list of paths to the PHP versions on your system.

sudo update-alternatives --list php 
/usr/bin/php5.6 /usr/bin/php7.0 /usr/bin/php7.1 /usr/bin/php7.2 /usr/bin/php7.3 /usr/bin/php7.4 /usr/bin/php8.0 

Set Default PHP Version

To set the default PHP version your system use the following update-alternatives command, passing the PHP version you wish to use at the end.

sudo update-alternatives --set php /usr/bin/php7.2 

If it works, the above command should return a message stating the new default PHP version:

update-alternatives: using /usr/bin/php7.2 to provide /usr/bin/php (php) in manual mode 

To confirm this run php -v :

PHP 7.2.34-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct 6 2020 12:01:28) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.34-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies 

Enable a PHP Version in Apache

To enable a new version of PHP in Apache, use the a2enmod utility in sudo mode, passing the name of the PHP version as the first argument.

Then restart the server to complete:

sudo service apache2 restart 

Setting PHP Version For a Single Site in Apache

To set a PHP version for a specific website in Apache, open the .htaccess file in its web root and paste the following line, changing php72 to the version you wish to use:

AddHandler application/x-httpd-php72 .php 

Change the PHP Version for a Site in Nginx

The PHP version for a site in Nginx can be changed in its configuration file. If you don’t know the name of the applications configuration file list them using the following command:

ls /etc/nginx/sites-available 

Now open the configuration file using nano in sudo mode, replacing skillsugar.com with the name of your configuration file:

sudo nano /etc/nginx/sites-available/skillsugar.com 

In the file, there should be a block of code that looks like this:

location ~ \.php$  include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; > 

Change PHP version number to the desired one and save the file.

Now test Nginx for an configuration errors:

If no errors are reported, restart Nginx to apply the changes using the following command:

sudo service nginx restart 

Conclusion

You now know how to change the default PHP version for your system and how to apply a particular version to specific sites on both Nginx and Apache HTTP servers.

Источник

How to Change Default PHP Version on Ubuntu

On your system, if you have installed multiple versions of PHP like PHP 8.2, PHP 7.4 and PHP 5.6. Currently, PHP 7.4 is the default version set for Apache and the command line interface (CLI). Maybe you have any other PHP versions installed on your system. Only one PHP version can be set as default at any time.

This tutorial will help you to change the default PHP version for CLI and Apache webserver. This can be achieved with a few sets of commands.

Switch to PHP 8.2

Before switching to PHP 8.2, you have to deactivate the previously activated Apache module. The following commands will deactivate the already activated Apache module and then activate PHP 8.2 Apache module. Then the next commands will set PHP 8.2 as the default version for the command line interface (CLI).

sudo a2dismod php* sudo a2enmod php8.2 sudo systemctl restart apache2 
sudo update-alternatives --set php /usr/bin/php8.2 sudo update-alternatives --set phar /usr/bin/phar8.2 sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.2 sudo update-alternatives --set phpize /usr/bin/phpize8.2 sudo update-alternatives --set php-config /usr/bin/php-config8.2 

Note: The phpize8.2 and php-config8.2 command is available in php8.2-dev package. This is more useful for compiling PHP modules using pecl.

If you want a different version (eg: PHP 8.1 or PHP 8.0) to be configured as default, just replace 8.2 with the required version.

Switch to PHP 7.4

Similarly, if you need to configure PHP 7.4 as the default version in your system. The following set of commands will change the default PHP to 7.4 for the Apache web server and command line interface.

sudo a2dismod php* sudo a2enmod php7.4 sudo systemctl restart apache2 
sudo update-alternatives --set php /usr/bin/php7.4 sudo update-alternatives --set phar /usr/bin/phar7.4 sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4 sudo update-alternatives --set phpize /usr/bin/phpize7.4 sudo update-alternatives --set php-config /usr/bin/php-config7.4 

Note – The phpize7.4 and php-config7.4 command is available in php7.4-dev package. This is more useful for compiling PHP modules using pecl.

Switch to PHP 5.6

PHP 5.6 is an outdated version. So if you have an application that required PHP 5.6, consider them to upgrade to last PHP version. The following set of commands will change the default PHP to 5.6 for the Apache web server and command line interface.

sudo a2dismod php* sudo a2enmod php5.6 sudo systemctl restart apache2 
sudo update-alternatives --set php /usr/bin/php5.6 sudo update-alternatives --set phar /usr/bin/phar5.6 sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6 sudo update-alternatives --set phpize /usr/bin/phpize5.6 sudo update-alternatives --set php-config /usr/bin/php-config5.6 

Note: The phpize5.6 and php-config5.6 command is available in php5.6-dev package. This is more useful for compiling PHP modules using pecl.

Conclusion

This tutorial helps you to change the default PHP versions on an Ubuntu system. This helps you to change the default PHP version for the Apache web server and the command line interface.

Источник

How to Change PHP Version in cPanel

Learn How to Change Your PHP Version in cPanel

Our PHP hosting services empower you to have full control over your PHP versions with the latest cPanel functionality. With just a few clicks, you can switch between different versions and even use different versions across sites in the same cPanel. This is especially helpful when upgrading older sites, troubleshooting, or dealing with third-party applications. Let’s take a look at how to make the most of PHP on all types of servers!

displays MultiPHP Manager icon in cPanel

Note: Be sure to click on MultiPHP Manager and not MultiPHP INI Editor. The INI Editor is a different piece of software that helps you fine tune your PHP settings on individual sites.

Change the PHP Version on Your Sites

Now that you know what the MultiPHP Manager is, we’ll show you how to use it to change the PHP version for your sites.

Note: Before changing the PHP version on a website or application, we suggest performing a backup first. Once the PHP version has been changed, be sure to verify that your sites and applications are working as intended.

  1. Login to cPanel.
  2. Click the MultiPHPManager link in the Software section of cPanel.
  3. Check the box for the site or sites you wish to update.
    Select the Site You Want to Change the PHP Version On
  4. Select the desired PHP Version from the drop-down menu.
    Choosing PHP Version
  5. Click the Apply button.
    Applying New PHP Version

That’s all it takes to change the PHP version used on any site you choose. If you are having issues with a site and want to double-check the version of PHP in use, remember that you can always set up a PHPinfo page for the site as well. Learn more about cPanel from our Managed VPS Hosting Product Guide.

Make sure your business, agency, or reseller clients are always connected and powered on with our optimized Managed VPS Hosting.

Источник

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