How to Install and Configure Nginx on Ubuntu

20 Oct 2024 03:07 AM

Setting up a robust web server is a critical task for anyone managing websites or applications. Nginx is a powerful, efficient, and highly scalable web server that is often used for serving static files, reverse proxying, and load balancing. In this guide, we’ll walk you through how to install and configure Nginx on Ubuntu, one of the most popular Linux distributions.

Step 1: Update Your Package Index

Before installing any new software, it’s a good practice to ensure your package index is up-to-date. This ensures that you’re pulling the latest versions of packages from the repository. To update the package list, open your terminal and run the following command:

sudo apt update

This will refresh the local package index with the latest updates from the repositories.

Step 2: Install Nginx

Once the package index is updated, installing Nginx is just one command away. Use the following command to install Nginx on your Ubuntu system:

sudo apt install nginx

Ubuntu will automatically fetch and install Nginx along with its dependencies. Once installed, Nginx is typically set to start immediately, but we’ll verify this in the next step.

Step 3: Start and Enable Nginx

After installation, Nginx should start automatically. To check whether Nginx is running, use the following systemd command:

sudo systemctl status nginx

You should see an active status indicating that Nginx is up and running.

If for any reason Nginx is not running, you can manually start the service by typing:

sudo systemctl start nginx

To ensure Nginx starts automatically every time your server boots up, enable it with:

sudo systemctl enable nginx

Now Nginx will be active even after reboots, ensuring your web server is always up.

Step 4: Allow Nginx Through the Firewall

If your server has a firewall enabled, you’ll need to adjust the settings to allow traffic on HTTP (port 80) and HTTPS (port 443). Ubuntu’s Uncomplicated Firewall (UFW) allows pre-configured profiles for common services like Nginx. To allow HTTP and HTTPS traffic, use the following command:

sudo ufw allow 'Nginx Full'

This opens up both port 80 and port 443 for your web server. To confirm that the firewall rule is applied, you can check the firewall status:

sudo ufw status

You should see a line that allows “Nginx Full” traffic.

Step 5: Verify the Nginx Installation

At this point, Nginx should be installed and running. To verify that everything is working, open your browser and navigate to your server’s IP address:

http://your_server_ip

If Nginx is installed correctly, you’ll see the default Nginx welcome page, which looks something like this:

 

This confirms that your Nginx server is up and running successfully.

Step 6: Configuring Nginx for Your Website

Now that Nginx is running, it’s time to configure it for your website or application. Nginx’s default configuration files are located at:

/etc/nginx/nginx.conf

However, the best practice is to configure your website using the /etc/nginx/sites-available/ and /etc/nginx/sites-enabled/ directories. This method allows you to easily manage multiple websites on the same server.

Creating a New Server Block

To set up a new website, you’ll need to create a new server block configuration file in the sites-available directory. This is how you specify how Nginx should handle incoming requests for your domain.

Let’s create a new configuration file for a website called example.com:

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

In this file, add the following basic configuration:

server {
    listen 80;
    server_name example.com www.example.com;

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

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

Let’s break this down:

  • listen 80;: Nginx listens for HTTP traffic on port 80.
  • server_name example.com www.example.com;: Specifies the domain name for your website.
  • root /var/www/example.com/html;: Defines the directory where your website’s files are stored.
  • index index.html index.htm;: Specifies the default file that Nginx should serve when no file is requested (typically the homepage).
  • location / {}: Handles requests for the root directory, using the try_files directive to serve files if they exist, or return a 404 error if they don’t.

Step 7: Enable Your Website

Once your server block configuration is complete, you need to enable it. To do this, create a symbolic link from the sites-available directory to the sites-enabled directory:

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

This tells Nginx to use the configuration for example.com.

Step 8: Test Your Nginx Configuration

Before restarting Nginx, it’s always a good idea to test your configuration for any syntax errors. You can do this with the following command:

sudo nginx -t

If everything is configured correctly, you should see a message saying:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 9: Restart Nginx

Finally, restart Nginx to apply the new configuration:

sudo systemctl restart nginx

Your website should now be live. You can verify this by opening your browser and visiting example.com.

Conclusion

Congratulations! You’ve successfully installed and configured Nginx on Ubuntu. With Nginx running, you have a powerful, efficient web server capable of handling a wide range of use cases, from serving simple websites to acting as a reverse proxy for complex applications.

Feel free to expand your Nginx knowledge by exploring more advanced configurations, such as SSL setup, load balancing, and caching. Happy hosting!

 

1
10