Discover how to set up and maintain various PHP versions on Ubuntu. To ensure that your server is running the appropriate version of PHP for your applications, use this comprehensive guide to install PHP 8.2, 8.3, and use Apache2 to switch between them.
Installing and Using Different PHP Versions on Ubuntu: A Complete Guide
- Make System Updates To make sure you are installing the most recent packages, start by updating your package lists.
sudo apt update
2. Install the PHP Repository. Add the Ondrej PHP PPA repository to obtain the most recent versions of PHP.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
3. Install several PHP versions, such as PHP 8.2 and 8.3. Install the desired PHP versions along with their modules.
- Regarding PHP 8.2:
sudo apt install php8.2 php8.2-fpm php8.2-cli php8.2-mysql php8.2-curl php8.2-xml php8.2-mbstring php8.2-soap php8.2-intl
- Regarding PHP 8.3:
sudo apt install php8.3 php8.3-fpm php8.3-cli php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring php8.3-soap php8.3-intl
4. To make PHP the default version for your system, you can use the update-alternatives command.
sudo update-alternatives --config php
To set PHP as the default version on your system, you can use the update-alternatives command.
5. You will be asked to select the PHP version you wish to use. The version number, such as PHP 8.2 or PHP 8.3, must be entered. For example:
- Disable the current PHP version:
sudo a2dismod php7.x
- Enable the PHP 8.2 module:
sudo a2enmod php8.2
- Enable the PHP 8.2 module:
sudo a2enmod php8.3
Finally, to implement the changes, restart Apache2:
sudo systemctl restart apache2
Check the PHP Versions Installed Verify the PHP version that is currently in use.
php -v
Check the PHP Versions Installed Verify the PHP version that is currently in use.
sudo systemctl status php8.2-fpm
sudo systemctl status php8.3-fpm
Access Different Versions of PHP If you configure your web server (Apache, Nginx, etc.) to point to different PHP-FPM sockets or modules, you can have different PHP versions running for different applications. Set Apache to use PHP 8.2 for one site and PHP 8.3 for another, for instance.
You can successfully install and switch between PHP versions on Ubuntu by following these steps, which will guarantee compatibility with a wide range of applications.