Install Nginx and PHP in Linux Cloud Computing Sever

On January 5, 2017, in Other Online Technology, by James Liu

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:

pa 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.

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 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”.

However, 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:
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 = nobody
#listen.mode = 0666

Replace with:

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

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






Leave a Reply

Free WordPress Themes

WordPress Blog