How To Install Nginx on Ubuntu 20.04

Nginx is a powerful and popular web server that is widely used to serve static content, reverse proxy, and more. In this guide, we'll walk through the steps to install Nginx on Ubuntu 20.04.

Prerequisites: You should have sudo privileges on your Ubuntu server. If you're logged in as a non-root user, make sure your user account is added to the sudo group, or that you have direct root access.

Step 1: Update the Package Repository

Before installing any new software, it's a good practice to ensure that your package repository is up-to-date.

sudo apt update 

Step 2: Install Nginx

Once the package repository is updated, you can proceed to install Nginx using the following command:

sudo apt install nginx 

Step 3: Start Nginx and Enable it to Start on Boot

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

sudo systemctl start nginx
sudo systemctl enable nginx 

Step 4: Check the status of Nginx

To check the status of Nginx on your Ubuntu 20.04 server, you can use the systemctl command. Here are the steps:

sudo systemctl status nginx 

This command will display information about the current status of the Nginx service, including whether it is active, the process ID (PID), and recent log entries. If Nginx is running without any issues, you should see output similar to the following:

● 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 [Timestamp] [Timezone]; [Duration]
     Docs: man:nginx(8)
 Main PID: [PID]
    Tasks: [Number] (limit: [Limit])
   Memory: [Memory]
   CGroup: /system.slice/nginx.service
           ├─[PID] nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─[PID] nginx: worker process

[Additional information about the service] 

If Nginx is not running, the output will indicate that the service is inactive or failed, and you might see error messages that can help diagnose the issue.

Use the q key to exit the status display and return to the command prompt.

Step 5: Verify the Installation

You can check if Nginx is running by accessing your server's IP address or domain name in a web browser. If everything is set up correctly, you should see the default Nginx welcome page.

If you are on this page, your server is running correctly and is ready to be managed.

Step 6: Configure Firewall (if applicable)

If you have a firewall enabled, you might need to allow traffic on the default Nginx port (port 80). Use the following command to open the necessary port:

sudo ufw allow 'Nginx HTTP' 

Additional Configuration (Optional)

If you're hosting multiple websites on your server, you can set up server blocks to manage them. The Nginx configuration files are located in the /etc/nginx/sites-available/ directory.

Setting up server blocks in Nginx, also known as virtual hosts, allows you to host multiple websites on a single server. Each server block can have its own configuration, including separate domain names, directories, and settings. Here's a guide on how to set up server blocks on Nginx:

Step 1: Server Blocks

Nginx configuration files for server blocks are typically stored in the /etc/nginx/sites-available/ directory. Create a configuration file for each of your websites:

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

Replace "example.com" with your actual domain name. In this file, you'll define the configuration for your website. Here's a basic example:

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;
    }
}
  • listen: The port that Nginx should listen on (80 for HTTP).
  • server_name: Your domain names separated by spaces.
  • root: The root directory of your website.
  • index: The default file to serve (e.g., index.html).

Step 2 :Create Symbolic Links to Enable Server Blocks

Create symbolic links from the sites-available directory to the sites-enabled directory:

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

Repeat this step for each server block you've created.

Step 3: Test Nginx Configuration

Before restarting Nginx, ensure that there are no syntax errors in your configuration:

sudo nginx -t

If you receive a message like "nginx: configuration file /etc/nginx/nginx.conf test is successful," you can proceed. If there are errors, review the error message and correct the configuration files.

Step 4: Restart Nginx

Apply the changes by restarting the Nginx service:

sudo systemctl restart nginx

Step 5: Adjust Firewall Rules (if applicable)

If you're using a firewall, update the rules to allow traffic for your additional server blocks:

sudo ufw allow 'Nginx Full'

Step 6: Repeat for Additional Server Blocks

Repeat steps 1-5 for each additional website you want to host on the server, creating separate configuration files and symbolic links.

Step 7: Test Your Configuration

Open your web browser and navigate to each of your domain names to ensure that the server blocks are working correctly.

Step 8: SSL/TLS Configuration (Optional)

For secure connections, consider setting up SSL/TLS certificates using Let's Encrypt or other certificate authorities.

Conclusion

Congratulations! You've successfully installed Nginx on Ubuntu 20.04. Feel free to explore additional configurations and optimizations based on your specific requirements.

Remember, this guide provides a basic setup. Depending on your use case, you might need to make further configurations to meet your specific needs. Happy blogging!

Comments

Leave a Reply