Ngnix Web Server

Submitted by damar on Wed, 06/28/2017 - 13:31

Damar Thapa


Legal Notice

Copyright © 2017 | DT's Den |
Disclaimer: I provide no warranty of any kind, express or implied, that the information provided is complete, accurate, and reliable. So, you use the information at your own risk.


This article is to document the procedure I took to setup NginX Web server to use as a local server for testing purposes. This article is to get started using NginX, so it does not replaces any, official or unofficial, NginX tutorials or manuals.

1. Introduction

This week, after uploading the site to the external server, I sat down to clean up my local server, and create an environment for future testing and upgrading purposes. Seeing the mess and the slow response of the server partly because of this, I decided to have a thorough clean up and replace Apache web server with Nginx.
The Apache Web Server is a flexible, modular and powerful system, and I have been using this for all my projects (except in few occasions). For a long time, from the early years of the Internet to 2015/2016, the Apache web server was the leading web server on the Internet. This position now has been taken by Microsoft's Internet Information Services (IIS), and there are other players in the market.
The Nginx web server is one of the emerging web servers, gaining popularity, in recent years. It is a free, open-source, system, like Apache. It, however, took the side of performance over flexibility in its design, giving a clear choice (flexibility vs. performance) for the Open Source communities.
I am aiming to try Nginx locally before implementing in the external servers!

2. Installing Nginx

I am running Ubuntu 16.04 server, and to install Nginx server, I ran the following commands on my bash terminal:
:~$ sudo apt-get update
:~$ sudo apt-get install nginx
And, the following commands are to start, restart and stop the Nginx:
:~$ sudo systemctl start nginx
:~$ sudo systemctl restart nginx
:~$ sudo systemctl stop nginx>

3. Configuring Nginx

The Nginx has one master process and multiple work processes.
  • Master Process: It reads and evaluates configuration, and maintains worker processes.
  • Worker Processes: They perform the actual jobs.
The Nginx configuration file, named 'nginx.conf' by default, can be in one of the following locations:
  • /usr/local/nginx/conf,
  • /etc/nginx, or
  • /usr/local/etc/nginx
The configuration file defines directives that control the Nginx and its modules. There are two types of directives:
  • Simple Directives, and
  • Block Directives
A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;).  A block directive defines a block of directives within '{ ' and '}'.
The configuration file, in my system, is found to be /etc/nginx/nginx.conf with the following content:
user www-data;
worker_processes auto;
pid /run/;

events {
	worker_connections 768;
	   # multi_accept on;

http {
	# Basic Settings
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

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

	# SSL Settings

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	# Logging Settings

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
       # Gzip Settings

       gzip on;
       gzip_disable "msie6";

       # gzip_vary on;
       # gzip_proxied any;
       # gzip_comp_level 6;
       # gzip_buffers 16 8k;
       # gzip_http_version 1.1;
       # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

       client_max_body_size 50M;
       # Virtual Host Configs

       include /etc/nginx/conf.d/*.conf;
       include /etc/nginx/sites-enabled/*;
In the configuration file above, user, worker_processes and pid are simple directives, while events, http , etc are block directives.
A line that starts with '#' is a single line comment.
In the above configuration file, under http block directive, the following two lines are included:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
The above two lines ensure that nginx reads all files with the extension of '.conf' from '/etc/nginx/conf.d' folder, and any files (regardless of their extensions, or no extensions) from /etc/nginx/sites-enabled' folder.
In my system, it does not have any files in '/etc/nginx/conf.d' folder, but there is one file, named 'default', in '/etc/nginx/sites-enabled/ folder.
The content of the above-said '/etc/nginx/sites-enabled/default' file:
 server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# root /var/www/html;

	root /home/damar/www/nginx/html;

	# Add index.php to the list if you are using PHP
	index index.php index.html index.htm index.nginx-debian.html;

	server_name localhost;

	location / {
		index index.php;
		try_files $uri $uri/ /index.php$is_args$args;
	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	location ~ /\.ht {
		deny all;
In the above default file, 'root' directive has played the role of 'DocumentRoot' directive of Apache Server configuration. It defines the location (directory) in the web server where the web contents should reside.

4. Installation and Configuration of php7.1-fpm

The “PHP-FastCGI process manager”, php-fpm, is a FastCGI implementation for PHP that a web server, like Nginx, can use to serve its contents.
The php 7.1 is already running in my system, so I installed php7.1-fpm with the following command:
:~$ sudo apt-get install php7.1-fpm
To configure php processor, I set the following in /etc/php/7.1/fpm/php.ini file:
Setting 'cgi.fix_pathinfo' to zero sets 'PATH_TRANSLATED' to 'SCRIPT_FILENAME', ignoring 'PATH_INFO'.
To make ngnix to use php-fpm, I added the following directive in /etc/ngnix/sites-available/default file:
# pass the PHP scripts to FastCGI server listening on
location ~ \.php$ {
	include snippets/fastcgi-php.conf;
	fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;		
The above included conf file, snippets/fastcgi-php.conf, has the following content:
# 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;  
And, the content of the above included fastcgi.conf file:
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200; 
By the way, php7.1-fpm runs as a daemon, like Nginx server, so it has to be started to use it, and reloaded or restarted to reflect any changes in the configuration files.
:~$ sudo  service php7.1-fpm start
:~$ sudo  service php7.1-fpm restart
:~$ sudo  service php7.1-fpm stop 

5. Testing Configuration

After restarting ngnix and php7.1-fpm services, the system can be tested with an php file with the following content:
The screenshot of the output of the above php file should be similar to the following:

6. Summary

This article covers the process of installing and configuring Ngnix Web Server and php-fpm under Ubuntu 16.04 server.
The future article will cover the installation of Drupal 8 on this web server.

A. Revision History

Revision History
Revision 0.0-0 Wed, 28 June 2017 Damar Thapa