OK. This is another article about installing Nginx in Linux, excuse me, in Linux cloud server. In the article, you will find lots of steps which are mentioned in other website. So, why do I write another article for this old fashion topic?

I am currently configuring my Linux server which were purchased two weeks ago from a cloud computer vendor, the most famous one. During the configuration process, I start from Google search and gather all necessary knowledge from several websites. The problem is here. There is no one website which can give me a full technical note to help me successfully setup the web service with Nginx. So here is the motivation for me to write this article.

Install Nginx on CentOS

You may ask why Nginx, but not apache. Please check this Nginx vs Apache article. Next question, why use CentOS? Good question. I don’t have any preferring Linux system, but I get this cloud Linux server with CentOS somehow, so I don’t have any choice but use CentOS. If you don’t know your Linux server information, please check this article, Linux Cloud Computing Server Howto. Now, let’s start to setup our Nginx on CentOS Linux cloud server.

#1. Add Nginx Repository

sudo yum install epel-release

#2. Install Nginx

sudo yum install nginx

#3. Start Nginx

sudo systemctl start nginx

Above three steps are found on here. However, I am unfortunately to get an error in step 3:

-bash: systemctl: command not found

It seems that systemctl is not a common utility in Linux system. Here I don’t want to figure out what it is. Just jump to the alternative way. Please try this:

sudo service nginx start

It works now. Let’s check the process of Nginx with following command:

ps aux | grep nginx

Unfortunately, error again! I hope you will not meet with these problem messages:

nginx: [alert] could not open error log file: open() “/var/log/nginx/error.log” failed (13: Permission denied)
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ
2016/12/14 01:06:35 [warn] 14899#0: the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:5
2016/12/14 01:06:35 [emerg] 14899#0: mkdir() “/var/lib/nginx/tmp/client_body” failed (13: Permission denied)

No doubt, it’s permission problem. Thanks Google. Here is the solution. First, stop the nginx we started just now with following command:

sudo service nginx stop

Then, let’s use this command to start nginx again:

sudo /etc/init.d/nginx start

Here we go. Let’s check the process again:

ps aux | grep nginx

Done! Nginx is up. By default Nginx will provide web service on port 80 (we can change this in configuration file later). We can use following command to check if the port 80 is opening.

netstat -anp | grep 80

Let’s start a browser and see if it really words. In ssh console, I don’t have browser, so I am using wget to test.


Here is what I get:

–2016-12-25 08:28:49–
Connecting to… connected.
HTTP request sent, awaiting response… 403 Forbidden
2016-12-25 08:28:49 ERROR 403: Forbidden.

This is another permission error. But different from previous error, this means Nginx doesn’t have permission to access the root folder. So we need to edit the nginx.conf to set a new root path.

Configure Nginx on CentOS

Let’s go to /etc/nginx/ folder and edit nginx.conf file. Basically, we don’t need to change this as we just need to find two line:

user nginx;

This is the user “nginx” which will be used to access the file system by nginx process. Therefore, we need to make sure there is a user nginx, usually with user group nginx. This user and user group will be used when we configure the php-fpm.

We can check if the user exist with following commnad:

grep nginx /etc/passwd


id nginx

If the user nginx doesn’t exist, we can use following command to create a new one:

sudo adduser --system --no-create-home --user-group -s /sbin/nologin nginx

The option –user-group will create a group with the same name as the user.

include /etc/nginx/conf.d/*.conf;

Nginx will load all modular configuration files from above files. So next we go to /etc/nginx/conf.d folder. In my folder, I find three .conf files:

  • default.conf
  • ssl.conf
  • virtual.conf

ssl.conf and virtual.conf are empty. Let’s edit default.conf file. In default.conf file, please find following line:

root /usr/share/nginx/html

Change to:

root /var/www;

The above statement means the root folder path. Now we change the root folder to a new path /var/www. Next step, let’s set the access permission to allow Nginx can access this folder. First, let’s change the folder owner to user nginx:

Sudo chown –R nginx:nginx /var/www

Next, change the access permission:

Sudo chmod –R 755 /var/www


Install MYSQL in Linux

As LEMP, the “M” indicates the MYSQL or MariaDB database. Here I will simply list the installation steps, errors and solutions for installing MYSQL/MariaDB on CentOS Linux.

#1. Try to install Mysql on CentOS

sudo yum install mysql-server

#2. Start Mysql Server on CentOS

systemctl start mysqld

However, I don’t know if you will meet with following error when you are trying to install the mysql server:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
No package mysql-server available.
Error: Nothing to do

Usually it is because the system cannot find the mysql repository. Alternatively, we can install the MariaDB instead with following command:

yum -y install mariadb-server mariadb

I don’t get any problem with above command.

#2. Start MariaDB
Next, let’s start MariaDB service before we setup it.

systemctl start mariadb

#3. Setup root Password
Now, let’s make our MariaDB more secure.


Install PHP in Linux

For different Linux distribution, installing PHP is easy, but configuration is quite different. For Ubuntu, I just follow the user guide which I got from Google. So far, I have not got any problems. So here I will not repeat the words again. Please search google with term like “install php7 in ubuntu nginx”.

I am just using following simple command to install the PHP-FPM:

yum install php-fpm

You can also use below command to install the PHP-cli:

yum install php

Currently, not all CentOS has PHP7 in the repository. So if you are running above command, it may install php 5.4, for example. You can use php -v to check the version you have installed.

Then, if an old version php is already installed, and you want to install the latest PHP, you need to uninstall the old version with following command:

yum remove php-cli mod_php php-common

After that, let’s add the IUS repository on CentOS.

cd ~
curl 'https://setup.ius.io/' -o setup-ius.sh
sudo bash setup-ius.sh

Then, you can search what the latest version of PHP is available to install:

yum search php7

Find the exact name of php7, then use following command to install php7:

yum install php74 php74-cli
yum install php74-fpm

Configure Nginx to Handle PHP with PHP-FPM

For CentOS Linux, I spend quite a lot of time to figure it out. Copy following config into default.conf file:

location ~ \.php$ {
include fastcgi-php.conf;
fastcgi_pass unix:/var/run/php-fpm.sock;

Next, go to folder /etc/nginx and create a file fastcgi-php.conf and paste following content inside:

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

Next, let’s edit /etc/php-fpm.d/www.conf file. Find:

listen =

Replace with:

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


#listen.owner = nobody
#listen.group = nobody
#listen.mode = 0666

Replace with:

listen.owner = nginx
listen.group = nginx
listen.mode = 0666

In the end, edit the /etc/php.ini file by replace following line:




Start the php-fpm service by following code:

systemctl start php-fpm


Addition: Install Python3 on CentOS

By default, CentOS has installed Python2.7. To install Python3, we can use following command:

sudo yum install epel-release
sudo yum install python34
curl -O https://bootstrap.pypa.io/get-pip.py
sudo /usr/bin/python3.4 get-pip.py
Previous PostNext Post


  1. In case someone is reading this in the future if you face the error [emerg] 14899#0: mkdir() “/var/lib/nginx/tmp/client_body” failed (13: Permission denied)

    Do the following:

    sudo mkdir /var/lib/nginx/tmp/

    sudo mkdir /var/lib/nginx/tmp/client_body

    sudo service nginx restart

    Thats should do it!

Leave a Reply

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