Skip to content

Instantly share code, notes, and snippets.

@RatserX
Last active December 23, 2019 23:08
Show Gist options
  • Save RatserX/5572539e9c3d37ccc64f8dad326eccf3 to your computer and use it in GitHub Desktop.
Save RatserX/5572539e9c3d37ccc64f8dad326eccf3 to your computer and use it in GitHub Desktop.
Setup NGINX, FastCGI and PHP on CentOS/RHEL for hosting sites on different ports and PHP versions

Setup NGINX, 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 NGINX

yum install nginx

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-fpm 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-fpm 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 - Setup PHP-FPM for each PHP version

Setup the permissions and listen parameters on the configuration files for PHP-FPM.

# PHP 5.6
vim /opt/remi/php56/root/etc/php-fpm.d/www.conf
# PHP 7.0
vim /etc/opt/remi/php70/php-fpm.d/www.conf

www.conf

user = nginx
group = nginx

php56/root/etc/php-fpm.d/www.conf

listen = 127.0.0.1:9000

php70/root/etc/php-fpm.d/www.conf

listen = 127.0.0.1:9001

Step 4 - Configure the sites

Change directory to the default web directory.

# CentOS/RHEL 6
cd /usr/share/nginx/www
# CentOS/RHEL 7
cd /usr/share/nginx/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 var_export($_SERVER); phpinfo(); ?>

Step 5 - Setup the server blocks

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

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

php56-example.conf

server {
    listen 81 default_server;

    server_name _;

    root /usr/share/nginx/html/php56-example;
    
    #error_page 404 errors/404.html;
    access_log /var/log/nginx/php56-example.access.log;

    index index.php index.html index.htm;
    
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }
    
    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        #fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param PHP_FCGI_CHILDREN 4;
        fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
    }
    
    location ~ /\.ht {
        deny  all;
    }
}

php70-example.conf

server {
    listen 82 default_server;

    server_name _;

    root /usr/share/nginx/html/php70-example;
    
    #error_page 404 errors/404.html;
    access_log /var/log/nginx/php70-example.access.log;

    index index.php index.html index.htm;
    
    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log off;
        expires max;
    }
    
    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        #fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_pass 127.0.0.1:9001;
        fastcgi_param PHP_FCGI_CHILDREN 4;
        fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
    }
    
    location ~ /\.ht {
        deny  all;
    }
}

Step 6 - Check the NGINX configuration

nginx -t

Step 7 - Configure SELinux

Add the site ports to the whitelist

# Server
semanage port -a -t http_port_t -p tcp 81
semanage port -a -t http_port_t -p tcp 82
# Socket
semanage port -m -t http_port_t -p tcp 9000
semanage port -m -t http_port_t -p tcp 9001

Check the port whitelist

semanage port -l | grep http_port_t

Step 8 - Restart NGINX

systemctl enable nginx
systemctl restart nginx

Step 9 - Restart PHP-FPM

systemctl enable php56-php-fpm php70-php-fpm
systemctl restart php56-php-fpm php70-php-fpm

Step 10 - 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/nginx/error.log
  • [error] 121177#0: *1467 upstream timed out
    • This is caused by a timeout from either a regular or a fastcgi upstream. You can increase either the proxy_read_timeout or the fastcgi_read_timeout to fix it.
    • References: 1

References

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