Posted on
Web Development

Installing and configuring Nginx on Linux

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Comprehensive Guide to Installing and Configuring Nginx on Linux for Web Developers

Nginx is a high-performance web server that is also used as a reverse proxy, mail proxy, and HTTP cache. It is known for its stability, rich feature set, simple configuration, and low resource consumption. In this comprehensive guide, we will cover how to install, configure, and get your Nginx up and running on a Linux system.

Choosing Your Linux Distribution

Before diving into the installation process, it's important to choose a Linux distribution. Nginx can run on various distributions such as Ubuntu, CentOS, Debian, Fedora, and more. For the purposes of this guide, we will focus on installing Nginx on Ubuntu which is one of the most popular distributions especially for web development purposes.

Step 1: Installing Nginx

First, you need to update your package manager. On Ubuntu, you would use the apt package manager. Open your terminal and execute the following commands:

sudo apt update
sudo apt install nginx

After installing Nginx, the service should start automatically. To verify if Nginx is running, you can either check its status with Systemd or use your browser:

sudo systemctl status nginx

Alternatively, you can open a browser and type your server's IP address. If Nginx is running, you should see the default Nginx landing page.

Step 2: Configuring the Firewall

If your server has a firewall enabled, you will need to allow HTTP and HTTPS traffic. On Ubuntu, if you are using ufw (Uncomplicated Firewall), you can enable these easily:

sudo ufw allow 'Nginx Full'
sudo ufw reload

This sets the firewall to allow traffic on ports 80 (HTTP) and 443 (HTTPS).

Step 3: Managing Nginx Service

To start, stop, restart, and reload your Nginx service after making configuration changes, use the following commands:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx

Using reload is generally safer than restart as it does not drop current connections.

Step 4: Configuring Nginx

Nginx configuration files are located in /etc/nginx. The main configuration file is /etc/nginx/nginx.conf. Virtual host configurations are typically held in /etc/nginx/sites-available with symbolic links in /etc/nginx/sites-enabled.

To set up a simple website:

  1. Create a directory for your website:

    sudo mkdir -p /var/www/mywebsite/html
    sudo chown -R $USER:$USER /var/www/mywebsite/html
    
  2. Create your index.html:

    echo "<html><body><h1>Hello, Nginx!</h1></body></html>" | sudo tee /var/www/mywebsite/html/index.html
    
  3. Set up a new virtual host: For instance, create a new configuration file: /etc/nginx/sites-available/mywebsite.

    server {
       listen 80;
       server_name mywebsite.com www.mywebsite.com;
    
       location / {
           root /var/www/mywebsite/html;
           index index.html index.htm;
       }
    }
    

    Create a symbolic link to enable this site:

    sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
    
  4. Test Nginx configuration for syntax errors:

    sudo nginx -t
    
  5. Reload Nginx:

    sudo systemctl reload nginx
    

Step 5: Securing Nginx with SSL/TLS

For HTTPS, SSL/TLS certificates are a must. You can obtain these for free from Let's Encrypt. Certbot is a popular choice to get and install Let's Encrypt certificates.

Install Certbot and its Nginx plugin:

sudo apt install certbot python3-certbot-nginx

Run it to configure SSL automatically:

sudo certbot --nginx -d mywebsite.com -d www.mywebsite.com

Follow the instructions, and it should automatically update your Nginx configuration files with SSL settings and renewals.

Conclusion

Configuring Nginx doesn't have to be hard. With this basic setup, you now have a running web server capable of serving static content. For dynamic content, you can further explore using Nginx as a reverse proxy to pass requests to a backend server, like Node.js, Python Flask, or others.

As web technologies continuously evolve, keep learning and updating your skills to make optimal use of tools like Nginx. Happy coding!

Further Reading

For further reading about Nginx and its configuration on Linux, consider the following resources:

  • DigitalOcean - How To Install Nginx on Ubuntu 20.04
    Link to tutorial
    This guide covers the basics of setting up Nginx on Ubuntu, including initial server setup and adjustments after installation.

  • NGINX Documentation
    Official Docs
    Official documentation providing detailed instructions and configurations for Nginx across different scenarios and requirements.

  • Linode - Configuring NGINX
    Config Guide
    This comprehensive guide explains how to configure Nginx, including setting up server blocks and securing traffic.

  • Certbot - Automatically enable HTTPS on your website with EFF's Certbot
    Certbot Official
    Learn how to secure your Nginx server with TLS/SSL certificates from Let's Encrypt using Certbot.

  • Red Hat Developer - Secure Nginx with Let's Encrypt on CentOS 7
    Secure Nginx Guide
    A focused guide on securing Nginx using Let's Encrypt on CentOS 7, specifically tailored for users of this Linux distribution.

These resources provide a range of perspectives and additional details suitable for both beginners and experienced users looking to delve into more complex configurations or troubleshooting.