To set the default PHP version when multiple versions are installed, you can use the update-alternatives
command on Linux-based systems like Ubuntu or Debian. Here's how you can do it:
Step 1: Check Installed PHP Versions
Run the following command to list the installed PHP versions:
php -v
Step 2: Check Available PHP Alternatives
List all PHP alternatives managed by update-alternatives
:
sudo update-alternatives --list php
You’ll see something like this:
/usr/bin/php7.4
/usr/bin/php8.1
Step 3: Set Default PHP Version
To set a specific version as the default, use:
sudo update-alternatives --set php /usr/bin/php8.1
Replace /usr/bin/php8.1
with the path to the desired PHP version.
Step 4: Use Interactive Selection (Optional)
If you want to select the version interactively:
sudo update-alternatives --config php
You’ll see a list of installed PHP versions:
There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status
------------------------------------------------------------
* 1 /usr/bin/php7.4 74 auto mode
2 /usr/bin/php8.1 81 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Enter the number corresponding to your desired PHP version.
Step 5: Verify the Change
Check the default PHP version again:
php -v
Step 6: Configure PHP for Web Server (Optional)
If you are running a web server like Apache or Nginx, ensure the selected PHP version is active for it.
For Apache:
Disable the current PHP module and enable the desired one:
sudo a2dismod php7.4
sudo a2enmod php8.1
sudo systemctl restart apache2
For Nginx:
Ensure the fastcgi_pass
directive in your Nginx configuration points to the correct PHP-FPM version, such as /run/php/php8.1-fpm.sock
.
Restart PHP-FPM and Nginx:
sudo systemctl restart php8.1-fpm
sudo systemctl restart nginx
Now, your default PHP version is set, and the web server should use it appropriately. Let me know if you face any issues!
No comments:
Post a Comment