How to Install Nginx on Linux Mint

Nginx is an open-source web server that has gained immense popularity over the years due to its lightweight and efficient design. It's known to handle a massive load of concurrent connections at an incredibly fast pace, making it the perfect choice for high-traffic websites. In this tutorial, we will cover the steps necessary to install Nginx on Linux Mint.

Prerequisites

Before we dive into the installation process, we need to make sure our system is up to date. You can achieve that by running the following commands in your terminal:

sudo apt update -y
sudo apt upgrade -y

Installing Nginx

You can install Nginx on Linux Mint using the default package manager. Here are the steps:

  1. Open a terminal window by pressing Ctrl+Alt+T or clicking on Terminal from the application menu.

  2. Execute the following command to install Nginx:

    sudo apt install nginx -y
    

    This will download and install Nginx and all the required packages.

  3. Once the installation is complete, the Nginx service should start automatically. You can check its status by running:

    sudo systemctl status nginx
    

    If everything is installed correctly, you should see something like this:

    ● nginx.service - A high-performance web server and a reverse proxy server
         Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
         Active: active (running) since Mon 2021-08-16 11:40:27 CDT; 1min 21s ago
           Docs: man:nginx(8)
        Main PID: 1234 (nginx)
           Tasks: 2 (limit: 1139)
          Memory: 2.0M
             CPU: 17ms
          CGroup: /system.slice/nginx.service
                  ├─1234 nginx: master process /usr/sbin/nginx -g daemon on; master_process>
                  └─1235 nginx: worker process
    
    Aug 16 11:40:27 ubuntu systemd[1]: Starting A high-performance web server and a r>
    Aug 16 11:40:27 ubuntu systemd[1]: nginx.service: Failed to parse PID from file /r>
    Aug 16 11:40:27 ubuntu systemd[1]: Started A high-performance web server and a re>
    
  4. By default, Nginx is configured to serve a default web page. You can verify that by opening a web browser and visiting http://localhost.

  5. Congratulations! You have successfully installed Nginx on your Linux Mint system.

Configuring Nginx

Now that Nginx is installed, you may want to customize its configuration. The Nginx configuration files are located in the /etc/nginx directory. The nginx.conf file is the main configuration file, while the sites-available directory contains configuration files for various virtual hosts.

Here are some basic configurations you may want to consider:

1. Server Blocks

You can create multiple server blocks to host multiple websites on a single server instance. Each server block has its own configuration file in the /etc/nginx/sites-available directory.

To create a new server block, follow these steps:

  1. Create a new configuration file in the sites-available directory. For example:

    cd /etc/nginx/sites-available
    sudo nano example.com
    

    This will open a new file called example.com in the Nano text editor.

  2. Add the following code to the file:

    server {
        listen 80;
        server_name example.com www.example.com;
    
        root /var/www/example.com/html;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
    

    Here, we are specifying that Nginx should listen on port 80 for requests to example.com. The server block will serve files from the /var/www/example.com/html directory and use index.html as the default page. Finally, the location block specifies how Nginx should handle requests for all other URLs.

  3. Save the file and exit the text editor by pressing Ctrl+O and Ctrl+X.

  4. Create a symbolic link to the sites-enabled directory to enable the new configuration file:

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

    This creates a symbolic link between the sites-available/example.com file and the sites-enabled directory.

  5. Test the configuration:

    sudo nginx -t
    

    If the test is successful, you should see the following message:

    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  6. Finally, restart Nginx to apply the changes:

    sudo systemctl restart nginx
    

2. SSL

Nginx also supports SSL encryption to secure HTTP connections over the internet. Here's how to enable SSL for your website:

  1. Generate a self-signed SSL certificate:

    sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/example.com.key -out /etc/nginx/ssl/example.com.crt
    

    This command generates a new SSL key pair and saves it to /etc/nginx/ssl directory.

  2. Add the following code to your server block:

    server {
        listen 443 ssl;
        server_name example.com www.example.com;
    
        root /var/www/example.com/html;
        index index.html;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        ssl_certificate /etc/nginx/ssl/example.com.crt;
        ssl_certificate_key /etc/nginx/ssl/example.com.key;
    }
    

    This specifies that Nginx should listen on port 443 for HTTPS traffic and use the SSL certificate we just created.

  3. Save the file and exit the text editor.

  4. Test the configuration:

    sudo nginx -t
    

    If the test is successful, you should see the following message:

    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  5. Finally, restart Nginx to apply the changes:

    sudo systemctl restart nginx
    

Conclusion

In this tutorial, we have covered the step-by-step process of installing Nginx on Linux Mint. We also demonstrated how to configure Nginx for serving multiple websites and enabling SSL encryption. You may explore more advanced configurations to further optimize and secure your web server.

If you want to self-host in an easy, hands free way, need an external IP address, or simply want your data in your own hands, give IPv6.rs a try!

Alternatively, for the best virtual desktop, try Shells!