How to Install Nginx in Ubuntu or CentOS/RHEL?

A step-by-step guide to installing Nginx in UNIX platforms.

Nginx was released in 2004 and since then growing rapidly.

Nginx holds more than 36% market share in web servers.

Let’s take a look at installation procedure for various UNIX distro.

Ubuntu 16.x/17.x

  • Ensure you are logged in as a root
  • Execute the following command
apt-get install nginx

It will take few seconds to install.

RHEL/CentOS 6.x/7.x

As usual, log in as a root and use the below yum command

yum install nginx

It will install including the dependencies. Below snapshot from CentOS 6.x

  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:2018.01-1.el6                   
  GeoIP-GeoLite-data-extra.noarch 0:2018.01-1.el6                     fontconfig.x86_64 0:2.8.0-5.el6                             
  freetype.x86_64 0:2.3.11-17.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                                
  libxslt.x86_64 0:1.1.26-2.el6_3.1                                   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                             

Installation Using Source Code

If your server doesn’t have the Internet connectivity or working on DMZ server, then you can compile from source code to install it.

The following demonstration is from CentOS/RHEL.

  • Extract the downloaded file
gunzip –c nginx-1.12.2.tar.gz | tar xvf –
  • After extract, you should see following files and folders
[[email protected] nginx-1.12.2]# ls -ltr
-rw-r--r--. 1 chandan scorpio_ckumar     49 Oct 17 13:16 README
-rw-r--r--. 1 chandan scorpio_ckumar   1397 Oct 17 13:16 LICENSE
-rwxr-xr-x. 1 chandan scorpio_ckumar   2481 Oct 17 13:16 configure
-rw-r--r--. 1 chandan scorpio_ckumar 423948 Oct 17 13:16
-rw-r--r--. 1 chandan scorpio_ckumar 278202 Oct 17 13:16 CHANGES
drwxr-xr-x. 9 chandan scorpio_ckumar     91 Feb  3 03:11 src
drwxr-xr-x. 2 chandan scorpio_ckumar     21 Feb  3 03:11 man
drwxr-xr-x. 2 chandan scorpio_ckumar     40 Feb  3 03:11 html
drwxr-xr-x. 4 chandan scorpio_ckumar     72 Feb  3 03:11 contrib
drwxr-xr-x. 2 chandan scorpio_ckumar    168 Feb  3 03:11 conf
drwxr-xr-x. 6 chandan scorpio_ckumar   4096 Feb  3 03:11 auto
[[email protected] nginx-1.12.2]# 

Now, we are ready to configure and install it.

You can control installation location using --prefix=path parameter.

Let’s install under /opt/nginx with SSL module.

./configure --prefix=/opt/nginx --with-http_ssl_module 
make install

Note: if you are doing this on a brand new server where the compiler is not installed then you got to install the following.

yum install gcc pcre-devel openssl-devel

Upon completion, you should see nginx is installed under /opt/nginx with following folders.

[[email protected] nginx]# ls -ltr
drwxr-xr-x. 2 root root   19 Feb  3 03:19 sbin
drwxr-xr-x. 2 root root 4096 Feb  3 03:19 conf
drwxr-xr-x. 2 root root    6 Feb  3 03:19 logs
drwxr-xr-x. 2 root root   40 Feb  3 03:19 html
[[email protected] nginx]# 

Congratulation, You have just installed Nginx!

Stop and Start Procedure

If you compiled using the source code then to start

  • Go to sbin folder and execute nginx

To stop

./nginx –s stop

If installed using repository then

service nginx start
service nginx stop

What next?

If this is for production then do necessary hardening and security configuration and if exploring to learn then check out this online course.

Leave a Comment

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