Share your love
Best Way To Get PHP 7.4 on Debian 12 Bookworm
This guide intends to teach you to Get and Install PHP 7.4 on Debian 12 Bookworm. As you know, Debian 12 ships with PHP 8.2. But for some cases like software dependencies, you need to install PHP 7.4. So you can proceed to the following steps to see how you can install PHP 7.4 on Debian 12. Note that it is recommended to use the latest versions until it is necessary to use a specific version.
Steps To Get and Install PHP 7.4 on Debian 12
Before you start the PHP 7.4 installation, log in to your server as a non-root user with sudo privileges. For this purpose, you can check the Initial Server Setup on Debian 12.
Then, proceed to the next step to add the PHP Sury repository to your server and install PHP 7.4.
Step 1 – Required Packages For Installing PHP 7.4
First, run the system update with the following command:
sudo apt update
Then, install the required packages with the command below:
sudo apt install software-properties-common apt-transport-https wget -y
Now you must add the PHP Sury repository to get your desired PHP version on Debian 12. To do this, you can run the command below:
# sudo 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
Again run the system update to apply the new repository:
sudo apt update
At this point, you can simply install PHP 7.4 with Apache and Nginx modules.
Step 2 – Install PHP 7.4 with Apache on Debian 12
If you are using an Apache web server, you can use PHP as an Apache module and PHP-FPM. To do this, you can run:
sudo apt install php7.4 libapache2-mod-php7.4 -y
Also, you can install PHP-FPM by using the command below:
sudo apt install php7.4-fpm libapache2-mod-fcgid -y
Note: By default, PHP-FPM is not enabled for Apache. You must enable it by the following commands:
sudo a2enmod proxy_fcgi setenvif && sudo a2enconf php7.4-fpm
Then, restart Apache to apply the changes:
sudo systemctl restart apache2
Verify your PHP 7.4 installation by checking its version:
php --version
Step 3 – Install PHP 7.4 with Nginx on Debian 12
As you know, Nginx does not contain native PHP processing like some other web servers like Apache. You will need to install PHP-FPM to handle the PHP files.
To install PHP 7.4 and PHP 7.4-FPM, run the command below:
sudo apt install php7.4 php7.4-fpm php7.4-cli -y
PHP-FPM will start automatically when your installation is completed.
This is an example for all server blocks that process PHP files that need the location ~ .php$ added.
server {
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
Restart Nginx to apply the changes:
sudo systemctl restart nginx
Also, you can verify that PHP 7.4 is installed on your server by checking its version:
php --version
Step 3 – Multiple PHP Versions on Debian 12
If you have multiple PHP versions like PHP 8.2, it will automatically use the latest PHP version. To switch between PHP versions, you can simply use:
sudo update-alternatives --set php /usr/bin/php7.4
It will load the PHP 7.4. To switch back to PHP 8.2, you can replace the version.
Conclusion
At this point, you have learned to Install PHP 7.4 on Debian 12 Bookworm by using the PHP Sury repository also, you have learned to install it by using Apache and Nginx. Also, if you have multiple versions of PHP, you can simply switch between your PHP versions.
Hope you enjoy it. Also, you may like to read the following guides:
Upgrade Default PHP to PHP 8.3 in Debian / Ubuntu
Find Apache and PHP Version Installed on Linux