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

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/;
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
    # 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/
    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 8 super heroic Linux commands that you probably aren't using

Shaun Freeman @Zendmaster

I liked a @YouTube video How to Maximize Your Productivity (As a Software Developer or Learning Programming)

Shaun Freeman @Zendmaster

I liked a @YouTube video From Carnivore Diet to Near-Death: Lessons Learned From Today's Most Popular Fad Diet

Shaun Freeman @Zendmaster

I liked a @YouTube video Carnivore Diet – Myths about Meat – Phil Escott – WHIS 2018

Shaun Freeman @Zendmaster