Setup Apache, FastCGI and PHP on Debian/Ubuntu for hosting sites on different ports and PHP versions
Update the installed packages.
apt update
Install the Ondřej PHP repository.
apt install software-properties-commonsudo
add-apt-repository ppa:ondrej/php
apt update
Check that the repositories are correctly installed.
grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/*
apt install apache2
In this case, PHP 5.6 and 7.0 will be installed
# PHP 5.6
apt-get install php5.6 php5.6-bcmath php5.6-dev php5.6-cli php5.6-cgi php5.6-common php5.6-gd php5.6-json php5.6-mbstring php5.6-xml php5.6-zip
# PHP 7.0
apt-get install php7.0 php7.0-bcmath php7.0-dev php7.0-cli php7.0-cgi php7.0-common php7.0-gd php7.0-json php7.0-mbstring php7.0-xml php7.0-zip
# Ubuntu 16.04
apt install libapache2-mod-fastcgi
# Ubuntu 18.04
apt install libapache2-mod-fcgid
Two handlers will be created, one for each PHP version installed.
# PHP 5.6
vim /var/www/cgi-bin/php56.fastcgi
chmod +x /var/www/cgi-bin/php56.fastcgi
chown www-data:www-data /var/www/cgi-bin/php56.fastcgi
# PHP 7.0
vim /var/www/cgi-bin/php70.fastcgi
chmod +x /var/www/cgi-bin/php70.fastcgi
chown www-data:www-data /var/www/cgi-bin/php70.fastcgi
php56.fastcgi
#!/bin/bash
PHPRC="/etc/php/5.6/cgi/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/lib/cgi-bin/php5.6
php70.fastcgi
#!/bin/bash
PHPRC="/etc/php/7.0/cgi/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/lib/cgi-bin/php7.0
Change directory to the default web directory.
# Ubuntu 16.04
cd /var/www
# Ubuntu 18.04
cd /var/www/html
Create the folders each site.
mkdir php56-example php70-example
Create the index files for each site.
vim php56-example/index.php
vim php70-example/index.php
index.php
<?php phpinfo(); ?>
Open the configuration file on /etc/apache2/ports.conf and add the following ports.
Listen 81
Listen 82
Two vhosts will be created, one for each site, on ports 81 and 82 respectively.
cd /etc/apache2/sites-available
vim php56-example.conf
vim php70-example.conf
php56-example.conf
<VirtualHost *:81>
ServerName php56-example
ServerAdmin [email protected]
DocumentRoot /var/www/html/php56-example
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/html/php56-example">
Options +Indexes +FollowSymLinks +ExecCGI
AddHandler fcgid-script .php
FCGIWrapper /var/www/cgi-bin/php56.fastcgi .php
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
php70-example.conf
<VirtualHost *:82>
ServerName php70-example
ServerAdmin [email protected]
DocumentRoot /var/www/html/php70-example
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/html/php70-example">
Options +Indexes +FollowSymLinks +ExecCGI
AddHandler fcgid-script .php
FCGIWrapper /var/www/cgi-bin/php70.fastcgi .php
AllowOverride All
Order allow,deny
Allow from All
</Directory>
</VirtualHost>
Disable the default PHP modules.
a2dismod php*
Enable the necessary modules.
a2enmod actions alias fcgid proxy_fcgi
Disable the default site.
a2dissite 000-default
Enable the corresponding sites.
a2ensite php56-example
a2ensite php70-example
/etc/init.d/apache2 reload
sudo ufw allow 'Apache'
sudo ufw allow 80/tcp
sudo ufw allow 81/tcp
sudo ufw disable
sudo ufw enable
- Typical log locations
- /var/log/httpd/error_log
- [cgi:error] AH01215
- Description: Usually this is caused by incorrect line endings on the .fastcgi files. Change the end of line sequence from CRLF to LF or recreate the file using vim.
- References: 1