Managing the PHP version for your website is crucial to ensure compatibility with different applications and frameworks. In this guide, we'll walk through the process of using different PHP versions on an Apache server running on Ubuntu. This flexibility is especially valuable when your website relies on specific PHP features or when you want to update to the latest PHP release while maintaining compatibility with existing code.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- An Apache web server is installed on your Ubuntu system.
- Multiple PHP versions are installed on your server. You can use packages like
php8.x
,php7.x
, etc.
Steps to Use Different PHP Versions for Each Website on Apache Ubuntu
Step 1: Install Apache and PHP:
If you haven't installed Apache and PHP, you can do so using the following commands:
sudo apt update -y
sudo apt install apache2 -y
sudo apt install php8.x php7.x php8.x-fpm php7.x-fpm -y
Replace 7.x
and 8.x
with the version you want to install.
Read More: How to Install Multiple PHP Versions on Ubuntu 22.04
Step 2: Install Apache PHP Module:
Install the Apache PHP module to integrate PHP with Apache:
sudo apt install libapache2-mod-php7.x libapache2-mod-php8.x -y
Again, replace 7.x
and 8.x
with the desired PHP version.
Step 3: Enable PHP Module:
Enable the PHP module for Apache:
sudo a2enmod php7.x
sudo a2enmod php8.x
Step 4: Restart Apache:
Restart the Apache server to apply the changes:
sudo service apache2 restart
Step 5: Configure Virtual Hosts:
If you have multiple websites on your server, configure virtual hosts to specify the PHP version for each site. Open the virtual host configuration file:
sudo nano /etc/apache2/sites-available/your-site.conf
Add or modify the following line to specify the PHP version:
<VirtualHost *:80>
ServerAdmin webmaster@your_domain
ServerName your_domain
DocumentRoot /var/www/your_directory
<Directory /var/www/your_directory>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.x-fpm.sock|fcgi://localhost/"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Replace 7.x
with your desired PHP version.
Explanation:
<VirtualHost *:80>
: Defines a virtual host listening on port 80.ServerAdmin
,ServerName
, andDocumentRoot
: Specifies the server admin email, the domain name, and the root directory for the virtual host.<Directory>
: Sets the configuration options for the specified directory.Options Indexes FollowSymLinks
: Enables directory listing and follows symbolic links.AllowOverride All
: Allows the use of.htaccess
files to override configuration.Require all granted
: Grants access to all users.<FilesMatch \.php$>
: Matches files with the.php
extension.SetHandler "proxy:unix:/var/run/php/php7.x-fpm.sock|fcgi://localhost/"
: Directs PHP requests to the specified PHP-FPM socket.ErrorLog
andCustomLog
: Define the error and access logs for the virtual host.
Save this configuration in a file, for example, your-site.conf
, and place it in the /etc/apache2/sites-available/
directory. Then, enable the virtual host and restart Apache:
sudo a2ensite your-site.conf
Step 6: Test Configuration:
Test your Apache configuration to ensure there are no syntax errors:
sudo apache2ctl configtest
If there are no errors, restart Apache:
sudo service apache2 restart
Conclusion
Congratulations! You've successfully configured Apache to use different PHP versions on your Ubuntu server. This flexibility allows you to run multiple websites with varying PHP requirements. Keep in mind that the specific steps may vary slightly depending on your server setup, so always refer to the official documentation for the most accurate information.