Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy.
Nginx是世界上最受欢迎的Web服务器之一,负责托管Internet上一些最大和流量最高的站点。 在大多数情况下,它比Apache更对资源友好,并且可以用作Web服务器或反向代理。
In this guide, we’ll explain how to install Nginx on your Ubuntu 20.04 server. For a more detailed version of this tutorial, please refer to How To Install Nginx on Ubuntu 20.04.
在本指南中,我们将说明如何在Ubuntu 20.04服务器上安装Nginx。 有关本教程的更多详细版本,请参阅如何在Ubuntu 20.04上安装Nginx 。
Because Nginx is available in Ubuntu’s default repositories, you can install it using the apt
packaging system.
由于Nginx在Ubuntu的默认存储库中可用,因此您可以使用apt
打包系统进行安装。
Update your local package index:
更新您的本地软件包索引:
Install Nginx:
安装Nginx:
If you followed the prerequisite server setup tutorial, then you have the UFW firewall enabled. Check the available ufw
application profiles with the following command:
如果您遵循了前提条件服务器设置教程,那么您将启用UFW防火墙。 使用以下命令检查可用的ufw
应用程序配置文件:
Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Let’s enable the most restrictive profile that will still allow the traffic you’ve configured, permitting traffic on port 80
:
让我们启用限制性最强的配置文件,该配置文件仍将允许您配置的流量,并允许端口80
上的流量:
Verify the change:
验证更改:
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 with the systemd
init system to make sure the service is running by typing:
通过键入以下内容,与systemd
初始化系统一起检查以确保服务正在运行:
Output
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset:>
Active: active (running) since Mon 2020-05-04 22:45:26 UTC; 1min 17s ago
Docs: man:nginx(8)
Main PID: 13255 (nginx)
Tasks: 2 (limit: 1137)
Memory: 4.6M
CGroup: /system.slice/nginx.service
├─13255 nginx: master process /usr/sbin/nginx -g daemon on; master>
└─13256 nginx: worker process
Access the default Nginx landing page to confirm that the software is running properly through your IP address:
访问默认的Nginx登录页面,以通过您的IP地址确认软件是否正常运行:
http://your_server_ip
You should receive the default Nginx landing page:
您应该收到默认的Nginx登陆页面:
When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, please refer to our Introduction to DigitalOcean DNS.
使用Nginx Web服务器时, 服务器块 (类似于Apache中的虚拟主机)可用于封装配置详细信息,并在一台服务器中托管多个域。 我们将建立一个名为your_domain的域,但是您应该用自己的域名替换它 。 要了解有关使用DigitalOcean设置域名的更多信息,请参阅我们的DigitalOcean DNS简介 。
Create the directory for your_domain
, using the -p
flag to create any necessary parent directories:
使用-p
标志创建your_domain
的目录,以创建任何必要的父目录:
sudo mkdir -p /var/www/your_domain/html
须藤mkdir -p / var / www / your_domain / html
Assign ownership of the directory:
分配目录的所有权:
sudo chown -R $USER:$USER /var/www/your_domain/html
须藤chown -R $ USER:$ USER / var / www / your_domain / html
The permissions of your web roots should be correct if you haven’t modified your umask
value, but you can make sure by typing:
如果您尚未修改umask
值,则您的Web根目录的权限应该正确,但是可以通过键入以下内容来确保:
sudo chmod -R 755 /var/www/your_domain
须藤chmod -R 755 / var / www / your_domain
Create a sample index.html
page using nano
or your favorite editor:
使用nano
或您喜欢的编辑器创建样本index.html
页面:
nano /var/www/your_domain/html/index.html
纳米/ var / www / your_domain /html/index.html
Inside, add the following sample HTML:
在其中,添加以下示例HTML:
Welcome to your_domain!
Success! The your_domain server block is working!
Save and close the file when you are finished.
完成后保存并关闭文件。
Make a new server block at /etc/nginx/sites-available/your_domain
:
在/etc/nginx/sites-available/ your_domain
一个新的服务器块:
sudo nano /etc/nginx/sites-available/your_domain
须藤纳米/ etc / nginx / sites-available / your_domain
Paste in the following configuration block, 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;
}
}
Save and close the file when you are finished.
完成后保存并关闭文件。
Enable the file by creating a link from it to the sites-enabled
directory:
通过创建指向sites-enabled
目录的链接来sites-enabled
文件:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
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:
现在启用了两个服务器块并将其配置为根据其listen
和server_name
指令来响应请求:
your_domain
: Will respond to requests for your_domain
and www.your_domain
.
your_domain
:将响应对your_domain
和www.your_domain
请求。
default
: Will respond to any requests on port 80
that do not match the other two blocks.
default
:将响应端口80
上与其他两个模块不匹配的任何请求。
To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf
file. Open the file:
为了避免添加其他服务器名称可能引起的哈希存储桶内存问题,有必要调整/etc/nginx/nginx.conf
文件中的单个值。 打开文件:
Find the server_names_hash_bucket_size
directive and remove the #
symbol to uncomment the line:
找到server_names_hash_bucket_size
指令并删除#
符号以取消注释该行:
...
http {
...
server_names_hash_bucket_size 64;
...
}
...
Test for syntax errors:
测试语法错误:
Restart Nginx to enable your changes:
重新启动Nginx以启用您的更改:
Nginx should now be serving your domain name. You can test this by navigating to http://your_domain
, where you should receive something like this:
Nginx现在应该为您的域名服务。 您可以通过导航到http:// your_domain
,您应该在其中收到以下内容:
Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.
现在,您已经安装了Web服务器,对于要提供的内容类型以及要用来创建更丰富体验的技术,有了很多选择。
If you’d like to build out a more complete application stack, check out this article on how to configure a LEMP stack on Ubuntu 20.04.
如果您想构建更完整的应用程序堆栈,请查看有关如何在Ubuntu 20.04上配置LEMP堆栈的本文。
翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04-quickstart