2 minutes reading time (478 words)

Install Nginx Web Server on Debian 11

How to install Nginx Web Server on Debian 11. Install Nginx and configure Uncomplicated Firewall (ufw). Create a simple test website and basic Nginx http server block.

Install Nginx Web Server

# update packages
apt update && apt upgrade

# install nginx
apt install nginx

# check nginx status
systemctl status nginx 
# check nginx version
nginx -v 

Configure Uncomplicated Firewall (ufw) for Nginx

# add ufw firewall rules to allow http and https
ufw allow 80
ufw allow 443

# check ufw status
ufw status 

Reloading and restarting Nginx

#restart nginx
service nginx restart

#stop nginx
service nginx stop

#start nginx
service nginx start

#reload nginx
service nginx reload 

Whats the difference between restarting and reloading Nginx? 

When you use the reload command, Nginx will first check if its config files are valid, and if there are any errors, Nginx will stop loading the new config and continue to run using the old configuration.

Checking Nginx Configuration

# check nginx config files
nginx -t 

Nginx user on Debian

The Nginx web server runs as user www-data on Debian

Nginx configuration files

/etc/nginx/nginx.conf
The default config file that Nginx reads when the web server starts

sites-available and sites-enabled
/etc/nginx/sites-available
/etc/nginx/sites-enabled

sites-available contains all the website config files, sites-enabled contains live websites that are running on the web server

Web sites are enabled by creating a symbolic link to the config file in /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/

Create simple website for testing 

Create a simple website with an HTML page for testing

Create website folders
mkdir /var/www/yourdomain.com/html -p

Change owner and group to nginx user
chown -Rf www-data:www-data /var/www/yourdomain.com

Change folder permissions
chmod -Rf 750 /var/www/yourdomain.com

Create a test index.html page for the new site
nano /var/www/yourdomain.com/html/index.html 
<html>
    <head>
        <title>Welcome to Your Domain </title>
    </head>
    <body>
        <h1>Nginx web server has been setup for yourdomain.com </h1>
<p>This is a test page.</p>
    </body>
</html> 

Create basic Nginx http server block

nano /etc/nginx/sites-available/yourdomain.com.conf

server {
    listen 80;
    root /var/www/yourdomain.com/html;
    index index.html;
    server_name yourdomain.com;
} 

Link the web site config file to sites-enabled
sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled

Test the nginx config
nginx -t


Reload nginx
service nginx reload

Test the new site by changing your local hosts file or adding an internal DNS entry for the website.

Example: Windows hosts file

C:\Windows\System32\drivers\etc\hosts

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Saturday, 23 September 2023
You can help support this website by buying me a coffee!
Buy Me A Coffee