Nginx is a fast, lightweight web server that is excellent for hosting websites and functioning as a reverse proxy. On Ubuntu, you install Nginx with a single apt command, after which you configure server blocks for your domains and enable SSL for a secure connection. In this article, you’ll go through the complete installation and basic configuration, including firewall settings and testing your configuration.
Installing Nginx on Ubuntu
Before installing Nginx, update the package list so you get the latest version from the official Ubuntu repository.
Update package list
Run sudo apt update to refresh the list of available packages.
Install Nginx
Install the web server with sudo apt install nginx -y. Ubuntu automatically places the configuration in /etc/nginx.
Verify installation
Open your server’s IP address in a browser. If you see the default Nginx welcome page, the service is running correctly.
Managing firewall and service
Ubuntu often uses UFW as a firewall. Without the proper rule, you’ll block incoming traffic on ports 80 and 443, preventing visitors from reaching your site.
sudo ufw allow 'Nginx Full'opens both HTTP and HTTPS.sudo systemctl status nginxshows whether the service is active.sudo systemctl restart nginxrestarts Nginx after a configuration change.sudo systemctl enable nginxensures Nginx starts automatically after a reboot.
sudo nginx -t before restarting the service. This prevents downtime caused by a typo.Configuring server blocks
A server block (the Nginx equivalent of an Apache virtual host) determines which website is shown for which domain. You create a separate configuration file for each domain.
- Create a folder for your website:
sudo mkdir -p /var/www/example.com/html. - Create a new configuration file:
sudo nano /etc/nginx/sites-available/example.com. - Add a
serverblock withlisten,server_name, androot. - Enable the file with a symlink:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/. - Test the configuration with
sudo nginx -tand restart Nginx.
Don’t feel like tinkering with server configuration yourself? With web hosting from Tandata, you manage websites via Plesk, including NVMe storage and daily backups, without manually managing configuration files.
Setting up SSL with Let’s Encrypt
A valid SSL certificate is essential for a secure connection and for building visitor trust. With Certbot, you can arrange this for free and automatically for Nginx on Ubuntu.
- Install Certbot:
sudo apt install certbot python3-certbot-nginx -y. - Request a certificate:
sudo certbot --nginx -d example.com -d www.example.com. - Certbot automatically adjusts the Nginx configuration and enables HTTPS.
- Check automatic renewal with
sudo certbot renew --dry-run.
Make sure your domain correctly points to the server address before requesting a certificate; otherwise, validation will fail. If you manage your DNS settings via domain names, you can easily adjust records before using Certbot.
Frequently Asked Questions
Can I use Nginx and Apache simultaneously on the same server?
Yes, but not on the same port. You’ll need to run one of the two on a different port or use Nginx as a reverse proxy for Apache.
Why am I getting a 502 Bad Gateway error?
This usually indicates a problem with the backend application Nginx is forwarding to, for example a PHP-FPM process that isn’t running or is misconfigured.
How do I check the Nginx log files?
Access logs are located by default in /var/log/nginx/access.log and error messages in /var/log/nginx/error.log.
Do I need to update Nginx manually?
Using sudo apt update && sudo apt upgrade keeps Nginx up to date. On managed hosting, this is usually done automatically.
Does Nginx also work well for email?
Nginx isn’t a mail server, but it can be combined with separate mail services. For business email with a high security score, check out business email.
Conclusion
Installing Nginx on Ubuntu is quick with apt, but the real configuration lies in server blocks, firewall rules, and SSL. Follow these steps carefully and test every change before restarting, and you’ll have a stable and secure web server. Not in the mood for server management? Check out how easy it is to switch to Tandata, or consult the knowledge base for more technical explanations.