neogeo
neogeo

Reputation: 55

How do I change the default PHP base-version on shell?

I have Centos 7 and Virtualmin installed, with the tipycal php-fpm 5.4, 7.0, 7.1 that you can choice between the versions you prefer on every virtualhost via Virtualmin control panel, and everything works well.

But when I access to the server via SSH and check php -v I get this:

PHP 5.4.16 (cli) (built: Oct 30 2018 19:30:51) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

How can I select another php default/base version for the shell system?

Upvotes: 3

Views: 16187

Answers (4)

Boaventura
Boaventura

Reputation: 1409

Installation and Management of Multiple PHP Versions on CentOS 7

When installing PHP on CentOS 7, it is essential to follow some recommended practices to ensure a secure and efficient installation. Additionally, it is possible to install and run multiple versions of PHP simultaneously, allowing you to test your applications in different PHP environments. Here is a complete guide to installing PHP and managing multiple versions on CentOS 7:

Recommended Practices:

  1. Choosing the PHP Version: Previous versions of PHP, such as 7.1, 7.2, 7.3, and 7.4, are obsolete. It is recommended to install the latest stable versions, such as PHP 8.0, PHP 8.1, PHP 8.2, or PHP 8.3.

  2. System Update: Before installing PHP, it is important to ensure that your system is up to date. Use the following command to update all system packages:

    sudo yum update
    
  3. Enabling the Remi Repository: The latest PHP can be found in the Remi repository. To enable it, use the following command:

    sudo yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    sudo yum-config-manager --enable remi-php80  # For PHP 8.0
    sudo yum-config-manager --enable remi-php81  # For PHP 8.1
    sudo yum-config-manager --enable remi-php82  # For PHP 8.2
    sudo yum-config-manager --enable remi-php83  # For PHP 8.3
    
  4. PHP Installation and Additional Modules: After enabling the repositories, you can install the desired PHP versions. Install the core PHP packages and additional modules as needed for your applications. Here are some essential modules:

    sudo yum install -y php php-fpm php-mysqlnd php-opcache php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt
    

    Be sure to replace php with the specific package for the PHP version you want to install, for example, php81 for PHP 8.1.

  5. Configuration and Management: Configure Apache or NGINX to use a specific PHP version for different directories or sites. You can switch between different versions by restarting the PHP and web server services.

  6. Verification of Installation: Verify that the PHP versions were installed correctly using the php -v and php-fpm -v commands.

  7. Web Server Restart: After configuring the different PHP versions, restart the web server to apply the changes. Use the following commands to restart Apache and NGINX, respectively:

    sudo systemctl restart httpd  # For Apache
    sudo systemctl restart nginx  # For NGINX
    
  8. Activating the PHP Version: If you need to have more than one PHP version installed, you can activate the desired version directly. For example, to activate PHP 8.1, use the following command:

    sudo alternatives --set php /usr/bin/php-cgi-8.1
    

    This will change the default PHP version to 8.1. To verify that the change was applied correctly, use the php -v command.

With these recommended practices and the ability to install multiple PHP versions, you can have a flexible and secure development environment on CentOS 7. Remember to always check the official PHP documentation for updated information on supported and obsolete versions.

Upvotes: -1

Boaventura
Boaventura

Reputation: 1409

Install PHP Versions on CentOS 7

Setup Yum Repository First, you need to enable Remi and EPEL yum repositories on your system. Use the following command to install the EPEL repository on your CentOS and Red Hat 7/6 systems.

Use this command to install the EPEL yum repository on your system:

sudo yum install epel-release

And now execute one of the following commands as per your operating system version to install the Remi repository:

sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Install PHP 8 on CentOS Your system is prepared for the PHP installation from yum repositories. Use one of the following commands to install PHP 8.2 or PHP 8.3 on your system based on your requirements.

Install PHP 8.2

yum --enablerepo=remi-php82 install php

Install PHP 8.3

yum --enablerepo=remi-php83 install php

Check the installed PHP version

php -v

Example output:

PHP 8.2.x (cli) (built: Dec 17 2023 16:35:58) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies

Install PHP Modules You may also need to install additional PHP modules based on your application requirements. The command below will install some useful additional PHP modules.

For PHP 8.2

yum --enablerepo=remi-php82 install php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt

For PHP 8.3

yum --enablerepo=remi-php83 install php-xml php-soap php-xmlrpc php-mbstring php-json php-gd php-mcrypt

The text initially informed about installing PHP 7.4, 7.3, and 7.2, but now it has been updated to PHP 8.2 and 8.3.

Upvotes: 5

Floris
Floris

Reputation: 3127

Changing the default php version on centos was the question. Placing a path or alias in the .bashrc or .bash_profile file sets the php version for a specific user not the default to be used.

So in my case the solution where provided in these steps (using CentOs and Plesk):

  1. which php

/usr/bin/

Inside that directory when you do ls -la you find the old php folder.

  1. mv php php-backup - create a backup

  2. ln -s /opt/plesk/php/8.2/bin/php php - creates a new symlink to the desired php version.

  3. php -v

PHP 8.2.10 (cli) (built: Sep 4 2023 11:40:59) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.10, Copyright (c) Zend Technologies with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies

Et voilà. The default php has been set for the system. (when not overwritten by a profile)

Btw, I needed to set the default for the systems php because of running a php command from a supervisor script.

Upvotes: 0

Bogdan Stoica
Bogdan Stoica

Reputation: 4539

When you issue the php command on the shell it uses the default php version on the server which in your case is php-5.4.16

To use another php version, you have to check where the binaries for those php versions are and invoke them with their full path rather than just typing php.

For example, on CentOS, for PHP 7.2 for example, the full path of the php binary is: /opt/rh/rh-php72/root/usr/bin/php

root@virtualmin /root
» /opt/rh/rh-php72/root/usr/bin/php -v
PHP 7.2.24 (cli) (built: Nov  4 2019 10:23:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24, Copyright (c) 1999-2018, by Zend Technologies

Upvotes: 5

Related Questions