Deploy Your First Website with Nginx on Linux
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
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
2. Set file permissions:
sudo chown -R nginx:nginx /var/www/my-site
3. Create a basic HTML file:
echo "<h1>Welcome to my Nginx website</h1>" > /var/www/my-site/index.html
4. 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 80server_name: the domain or local name for your siteroot: the path where Nginx will look for site filesindex: the default page if no file is specified in the URLlocation /: matches the root of the sitetry_files: checks for file/directory; shows 404 if not found
5. Enable the site (Debian/Ubuntu only):
sudo ln -s /etc/nginx/sites-available/my-site /etc/nginx/sites-enabled/
6. Reload Nginx:
sudo nginx -t # Test configuration
sudo systemctl reload nginx
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.