- 1 1. Introduction
- 2 2. Installing Nginx on Ubuntu
- 3 3. Basic Nginx Configuration on Ubuntu
- 4 4. Starting and Managing Nginx
- 5 5. Nginx Firewall Settings and Security Hardening
- 6 6. Nginx Performance Tuning and Troubleshooting
- 7 7. Summary and Next Steps
1. Introduction
Many users are interested in using Nginx on Ubuntu, but beginners may not know where to start. This article provides an easy-to-understand, step-by-step explanation—from installing Nginx on Ubuntu to configuring and managing it—even if you have no prior experience.
What is Nginx?
Nginx (Engine-X) is a high-performance HTTP server and reverse proxy server. Due to its lightweight and flexible architecture, it is widely used in web servers and applications around the world. Its structure is particularly well-suited for handling high-traffic environments.
Benefits of Using Nginx on Ubuntu
Ubuntu is a popular Linux distribution with excellent compatibility with Nginx. Because Ubuntu uses the APT package management system, installation and updates are easy, and you can expect strong stability and security.
After reading this article, you will understand:
- How to install Nginx on Ubuntu
- Basic configuration and management procedures
- Performance tuning and troubleshooting techniques
Now, let’s get started.
2. Installing Nginx on Ubuntu
To run Nginx on Ubuntu, you must install it correctly. This section explains the installation steps in a clear and simple manner.
Check Required Packages
Before installing Nginx, make sure your system is up to date. Run the following commands:
sudo apt update
sudo apt upgradeInstall Nginx
To install Nginx, enter the following command:
sudo apt install nginxThis command downloads and installs Nginx directly from Ubuntu’s APT repository.
Verify Installation
Run the following command to check if Nginx was installed correctly:
sudo systemctl status nginxIf the status shows active (running), the installation was successful.
Configure the Firewall
Ubuntu includes UFW (Uncomplicated Firewall), which you can use to allow Nginx traffic.
sudo ufw allow 'Nginx Full'
sudo ufw enableNginx is now ready to communicate over port 80 (HTTP) and port 443 (HTTPS).
3. Basic Nginx Configuration on Ubuntu
Installing Nginx alone is not enough—you must configure it properly to use it effectively.
Location and Structure of Configuration Files
Nginx uses the following configuration files:
- Main configuration file:
/etc/nginx/nginx.conf - Site-specific configuration files: located in the
/etc/nginx/sites-available/directory
Typically, you create virtual host configuration files in the sites-available directory and then create symbolic links in the sites-enabled directory to activate them.
Configuring a Virtual Host
Virtual hosts allow a single server to manage multiple domains and projects. The following example configures example.com:
sudo nano /etc/nginx/sites-available/example.comEnter the configuration below:
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;
}
}Save the file, then enable the configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxConfiguring SSL/TLS
To enable secure communication, configure SSL/TLS certificates. You can obtain free certificates using Let’s Encrypt.
First, install certbot:
sudo apt install certbot python3-certbot-nginxThen issue the certificate and apply settings automatically:
sudo certbot --nginx -d example.com -d www.example.comEnsure certificate auto-renewal is enabled to prevent expiration.
4. Starting and Managing Nginx
Once Nginx is installed and configured, you need to learn how to start, stop, reload, and troubleshoot it. This section explains essential administrative commands.
Basic Nginx Commands
On Ubuntu, use the systemctl command to manage Nginx:
- Start Nginx
sudo systemctl start nginx- Stop Nginx
sudo systemctl stop nginx- Restart Nginx
Use when major configuration changes are made.
sudo systemctl restart nginx- Reload Nginx
Apply configuration changes without restarting.
sudo systemctl reload nginx- Check Nginx Status
sudo systemctl status nginxEnable Automatic Startup
To start Nginx automatically after system reboot:
- Enable auto-start
sudo systemctl enable nginx- Disable auto-start
sudo systemctl disable nginxLog Checks and Troubleshooting
If Nginx doesn’t work as expected, check the logs:
- Error log
sudo tail -f /var/log/nginx/error.log- Access log
sudo tail -f /var/log/nginx/access.logCommon Issues and Solutions
- Nginx doesn’t start
- Cause: Configuration file errors
- Solution:
bash sudo nginx -t
Check for syntax errors and correct them.
- Changes are not applied
- Cause: Nginx was not reloaded
- Solution:
Runsudo systemctl reload nginxafter changes.
- Ports not open
- Cause: Firewall settings
- Solution:
bash sudo ufw allow 'Nginx Full'

5. Nginx Firewall Settings and Security Hardening
Security is critical when operating Nginx. This section explains how to configure UFW and strengthen Nginx security.
Opening Ports with UFW
UFW is installed by default on Ubuntu. To enable Nginx communication, open the required ports:
- Check UFW status
sudo ufw status- Apply Nginx rules
sudo ufw allow 'Nginx Full'- Enable UFW
sudo ufw enableAdditional Security Enhancements
- Disable unnecessary HTTP methods
if ($request_method !~ ^(GET|POST|HEAD)$) {
return 444;
}- Disable directory listing
autoindex off;- Strengthen SSL/TLS
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers HIGH:!aNULL:!MD5;6. Nginx Performance Tuning and Troubleshooting
Nginx is known for its performance, but proper tuning can improve it even further. This section explains how to optimize resources and solve common issues.
Performance Tuning
1. Configure Worker Processes
Edit /etc/nginx/nginx.conf and optimize the number of worker processes:
worker_processes auto;2. Increase Worker Connections
worker_connections 1024;3. Enable HTTP/2
listen 443 ssl http2;sudo systemctl reload nginx4. Enable Compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;5. Configure Caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg)$ {
expires 30d;
access_log off;
}Common Problems and Solutions
1. Slow requests under high load
- Cause: insufficient worker process or connection settings
- Solution: adjust worker parameters or introduce load balancing
2. 403 Forbidden Error
- Cause: incorrect
rootpath or improper permissions - Solution:
- Verify the
rootdirective and fix permissions using: bash sudo chmod -R 755 /var/www/example.com/html sudo chown -R www-data:www-data /var/www/example.com/html
3. Server Timeout
- Cause: client request processing takes too long
- Solution:
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;4. Restart Errors
- Cause: configuration syntax issues
- Solution:
sudo nginx -t7. Summary and Next Steps
This article explained how to install, configure, and manage Nginx on Ubuntu. Let’s review what you’ve learned and identify possible next steps.
Article Review
1. Installing Nginx on Ubuntu
- Prepared necessary packages and learned how to install Nginx using APT
- Configured the firewall (UFW) to allow Nginx traffic
2. Basic Configuration
- Learned file locations, virtual host setup, and SSL/TLS configuration
3. Management Operations
- Mastered start, stop, restart, reload, and log investigation commands
4. Firewall and Security
- Opened required ports using UFW and disabled unnecessary HTTP methods
5. Performance Tuning and Troubleshooting
- Optimized worker processes, enabled HTTP/2, configured caching, and resolved common issues
Next Steps
Now that you understand the fundamental installation and configuration steps for Nginx on Ubuntu, consider advancing further:
- Learn advanced Nginx configuration
- Study load balancing and reverse proxy features
- Try additional modules such as the Nginx RTMP module
- Enhance security
- Consider implementing a Web Application Firewall (WAF)
- Introduce automation
- Use Ansible or Docker to automate configuration and deployment
- Use monitoring and optimization tools
- Implement Prometheus or Grafana to visualize and monitor performance
Final Thoughts
Nginx is a fast and flexible web server capable of supporting a wide range of use cases when configured correctly. Apply what you’ve learned here to real-world projects and continue exploring additional features through the official Nginx documentation and community resources.
I hope this guide helps you manage your web server more efficiently!


