One of the most common tasks in server administration and web application deployment is configuring an HTTP server to host our projects. By using Nginx on a Debian-based system, this can be achieved quickly and efficiently.
The goal here is to create a new website configuration step-by-step and have it ready to serve requests.
First, we must ensure Nginx is installed on our system. Run the following update and installation commands in your terminal:
sudo apt update sudo apt install nginx -y
This command downloads and installs the web server along with its basic dependencies.
Next, we will create the directory where our new website's files will be hosted (replace your-site.com with your domain name):
sudo mkdir -p /var/www/your-site.com/html
Now, let's define the basic content for the HTML file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to your-site.com</title>
</head>
<body>
<h1>Congratulations! The Nginx site is running correctly.</h1>
</body>
</html>
Now we need to instruct Nginx on how to respond when someone searches for our domain. To do this, we will create a configuration file in the sites-available block:
sudo nano /etc/nginx/sites-available/your-site.com
Inside this file, we will add the server block declaration that will listen on port 80:
server {
listen 80;
listen [::]:80;
root /var/www/your-site.com/html;
index index.html;
server_name your-site.com www.your-site.com;
location / {
try_files $uri $uri/ =404;
}
}
The configuration uses the root directive to point to the path of the index.html file we just created. If a request searches for a non-existent file, it is caught and returns a 404 error. If the request is successful, the result is sent directly to the user's browser.
To activate the site, you need to create a symbolic link that connects our file to the Nginx sites-enabled block:
sudo ln -s /etc/nginx/sites-available/your-site.com /etc/nginx/sites-enabled/
With this, the server now knows which folders and domains it should read. To avoid typos or syntax errors, we validate the configuration with the following command:
sudo nginx -t
If the terminal confirms that the syntax is correct, proceed to restart the service to apply the changes permanently:
sudo systemctl restart nginx
The complete workflow looks like this in the terminal:
1. Update and install Nginx
sudo apt update && sudo apt install nginx -y
2. Create the site directory
sudo mkdir -p /var/www/your-site.com/html
3. Assign the correct permissions to the directory
sudo chown -R $USER:$USER /var/www/your-site.com/html
sudo chmod -R 755 /var/www/your-site.com
4. Create the symbolic link for activation
sudo ln -s /etc/nginx/sites-available/your-site.com /etc/nginx/sites-enabled/
5. Check configuration and restart the service
sudo nginx -t
sudo systemctl restart nginx
To see the example in action, open your favorite web browser and enter your domain or your server's local IP address.
I hope this article was helpful to you! :)
Comentarios