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:
Open a terminal window by pressing
Ctrl+Alt+Tor clicking on Terminal from the application menu.Execute the following command to install Nginx:
sudo apt install nginx -yThis will download and install Nginx and all the required packages.
Once the installation is complete, the Nginx service should start automatically. You can check its status by running:
sudo systemctl status nginxIf 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>By default, Nginx is configured to serve a default web page. You can verify that by opening a web browser and visiting
http://localhost.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:
Create a new configuration file in the
sites-availabledirectory. For example:cd /etc/nginx/sites-available sudo nano example.comThis will open a new file called
example.comin the Nano text editor.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
80for requests toexample.com. The server block will serve files from the/var/www/example.com/htmldirectory and useindex.htmlas the default page. Finally, thelocationblock specifies how Nginx should handle requests for all other URLs.Save the file and exit the text editor by pressing
Ctrl+OandCtrl+X.Create a symbolic link to the
sites-enableddirectory 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.comfile and thesites-enableddirectory.Test the configuration:
sudo nginx -tIf the test is successful, you should see the following message:
nginx: configuration file /etc/nginx/nginx.conf test is successfulFinally, 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:
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.crtThis command generates a new SSL key pair and saves it to
/etc/nginx/ssldirectory.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
443for HTTPS traffic and use the SSL certificate we just created.Save the file and exit the text editor.
Test the configuration:
sudo nginx -tIf the test is successful, you should see the following message:
nginx: configuration file /etc/nginx/nginx.conf test is successfulFinally, 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.