How To Install Nginx In Linux (RHEL/Ubuntu)

Step by step process of how to install Nginx in Linux (Red Hat Enterprise Linux Server and Ubuntu server) – What is LEMP Stack? – How do I start nginx server ?

UNDERSTANDING THE SUBJECT MATTER

How Do You Say Nginx

I remember the day my project manager sent an email with an attached document as regards an upcoming project, I was supposed to deploy either Apache or Nginx as part of the prerequisite, but because I love to do new things, I chose Nginx over Apache.

I said, Sir, We are going to choose “in-gi-nix”. lol. That was how I first pronounced it.

My colleague Johnson[link] laughed so hard. With my innocent face, I said, What? He kept laughing and I started smiling as well because I knew I must have pronounced it the wrong way.

I tried to justify myself and I said something like “in-gin” again. Lol. Peter, my PM looked at me and started laughing as well. To cut the long story short. Johnson corrected me and said it is pronounced “engine-ex”.

Nginx is pronounced “engine-ex”.

To my students and tekneeders, I hope I didn’t bore you with my story, I just wanted to lighten up your mood.

Moving forward, what is Nginx?

What Is Nginx

Nginx is an open-source web server. It was developed by Igor Sysoev and released in 2004. Igor Sysoev wanted to solve the CK10 problem, i.e the problem of 10,000 users not being able to simultaneously hit a web site without having a performance issue. Hence he developed Nginx.

Nginx as of today can handle many simultaneous requests, even over 10,000 that Igor had in mind. Nginx doesn’t create a process for every request, it just creates a Parent process (master process) that creates other children processes (worker processes), thereby making every request work simultaneously in a smooth algorithm without affecting each other.

Nginx can also run on many operating systems such as Windows, MAC, Linux, etc.

Nginx, just like the Apache web server can handle hundreds of thousands of connections and can run thousands of connections of static contents. it is very fast, one of the fastest , arguably the fastest web server. it is very scalable as well.

Apart from Nginx being a web server, it also has other features too such as load balancing, reverse proxy, caching, media streaming, email proxy, (SMTP, IMAP, POP3), etc. With Nginx as a load balancer, you may not need other hardware load balancers.

All these features can be used all at once, thereby reducing the overhead cost of your environment by not needing to buy all these components one after the other. It’s all embedded in one open-source software, Nginx.

Where Has Nginx Been Used

Examples of company’s that uses Nginx are GitLab, Microsoft, Vmware, Google, IBM, WordPress, Apple, Linkedin, Netflix, Facebook, Intel, Cisco, etc.

How do I start Nginx on Linux

Just the way you start a service on the Linux system, you can also start Nginx that way by using the command,

# systemctl start nginx

To stop Nginx on Linux, you use the command,

# systemctl stop nginx.

To make Nginx server persistent on Linux, you use the command,

# systemctl enable nginx

To make Nginx server non persistent, you use the command,

# systemctl disable nginx

What Is LEMP Stack

If you know LAMP stack, you will also know LEMP stack. LAMP stack consists of (Linux, Apache, MySQL and, PHP), they are web development tools used for deploying, developing and managing web applications.

As a result of a high shift from Apache web server to the Nginx web server, the acronym LEMP too was invented. The Apache tool in LAMP was a replacement by “engine-ex” (Nginx). Hence, LEMP stack.

So, Lemp stack consists of (Linux, Nginx, MySQL and PHP). They are all web development tools.

How To Install LEMP Stack In Linux

If you are reading this article, I believe you have a Linux system. However, To install a Linux server, click on this link.

Nginx will be installed in the “ACTION TIME” section.

To install MySQL, click on this link

To install PHP, click on this link.

Having understood what Nginx is, let’s get to action.

ACTION TIME

How To Install Nginx Server In CentOS / RHEL 7&8

1. Add an extra repository file for nginx

NOTE: 1. you won’t need to do this if your server is on Microsoft Azure

2. you won’t need to do this on a RHEL 8 server, it comes by default

[root@HQPRD2 ~]# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/rhel/7/$basearch/
gpgcheck=0
enabled=1

2. Install the server

The latest version will be installed.

[root@HQPRD2 ~]# yum install nginx

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
nginx                                                    | 2.9 kB     00:00
nginx/x86_64/primary_db                                    | 166 kB   00:03
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.17.8-1.el7.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================
 Package       Arch           Version                       Repository     Size
=================
Installing:
 nginx         x86_64         1:1.17.8-1.el7.ngx            nginx         770 k

Transaction Summary
================
Install  1 Package

Total download size: 770 k
Installed size: 2.7 M
Is this ok [y/d/N]:

3. verify the status of the server

[root@HQPRD2 ~]# systemctl status nginx

● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: http://nginx.org/en/docs/
[root@HQPRD2 ~]#

4. Start the service

[root@HQPRD2 ~]# systemctl start nginx

5. verify again

[root@HQPRD2 ~]# systemctl status nginx

● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-02-19 00:53:50 PST; 1min 15s ago
     Docs: http://nginx.org/en/docs/
  Process: 8995 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 8996 (nginx)
   CGroup: /system.slice/nginx.service
           ├─8996 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.c...
           └─8997 nginx: worker process

Feb 19 00:53:50 HQPRD2 systemd[1]: Starting nginx - high performance web se.....
Feb 19 00:53:50 HQPRD2 systemd[1]: PID file /var/run/nginx.pid not readable...t.
Feb 19 00:53:50 HQPRD2 systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@HQPRD2 ~]#

6. open the https port (443) and http port (80)

[root@HQPRD2 ~]# firewall-cmd --add-port=443/tcp --permanent
[root@HQPRD2 ~]# firewall-cmd  --add-port=80/tcp --permanent

success

7. reload the firewall service.

[root@HQPRD2 ~]# firewall-cmd --reload
success

8. test the server

  • Install the elinks package if you don’t have it.
[root@HQPRD2 ~]# yum install elinks
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
Resolving Dependencies
--> Running transaction check
---> Package elinks.x86_64 0:0.12-0.37.pre6.el7 will be installed
--> Processing Dependency: libmozjs185.so.1.0()(64bit) for package: elinks-

  • test using elinks
# elinks http://<server - IP>
[root@HQPRD2 ~]# elinks http://192.168.170.150
install nginx in Linux

press the enter key,

  • test using curl

[root@HQPRD2 ~]# curl -I 192.168.170.150

HTTP/1.1 200 OK
Server: nginx/1.17.8
Date: Wed, 19 Feb 2020 10:00:13 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 21 Jan 2020 14:51:25 GMT
Connection: keep-alive
ETag: "5e270fed-264"
Accept-Ranges: bytes

[root@HQPRD2 ~]# curl 192.168.170.150

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@HQPRD2 ~]#


How To Uninstall Nginx Server In CentOS / RHEL 7&8

To uninstall nginx server from Red Hat/CentOS 7 & 8, use the command,

[root@HQPRD2 ~]# yum remove nginx

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.17.8-1.el7.ngx will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package       Arch           Version                      Repository      Size
================================================================================
Removing:
 nginx         x86_64         1:1.17.8-1.el7.ngx           @nginx         2.7 M

Transaction Summary
================================================================================
Remove  1 Package

Installed size: 2.7 M
Is this ok [y/N]:

How To Install Nginx Server In Ubuntu 16.04 Linux

Install the package using apt install,

root@ubuntu:~# apt install nginx

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  grub-pc-bin libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl
  libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Use 'sudo apt autoremove' to remove them.
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.
Need to get 3,494 B of archives.
After this operation, 38.9 kB of additional disk space will be used.
Get:1 http://azure.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx all 1.10.3-0ubuntu0.16.04.5 [3,494 B]
Fetched 3,494 B in 0s (157 kB/s)
Selecting previously unselected package nginx.
(Reading database ... 54750 files and directories currently installed.)
Preparing to unpack .../nginx_1.10.3-0ubuntu0.16.04.5_all.deb ...
Unpacking nginx (1.10.3-0ubuntu0.16.04.5) ...
Setting up nginx (1.10.3-0ubuntu0.16.04.5) ...

Verify if the service is started

root@ubuntu:~# 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: en
   Active: active (running) since Tue 2020-02-18 09:41:10 UTC; 5h 20min ago
 Main PID: 5803 (nginx)
   CGroup: /system.slice/nginx.service
           ├─5803 nginx: master process /usr/sbin/nginx -g daemon on; master_pro
           ├─5804 nginx: worker process
           └─5808 nginx: worker process

Feb 18 09:41:10 ubuntu systemd[1]: Starting A high performance web server and a
Feb 18 09:41:10 ubuntu systemd[1]: Started A high performance web server and a r
lines 1-11/11 (END)

How To Uninstall Nginx Server In Ubuntu Linux

root@ubuntu:~# apt remove nginx

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  fontconfig-config fonts-dejavu-core grub-pc-bin libaio1 libcgi-fast-perl
  libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl
  libfontconfig1 libgd3 libhtml-parser-perl libhtml-tagset-perl
  libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  libjbig0 libjpeg-turbo8 libjpeg8 liblwp-mediatypes-perl libtiff5
  libtimedate-perl liburi-perl libvpx3 libxpm4 mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
  nginx-common nginx-core
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  nginx
0 upgraded, 0 newly installed, 1 to remove and 8 not upgraded.
After this operation, 38.9 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 54753 files and directories currently installed.)
Removing nginx (1.10.3-0ubuntu0.16.04.5) ...
root@ubuntu:~#

Your feedback is welcomed. If you love others, you will share with others.

Be the first to comment

Leave a Reply

Your email address will not be published.


*