Multi-PHP and Web Server Installation Guide for Debian 12 and 11
Step 1: Update and Upgrade the System
First, ensure your system is up-to-date:
sudo apt update
sudo apt upgrade
Step 2: Add External Repository
Debian's default repository provides PHP 8.3 and 7.4. To install additional versions such as PHP 8.2, 8.1, 7.3, or 5.6, you'll need to add Ondřej Surý's PHP repository:
sudo apt install -y apt-transport-https lsb-release ca-certificates wget
wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list
sudo apt update
Step 3: Install Apache Web Server
Install Apache:
sudo apt install -y apache2
Enable and start the Apache service:
sudo systemctl enable apache2
sudo systemctl start apache2
Step 4: Install PHP Versions
You can install multiple PHP versions based on your needs:
Install PHP 8.3
sudo apt install -y php8.3 libapache2-mod-php8.3
Install PHP 8.2
sudo apt install -y php8.2 libapache2-mod-php8.2
Install PHP 7.4
For older applications:
sudo apt install -y php7.4 libapache2-mod-php7.4
Step 5: Configure Virtual Hosts in Apache
Create a configuration file for each application with a specific PHP version.
Example Configuration for PHP 8.3
sudo nano /etc/apache2/sites-available/php83.example.com.conf
Add the following content to the file:
<VirtualHost *:80>
ServerName php83.example.com
DocumentRoot /var/www/php83_app
<Directory /var/www/php83_app>
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php8.3-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
Enable the configuration and restart Apache:
sudo a2ensite php83.example.com.conf
sudo systemctl restart apache2
Example Configuration for PHP 7.4
sudo nano /etc/apache2/sites-available/php74.example.com.conf
Add the following content to the file:
<VirtualHost *:80>
ServerName php74.example.com
DocumentRoot /var/www/php74_app
<Directory /var/www/php74_app>
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>
Enable the configuration and restart Apache:
sudo a2ensite php74.example.com.conf
sudo systemctl restart apache2
Step 6: Verify PHP Installation
Create an info.php
file for each application:
sudo nano /var/www/php83_app/info.php
sudo nano /var/www/php74_app/info.php
Add the following content to both files:
<?php
phpinfo();
?>
Access the files in your browser:
- PHP 8.3:
http://php83.example.com/info.php
- PHP 7.4:
http://php74.example.com/info.php
Step 7: Switch Between PHP Versions for CLI (Optional)
You can switch PHP versions for the command line using the following commands:
Switch to PHP 7.4
sudo update-alternatives --set php /usr/bin/php7.4
Switch to PHP 8.3
sudo update-alternatives --set php /usr/bin/php8.3
With these steps, you can easily manage multiple PHP versions and configure Apache web server on Debian 12 and 11.
Explanation:
- Step 1: Updates the system to ensure all packages are current.
- Step 2: Adds the Ondřej Surý repository for additional PHP versions.
- Step 3: Installs the Apache web server and starts the service.
- Step 4: Installs various PHP versions based on project requirements.
- Step 5: Configures Apache to use different PHP versions for different virtual hosts.
- Step 6: Verifies the PHP installation by creating a
phpinfo()
page. - Step 7: Allows switching between PHP versions in the command line interface (CLI) using
update-alternatives
.