How to Install Nginx on a Linux Server

Nginx is a powerful, high-performance web server commonly used for serving static content, reverse proxying, and load balancing. It’s lightweight, reliable, and easy to configure.

Nginx has become one of the most popular web servers due to its speed and efficiency in handling large traffic volumes. Whether you’re hosting static websites, acting as a reverse proxy, or load balancing, Nginx offers a versatile solution.

Nginx in Linux

This article covers how to install Nginx on Linux distributions, including Ubuntu/Debian, CentOS/RHEL, and Fedora. We’ll also look at basic configurations and how to ensure that the Nginx service runs smoothly.

Prerequisites

Before starting, make sure you meet the following prerequisites:

  • A Linux server (Ubuntu, Debian, CentOS, RHEL, or Fedora).
  • A non-root user with sudo privileges.
  • Access to the terminal or SSH.

Install Nginx on Various Linux Distributions

Nginx installation differs slightly depending on the Linux distribution you’re using. This section will guide you through the installation process on popular Linux distributions, including Ubuntu/Debian, CentOS/RHEL, and Fedora. Each step will provide the specific commands and procedures for the distribution of your choice.

Install Nginx on Ubuntu/Debian

  • Update the System

Start by updating the package list to ensure you install the latest software.

sudo apt update && sudo apt upgrade -y
  • Install Nginx

To install Nginx on Ubuntu or Debian-based systems, use the following command:

sudo apt install nginx -y
  • Start and Enable Nginx

Once Nginx is installed, you need to start the service and enable it to start on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Once enabled, verify that Nginx is running correctly, and navigate to your server’s IP address in a web browser. You should see the default Nginx welcome page.

Install Nginx on CentOS/RHEL

  • Install EPEL Repository

For CentOS 7 and RHEL 7, install the Extra Packages for Enterprise Linux (EPEL) repository.

sudo yum install epel-release -y

For CentOS 8 and RHEL 8, the EPEL repository is included by default.

  • Install Nginx

Install Nginx using yum (CentOS 7) or dnf (CentOS 8 and RHEL 8).

sudo yum install nginx -y
sudo dnf install nginx -y
  • Start and Enable Nginx

Start Nginx and configure it to start automatically on boot.

sudo systemctl start nginx
sudo systemctl enable nginx
  • Verify Installation

Verify that Nginx is running by checking its status:

sudo systemctl status nginx

Install Nginx on Fedora

  • Update the System

Update your system’s package list to ensure you have the latest software versions:

sudo dnf update -y
  • Install Nginx

Fedora uses the dnf package manager, and Nginx is available in the default repositories. To install it, run the following command:

sudo dnf install nginx -y
  • Start and Enable Nginx

Once the installation is complete, start the Nginx service and enable it to start on boot:

sudo systemctl start nginxsudo systemctl enable nginx
  • Open Firewall Ports

If you’re using a firewall (like firewalld on Fedora), you’ll need to open ports 80 (HTTP) and 443 (HTTPS) to allow web traffic. Run the following commands:

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

These commands will allow HTTP and HTTPS traffic through the firewall, making your Nginx server accessible from the web.

  • Verify Installation

To verify that Nginx is running, open a web browser and go to your server’s IP address:

http://your_server_ip

You should see the default Nginx welcome page. Additionally, you can check the status of Nginx with:

sudo systemctl status nginx

Configuration of Nginx On a Linux Server

Nginx’s configuration files are usually located in /etc/nginx/. You can customize server behavior through these files.

The Default configuration file is /etc/nginx/nginx.conf, and it includes several settings for handling traffic and requests.

Check Out | How to Install Apache Web Server in Linux

Create a New Nginx Virtual Host

A server block (similar to Apache’s virtual hosts) can be used to configure Nginx to serve different websites.

  • Create a new server block file in /etc/nginx/sites-available/.
sudo nano /etc/nginx/sites-available/mywebsite.com
  • Add the following configuration to the file:
server {
    listen 80;
    server_name mywebsite.com www.mywebsite.com;

    root /var/www/mywebsite;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
  • Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/mywebsite.com /etc/nginx/sites-enabled/
  • Test Nginx for syntax errors:
sudo nginx -t
  • Reload Nginx to apply the changes:
sudo systemctl reload nginx

Managing Nginx on a Linux Server

Once Nginx is installed, you will frequently need to start, stop, or reload the Nginx service. Below are some useful commands:

  • Start Nginx:
sudo systemctl start nginx
  • Stop Nginx:
sudo systemctl stop nginx
  • Restart Nginx:
sudo systemctl restart nginx
  • Reload Nginx (useful for configuration changes):
sudo systemctl reload nginx
  • Check the status of Nginx:
sudo systemctl status nginx

Conclusion

Installing Nginx on a Linux server is a simple process, and it provides a powerful web server that can be easily configured for many different use cases. Whether serving static websites, acting as a reverse proxy, or balancing load across multiple servers, Nginx is a reliable, high-performance choice.

With this guide, you should now have Nginx installed and running on your Linux server, ready for further configuration based on your specific needs.

Leave A Comment