如何在Ubuntu20.04上安装Nginx[快速⼊门]
介绍 (Introduction)
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 .
在本指南中,我们将说明如何在Ubuntu 20.04服务器上安装Nginx。 有关本教程的更多详细版本,请参阅 。
步骤1 –安装Nginx (Step 1 – Installing 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:
更新您的本地软件包索引:
sudo apt update
sudo apt更新
Install Nginx:
安装Nginx:
sudo apt install nginx
sudo apt安装nginx
步骤2 –调整防⽕墙 (Step 2 – Adjusting the Firewall)
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:
sudo ufw app list
sudo 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上的流量:
sudo ufw allow 'Nginx HTTP'
sudo ufw允许'Nginx HTTP'
Verify the change:
验证更改:
sudo ufw status
sudo ufw状态
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
步骤3 –检查您的Web服务器 (Step 3 – Checking your Web Server)
Check with the systemd init system to make sure the service is running by typing:
通过键⼊以下内容,与systemd初始化系统⼀起检查以确保服务正在运⾏:
systemctl status nginx
systemctl状态nginx
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地址确认软件是否正常运⾏:
your_server_ip
You should receive the default Nginx landing page:
您应该收到默认的Nginx登陆页⾯:
步骤4 –设置服务器块(推荐) (Step 4 – Setting Up Server Blocks (Recommended))
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 .
使⽤Nginx Web服务器时, 服务器块 (类似于Apache中的虚拟主机)可⽤于封装配置详细信息,并在⼀台服务器中托管多个域。 我们将建⽴⼀个名为your_domain的域,但是您应该⽤⾃⼰的域名替换它 。 要了解有关使⽤DigitalOcean设置域名的更多信息,请参阅我们的 。
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:
/var/www/your_domain/html/index.html
/var/www/your_domain/html/index.html
<html>
<head>
<title>Welcome to your_domain!</title>
</head>
<body>
<h1>Success! The your_domain server block is working!</h1>
</body>
</html>
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:
粘贴在以下配置块中,为我们的新⽬录和域名更新:
/
etc/nginx/sites-available/your_domain
/ etc / nginx / sites-available / your_domain
server {
listen 80;
listen [::]:80;
root /var/www/your_domain/html;
index index.html index.inx-debian.html;
server_name your_ur_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 ur_domain.
your_domain :将响应对your_domain和ur_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/f file. Open the file:
为了避免添加其他服务器名称可能引起的哈希存储桶内存问题,有必要调整/etc/f⽂件中的单个值。 打开⽂件:
sudo nano /etc/f
须藤nano /etc/f
Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line:
到server_names_hash_bucket_size指令并删除#符号以取消注释该⾏:
/etc/f
/
etc/f
...
http {
...
server_names_hash_bucket_size 64;
...
}
...
Test for syntax errors:
测试语法错误:
sudo nginx -t
须藤Nginx -t
Restart Nginx to enable your changes:
重新启动Nginx以启⽤您的更改:
sudo systemctl restart nginx
sudo systemctl重启nginx
结论 (Conclusion)
ubuntu安装教程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 .
如果您想构建更完整的应⽤程序堆栈,请查看有关本⽂。
发布评论