Install Nginx CentOS 7

Stop apache if running:

# systemctl stop httpd

Install epel if not installed:

# yum install epel-release
# yum install nginx
# systemctl enable nginx
# systemctl start nginx

If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

# firewall-cmd --permanent --zone=public --add-service=http 
# firewall-cmd --permanent --zone=public --add-service=https
# firewall-cmd --reload

Open in a web browser:


Configure gzip compression:

# nano /etc/nginx/conf.d/gzip.conf
# `gzip` Settings
gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/ application/x-font-ttf font/opentype image/svg+xml image/x-icon;


# systemctl restart nginx

Configure default site block (replace domain with actual domain name):

# nano /etc/nginx/conf.d/default.conf
server {
    listen       80;

    # note that these lines are originally from the "location /" block
    root   /home/domain/public_html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
location ~* \.(jpg|jpeg|gif|png)$ {
expires 365d;

location ~* \.(pdf|css|html|js|swf)$ {
expires 2d;


Change apache listening port and any other “.conf” files for virtual domains to listen on port 8080:

# nano /etc/httpd/conf/httpd.conf
Listen 8080

Restart apache:

#systemctl restart httpd

Restart ngnix:

#systemctl restart nginx

Test gzip:

curl -H "Accept-Encoding: gzip" -I http://localhost/test.html
HTTP/1.1 200 OK
Server: nginx/1.10.2
Date: Fri, 15 Sep 2017 00:45:27 GMT
Content-Type: html
Last-Modified: Thu, 08 Jun 2017 16:46:42 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: W/"59397f72-4ddf"
Content-Encoding: gzip

Install php handler

# yum install php php-mysql php-fpm

Configure the PHP Processor:

nano /etc/php.ini

Uncomment and set from 1 to 0:


Next, open the php-fpm configuration file www.conf:

# nano /etc/php-fpm.d/www.conf

Find the line that specifies the listen parameter, and change it so it looks like the following:

listen = /var/run/php-fpm/php-fpm.sock

Next, find the lines that set the listen.owner and and uncomment them. They should look like this:

listen.owner = nobody = nobody

Lastly, find the lines that set the user and group and change their values from “apache” to “nginx”:

user = nginx
group = nginx
Then save and quit.

Now, we just need to start our PHP processor by typing:

# systemctl start php-fpm

This will implement the change that we made.

Next, enable php-fpm to start on boot:

# systemctl enable php-fpm

Restart Nginx to make the necessary changes:

# systemctl restart nginx

Test PHP Processing on your Web Server

Create an info php in the root directory:

# nano /home/domain/public_html/info.php

Open in a web browser:

This page basically gives you information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

If this was successful, then your PHP is working as expected.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.