How to Install Nginx on Ubuntu 20.04?
Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.
In this article, we will discuss how to install Nginx on Ubuntu 20.04 system, adjust the firewall, manage Nginx processes, and set up server blocks to host multiple domains from a single server.
Before you begin this tutorial, you should have a regular non-root user with sudo privileges configured on your system.
Before completing the last step of this article, you can also choose to register a domain name.
When you have an account available, begin by logging in as a non-root user.
Install Nginx
Since Nginx is available in Ubuntu's default repositories, it can be installed from these repositories using the apt packaging system.
Since this is our first interaction with the apt packaging system in this session, we will update our local package index so that we have access to the latest package lists. Afterwards, we can install nginx:
$ sudo apt update
$ sudo apt install nginx
Once you accept the process, apt will install Nginx and any required dependencies into your operating system.
Adjust the firewall
Before testing Nginx, you need to adjust your firewall software to allow access to the service. Nginx registers itself as a ufw service when it is installed, so you can directly allow Nginx access.
List the application configurations that ufw knows how to use by typing:
$ sudo ufw app list
We should see a list of our application configuration files:
Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
As the output shows, Nginx has three available configuration files:
- Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (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 to enable the most restrictive profile which will still allow the traffic we configured. For now, we only need to allow traffic on port 80.
This feature can be enabled with the following command:
$ sudo ufw allow 'Nginx HTTP'
You can verify the changes by running the following command:
$ sudo ufw status
The output will show that HTTP traffic is allowed:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Check the Web Server
At the end of the installation process, Ubuntu 20.04 starts Nginx. The web server should already be up and running.
We can check to make sure the service is running via the systemd init system by typing:
$ systemctl status nginx
The output is as follows
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-04-20 16:08:19 UTC; 3 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
The service has started successfully. However, the best way to test it is to actually request a page from Nginx.
We can confirm that the software is running properly by visiting the default Nginx login page by navigating to the server's IP address. If we don't know our server's IP address, we can find it using the icanhazip.com tool, which will give us the public IP address we received from another location on the internet:
$ curl -4 icanhazip.com
Once you have the server's IP address, enter it into your browser's address bar:
http://你的服务器ip
We should see the default Nginx page:

If you see this page, the server is running correctly.
Managing Nginx Processes
Now that we have our web server up and running, let's look at some basic management commands.
To stop the web server, use the following command:
$ sudo systemctl stop nginx
Start a stopped web service
$ sudo systemctl start nginx
Restart the service using the following command:
$ sudo systemctl restart nginx
If you are just making configuration changes, Nginx can usually reload without dropping connections.
$ sudo systemctl reload nginx
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, we can disable this behavior by typing:
$ sudo systemctl disable nginx
To re-enable the service so that it starts automatically at boot, use the following command:
$ sudo systemctl enable nginx
Now that we have learned the basic management commands, we should be ready to configure our site to resolve multiple domain names.
Setting up a server block (recommended)
When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and resolve multiple domain names from a single server. We will set up a domain called your_domain, but you should replace it with your own domain name.
Nginx on Ubuntu 20.04 comes with one server block enabled by default, with the directory /var/www/html configured. While this works fine for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, we will simply create a directory for our your_domain site within the /var/www directory, leaving /var/www/html as the default directory that client requests go to if they do not match any other site.
Create a 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, $USER
assign ownership of the directory using the PATH environment variable:
$ sudo chown -R $USER:$USER /var/www/your_domain/html
If you have not modified your umask value, the permissions on your web root should be correct, which sets the default file permissions. To ensure your permissions are correct and allow the owner to read, write, and execute files, while only granting read and execute permissions to the group and others, enter the following command:
$ sudo chmod -R 755 /var/www/your_domain
Next, create a sample page index.html using nano or your favorite editor:
$ nano /var/www/your_domain/html/index.html
Add the following content
index.html
<html>
<head>
<title>Welcome to jiyik.com!</title>
</head>
<body>
<h1>Success! The jiyik.com server block is working!</h1>
</body>
</html>
Save and close the file by pressing Ctrl+X to exit, then when prompted to save, enter Y
and press Enter.
In order for Nginx to serve this content, it is necessary to create a server block with the correct directives. Rather than modifying the default configuration file directly, create a new one in /etc/nginx/sites-available/your_domain:
$ sudo nano /etc/nginx/sites-available/your_domain
Add the following content
/etc/nginx/sites-available/your_domain
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; } }
Notice that we have updated the root configuration to our new directory and the server_name to our domain name.
Next, let’s enable the file by creating a link to the sites-enabled directory, which Nginx reads during startup:
$ sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
注意
: Nginx uses a common practice called symbolic links, or symlinks, to keep track of which server blocks are enabled. Creating a symbolic link is like creating a shortcut on disk so that you can latersites-enabled
delete the shortcut from the directory in while keeping the server block insites-available
if you ever want to enable it.
Now the two server blocks are enabled and configured to respond to requests according to their listen and server_name directives.
- your_domain:将响应对 your_domain 和 www.your_domain 的请求。
- default:将响应端口 80 上与其他两个块不匹配的任何请求。
为了避免由于添加额外的服务器名称而可能出现的哈希桶内存问题,有必要调整 /etc/nginx/nginx.conf 文件中的单个值。 打开文件:
$ sudo nano /etc/nginx/nginx.conf
找到 server_names_hash_bucket_size
指令并删除 # 符号来取消注释该行。 如果使用的是 nano,可以通过按 CTRL 和 w 快速搜索文件中的单词。
注意
:注释掉代码行——通常是将 # 放在一行的开头——是另一种禁用它们而无需实际删除它们的方法。 许多配置文件附带多个注释掉的选项,以便可以通过在活动代码和文档之间切换来启用或禁用它们。
/etc/nginx/nginx.conf
... http { ... server_names_hash_bucket_size 64; ... } ...
完成后保存并关闭文件。
接下来,进行测试以确保我们的 Nginx 文件中都没有语法错误:
$ sudo nginx -t
如果没有任何问题,重新启动 Nginx 使我们的更改生效:
$ sudo systemctl restart nginx
Nginx 现在应该可以为我们的域名提供服务了。 可以在导航栏中输入 http://your_domain 进行测试,应该在其中看到如下内容:
熟悉重要的 Nginx 文件和目录
现在已经知道如何管理 Nginx 服务,下面应该花几分钟时间熟悉一些重要的目录和文件。
内容
- /var/www/html:实际的 Web 内容,默认情况下仅包含之前看到的默认 Nginx 页面,是从/var/www/html目录中提供的。可以通过更改 Nginx 配置文件来进行更改。
服务器配置
- /etc/nginx :Nginx 配置目录。 所有 Nginx 配置文件都在这里面。
- /etc/nginx/nginx.conf :Nginx 的主配置文件。 可以修改它来更改 Nginx 全局配置。
- /etc/nginx/sites-available/ :可以存储每个站点服务器块的目录。 Nginx 不会使用在此目录中找到的配置文件,除非它们链接到启用了站点的目录。 通常,所有服务器块配置都在此目录中完成,然后通过链接到另一个目录启用。
- /etc/nginx/sites-enabled/ :存储启用的每个站点服务器块的目录。 通常,这些是通过链接到在 sites-available 目录中找到的配置文件来创建的。
- /etc/nginx/snippets :该目录包含那些包含在 Nginx 配置中的其他地方的配置片段。
服务器日志
- /var/log/nginx/access.log :对 Web 服务器的每个请求都记录在此日志文件中,除非在 Nginx 中进行配置禁修改为其他文件或者不记录访问日志。
- /var/log/nginx/error.log :任何 Nginx 错误都会记录在这个日志中。
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to use Let's Encrypt with Nginx to configure https in Ubuntu 20.04
Publish Date:2025/04/07 Views:123 Category:OPERATING SYSTEM
-
Let's Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, enabling encrypted HTTPS on your web server. It simplifies the process by providing a software client, Certbot, which a
Install WordPress with Nginx on Ubuntu 18.04
Publish Date:2025/04/07 Views:86 Category:OPERATING SYSTEM
-
WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
Publish Date:2025/03/26 Views:131 Category:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
Nginx load balancing settings
Publish Date:2025/03/18 Views:198 Category:NETWORK
-
At this stage, load balancing is a widely used technology. Nginx, as a load balancing server for http, is being used more and more widely. There are three ways to set up Nginx load balancing: Round-robin - This method distributes access req
Nginx load balancing health_check analysis
Publish Date:2025/03/18 Views:56 Category:NETWORK
-
In Nginx load balancing, it is difficult to guarantee that every application server can run normally all the time. However, we can set Nginx to detect these application servers and detect which of them are inaccessible. There are two ways t
HTTP2 Tutorial - How to Configure HTTP2 with Nginx
Publish Date:2025/03/17 Views:195 Category:NETWORK
-
HTTP2 was officially released in 2015. If your website is still using HTTP/1.1, you may be out of date. Don't worry, here we will see how to use Nginx to upgrade your website to HTTP2. Install Nginx I feel that this column is redundant. Sin
Deep understanding of Nginx's server block selection algorithm
Publish Date:2025/03/17 Views:98 Category:NETWORK
-
Nginx is one of the most popular web servers in the world. It can successfully handle high loads with many concurrent client connections and can be used as a web server, mail server, or reverse proxy server. In this article, we will discuss
In-depth understanding of Nginx Location block matching algorithm
Publish Date:2025/03/17 Views:65 Category:NETWORK
-
Similar to the process that Nginx uses to select the Server block that will handle a request , Nginx also has an established algorithm to decide which Location block within a Server block to use to handle a request. location block syntax