How to Install Nginx Web Server on Linux

If you’re thinking of running a website, you’re more likely to go with Nginx. Here's how to install and set it up.

How to Install and Configure Nginx Web Server

This guide will help you install Nginx on various Linux distros and you’ll learn about important Nginx configuration files and directories.

Nginx is the most popular web server due to its performance and ease of use. It’s a free and open-source high-performance HTTP server. In addition to its web server capabilities, Nginx can also function as a reverse proxy and load balancer.

Following the steps below will show you how to install Nginx on various Linux distros like Ubuntu, Debian, CentOS, Rocky Linux, AlmaLinux, and test its functionality.

In addition, if you want to know how to secure your Nginx with free Let’s Encrypt SSL certificate, I recommend going through our guide Let’s Encrypt: Get Free SSL Certificate Using Certbot.

Table of Contents

1. Installing Nginx

Before you begin, you should have a regular, non-root user with sudo privileges.

1.1 Install Nginx on Ubuntu or Debian

Nginx is available in the default Ubuntu and Debian repositories so you can easily install it using the apt package management tool.

Let’s first make sure your system is up to date:

sudo apt update

Afterward, you can install Nginx:

sudo apt install nginx
Install Nginx on Ubuntu or Debian

1.2 Install Nginx on CentOS 7

On CentOS 7, Nginx packages are available in the EPEL (Extra Packages for Enterprise Linux) repositories. If you don’t have it already installed you can do it by typing:

sudo yum install epel-release
Install EPEL Repository on CentOS 7

Now you can proceed installing Nginx:

sudo yum install nginx
Install Nginx on CentOS 7

If you see this, press y and then hit Enter to accept the EPEL GPG key.

Accept EPEL GPG Key on CentOS 7

Once the installation is complete, enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

And finally you need to open both HTTP (80) and HTTPS (443) ports.

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

1.3 Install Nginx on CentOS 8, Rocky Linux, or AlmaLinux

In order to install Nginx, we’ll use the dnf package manager, which is the default package manager on CentOS 8, Rocky Linux and AlmaLinux.

First, update all the available packages:

sudo dnf upgrade

After the update is complete, install Nginx by running the following command:

sudo dnf install nginx
Install Nginx on CentOS 8, Rocky Linux, or Alma Linux

Once the installation is complete, enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

To allow HTTP (80) and HTTPS (443) traffic on the firewall, execute the command:

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

2. Check the Nginx Service Status

To confirm that the Nginx web server is running, execute the command:

sudo systemctl status nginx
Nginx Service Status

From the output shown above you can conclude that the Nginx service is up and running.

Additionally you can access the default Nginx landing page to confirm that the web server is running properly by navigating to your server’s IP address.

If you don’t know how to find out the IP address of the server, this article will help you find it. 

This will display the default Nginx landing page indicating that all is well.

Default Landing Page

3. Nginx Configuration Files and Directories

Now that Nginx is installed, there are important folders and locations that you should be aware of. 

3.1 Server Configuration Files

  • /etc/nginx:  The main directory containing all the Nginx configuration files.
  • /etc/nginx/nginx.conf: The main Nginx configuration file.
  • /etc/nginx/sites-available: The directory where individual website are defined. Keep in mind that Nginx will not use the configuration files found in this directory unless they are linked to the /etc/nginx/sites-enabled directory.
  • /etc/nginx/sites-enabled: List of websites actively being served by Nginx.

To activate websites so they’re linked to the /etc/nginx/sites-enable directory, use the command shown below to create a symlink of the website configuration:

sudo ln -s /etc/nginx/sites-available/mydomain.com.conf /etc/nginx/sites-enabled/

Of course, you need to replace mydomain.com.conf with your VirtualHost .conf file.

Related: How to Create Nginx Virtual Host (Server Block)

3.2 Nginx Logs

The Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. 

  • access.log: Every request to your web server is recorded in this log file.
  • error.log: A log of any errors generated in Nginx. This is where you will come to troubleshoot when your server is not running as expected.

3.3 Default Public Web Directory

By default Nginx have a default document root set up in its base configuration files. When you create a virtual host or server block, the web server looks for website files in the document root directory specified in these configuration files.

  • /var/www/html: On Ubuntu and Debian, Nginx stores its documents here.
  • /usr/share/nginx/html: On CentOS, Rocky Linux, and Alma Linux the default Nginx webpage is located here.

Conclusion

In this tutorial you learned how to install Nginx on various Linux distros. Now it’s up to you to figure out what content you want to serve to your users.

Related: How to Configure Nginx to Work with PHP via PHP-FPM

If you want to learn more about Nginx and how it works, you can check out the official Nginx documentation.

Leave a Reply

Your email address will not be published.

Latest from Linux Knowledge