Step 1 – Installing Nginx
sudo apt update sudo apt install nginx
Step 2 – Adjusting the Firewall
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service.
List the application configurations that ufw
knows how to work with by typing:
sudo ufw app list
You should get a listing of the application profiles:
Output Available applications: ... Nginx Full Nginx HTTP Nginx HTTPS ...
As you can see, there are three profiles available for Nginx:
- Nginx Full: This profile opens both port
80
(normal, unencrypted web traffic) and port443
(TLS/SSL encrypted traffic) - Nginx HTTP: This profile opens only port
80
(normal, unencrypted web traffic) - Nginx HTTPS: This profile opens only port
443
(TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since we haven’t configured SSL for our server yet in this guide, we will only need to allow traffic for HTTP on port 80
.
You can enable this by typing:
sudo ufw allow 'Nginx HTTP'
You can verify the change by typing:
sudo ufw status
You should see HTTP traffic allowed in the displayed output:
Output Status: active To Action From -- ------ ---- OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)
Step 3 – Checking your Web Server
systemctl status nginx
You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, try typing this at your server’s command prompt:
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'
Step 4 – Managing the Nginx Process
sudo systemctl stop nginx sudo systemctl start nginx sudo systemctl restart nginx sudo systemctl reload nginx sudo systemctl disable nginx sudo systemctl enable nginx
Step 5 – Setting Up Server Blocks
Create the directory for your_domain as follows, using the -p
flag to create any necessary parent directories:
sudo mkdir -p /var/www/your_domain/html
Next, assign ownership of the directory with the $USER
environment variable, which should reference your current system user:
sudo chown -R $USER:$USER /var/www/your_domain/html
The permissions of your web root should be correct if you haven’t modified your umask
value, but you can make sure by typing:
sudo chmod -R 755 /var/www/your_domain
Next, create a sample index.html
page using nano
or your favorite editor:
nano /var/www/your_domain/html/index.html
In order for Nginx to serve this content, we need to create a server block with the correct directives that point to our custom web root. Instead of modifying the default configuration file directly, let’s make a new one at /etc/nginx/sites-available/your_domain
:
sudo vim /etc/nginx/sites-available/your_domain
Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:
server { listen 80; listen [::]:80; root /var/www/your_domain/html; index index.html index.htm index.nginx-debian.html; server_name your_domain www.your_domain; location / { try_files $uri $uri/ =404; } }
Next, let’s enable this server block by creating a symbolic link to our custom configuration file inside the sites-enabled
directory, which Nginx reads from during startup:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Two server blocks are now enabled and configured to respond to requests based on their listen
and server_name
directives (you can read more about how Nginx processes these directives here):
your_domain
: Will respond to requests foryour_domain
andwww.your_domain
.default
: Will respond to any requests on port80
that do not match the other two blocks.
To avoid a possible hash bucket memory problem that can arise from adding additional server names to your configuration, it is necessary to adjust a single value in the /etc/nginx/nginx.conf
file. Open the file:
sudo nano /etc/nginx/nginx.conf
... http { ... server_names_hash_bucket_size 64; ... } ..
Next, test to make sure that there are no syntax errors in any of your Nginx files:
sudo nginx -t
If there aren’t any problems, you will see the following output:
Output nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Once your configuration test passes, restart Nginx to enable your changes:
sudo systemctl restart nginx
Nginx should now be serving your domain name. You can test this by navigating to http://your_domain
Step 6 – Getting Familiar with Important Nginx Files and Directories
Now that you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.
Content
/var/www/html
: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the/var/www/html
directory. This can be changed by altering Nginx configuration files.
Server Configuration
/etc/nginx
: The Nginx configuration directory. All of the Nginx configuration files reside here./etc/nginx/nginx.conf
: The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration./etc/nginx/sites-available/
: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to thesites-enabled
directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory./etc/nginx/sites-enabled/
: The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in thesites-available
directory./etc/nginx/snippets
: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.
Server Logs
/var/log/nginx/access.log
: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise./var/log/nginx/error.log
: Any Nginx errors will be recorded in this log.
来源:
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-debian-10