Configure Nginx on Linux Server: Ubuntu, CentOS, and Debian Tutorial

Nginx (pronounced “Engine-X”) is a high-performance web server that is widely used for serving static content, reverse proxying, and load balancing. It’s known for its low resource consumption and high scalability. If you’re looking to configure Nginx on your Linux server, this guide will walk you through the process in detail, helping you set up a reliable and secure web server.

Configure Nginx on a Linux

In this guide, we will cover the installation of Nginx, configuration of server blocks (virtual hosts), SSL configuration, module management, and monitoring practices. By the end, you’ll have a fully configured Nginx server ready to serve your website securely and efficiently.

Prerequisites

Before diving into the Nginx setup, ensure you meet these prerequisites:

  • A Linux-based system (Ubuntu, Debian, CentOS, RHEL, Fedora, etc.).
  • A non-root user with sudo privileges.
  • Basic familiarity with the Linux command line.
  • A server with internet access for downloading packages and obtaining SSL certificates.

With these prerequisites in place, let’s get started!

Install Nginx

The first step to configure Nginx is to install the server on your Linux machine. The installation process varies slightly depending on the distribution you’re using.

Ubuntu/Debian:

  • Update your package list:
sudo apt update
sudo apt install nginx
  • Once installed, start the Nginx service:
sudo systemctl start nginx
  • Enable Nginx to start on boot:
sudo systemctl enable nginx
  • Verify the installation by checking the Nginx version:
nginx -v

CentOS/RHEL/Fedora:

  • For CentOS 7 and above, use the following command to install Nginx:
sudo yum install nginx
  • On Fedora:
sudo dnf install nginx
  • Start and enable Nginx:
sudo systemctl start nginxsudo systemctl enable nginx
  • Confirm the installation by checking the Nginx version:
nginx -v

Adjust Firewall Settings

If you’re running a firewall on your Linux server (which you should), you need to allow HTTP and HTTPS traffic. This ensures that your web server is accessible from the internet.

For Ubuntu/Debian (UFW):

  • Allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
  • Reload UFW to apply changes:
sudo ufw reload

For CentOS/RHEL/Fedora (Firewalld):

  • Allow HTTP and HTTPS traffic through the firewall:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=https
  • Reload the firewall to apply the changes:
sudo firewall-cmd --reload

Verify Nginx Installation

At this point, Nginx should be running on your server. You can verify the installation by opening a web browser and navigating to your server’s IP address or domain name.

For example, if your server’s IP address is 192.168.1.1, type:

http://192.168.1.1

You should see the default Nginx welcome page, which confirms that Nginx is installed and working correctly.

Configure Nginx Server Blocks (Virtual Hosts)

Server blocks (also known as virtual hosts) are used to serve different websites from the same Nginx instance. Let’s set up a simple server block for your website.

Create a New Server Block:

  • Navigate to the Nginx configuration directory:
cd /etc/nginx/sites-available/
  • Create a new file for your website (e.g., yourdomain.com):
sudo nano /etc/nginx/sites-available/yourdomain.com
  • Add the following basic server block configuration:
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    root /var/www/yourdomain.com;
    index index.html index.htm;

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

Enable the Server Block:

To enable the server block, create a symbolic link from sites-available to sites-enabled:

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

Test and Reload Nginx:

  • Test the Nginx configuration for syntax errors:
sudo nginx -t
  • If the test passes, reload Nginx to apply the changes:
sudo systemctl reload nginx

Secure Nginx with SSL (HTTPS)

For enhanced security, it’s recommended to serve your site over HTTPS using an SSL certificate. Let’s Encrypt provides free SSL certificates, and we’ll configure SSL for your website.

Install Certbot:

  • Install Certbot and the Nginx plugin:
sudo apt install certbot python3-certbot-nginx
  • Request a certificate for your domain:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • Follow the instructions to complete the SSL certificate installation.

Redirect HTTP to HTTPS:

  • Modify your server block to force HTTPS by adding the following configuration:
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$server_name$request_uri;
}
  • Reload Nginx to apply changes:
sudo systemctl reload nginx

Enable and Configure Nginx Modules

Nginx comes with a variety of modules that enhance its functionality. Some common modules include ngx_http_rewrite_module for URL rewriting, ngx_http_ssl_module for SSL, and ngx_http_gzip_module for compression.

List Available Modules:

You can list the enabled modules with the following command:

nginx -V

If you need to enable or disable modules, you can edit Nginx’s configuration files accordingly.

Monitor and Maintain Nginx

Proper monitoring and maintenance ensure your Nginx server runs smoothly. Regularly check the status of the service and monitor logs for errors or issues.

Check Nginx Status:

To check if Nginx is running:

sudo systemctl status nginx

View Nginx Logs:

You can view the following logs for any issues:

  • Access log: /var/log/nginx/access.log
  • Error log: /var/log/nginx/error.log

Log Rotation:

To prevent log files from growing too large, configure log rotation using tools like logrotate.

Conclusion

Configuring Nginx on a Linux server is an essential task for anyone looking to host a website or web application. By following the steps outlined in this guide, you can set up Nginx to serve your website efficiently, securely, and with minimal resource usage. You’ve also learned how to configure server blocks for multiple sites, secure Nginx with SSL, enable and configure useful modules, and monitor server performance.

As you continue working with Nginx, explore additional features like load balancing, caching, and reverse proxying to further enhance your server’s capabilities. For more in-depth documentation, be sure to check out the official Nginx documentation.

Leave A Comment