Tag Archives: how to install nginx in linux

Nginx installation on Linux server

Step by step Nginx installation on YUM or APT based Linux servers. Know about configuration files, log files, and directory listing in Nginx.

nginx installation in linux

Nginx is a popular web server and preferred over Apache. Many websites and blogs today run on Nginx webserver. In this article, we are going to see how to install Nginx in Linux.

Nginx Installation

Nginx is available for install in EPEL repositories for YUM based Linux systems and in default repositories of APT (Debian) based systems. Depending on your system type install the Nginx package. Sample outputs below :

# yum install nginx
Loaded plugins: amazon-id, rhui-lb, search-disabled-repos, security
Setting up Install Process
epel/metalink                                                                                                                         | 4.4 kB     00:00
epel                                                                                                                                  | 4.3 kB     00:00
https://mirrors.tuna.tsinghua.edu.cn/epel/6/x86_64/repodata/repomd.xml: [Errno -1] repomd.xml does not match metalink for epel
Trying other mirror.
epel                                                                                                                                  | 4.3 kB     00:00
rhui-REGION-client-config-server-6                                                                                                    | 2.9 kB     00:00
rhui-REGION-rhel-server-releases                                                                                                      | 3.5 kB     00:00
rhui-REGION-rhel-server-releases-optional                                                                                             | 3.5 kB     00:00
rhui-REGION-rhel-server-rh-common                                                                                                     | 3.8 kB     00:00
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.10.2-1.el6 will be installed
--> Processing Dependency: nginx-filesystem = 1.10.2-1.el6 for package: nginx-1.10.2-1.el6.x86_64
--> Processing Dependency: nginx-all-modules = 1.10.2-1.el6 for package: nginx-1.10.2-1.el6.x86_64
--> Processing Dependency: nginx-filesystem for package: nginx-1.10.2-1.el6.x86_64
--> Running transaction check
---> Package nginx-all-modules.noarch 0:1.10.2-1.el6 will be installed
--> Processing Dependency: nginx-mod-stream = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
--> Processing Dependency: nginx-mod-mail = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
--> Processing Dependency: nginx-mod-http-xslt-filter = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
--> Processing Dependency: nginx-mod-http-perl = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
--> Processing Dependency: nginx-mod-http-image-filter = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
--> Processing Dependency: nginx-mod-http-geoip = 1.10.2-1.el6 for package: nginx-all-modules-1.10.2-1.el6.noarch
---> Package nginx-filesystem.noarch 0:1.10.2-1.el6 will be installed
--> Running transaction check
---> Package nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6 will be installed
--> Processing Dependency: GeoIP for package: nginx-mod-http-geoip-1.10.2-1.el6.x86_64
--> Processing Dependency: libGeoIP.so.1()(64bit) for package: nginx-mod-http-geoip-1.10.2-1.el6.x86_64
---> Package nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6 will be installed
--> Processing Dependency: gd for package: nginx-mod-http-image-filter-1.10.2-1.el6.x86_64
--> Processing Dependency: libgd.so.2()(64bit) for package: nginx-mod-http-image-filter-1.10.2-1.el6.x86_64
---> Package nginx-mod-http-perl.x86_64 0:1.10.2-1.el6 will be installed
---> Package nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6 will be installed
---> Package nginx-mod-mail.x86_64 0:1.10.2-1.el6 will be installed
---> Package nginx-mod-stream.x86_64 0:1.10.2-1.el6 will be installed
--> Running transaction check
---> Package GeoIP.x86_64 0:1.6.5-1.el6 will be installed
--> Processing Dependency: geoipupdate for package: GeoIP-1.6.5-1.el6.x86_64
--> Processing Dependency: GeoIP-data for package: GeoIP-1.6.5-1.el6.x86_64
---> Package gd.x86_64 0:2.0.35-11.el6 will be installed
--> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-11.el6.x86_64
--> Running transaction check
---> Package GeoIP-GeoLite-data.noarch 0:2017.01-1.el6 will be installed
--> Processing Dependency: GeoIP-GeoLite-data-extra = 2017.01-1.el6 for package: GeoIP-GeoLite-data-2017.01-1.el6.noarch
---> Package geoipupdate.x86_64 0:2.2.1-2.el6 will be installed
---> Package libXpm.x86_64 0:3.5.10-2.el6 will be installed
--> Running transaction check
---> Package GeoIP-GeoLite-data-extra.noarch 0:2017.01-1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================
 Package                                      Arch                    Version                        Repository                                         Size
=============================================================================================================================================================
Installing:
 nginx                                        x86_64                  1.10.2-1.el6                   epel                                              462 k
Installing for dependencies:
 GeoIP                                        x86_64                  1.6.5-1.el6                    epel                                              113 k
 GeoIP-GeoLite-data                           noarch                  2017.01-1.el6                  epel                                              468 k
 GeoIP-GeoLite-data-extra                     noarch                  2017.01-1.el6                  epel                                               23 M
 gd                                           x86_64                  2.0.35-11.el6                  rhui-REGION-rhel-server-releases                  142 k
 geoipupdate                                  x86_64                  2.2.1-2.el6                    epel                                               28 k
 libXpm                                       x86_64                  3.5.10-2.el6                   rhui-REGION-rhel-server-releases                   51 k
 nginx-all-modules                            noarch                  1.10.2-1.el6                   epel                                              7.7 k
 nginx-filesystem                             noarch                  1.10.2-1.el6                   epel                                              8.5 k
 nginx-mod-http-geoip                         x86_64                  1.10.2-1.el6                   epel                                               14 k
 nginx-mod-http-image-filter                  x86_64                  1.10.2-1.el6                   epel                                               16 k
 nginx-mod-http-perl                          x86_64                  1.10.2-1.el6                   epel                                               26 k
 nginx-mod-http-xslt-filter                   x86_64                  1.10.2-1.el6                   epel                                               16 k
 nginx-mod-mail                               x86_64                  1.10.2-1.el6                   epel                                               43 k
 nginx-mod-stream                             x86_64                  1.10.2-1.el6                   epel                                               36 k

Transaction Summary
=============================================================================================================================================================
Install      15 Package(s)

Total download size: 24 M
Installed size: 49 M
Is this ok [y/N]: y
Downloading Packages:
(1/15): GeoIP-1.6.5-1.el6.x86_64.rpm                                                                                                  | 113 kB     00:00
(2/15): GeoIP-GeoLite-data-2017.01-1.el6.noarch.rpm                                                                                   | 468 kB     00:00
(3/15): GeoIP-GeoLite-data-extra-2017.01-1.el6.noarch.rpm                                                                             |  23 MB     00:01
(4/15): gd-2.0.35-11.el6.x86_64.rpm                                                                                                   | 142 kB     00:00
(5/15): geoipupdate-2.2.1-2.el6.x86_64.rpm                                                                                            |  28 kB     00:00
(6/15): libXpm-3.5.10-2.el6.x86_64.rpm                                                                                                |  51 kB     00:00
(7/15): nginx-1.10.2-1.el6.x86_64.rpm                                                                                                 | 462 kB     00:00
(8/15): nginx-all-modules-1.10.2-1.el6.noarch.rpm                                                                                     | 7.7 kB     00:00
(9/15): nginx-filesystem-1.10.2-1.el6.noarch.rpm                                                                                      | 8.5 kB     00:00
(10/15): nginx-mod-http-geoip-1.10.2-1.el6.x86_64.rpm                                                                                 |  14 kB     00:00
(11/15): nginx-mod-http-image-filter-1.10.2-1.el6.x86_64.rpm                                                                          |  16 kB     00:00
(12/15): nginx-mod-http-perl-1.10.2-1.el6.x86_64.rpm                                                                                  |  26 kB     00:00
(13/15): nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64.rpm                                                                           |  16 kB     00:00
(14/15): nginx-mod-mail-1.10.2-1.el6.x86_64.rpm                                                                                       |  43 kB     00:00
(15/15): nginx-mod-stream-1.10.2-1.el6.x86_64.rpm                                                                                     |  36 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                        6.1 MB/s |  24 MB     00:03
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : GeoIP-GeoLite-data-extra-2017.01-1.el6.noarch                                                                                            1/15
  Installing : GeoIP-GeoLite-data-2017.01-1.el6.noarch                                                                                                  2/15
  Installing : nginx-filesystem-1.10.2-1.el6.noarch                                                                                                     3/15
  Installing : geoipupdate-2.2.1-2.el6.x86_64                                                                                                           4/15
  Installing : GeoIP-1.6.5-1.el6.x86_64                                                                                                                 5/15
  Installing : libXpm-3.5.10-2.el6.x86_64                                                                                                               6/15
  Installing : gd-2.0.35-11.el6.x86_64                                                                                                                  7/15
  Installing : nginx-mod-http-geoip-1.10.2-1.el6.x86_64                                                                                                 8/15
  Installing : nginx-mod-stream-1.10.2-1.el6.x86_64                                                                                                     9/15
  Installing : nginx-mod-http-perl-1.10.2-1.el6.x86_64                                                                                                 10/15
  Installing : nginx-mod-http-image-filter-1.10.2-1.el6.x86_64                                                                                         11/15
  Installing : nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64                                                                                          12/15
  Installing : nginx-1.10.2-1.el6.x86_64                                                                                                               13/15
  Installing : nginx-mod-mail-1.10.2-1.el6.x86_64                                                                                                      14/15
  Installing : nginx-all-modules-1.10.2-1.el6.noarch                                                                                                   15/15
  Verifying  : nginx-mod-mail-1.10.2-1.el6.x86_64                                                                                                       1/15
  Verifying  : GeoIP-1.6.5-1.el6.x86_64                                                                                                                 2/15
  Verifying  : nginx-mod-http-geoip-1.10.2-1.el6.x86_64                                                                                                 3/15
  Verifying  : libXpm-3.5.10-2.el6.x86_64                                                                                                               4/15
  Verifying  : nginx-mod-stream-1.10.2-1.el6.x86_64                                                                                                     5/15
  Verifying  : nginx-all-modules-1.10.2-1.el6.noarch                                                                                                    6/15
  Verifying  : GeoIP-GeoLite-data-2017.01-1.el6.noarch                                                                                                  7/15
  Verifying  : nginx-mod-http-perl-1.10.2-1.el6.x86_64                                                                                                  8/15
  Verifying  : nginx-mod-http-image-filter-1.10.2-1.el6.x86_64                                                                                          9/15
  Verifying  : nginx-1.10.2-1.el6.x86_64                                                                                                               10/15
  Verifying  : geoipupdate-2.2.1-2.el6.x86_64                                                                                                          11/15
  Verifying  : GeoIP-GeoLite-data-extra-2017.01-1.el6.noarch                                                                                           12/15
  Verifying  : nginx-filesystem-1.10.2-1.el6.noarch                                                                                                    13/15
  Verifying  : gd-2.0.35-11.el6.x86_64                                                                                                                 14/15
  Verifying  : nginx-mod-http-xslt-filter-1.10.2-1.el6.x86_64                                                                                          15/15

Installed:
  nginx.x86_64 0:1.10.2-1.el6

Dependency Installed:
  GeoIP.x86_64 0:1.6.5-1.el6                             GeoIP-GeoLite-data.noarch 0:2017.01-1.el6      GeoIP-GeoLite-data-extra.noarch 0:2017.01-1.el6
  gd.x86_64 0:2.0.35-11.el6                              geoipupdate.x86_64 0:2.2.1-2.el6               libXpm.x86_64 0:3.5.10-2.el6
  nginx-all-modules.noarch 0:1.10.2-1.el6                nginx-filesystem.noarch 0:1.10.2-1.el6         nginx-mod-http-geoip.x86_64 0:1.10.2-1.el6
  nginx-mod-http-image-filter.x86_64 0:1.10.2-1.el6      nginx-mod-http-perl.x86_64 0:1.10.2-1.el6      nginx-mod-http-xslt-filter.x86_64 0:1.10.2-1.el6
  nginx-mod-mail.x86_64 0:1.10.2-1.el6                   nginx-mod-stream.x86_64 0:1.10.2-1.el6

Complete!
# apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-59 linux-headers-4.4.0-59-generic linux-headers-4.4.0-66 linux-headers-4.4.0-66-generic linux-image-4.4.0-59-generic
  linux-image-4.4.0-66-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5 libvpx3 libxpm4 libxslt1.1 nginx nginx-common
  nginx-core
0 upgraded, 14 newly installed, 0 to remove and 64 not upgraded.
Need to get 2,999 kB of archives.
After this operation, 9,781 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 libjpeg-turbo8 amd64 1.4.2-0ubuntu3 [111 kB]
Get:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 libjbig0 amd64 2.1-3.1 [26.6 kB]
Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 fonts-dejavu-core all 2.35-1 [1,039 kB]
Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 fontconfig-config all 2.11.94-0ubuntu1.1 [49.9 kB]
Get:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libfontconfig1 amd64 2.11.94-0ubuntu1.1 [131 kB]
Get:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 libjpeg8 amd64 8c-2ubuntu8 [2,194 B]
Get:7 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libtiff5 amd64 4.0.6-1ubuntu0.1 [146 kB]
Get:8 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 libvpx3 amd64 1.5.0-2ubuntu1 [732 kB]
Get:9 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libxpm4 amd64 1:3.5.11-1ubuntu0.16.04.1 [33.8 kB]
Get:10 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 libgd3 amd64 2.1.1-4ubuntu0.16.04.6 [126 kB]
Get:11 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial/main amd64 libxslt1.1 amd64 1.1.28-2.1 [145 kB]
Get:12 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-common all 1.10.0-0ubuntu0.16.04.4 [26.6 kB]
Get:13 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-core amd64 1.10.0-0ubuntu0.16.04.4 [428 kB]
Get:14 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx all 1.10.0-0ubuntu0.16.04.4 [3,498 B]
Fetched 2,999 kB in 2s (1,135 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libjpeg-turbo8:amd64.
(Reading database ... 138589 files and directories currently installed.)
Preparing to unpack .../libjpeg-turbo8_1.4.2-0ubuntu3_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.4.2-0ubuntu3) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.1-3.1_amd64.deb ...
Unpacking libjbig0:amd64 (2.1-3.1) ...
Selecting previously unselected package fonts-dejavu-core.
Preparing to unpack .../fonts-dejavu-core_2.35-1_all.deb ...
Unpacking fonts-dejavu-core (2.35-1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.94-0ubuntu1.1_all.deb ...
Unpacking fontconfig-config (2.11.94-0ubuntu1.1) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.94-0ubuntu1.1_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.94-0ubuntu1.1) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.6-1ubuntu0.1_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.6-1ubuntu0.1) ...
Selecting previously unselected package libvpx3:amd64.
Preparing to unpack .../libvpx3_1.5.0-2ubuntu1_amd64.deb ...
Unpacking libvpx3:amd64 (1.5.0-2ubuntu1) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.11-1ubuntu0.16.04.1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.11-1ubuntu0.16.04.1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.1-4ubuntu0.16.04.6_amd64.deb ...
Unpacking libgd3:amd64 (2.1.1-4ubuntu0.16.04.6) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2.1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2.1) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.10.0-0ubuntu0.16.04.4_all.deb ...
Unpacking nginx-common (1.10.0-0ubuntu0.16.04.4) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.10.0-0ubuntu0.16.04.4_amd64.deb ...
Unpacking nginx-core (1.10.0-0ubuntu0.16.04.4) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.10.0-0ubuntu0.16.04.4_all.deb ...
Unpacking nginx (1.10.0-0ubuntu0.16.04.4) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu13) ...
Setting up libjpeg-turbo8:amd64 (1.4.2-0ubuntu3) ...
Setting up libjbig0:amd64 (2.1-3.1) ...
Setting up fonts-dejavu-core (2.35-1) ...
Setting up fontconfig-config (2.11.94-0ubuntu1.1) ...
Setting up libfontconfig1:amd64 (2.11.94-0ubuntu1.1) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libtiff5:amd64 (4.0.6-1ubuntu0.1) ...
Setting up libvpx3:amd64 (1.5.0-2ubuntu1) ...
Setting up libxpm4:amd64 (1:3.5.11-1ubuntu0.16.04.1) ...
Setting up libgd3:amd64 (2.1.1-4ubuntu0.16.04.6) ...
Setting up libxslt1.1:amd64 (1.1.28-2.1) ...
Setting up nginx-common (1.10.0-0ubuntu0.16.04.4) ...
Setting up nginx-core (1.10.0-0ubuntu0.16.04.4) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.4) ...
Processing triggers for libc-bin (2.23-0ubuntu7) ...
Processing triggers for systemd (229-4ubuntu13) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...

After successful installation, start nginx service.

# service nginx start (Redhat systems)
Starting nginx:                                            [  OK  ]
OR
# systemctl start nginx (RHEL7 & debian)
Starting nginx:                                            [  OK  ]

Verify service is running.

# service nginx status
nginx (pid  1689) is running...

OR

# systemctl status nginx
● 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 Tue 2017-04-04 10:24:19 UTC; 2min 15s ago
 Main PID: 6734 (nginx)
   CGroup: /system.slice/nginx.service
           ├─6734 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           └─6735 nginx: worker process

That’s it. Your installation is complete and your webserver is up and running.

Nginx server test

Now you can enter your server IP in the browser (http://x.x.x.x/) to check if your Nginx webserver is serving page or not. If you are getting a connection timed out, you may want to check firewall settings on your server to allow port 80.

You will be served with test page like below :

nginx test page on YUM based systems like Red Hat
nginx test page on APT based systems like Ubuntu

This confirms that your Nginx web server installation is successful and it’s ready for customization.

nginx configurations

The default server root directory is /usr/share/nginx/html. Files that are placed in there will be served on your web server. You can place your HTML files here to serve a static website! The test page you seen above is file index.html under /usr/share/nginx/html. This default directory can be changed under Nginx configuration file  /etc/nginx/conf.d/default.conf.

nginx global configuration file is /etc/nginx/nginx.conf In this conf file, you can set the user to run Nginx, its number of processes to fork, etc.

Default log files error.log (for errors) and access.log (logging accesses) are located under /var/log/nginx

Directory listing in nginx

If you place a few files or directories in root directory then you will not see a listing of them in your browser as we saw in Apache configuration. To enable this you need to add autoindex on; in config file after root directive. Look at my config below :

# cat /etc/nginx/conf.d/default.conf
server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  _;
    root         /tmp;
    autoindex on;

----- output clipped ----

In the above config, I defined /tmp as root and turned on autoindex (highlighted lines). Restart Nginx services after saving the above changes. Now if I check my webpage (http://x.x.x.x/) it will list all files/directories under /tmp

Directory listing in nginx

You can see in above screenshot, /tmp directory listing is presented by Nginx. If you don’t put autoindex on then it will serve you 404 forbidden error!