Home » Guides » How to Install NGINX on Linux: Ubuntu, RHEL, CentOS, etc

How to Install NGINX on Linux: Ubuntu, RHEL, CentOS, etc

Like Apache, NGINX is also a web server that can host websites. However, the motivation behind installing NGINX on a system is far beyond a simple server. The software is built to offer low memory usage with high concurrency.

As such, it uses an asynchronous, event-driven method to handle requests in a single thread rather than creating new processes for each web request. Such a process doesn’t block other incoming requests, making NGINX a versatile reverse proxy and load balancer. Hence, most users get both Apache and NGINX. But how to install it on a Linux distro? Let’s find out with our recent article.

What is NGINX?

The story behind the development of NGINX is somewhat related to Apache. When working on a solution to the C10K problem, NGINX’s developer Igor Sysoev grew frustrated with Apache’s inability to handle 10000+ concurrent connections.

NGINX Logo

And NGINX was his answer that was more than a web server, with the ability to handle numerous incoming requests while maintaining speed, performance, high concurrency, and low memory usage.

The software was publicly released in 2004 as a free, open-source service under the 2-clause BSD license. Since its inception, the software has seen massive growth as the globe’s top 1000 websites are hosted using it. Another contributing factor is NGINX itself being so versatile. It is a web server that is essentially a reverse proxy, load balancer, mail proxy, and HTTP cache.

NGINX explained in 100 seconds – Fireship

Uses of NGINX

When it comes to the usage standpoint, NGINX definitely has a lot to offer. These are the most significant uses of NGINX –

  • Creating web servers that can scale and manage millions of traffic.
  • As a reverse proxy to manage your web requests.
  • For load balancing purposes of maintaining stable performance on your stack.
  • Caching resources to make your web pages load lightning fast.
  • In the CDN infrastructure to deliver content from the nearest location to the user.

A table of NGINX use cases

Use CaseDescription
Web ServerNGINX can serve as a web server for hosting websites and web applications.
Reverse ProxyIt can act as a reverse proxy, forwarding client requests to backend servers. Widely used to direct parts of domains to different servers.
Load BalancerNGINX software can distribute incoming traffic across multiple servers to ensure load balancing and high availability.
CachingTo cache resources to accelerate website loading times by serving frequently accessed content from memory.
CDN InfrastructureNGINX is used in Content Delivery Networks (CDNs) to deliver content from the nearest edge location to users for improved performance.

Prerequisites to install NGINX

There aren’t many conditions to install NGINX on a system, all the user has to ensure is:

  • Sufficient free disk space
  • A working internet connection
  • Access to a Linux system
  • And root access (optional)

NGINX installation process

NGINX is available with most Linux distros in the usual package repositories. And users can always install it using the correct command codes for their distros. However, sometimes these packages need updating as they can be somewhat out-of-date. Hence, those who want the latest version can always use the packages from the official NGINX website or build directly from the source.

In this article, we have discussed both methods, such as:

Method 1. Building or Compiling NGINX from source code

Step 1. First, launch the command terminal on your Linux distro.

Step 2. Then, update your system (APT) repository.

sudo apt-get update

Step 3. After that, install the development libraries and source code compilers:

sudo apt-get install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev

Step 4. Now download the NGINX source code from the website:

wget  http://nginx.org/download/nginxrelease$.tar.gz

Note – Replace nginxrelease$ with the version name you want to download.

Step 5. Since the downloaded file is in the TAR format, extract it using the following command:

tar -zxvf nginxrelease$.tar.gz

Step 6. Now, point to the directory you have extracted the files using the CD command.

cd nginxrelease$

Step 7. Then configure the file as per your requirement:

./configure --prefix

Step 8. Finally, compile the build using the MAKE command:

make

Step 9. And lastly, install the compiled build:

make install

Method 2. Installing NGINX from package repositories

Now, let us proceed with the package installation on various Linux distros.

On RHEL/CentOS/Oracle Linux/Rocky Linux/Alma Linux

Note – If the user does not have root access, use the SUDO command.

Step 1. Start by launching the command terminal.

Step 2. Then install the prerequisites to set up the YUM repository:

sudo yum install yum-utils

Step 3. Create a file for the YUM repository using the vi text editor.

vi /etc/yum.repos.d/nginx.repo

Step 4. Now append the following content to the file you created in the last step:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

Note – Users have to change $releasever with the distro name and version.

Step 5. Lastly, install NGINX.

sudo yum install nginx

Installing NGINX on Ubuntu/Debian

Step 1. Start the command terminal.

Step 2. Now install the prerequisites to the APT repository.

sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring

Step 3. After that, import an official NGINX signing key for package authentication via the APT repository.

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

Step 4. Then view the downloaded file to verify whether the correct key was downloaded.

gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

Step 5. If the result matches the following, proceed. Otherwise, remove the file and re-download.

pub rsa2048 2011-08-19 [SC] [expires: 2024-06-14] 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 uid nginx signing key <[email protected]>

Step 6. Now run the following command to set up the APT repository:

echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list

Step 7. Finally, pin the custom repository package (built in the last step), to gain priority over the native APT package.

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n"
\ | sudo tee /etc/apt/preferences.d/99nginx

Step 8. Then update your APT repository.

sudo apt update

Step 9. And lastly, install NGINX.

sudo apt install nginx

Note – For Ubuntu users, all they have to do is to replace Debian with Ubuntu from any file name, web address or command list. The rest of the process remains the same as Ubuntu also utilizes the APT repository and command. For further help, we have highlighted the necessary places.

On SLES

Step 1. Launch the command terminal.

Step 2. Then install the prerequisites.

sudo zypper install curl ca-certificates gpg2

Step 3. Now run the given command to set up the ZYPPER repository.

sudo zypper addrepo --gpgcheck --type yum --refresh --check
\ 'http://nginx.org/packages/sles/$releasever_major' nginx-stable

Step 4. After that, import an official NGINX key to authenticate the package:

curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key

Step 5. View the key using the command:

gpg --with-fingerprint /tmp/nginx_signing.key

Step 6. Verify the key. It should match the following output:

pub 2048R/7BD9BF62 2011-08-19 [expires: 2024-06-14] Key fingerprint = 573B FD6B 3D8F BC64 1079  A6AB ABF5 BD82 7BD9 BF62 uid nginx signing key <[email protected]>

Step 7. Upon a successful verification, import the key to the RPM database.

sudo rpmkeys --import /tmp/nginx_signing.key

Step 8. Finally, install NGINX.

sudo zypper install nginx

On Alpine

Step 1. Start with the command terminal and install the prerequisites.

sudo apk add openssl curl ca-certificates

Step 2. Now set up the APK repository:

printf "%s%s%s%s\n" \
    "@nginx " \
    "http://nginx.org/packages/alpine/v" \
    `egrep -o '^[0-9]+\.[0-9]+' /etc/alpine-release` \
    "/main" \
    | sudo tee -a /etc/apk/repositories

Step 3. Then, import an official NGINX key to authenticate the APK repository.

curl -o /tmp/nginx_signing.rsa.pub https://nginx.org/keys/nginx_signing.rsa.pub

Step 4. View the downloaded key:

openssl rsa -pubin -in /tmp/nginx_signing.rsa.pub -text -noout

Step 5. After that, verify the output according to what’s shown here:

Public-Key: (2048 bit)
Modulus:
    00:fe:14:f6:0a:1a:b8:86:19:fe:cd:ab:02:9f:58:
    2f:37:70:15:74:d6:06:9b:81:55:90:99:96:cc:70:
    5c:de:5b:e8:4c:b2:0c:47:5b:a8:a2:98:3d:11:b1:
    f6:7d:a0:46:df:24:23:c6:d0:24:52:67:ba:69:ab:
    9a:4a:6a:66:2c:db:e1:09:f1:0d:b2:b0:e1:47:1f:
    0a:46:ac:0d:82:f3:3c:8d:02:ce:08:43:19:d9:64:
    86:c4:4e:07:12:c0:5b:43:ba:7d:17:8a:a3:f0:3d:
    98:32:b9:75:66:f4:f0:1b:2d:94:5b:7c:1c:e6:f3:
    04:7f:dd:25:b2:82:a6:41:04:b7:50:93:94:c4:7c:
    34:7e:12:7c:bf:33:54:55:47:8c:42:94:40:8e:34:
    5f:54:04:1d:9e:8c:57:48:d4:b0:f8:e4:03:db:3f:
    68:6c:37:fa:62:14:1c:94:d6:de:f2:2b:68:29:17:
    24:6d:f7:b5:b3:18:79:fd:31:5e:7f:4c:be:c0:99:
    13:cc:e2:97:2b:dc:96:9c:9a:d0:a7:c5:77:82:67:
    c9:cb:a9:e7:68:4a:e1:c5:ba:1c:32:0e:79:40:6e:
    ef:08:d7:a3:b9:5d:1a:df:ce:1a:c7:44:91:4c:d4:
    99:c8:88:69:b3:66:2e:b3:06:f1:f4:22:d7:f2:5f:
    ab:6d
Exponent: 65537 (0x10001)

Step 6. Now move the key to the APK storage.

sudo mv /tmp/nginx_signing.rsa.pub /etc/apk/keys/

Step 7. Lastly, install NGINX.

sudo apk add nginx@nginx

Installing NGINX on Amazon Linux

Step 1. Use the command terminal to install the prerequisites.

sudo yum install yum-utils

Step 2. Create a file, using the vi text editor to set up the YUM repository.

vi /etc/yum.repos.d/nginx.repo

Step 3. Append the following in the file:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/amzn2/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

Note – Users can switch amzn2 (highlighted) with amzn/2023 for the latest Amazon Linux 2023 distro.

Step 4. Finally, install the NGINX software.

sudo yum install nginx

If you don’t want to install Apache on your Linux-based machine, NGINX is the best way to go.

After NGINX Installation

Once you successfully install NGINX, there are a few more things you should take care of.

If you are using UFW firewall

Uncomplicated Firewall (UFW) is a simple firewall the comes with some Linux distributions. If you are using it, you should run the following command after NGINX installation.

sudo ufw app list

It should return something like this. If not, you should enable the necessary firewall exceptions for NGINX to function properly.

Output
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

Testing NGINX

Now that you have successfully installed NGINX on your Linux machine, it’s time to make sure that we are able to create and run a web server using it pointing it to a domain. Let’s create a simple website in NGINX.

Be sure to replace “example” in this section with the name of your website.

Step 1. Create a directory for our test website.

sudo mkdir -p /var/www/example

Step 2. Assign the ownership of the directory to the current user.

sudo chown -R $USER:$USER /var/www/example

Step 3. Assign the directory permissions.

sudo chmod -R 755 /var/www/example

Step 4. Create a sample index.html file.

sudo nano /var/www/example/index.html

Step 5. In the nano editor, add the following code to the index.html file.

<html>
    <head>
        <title>Testing</title>
    </head>
    <body>
        <h1>Success!</h1>
    </body>
</html>

Step 6. Save the file by pressing Ctrl + O and exit the editor by pressing Ctrl + X.

Step 7. Create a new site in Nginx.

sudo nano /etc/nginx/sites-available/example

Step 8. Paste the following code to the file.

server {
        listen 80;
        listen [::]:80;

        root /var/www/example;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Step 9. Save and close the file by pressing Ctrl + O and Ctrl + X.

Step 10. Check NGINX for any errors.

sudo nginx -t

Step 11. Enable the site.

sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/

Step 12. Restart NGINX

sudo systemctl restart nginx

Now, when you open your domain (example.com), you should see a page like this.

NGINX Welcome Page
NGINX Welcome Page

FAQs about NGINX installation

Can I install NGINX alongside Apache on the same server?

Yes, it is possible to install NGINX alongside Apache on the same server. NGINX is installed along with Apache, commonly when you want to utilize NGINX as a reverse proxy or load balancer in front of an Apache web server. NGINX and Apache can coexist peacefully, serving different purposes in your web infrastructure.

Is NGINX suitable for small-scale websites, or is it primarily for large-scale applications?

NGINX is suitable for a wide range of websites, from small-scale to large-scale applications. The size of the application doesn’t matter. Its efficiency, low resource usage, and scalability make it an excellent choice for all types of web hosting environments.

How to secure NGINX installation after the initial setup?

NGINX is pretty secure by default. Additionally, securing your NGINX installation involves steps like enabling HTTPS with SSL/TLS certificates, implementing access controls, and following security best practices. Detailed instructions for securing NGINX can be found in the official NGINX documentation. You may also hire a professional to do it for you.

Where can I find official NGINX documentation?

You can find official NGINX documentation and guides on the NGINX website. You will be able to find everything you need for your NGINX server there. The documentation covers advanced configurations, optimization techniques, and best practices for getting the most out of the NGINX server.

Are there any alternatives to NGINX for web serving and reverse proxying?

Yes, there are alternative web servers and reverse proxy solutions available apart from NGINX. Some popular alternatives to NGINX include Apache HTTP Server, LiteSpeed, Caddy, and HAProxy. The choice of a web server or reverse proxy depends on your specific requirements and preferences. Make sure to do proper research before choosing one.

Gilbert John Avatar