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.


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:


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:

name=nginx stable repo

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.


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)
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:

name=nginx stable repo

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.

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

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.


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