🌐 Introduction: What is a Web Server and Why Use Nginx?

When you type an address in your browser (like example.com), your browser sends an HTTP request to a server. The web server is the program that receives this request and sends back a response—usually an HTML page.

Nginx is a fast and lightweight web server, capable of handling a large number of concurrent connections. It is often used for:

  • Serving static websites
  • Acting as a reverse proxy
  • Load balancing
  • Hosting APIs

It’s widely used in production environments and also great for hosting your personal site or blog.


🎯 Goal of This Guide

We’ll learn how to:

  • Install Nginx on a Linux server
  • Create a simple website
  • Understand its file structure
  • Set up a Virtual Host (custom site)

🛠️ Installing Nginx

➤ Debian / Ubuntu:

sudo apt update
sudo apt install nginx

➤ CentOS / Fedora / RHEL:

sudo dnf install nginx          # or 'yum install nginx' on CentOS 7
sudo systemctl enable nginx
sudo systemctl start nginx

📡 Test the Service

Check that Nginx is running:

sudo systemctl status nginx

Then open your browser:
http://SERVER_IP

You should see Nginx’s default welcome page ✅


🗂️ File Structure (Debian/Ubuntu)

  • /etc/nginx/nginx.conf: main configuration file
  • /etc/nginx/sites-available/: directory for available site configs
  • /etc/nginx/sites-enabled/: directory for enabled sites (symbolic links)
  • /var/www/html/: default folder for web content

On CentOS/RHEL, it’s mostly managed in /etc/nginx/nginx.conf and /usr/share/nginx/html.


🌍 Create a Custom Site (Virtual Host)

  1. Create the site directory:
sudo mkdir -p /var/www/my-site
  1. Set file permissions
sudo chown -R nginx:nginx /var/www/my-site
  1. Create a basic HTML file:
echo "<h1>Welcome to my Nginx website</h1>" > /var/www/my-site/index.html
  1. Create the site configuration file:
sudo nano /etc/nginx/sites-available/my-site

Paste the following:

server {
    listen 80;                        # Listen on HTTP port
    server_name my-site.local;       # Domain or hostname

    root /var/www/my-site;           # Directory containing site files
    index index.html;                # Default file to serve

    location / {
        try_files $uri $uri/ =404;   # Show file if exists, else 404
    }
}

🔍 Explanation:

  • listen 80;: listen for HTTP requests on port 80
  • server_name: the domain or local name for your site
  • root: the path where Nginx will look for site files
  • index: the default page if no file is specified in the URL
  • location /: matches the root of the site
  • try_files: checks for file/directory; shows 404 if not found
  1. Enable the site (Debian/Ubuntu only):
sudo ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/
  1. Reload Nginx:
sudo nginx -t     # Test configuration
sudo systemctl reload nginx
  1. (Optional) Edit your /etc/hosts file locally:
sudo nano /etc/hosts

Add:

SERVER_IP my-site.local

This allows you to access http://my-site.local from your browser.


🔒 Bonus: Enable HTTPS with Let’s Encrypt

If you own a real domain, you can easily secure it with Certbot:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx

✅ Conclusion

Congrats! You’ve now:

  • Installed Nginx,
  • Set up a custom website,
  • And understood the basics of how Nginx works.

Next step? Try setting up a reverse proxy or host a dynamic web app.