Install Nginx and set up your first website
Install Nginx on Debian or Ubuntu, create a server block for your domain, open the firewall and reload the configuration safely.
Nginx is one of the most widely used web servers in the world: fast, light on memory and ideal for static sites, PHP applications and as a reverse proxy. This guide installs it on Debian 12 or Ubuntu 24.04 and sets up a first website under your domain.
Requirements
- A server running Debian 12 or Ubuntu 24.04 and SSH access as a user with
sudo - A domain whose A record (and AAAA record for IPv6) points to your server's IP
Install Nginx
sudo apt update
sudo apt install nginx
Nginx starts right after the installation. Check its status:
systemctl status nginx
If you open http://YOUR-IP in the browser now, you will see the Nginx welcome page.
Open the firewall
If you use UFW, allow HTTP and HTTPS:
sudo ufw allow 'Nginx Full'
sudo ufw status
At TryNxt there is also the panel firewall. Ports 80 and 443 must be open there as well if you restrict incoming connections.
Create a directory for the website
sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com/html
echo '<h1>It works!</h1>' > /var/www/example.com/html/index.html
Replace example.com with your domain everywhere.
Create a server block
Nginx calls the configuration of a website a "server block". Create a new file:
sudo nano /etc/nginx/sites-available/example.com
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable the site with a link and remove the default site:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
Test and reload the configuration
Always test the syntax before reloading. That way a typo shows up before your website goes down:
sudo nginx -t
sudo systemctl reload nginx
reload applies the new configuration without interrupting running connections.
Set up HTTPS
Browsers mark websites without HTTPS as "not secure". You get a free certificate in two minutes with Certbot, step by step in our guide Let's Encrypt certificate with Certbot.
Useful commands
| Command | Effect |
|---|---|
sudo nginx -t |
Test the configuration |
sudo systemctl reload nginx |
Reload the configuration |
sudo systemctl restart nginx |
Restart Nginx |
sudo tail -f /var/log/nginx/error.log |
Follow errors live |
sudo tail -f /var/log/nginx/access.log |
Follow requests live |
Common errors
- 403 Forbidden: Nginx cannot read the directory or there is no
indexfile. Check the permissions withls -la /var/www/example.com/html. - The default page still shows: The link in
sites-enabledis missing, orserver_namedoes not match the requested domain. - "Address already in use": Another service, often Apache, is using port 80. Find it with
sudo ss -ltnp | grep :80.
For the community
Know your stuff? Write a guide.
Share what you know: we review every submitted guide, publish it under your name and say thank you with credit for your services.