- Published on
Nginx Installation Guide on Ubuntu for Beginners
- Authors

- Name
- Khoa (Atlas Labs)
- Occupation
- Full-stack developer
1. Introduction to Nginx
What is Nginx?
Nginx is a powerful web server designed to handle thousands of simultaneous requests, with much higher performance than Apache. In addition to serving static websites, Nginx is also used as a reverse proxy, load balancer, and API gateway.
Comparing Nginx with Apache
- Performance: Nginx is better at handling many requests.
- Resources: Nginx uses less RAM.
- Configuration: Apache has more modules, but Nginx is simpler and more efficient.
Advantages of Nginx
- High performance, fast processing.
- HTTP/2 support.
- Easy to install and configure.
2. Why Choose Nginx?
Suitable for Various Types of Websites
- Static websites.
- Dynamic websites combined with Node.js, PHP.
- API applications.
Scalability
- Reverse proxy: Forward requests to a backend server.
- Load balancing: Distribute requests evenly across servers.
3. Preparing the Environment
Installing Ubuntu
- Download the ISO from the Ubuntu website.
- Create a bootable USB and install.
- Configure networking and install essential packages.
Update the System
sudo apt update && sudo apt upgrade -y
4. Installing Nginx
Install Nginx
Use the following command to install:
sudo apt install nginx -y
Check Nginx Status
sudo systemctl status nginx
If Nginx has been successfully installed, you will see the status "active (running)".
5. Basic Configuration
Nginx Directory Structure
/etc/nginx/nginx.conf: The main configuration file./etc/nginx/sites-available/: Stores virtual host configuration files./var/www/html/: The default web root directory.
Edit the Default Configuration File
Open the file:
sudo nano /etc/nginx/sites-available/default
Edit to serve a website:
server {
listen 80;
server_name localhost;
root /var/www/html;
location / {
index index.html;
}
}
Reload Nginx:
sudo systemctl reload nginx
Create a Server Block
Create a new configuration file:
sudo nano /etc/nginx/sites-available/mywebsite
Add the configurations:
server {
listen 80;
server_name example.com;
root /var/www/mywebsite;
location / {
index index.html;
try_files $uri $uri/ =404;
}
}
Create a symbolic link to sites-enabled:
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
6. Accessing Nginx
Open a browser and enter your IP address or domain name to access the website.
7. Customizing Configuration
Virtual Host
Serve multiple websites by creating multiple server blocks in sites-available.
SSL
Install Let's Encrypt:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com
Caching
Add caching headers to the configuration:
location / {
expires 1d;
add_header Cache-Control "public, no-transform";
}
8. Optimizing Nginx
Worker Processes
Maximize the number of worker processes:
worker_processes auto;
Connections
Increase the number of connections:
worker_connections 1024;
Gzip Compression
Enable gzip:
gzip on;
gzip_types text/plain application/json;
9. Nginx Management Commands
Start:
sudo systemctl start nginxStop:
sudo systemctl stop nginxRestart:
sudo systemctl restart nginxCheck logs:
sudo tail -f /var/log/nginx/access.log sudo tail -f /var/log/nginx/error.log
10. Conclusion
Summary
This article guides you step by step through installing and configuring Nginx on Ubuntu, highlighting its advantages such as high performance and flexibility.
Advice
Beginners should follow the steps in this article and explore further customizations in the official Nginx documentation.