Installing Nginx in CentOS 7

Installing Nginx in CentOS 7

Following on from my last post of upgrading MySQL to MariaDB my next task was to install Nginx as a replacement web server to Apache. I've been using Nginx for over a year now and am really impressed with it's performance compared to Apache. I will also be enabling Nginx to use OpenCart.

With that installing it on CentOS 7 is doddle, first thing is to import the repository

sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

Then install it using yum

sudo yum install nginx

then start and enable the service so it starts at boot time

sudo systemctl start nginx
sudo systemctl enable nginx

That's the basic we will need to add some basic configuration so we need to edit '/etc/nginx/nginx.conf' and replace it's contents with

worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
user nginx;

events {
    worker_connections 1024;
    multi_accept on;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    server_names_hash_bucket_size 64;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}

We will store our website config files in the '/etc/nginx/conf.d' directory next we will enable site config which is using OpenCart, now my website root directory is in the '/home/centos/public' directory which is running under a system user called 'centos' (just substitute 'centos' with the user you are using) so if the user is not there we will have to add them so in the terminal

sudo adduser centos

Then add a password

sudo passwd centos

And type your password when prompted. We will also need to make our public directory and make sure our user directories has all the right permissions so these commands should do it.

sudo chmod 755 /home/centos
sudo mkdir /home/centos/public
sudo chown -R centos:centos /home/centos/public
sudo chmod 755 /home/centos/public

Now we can add our OpenCart Nginx config file so open the file with

sudo nano /etc/nginx/conf.d/opencart.conf

and put this in it and save the file

server {
    listen 80;

    root /home/centos/public;
    index index.html index.php;

    # Make site accessible from http://localhost/
    server_name my-opencart-website.com
    client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$is_args$args;
    }

    # START  Rewrite rules
    rewrite ^/sitemap.xml$ /index.php?route=feed/google_sitemap last;
    rewrite ^/googlebase.xml$ /index.php?route=feed/google_base last;
    rewrite ^/download/(.*) /index.php?route=error/not_found last;

    if (!-f $request_filename) {
        set $rule_3 1$rule_3;
    }

    if (!-d $request_filename) {
        set $rule_3 2$rule_3;
    }

    if ($uri !~ ".*.(ico|gif|jpg|jpeg|png|js|css)") {
        set $rule_3 3$rule_3;
    }

    if ($rule_3 = "321") {
        rewrite ^/([^?]*) /index.php?_route_=$1 last;
    }
    # END  Rewrite rules

    location  ~ \.php$ {
        try_files $uri =404;
        proxy_buffer_size 128k;
        proxy_buffers 4 128k;

        fastcgi_buffers 16 128k;
        fastcgi_buffer_size 128k;
        fastcgi_pass   unix:/var/run/php-fpm.sock;
        fastcgi_read_timeout 1200;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Dont worry too much about the location block at line 37 as this just deals with the PHP handling also the rewrite rules are because OpenCart has weird rules for routing page and SEO urls.

Now we have done that we can restart Nginx with

sudo service nginx restart

You can test the configuration by putting a 'index.html' file in the public folder and you should see it working, we can't yet upload OpenCart or test PHP yet because we will need to compile that, I will deal with that in my next post.

Any question please use the comments and I will try my best in answering.


25/01/2016 22:50:00 Shaun Freeman Filed Under: MySQL CentOS, Linux, Nginx, Server

Twitter Feed
Shaun Freeman @Zendmaster

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/Uu3RzICAjp Installing a Hyper 212 EVO on AMD AM3+

Shaun Freeman @Zendmaster

Setting Up A Test Environment For PHP In VirtualBox - Part 6 Installing Multiple PHP Versionss: https://t.co/xNXeX4XDlQ via @YouTube

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/xNXeX4XDlQ Setting Up A Test Environment For PHP In VirtualBox - Part 6 Installing

Shaun Freeman @Zendmaster

Banished LP #1 - Humble Beginnings: https://t.co/5riUkijyLq via @YouTube

Shaun Freeman @Zendmaster

Installing Banished on Ubuntu 18.04: https://t.co/5judq1fMSc via @YouTube