DEV Community

S3CloudHub
S3CloudHub

Posted on

Installing Nginx Web Server on Linux: A Step-by-Step Guide

When it comes to setting up a powerful and efficient web server, Nginx is a popular choice due to its performance and reliability. This guide walks you through the process of installing and configuring the Nginx web server on a Linux machine. Let’s get started!

Image description

What is Nginx?

Nginx is an open-source, high-performance web server and reverse proxy server that handles concurrent connections efficiently. It’s widely used for hosting websites, load balancing, and as a reverse proxy for applications.

Step 1: Update Your System
Before installing any software, it’s always a good practice to update your system packages.

sudo apt update && sudo apt upgrade -y   # For Debian/Ubuntu-based systems
sudo yum update -y                       # For CentOS/RHEL-based systems
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Nginx
Nginx is included in the default repositories of most Linux distributions, making it easy to install.

For Debian/Ubuntu

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

For CentOS/RHEL

sudo yum install epel-release -y
sudo yum install nginx -y
Enter fullscreen mode Exit fullscreen mode

For Fedora

sudo dnf install nginx -y
Enter fullscreen mode Exit fullscreen mode

Step 3: Start and Enable Nginx Service
Once installed, you need to start the Nginx service and ensure it runs on boot.

sudo systemctl start nginx
sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

Check the status to confirm it’s running:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Firewall
To allow web traffic to access your server, you must open HTTP (port 80) and optionally HTTPS (port 443) in the firewall.

For UFW (Ubuntu):

sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw reload
Enter fullscreen mode Exit fullscreen mode

For Firewalld (CentOS/RHEL):

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Enter fullscreen mode Exit fullscreen mode

Step 5: Verify Installation
Open a web browser and enter your server’s IP address. If everything is set up correctly, you should see the default Nginx welcome page.

http://<your-server-ip>
Enter fullscreen mode Exit fullscreen mode

You can find your server’s IP address using:

hostname -I
Enter fullscreen mode Exit fullscreen mode

Step 6: Customize Nginx Configuration
The Nginx configuration files are located in /etc/nginx/.

- Default Configuration File: /etc/nginx/nginx.conf
- Site Configuration Files: /etc/nginx/sites-available/ (Debian/Ubuntu)
To host your website, create a configuration file in the sites-available directory and link it to sites-enabled:

sudo nano /etc/nginx/sites-available/example.com
Enter fullscreen mode Exit fullscreen mode

Add the following content:

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

Create the symbolic link:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

Test the configuration:

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

Reload Nginx to apply changes:

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

Step 7: Test Your Web Server
Create a test HTML file to ensure your configuration works.

sudo mkdir -p /var/www/example.com
sudo nano /var/www/example.com/index.html
Enter fullscreen mode Exit fullscreen mode

Add the following content:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Nginx</title>
</head>
<body>
    <h1>Success! Your Nginx server is working.</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Save the file and visit your website in the browser:

http://<your-domain>
Enter fullscreen mode Exit fullscreen mode

Troubleshooting Tips

  • Check Logs:
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log
Enter fullscreen mode Exit fullscreen mode
  • Restart Nginx:
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You’ve successfully installed and configured the Nginx web server on your Linux machine. With this setup, you’re now ready to host your websites or applications efficiently. Explore more advanced configurations like HTTPS with SSL certificates or load balancing to further enhance your server’s capabilities.

If you found this guide helpful, don’t forget to share it with others and drop your comments below! 🚀

Top comments (0)