{"id":620,"date":"2025-11-22T12:54:11","date_gmt":"2025-11-22T09:54:11","guid":{"rendered":"https:\/\/admin.danychrys.ro:4433\/?p=620"},"modified":"2025-11-23T02:16:53","modified_gmt":"2025-11-22T23:16:53","slug":"configuring-the-multiple-php-versions-with-apache-on-ubuntu","status":"publish","type":"post","link":"https:\/\/danychrys.go.ro\/web\/configuring-the-multiple-php-versions-with-apache-on-ubuntu\/","title":{"rendered":"Configuring the Multiple PHP Versions with Apache on Ubuntu"},"content":{"rendered":"<h3>1. Installing Apache2<\/h3>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo apt update\r\nsudo apt install apache2 libapache2-mod-fcgid<\/code><\/pre>\n<\/div>\n<p style=\"text-align: justify;\">Now enable a few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with the Apache server.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo a2enmod actions fcgid alias proxy_fcgi<\/code><\/pre>\n<\/div>\n<h3>2. Installing PHP Versions<\/h3>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo apt install software-properties-common\r\nsudo add-apt-repository ppa:ondrej\/php<\/code><\/pre>\n<\/div>\n<p>For this tutorial, we are using PHP 8.3 and PHP 8.4 to configure with the Apache webserver. To use the multiple PHP versions, we will use PHP FPM and FastCGI.<br \/>\nLet\u2019s install the following packages on your system.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo apt update\r\nsudo apt install php8.3 php8.3-fpm\r\n$ sudo apt install php8.4 php8.4-fpm<\/code><\/pre>\n<\/div>\n<p>After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo systemctl status php8.3-fpm\r\nsudo systemctl status php8.4-fpm<\/code><\/pre>\n<\/div>\n<h3>3. Configuring Apache Virtual Hosts<\/h3>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo mkdir \/var\/www\/php83\r\nsudo mkdir \/var\/www\/php84<\/code><\/pre>\n<\/div>\n<p>Create an index.php file containing the phpinfo() function.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>echo \"\" | sudo tee -a \/var\/www\/php83\/index.php\r\necho \"\" | sudo tee -a \/var\/www\/php84\/index.php<\/code><\/pre>\n<\/div>\n<p>Let\u2019s start the creation of VirtualHost. Apache keeps all the VirtualHost configuration files under \/etc\/apache2\/sites-available with the extension .conf .<br \/>\nCreate a file for the first virtual host and edit in your favorite text editor.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo nano \/etc\/apache2\/sites-available\/php83.example.com.conf<\/code><\/pre>\n<\/div>\n<p>Add the following content. Make sure to use the correct ServerName and directory path according to your setup. This website is configured to work with PHP 8.3.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>&lt;VirtualHost *:80&gt;\r\nServerName php83.example.com\r\nDocumentRoot \/var\/www\/php83\r\n&lt;Directory \/var\/www\/php83&gt;\r\nOptions -Indexes +FollowSymLinks +MultiViews\r\nAllowOverride All\r\nRequire all granted\r\n&lt;\/Directory&gt;\r\n&lt;FilesMatch \\.php$&gt;\r\nSetHandler \"proxy:unix:\/var\/run\/php\/php8.3-fpm.sock|fcgi:\/\/localhost\"\r\n&lt;\/FilesMatch&gt;\r\n&lt;\/VirtualHost&gt;<\/code><\/pre>\n<\/div>\n<p>Similarly, create a second VirtualHost configuration file to work with PHP 8.4.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo nano \/etc\/apache2\/sites-available\/php84.example.com.conf<\/code><\/pre>\n<\/div>\n<p>Add the following content to file with proper ServerName and DocumentRoot.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>&lt;VirtualHost *:80&gt;\r\nServerName php84.example.com\r\nDocumentRoot \/var\/www\/php84\r\n&lt;Directory \/var\/www\/php84&gt;\r\nOptions -Indexes +FollowSymLinks +MultiViews\r\nAllowOverride All\r\nRequire all granted\r\n&lt;\/Directory&gt;\r\n&lt;FilesMatch \\.php$&gt;\r\nSetHandler \"proxy:unix:\/var\/run\/php\/php8.4-fpm.sock|fcgi:\/\/localhost\"\r\n&lt;\/FilesMatch&gt;\r\n&lt;\/VirtualHost&gt;<\/code><\/pre>\n<\/div>\n<p>Both of the websites are configured now. But they are still not active. Apache keeps active sites under \/etc\/apache2\/sites-enabled directory. You can simply create a symbolic link of config files to this directory or use the below command to do the same.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo a2ensite php83.example.com\r\n$sudo a2ensite php84.example.com<\/code><\/pre>\n<\/div>\n<p>After making all the changes restart Apache to reload new settings changes.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo systemctl restart apache2<\/code><\/pre>\n<\/div>\n<h3>4. Test Setup<\/h3>\n<p>Edit \/etc\/hosts file on your local system and make an entry like below. This will resolve temporary names to localhost IP address.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>sudo nano \/etc\/hosts<\/code><\/pre>\n<\/div>\n<p>Add following entry to end of file<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>127.0.0.1 php83.example.com\r\n127.0.0.1 php84.example.com<\/code><\/pre>\n<\/div>\n<p>Open a web browser and visit both of the sites. You will see that php83.example.com shows the version PHP 8.3 and php84.example.com is showing the PHP 8.4 as the configuration.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>http:\/\/php83.example.com\r\nhttp:\/\/php84.example.com<\/code><\/pre>\n<\/div>\n<h3>OPTIONAL<\/h3>\n<p>Command to install most used modules on php:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>sudo apt install php8.4 libapache2-mod-php8.4 imagemagick php8.4-mcrypt php8.4-redis php8.4-dom ffmpeg php8.4-imagick php8.4-mysql php8.4-mysqli \r\nphp8.4-fpm php8.4-common php8.4-gd php8.4-curl php8.4-zip php8.4-xml php8.4-mbstring php8.4-bz2 php8.4-memcached php8.4-ldap php8.4-intl php8.4-sqlite3 -y <\/code><\/pre>\n<\/div>\n<p>To restart apache and php-fpm run this command:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>sudo systemctl restart apache2\r\nsudo service php8.4-fpm restart<\/code><\/pre>\n<\/div>\n<p>Replace 8.4 with your version.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Installing Apache2 sudo apt update sudo apt install apache2 libapache2-mod-fcgid Now enable a few modules required for the configuration of multiple PHP versions with Apache. These modules are necessary to integrate PHP FPM and FastCGI with the Apache server. sudo a2enmod actions fcgid alias proxy_fcgi 2. Installing PHP Versions sudo apt install software-properties-common sudo&hellip;<\/p>\n","protected":false},"author":1,"featured_media":624,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-5","description-off"],"_links":{"self":[{"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/posts\/620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/comments?post=620"}],"version-history":[{"count":5,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/posts\/620\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/posts\/620\/revisions\/665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/media\/624"}],"wp:attachment":[{"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/media?parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/categories?post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danychrys.go.ro\/web\/wp-json\/wp\/v2\/tags?post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}