Skip to content

Instantly share code, notes, and snippets.

@RatserX
Last active August 10, 2019 07:30
Show Gist options
  • Save RatserX/b67ac5dc24e05ee6747950f40d232e7b to your computer and use it in GitHub Desktop.
Save RatserX/b67ac5dc24e05ee6747950f40d232e7b to your computer and use it in GitHub Desktop.
Setup Apache, FastCGI and PHP on CentOS/RHEL for hosting sites on different ports and PHP versions

Setup Apache, FastCGI and PHP on CentOS/RHEL for hosting sites on different ports and PHP versions

Prerequisites

Update the installed packages.

yum update

Install the EPEL repository.

# CentOS/RHEL 6
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
# CentOS/RHEL 7
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install epel-release

Install the REMI repository.

# CentOS/RHEL 6
rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-6.rpm
# CentOS/RHEL 7
rpm -Uvh https://rpms.remirepo.net/enterprise/remi-release-7.rpm

Check that the repositories are correctly installed.

yum repolist

Step 1 - Install Apache

yum install httpd

Step 2 - Install the PHP versions

In this case, PHP 5.6 and 7.0 will be installed

# PHP 5.6
yum install php56 php56-php-bcmath php56-cli php56-php-ctype php56-php-gd php56-php-json php56-php-mbstring php56-php-openssl php56-php-pdo php56-php-tokenizer php56-php-xml php56-php-zip
# PHP 7.0
yum install php70 php70-php-bcmath php70-cli php70-php-ctype php70-php-gd php70-php-json php70-php-mbstring php70-php-openssl php70-php-pdo php70-php-tokenizer php70-php-xml php70-php-zip

Step 3 - Install FastCGI

yum install mod_fcgid

Step 4 - Disable the default PHP handler

Open the configuration file on /etc/httpd/conf.d/php.conf and remove or comment the following lines.

<FilesMatch \.phps$>
    SetHandler application/x-httpd-php-source
</FilesMatch>

Step 5 - Setup the FastCGI handler for each PHP version

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 apache:apache /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 apache:apache /var/www/cgi-bin/php70.fastcgi

php56.fastcgi

#!/bin/bash

PHPRC="/opt/remi/php56/root/etc/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php56-cgi

php70.fastcgi

#!/bin/bash

PHPRC="/opt/remi/php70/root/etc/php.ini"
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=1000
export PHPRC
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec /usr/bin/php70-cgi

Step 6 - Configure the sites

Change directory to the default web directory.

# CentOS/RHEL 6
cd /var/www
# CentOS/RHEL 7
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(); ?>

Step 7 - Change the default directory index

Open the configuration file on /etc/httpd/conf/httpd.conf and make sure the DirectoryIndex is similar to the following.

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

Step 8 - Add the listen ports

Open the configuration file on /etc/httpd/conf/httpd.conf and add the following ports.

Listen 81
Listen 82

Step 9 - Setup the virtual hosts

Two vhosts will be created, one for each site, on ports 81 and 82 respectively.

cd /etc/httpd/conf.d
vim php56-example.conf
vim php70-example.conf

php56-example.conf

<VirtualHost *:81>
    ServerName php56-example
    ServerAdmin php56-example@example.com
    DocumentRoot /var/www/html/php56-example
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
    <Directory "/var/www/html/php56-example">
        Options +Indexes +FollowSymLinks +ExecCGI
        AddHandler php-fastcgi .php
        Action php-fastcgi /cgi-bin/php56.fastcgi
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

php70-example.conf

<VirtualHost *:82>
    ServerName php70-example
    ServerAdmin php70-example@example.com
    DocumentRoot /var/www/html/php70-example
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
    <Directory "/var/www/html/php70-example">
        Options +Indexes +FollowSymLinks +ExecCGI
        AddHandler php-fastcgi .php
        Action php-fastcgi /cgi-bin/php70.fastcgi
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

Step 10 - Restart Apache

service httpd restart

Step 11 - Setup the firewall

sudo firewall-cmd --permanent --add-port=81/tcp
sudo firewall-cmd --permanent --add-port=82/tcp
sudo firewall-cmd --reload

Troubleshooting

  • 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

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment