Edited 3 weeks ago by ExtremeHow Editorial Team
DebianNginxWeb ServerWeb DevelopmentServer SetupLinuxSystem AdministrationOpen SourceCLIIT
This content is available in 7 different language
Nginx is a widely used web server software known for its high performance, stability, low resource consumption, and simple configuration. In this detailed guide, we will see how to set up a web server with Nginx on Debian operating system. Before we dive into the steps, it is essential to understand what Nginx is and why it is beneficial for web server configuration.
Nginx (pronounced as "engine-x") is an open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started as a project to address the C10k problem - handling ten thousand connections at once. Today, Nginx is popular for its performance and efficiency in managing high loads. It can act as a reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, a load balancer, and an HTTP cache, among other uses. Being open-source, it is free to use and is constantly being improved by global contributors.
Before you begin, make sure you have the following things:
sudo
privileges.Once you have these prerequisites met, you can proceed to install and configure Nginx on your Debian server.
The first step is to make sure your server is up to date with the latest packages. An updated system reduces incompatibility issues and enhances security. Use the following command to update your server:
sudo apt update sudo apt upgrade
apt update
command refreshes the list of packages and their available versions. apt upgrade
command upgrades older packages with newer versions.
Debian's APT package manager makes it easy to install software packages such as Nginx. Execute the following command to install Nginx:
sudo apt install nginx
This command will get and install the Nginx package and its dependencies. Once the installation is complete, Nginx will start running automatically.
Once Nginx is installed, it is necessary to verify its installation and ensure that it is running correctly. Use the following command to check the status of Nginx:
sudo systemctl status nginx
The expected output shows that Nginx is active and running. If you want to start or stop the Nginx service, you can use:
sudo systemctl start nginx sudo systemctl stop nginx
After confirming that Nginx is running, open a web browser and look for your server's IP address. If you don't know your server's IP address, you can find it out like this:
hostname -I
When you enter your server's IP address in the browser's address bar, you should see the default Nginx welcome page. This verifies that Nginx is rendering web pages correctly.
On Debian systems, UFW (Uncomplicated Firewall) is commonly used as firewall management software. If UFW is running on your server, you must allow HTTP traffic to publicly access your web server through the firewall. You can check the current status of UFW as follows:
sudo ufw status
If UFW is activated, use this command to allow Nginx HTTP traffic:
sudo ufw allow 'Nginx HTTP'
Double-check the situation to make sure the relevant rules apply:
sudo ufw status
Nginx runs as a service in the background. You can manage this service using the systemctl command. Here are some useful systemctl commands to manage Nginx:
sudo systemctl start nginx
: Starts the Nginx service.sudo systemctl stop nginx
: Stops the Nginx service.sudo systemctl restart nginx
: Restarts the Nginx service. Use this after making changes to configuration files or resolving minor issues.sudo systemctl reload nginx
: Restarts Nginx without dropping connections. Useful for applying changes without interrupting users.sudo systemctl enable nginx
: Sets Nginx to start automatically at boot.sudo systemctl disable nginx
: Prevents Nginx from starting automatically at boot.Nginx configuration files are typically located in the /etc/nginx
directory. The main configuration file is /etc/nginx/nginx.conf
, which contains instructions for managing modules, setting user permissions, and more.
Often, virtual host configuration is managed separately in the /etc/nginx/sites-available
directory. This directory contains configuration files for individual domains, which are symlinked to /etc/nginx/sites-enabled
to be activated. You can also find configuration snippets in /etc/nginx/snippets
for reusable settings.
Server blocks, Apache's equivalent of virtual hosts, allow Nginx to run multiple sites on a single server. First, go to /etc/nginx/sites-available/
and create a new configuration file:
sudo nano /etc/nginx/sites-available/example.com
In the following configuration file, replace "example.com" with your domain name:
server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
With this setup, requests for 'example.com' will be proxied to the application running on 127.0.0.1:8080. Use sensible default values or adjust the configuration based on your needs.
Create a symbolic link to your configuration file at site-enabled
:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
This action enables your server block. Test the new configuration to detect possible errors:
sudo nginx -t
If there are no errors, restart Nginx to apply the changes:
sudo systemctl restart nginx
You can test the setup by creating a basic static HTML file. Create a new directory that will act as your web root:
sudo mkdir -p /var/www/example.com/html
Set the appropriate permissions:
sudo chown -R $USER:$USER /var/www/example.com/html
Create a simple HTML file:
nano /var/www/example.com/html/index.html
Add the following content to index.html
:
<!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome to example.com!</h1> </body> </html>
Go to http://your_server_ip
or http://example.com
to view your webpage.
To secure your web server, it is recommended to implement an SSL/TLS certificate for the site. Free SSL certificates can be obtained from Let's Encrypt, a certificate authority that offers free certificates with automation support.
Install the Certbot utility and the Nginx plugin for SSL certificate retrieval and installation:
sudo apt install certbot python3-certbot-nginx
Next, request a certificate using Certbot:
sudo certbot --nginx
Certbot will prompt for necessary information such as your domain name and email. Upon successful completion, Certbot automatically reloads Nginx. Visit https://example.com
to confirm the secure HTTPS connection with the padlock icon.
Certbot sets up a systemd job to automatically handle SSL certificate renewals. You can also perform a manual renewal check:
sudo certbot renew --dry-run
Check the upgrade and make sure it is successful without any errors.
Once Nginx is configured, you can now host and serve various websites. Remember to monitor server performance, manage logs, and keep Nginx and server packages up-to-date to maintain security and efficiency. The Nginx web server is a powerful tool suitable for many web tasks and architecture needs.
Setting up a web server with Nginx on Debian involves defining configuration, adding server blocks, and ensuring security with SSL. Understanding the basics of Nginx installation, configuration, and management lays the groundwork for more complex applications and service deployments. You should now have a running web server ready to serve pages or applications with high efficiency.
If you find anything wrong with the article content, you can