If you’re looking to set up a web server on Debian, Nginx is a popular choice due to its lightweight nature and efficient handling of high traffic. In this guide, we’ll walk through the steps to set up Nginx on a Debian machine and configure it to serve web content.
Step 1: Install Nginx
First, update the package manager:
sudo apt update
Then, install Nginx:
sudo apt install nginx
Step 2: Adjust Firewall Settings
Next, you’ll want to adjust your firewall settings to allow HTTP and HTTPS traffic. If you’re using ufw, you can do this with the following commands:
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'Nginx HTTPS'
sudo ufw enable
Step 3: Configure Nginx
By default, Nginx will serve the files located in the /var/www/html
directory. If you’d like to serve content from a different directory, you’ll need to adjust the configuration file.
Open the configuration file with your preferred text editor:
sudo nano /etc/nginx/sites-available/default
Then, adjust the root
setting to point to your desired directory:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /path/to/your/directory;
index index.html;
server_name your-domain.com www.your-domain.com;
location / {
try_files $uri $uri/ =404;
}
}
Save and close the file, then test the configuration with the following command:
sudo nginx -t
If the test is successful, restart Nginx with the following command:
sudo systemctl restart nginx
Step 4: Test the Server
Finally, you’ll want to test that the server is up and running. Open a web browser and navigate to your server’s IP address or domain name. You should see the content located in the directory you specified in the configuration file.
Congratulations! You’ve successfully set up a web server on Debian using Nginx. From here, you can customize the server to your liking, add SSL certificates for secure connections, and more.