How to install and setup Nginx on Debian Linux 9

Posted on in Categories , , last updated August 3, 2017

How do I install and configure Nginx web server on a Debian Linux 9 server running on the cloud?

Nginx is a free and open source HTTP/HTTPS web server. It can also work as a proxy server. Nginx used as a reverse proxy server for HTTP, HTTPS, and other protocols. You can use Nginx instead of traditional Apache HTTP 2 server. This tutorial shows you how to install and setup Nginx web server on your Debian 9 server.

Step 1: Install Nginx HTTP server

Type the following apt-get command/apt command to update your system:
$ sudo apt-get update
$ sudo apt-get upgrade

Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  libdns-export162 libisc-export160
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 1186 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://security.debian.org stretch/updates/main amd64 libisc-export160 amd64 1:9.10.3.dfsg.P4-12.3+deb9u2 [334 kB]
Get:2 http://security.debian.org stretch/updates/main amd64 libdns-export162 amd64 1:9.10.3.dfsg.P4-12.3+deb9u2 [851 kB]
Fetched 1186 kB in 0s (3744 kB/s)         
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 8419 files and directories currently installed.)
Preparing to unpack .../libisc-export160_1%3a9.10.3.dfsg.P4-12.3+deb9u2_amd64.deb ...
Unpacking libisc-export160 (1:9.10.3.dfsg.P4-12.3+deb9u2) over (1:9.10.3.dfsg.P4-12.3+deb9u1) ...
Preparing to unpack .../libdns-export162_1%3a9.10.3.dfsg.P4-12.3+deb9u2_amd64.deb ...
Unpacking libdns-export162 (1:9.10.3.dfsg.P4-12.3+deb9u2) over (1:9.10.3.dfsg.P4-12.3+deb9u1) ...
Setting up libisc-export160 (1:9.10.3.dfsg.P4-12.3+deb9u2) ...
Setting up libdns-export162 (1:9.10.3.dfsg.P4-12.3+deb9u2) ...
Processing triggers for libc-bin (2.24-11+deb9u1) ...

To install Nginx server, type:
$ sudo apt-get install nginx
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libexpat1 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu57 libjbig0 libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo
  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5 libwebp6
  libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4 libxslt1.1 nginx-common nginx-full sgml-base xml-core
Suggested packages:
  libgd-tools geoip-bin fcgiwrap nginx-doc ssl-cert sgml-base-doc debhelper
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core geoip-database libexpat1 libfontconfig1 libfreetype6 libgd3 libgeoip1 libicu57 libjbig0 libjpeg62-turbo libnginx-mod-http-auth-pam libnginx-mod-http-dav-ext libnginx-mod-http-echo
  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-subs-filter libnginx-mod-http-upstream-fair libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libpng16-16 libssl1.1 libtiff5 libwebp6
  libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4 libxslt1.1 nginx nginx-common nginx-full sgml-base xml-core
0 upgraded, 38 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.5 MB of archives.
After this operation, 58.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://security.debian.org stretch/updates/main amd64 libexpat1 amd64 2.2.0-2+deb9u1 [83.4 kB]
Get:2 http://cdn-fastly.deb.debian.org/debian stretch/main amd64 libxau6 amd64 1:1.0.8-1 [20.7 kB]
....
..
..
Setting up libxpm4:amd64 (1:3.5.12-1) ...
Setting up libgd3:amd64 (2.2.4-2) ...
Setting up libnginx-mod-http-image-filter (1.10.3-1+deb9u1) ...
Setting up nginx-full (1.10.3-1+deb9u1) ...
Setting up nginx (1.10.3-1+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u1) ...
Processing triggers for sgml-base (1.29) ...

By default Nginx server started on Debian 9 and it can be verified with the netstat command/ss command and ps command as follows:
$ ps aux | grep nginx
$ pgrep nginx

www-data  1099  0.0  0.0 159836  3100 ?        S    10:11   0:00 nginx: worker process
www-data  1100  0.0  0.0 159836  3100 ?        S    10:11   0:00 nginx: worker process
www-data  1101  0.0  0.0 159836  3100 ?        S    10:11   0:00 nginx: worker process

Use the netstat command as follows to verify that port 80 is open:
$ netstat -tulpn | grep :80
Sample outputs:

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1098/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      1098/nginx: master

Step 2: Start/stop/restart nginx command

To enable Nginx on boot run the following systemctl command:
$ sudo systemctl enable nginx

Synchronizing state of nginx.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable nginx

Restart nginx server command

$ sudo systemctl restart nginx

Stop nginx server command

$ sudo systemctl stop nginx

Start nginx server command

$ sudo systemctl start nginx

Find status of nginx server command

$ sudo systemctl status nginx
Sample outputs:

* nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-08-03 10:18:49 UTC; 32s ago
     Docs: man:nginx(8)
  Process: 1361 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 1363 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 1362 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 1364 (nginx)
    Tasks: 21 (limit: 4915)
   CGroup: /system.slice/nginx.service
           ??1364 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ??1365 nginx: worker process
           ??1366 nginx: worker process
           ??1367 nginx: worker process
           ??1368 nginx: worker process
           ??1369 nginx: worker process
           ??1370 nginx: worker process
           ??1371 nginx: worker process
           ??1372 nginx: worker process
           ??1373 nginx: worker process
           ??1374 nginx: worker process
           ??1375 nginx: worker process
           ??1376 nginx: worker process
           ??1377 nginx: worker process
           ??1378 nginx: worker process
           ??1379 nginx: worker process
           ??1380 nginx: worker process
           ??1381 nginx: worker process
           ??1382 nginx: worker process
           ??1383 nginx: worker process
           ??1384 nginx: worker process
 
Aug 03 10:18:49 debian9-www systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 03 10:18:49 debian9-www systemd[1]: Started A high performance web server and a reverse proxy server.

Step 3: Test it

Fire a web browser and type url:
http://your-domain/
Or find your public IP address using the ip command/ifconfig command:
$ ip a
OR
$ ifconfig eth0
Sample outputs:

eth0      Link encap:Ethernet  HWaddr f2:3c:91:60:1d:cb  
          inet addr:45.33.115.105  Bcast:45.33.115.255  Mask:255.255.255.0
          inet6 addr: 2600:3c00::f03c:91ff:fe60:1dcb/64 Scope:Global
          inet6 addr: fe80::f03c:91ff:fe60:1dcb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:152860323 errors:0 dropped:0 overruns:0 frame:0
          TX packets:194050118 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:555971036475 (555.9 GB)  TX bytes:107798769402 (107.7 GB)

Fire a web browser and type url:
http://45.33.115.105/
Sample outputs:

Fig.01: Testing your Nginx  web server on Debian 9
Fig.01: Testing your Nginx web server on Debian 9

Step 4: Configure Nginx web server

By default all HTML/css/images files are stored in /var/www/html/ directory. This is known as the root of your web server. It is defined in /etc/nginx/sites-enabled/default config file. You can edit it with either vim command/nano command
$ sudo vi /etc/nginx/sites-enabled/default
OR
$ sudo nano /etc/nginx/sites-enabled/default
The /etc/nginx/nginx.conf is your main nginx config file and /etc/nginx/sites-enabled/default is default config file.
$ sudo nano /etc/nginx/nginx.conf

Creating your own config file

First create a user named cbzwww for www.cyberciti.biz domain using the useradd command:
$ sudo useradd -c "cyberciti.biz user" -d /home/nginx-www.cyberciti.biz -s /usr/sbin/nologin -m cbzwww
To lock the password for the cbzwww account, enter:
$ sudo passwd -l cbzwww
passwd: password expiry information changed.

Verify it:
$ ls -l /home/
Sample outputs:

total 4
drwxr-xr-x 2 cbzwww cbzwww 4096 Aug  3 10:37 nginx-www.cyberciti.biz

To create a Virtual Hosts named www.cyberciti.biz, enter;
$ sudo nano /etc/nginx/sites-available/www.cyberciti.biz.conf
Append the basic config:

server {
	listen 80;
	listen [::]:80;
 
	server_name www.cyberciti.biz;
 
	root /home/nginx-www.cyberciti.biz;
	index index.html;
 
	location / {
		try_files $uri $uri/ =404;
	}
}

Save and close the file. You must create a soft link as follows:
$ sudo ln -s /etc/nginx/sites-available/www.cyberciti.biz.conf /etc/nginx/sites-enabled/www.cyberciti.biz.conf
Finally test and reload server:
$ sudo nginx -t
$ sudo systemctl reload nginx

Now create a test file at
$ sudo nano /home/nginx-www.cyberciti.biz/index.html
Append the following:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nixCraft!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to www.cyberciti.biz!</h1>
<p>This is a test for Nginx virtual host.</p>
 
<p><em>Thank you for using nixCraft tutorials.</em></p>
</body>
</html>

Save and close the file. Set correct permissions using chmod command and chown command:
$ sudo chown cbzwww:cbzwww /home/nginx-www.cyberciti.biz/index.html
$ sudo chmod 0444 /home/nginx-www.cyberciti.biz/index.html
$ ls -l /home/nginx-www.cyberciti.biz/index.html
-r--r--r-- 1 cbzwww cbzwww 374 Aug 3 10:52 /home/nginx-www.cyberciti.biz/index.html

Test it by typing url:
http://www.cyberciti.biz/
Sample outputs:

Fig.02: Nginx web server  virtual domain test
Fig.02: Nginx web server virtual domain test

And there you have it, Nginx server installed and configured on Debian 9 server.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment