博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
debian安装nginx_如何在Debian 10上安装Nginx
阅读量:2509 次
发布时间:2019-05-11

本文共 15260 字,大约阅读时间需要 50 分钟。

debian安装nginx

介绍 (Introduction)

is one of the most popular web servers in the world and 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.

是世界上最受欢迎的Web服务器之一,负责托管Internet上一些最大和流量最高的站点。 在大多数情况下,它比Apache更对资源友好,并且可以用作Web服务器或反向代理。

In this guide, we’ll discuss how to install Nginx on your Debian 10 server.

在本指南中,我们将讨论如何在Debian 10服务器上安装Nginx。

先决条件 (Prerequisites)

Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server and an active firewall. You can learn how to set these up by following our .

在开始本指南之前,您应该已经在服务器和活动防火墙上配置了具有sudo特权的常规非root用户。 您可以通过遵循我们来学习如何进行设置。

When you have an account available, log in as your non-root user to begin.

如果您有可用的帐户,请以非root用户身份登录以开始。

步骤1 –安装Nginx (Step 1 – Installing Nginx)

Because Nginx is available in Debian’s default repositories, it is possible to install it from these repositories using the apt packaging system.

由于Nginx在Debian的默认存储库中可用,因此可以使用apt打包系统从这些存储库中安装Nginx。

Since this is our first interaction with the apt packaging system in this session, let’s first update our local package index so that we have access to the most recent package listings:

由于这是我们在本次会议中与apt打包系统的第一次交互,因此我们首先更新我们的本地包索引,以便可以访问最新的包清单:

  • sudo apt update

    sudo apt更新

We can now install nginx:

现在我们可以安装nginx

  • sudo apt install nginx

    sudo apt安装nginx

When prompted to confirm the installation, hit Enter to proceed. After that, apt will install Nginx and any required dependencies to your server.

当提示您确认安装时,请Enter继续。 之后, apt将为您的服务器安装Nginx和所有必需的依赖项。

步骤2 –调整防火墙 (Step 2 – Adjusting the Firewall)

Before testing Nginx, the firewall software needs to be adjusted to allow access to the service.

在测试Nginx之前,需要调整防火墙软件以允许访问该服务。

List the application configurations that ufw knows how to work with by typing:

键入以下内容,列出ufw知道如何使用的应用程序配置:

  • sudo ufw app list

    sudo ufw应用程序列表

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提供了三个配置文件:

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)

    Nginx Full :此配置文件同时打开端口80 (正常,未加密的网络流量)和端口443 (TLS / SSL加密的流量)

  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)

    Nginx HTTP :此配置文件仅打开端口80 (正常,未加密的网络流量)

  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

    Nginx HTTPS :此配置文件仅打开端口443 (TLS / SSL加密流量)

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.

建议您启用限制性最强的配置文件,该配置文件仍将允许您配置的流量。 由于在本指南中我们尚未为服务器配置SSL,因此我们只需要允许端口80上的HTTP通信。

You can enable this by typing:

您可以通过键入以下内容启用此功能:

  • sudo ufw allow 'Nginx HTTP'

    sudo ufw允许'Nginx HTTP'

You can verify the change by typing:

您可以通过键入以下内容来验证更改:

  • sudo ufw status

    sudo ufw状态

You should see HTTP traffic allowed in the displayed output:

您应该在显示的输出中看到允许的HTTP流量:

Output   
Status: activeTo 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)

At the end of the installation process, Debian 10 starts Nginx. The web server should already be up and running.

在安装过程结束时,Debian 10将启动Nginx。 Web服务器应该已经启动并正在运行。

We can check with the systemd init system to make sure the service is running by typing:

我们可以通过输入以下内容来检查systemd init系统,以确保服务正在运行:

  • 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: enabled) Active: active (running) since Wed 2019-07-03 12:52:54 UTC; 4min 23s ago Docs: man:nginx(8) Main PID: 3942 (nginx) Tasks: 3 (limit: 4719) Memory: 6.1M CGroup: /system.slice/nginx.service ├─3942 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─3943 nginx: worker process └─3944 nginx: worker process

As you can see above, the service appears to have started successfully. However, the best way to test this is to actually request a page from Nginx.

从上面可以看到,该服务似乎已成功启动。 但是,测试此问题的最佳方法是实际从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地址来访问默认的Nginx登录页面,以确认软件是否正常运行。 如果您不知道服务器的IP地址,请尝试在服务器的命令提示符下键入以下内容:

  • ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

    ip addr显示eth0 | grep inet | awk'{print $ 2; }'| sed's /\/.*$//'

You will get back a few lines. You can try each in your web browser to see if they work.

您将返回几行。 您可以尝试在网络浏览器中查看它们是否正常工作。

When you have your server’s IP address, enter it into your browser’s address bar:

拥有服务器的IP地址后,将其输入到浏览器的地址栏中:

http://your_server_ip

You should see the default Nginx landing page:

您应该看到默认的Nginx登陆页面:

This page is included with Nginx to show you that the server is running correctly.

Nginx包含此页面,以向您显示服务器正在正常运行。

步骤4 –管理Nginx流程 (Step 4 – Managing the Nginx Process)

Now that you have your web server up and running, let’s review some basic management commands.

现在您已启动并运行了Web服务器,让我们回顾一些基本的管理命令。

To stop your web server, type:

要停止您的Web服务器,请输入:

  • sudo systemctl stop nginx

    sudo systemctl停止nginx

To start the web server when it is stopped, type:

要在停止时启动Web服务器,请键入:

  • sudo systemctl start nginx

    sudo systemctl启动nginx

To stop and then start the service again, type:

要停止然后再次启动该服务,请键入:

  • sudo systemctl restart nginx

    sudo systemctl重启nginx

If you are simply making configuration changes, Nginx can often reload without dropping connections. To do this, type:

如果您只是在更改配置,Nginx通常可以在不断开连接的情况下重新加载。 为此,请键入:

  • sudo systemctl reload nginx

    须藤systemctl重新加载nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:

默认情况下,Nginx配置为在服务器启动时自动启动。 如果这不是您想要的,则可以通过键入以下内容来禁用此行为:

  • sudo systemctl disable nginx

    sudo systemctl禁用Nginx

To re-enable the service to start up at boot, you can type:

要重新启用该服务以在引导时启动,可以键入:

  • sudo systemctl enable nginx

    sudo systemctl启用nginx

步骤5 –设置服务器块 (Step 5 – Setting Up Server Blocks)

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 on a single server. We will set up a domain called your_domain. To learn more about setting up a domain name with DigitalOcean, see our .

使用Nginx Web服务器时, 服务器块 (类似于Apache中的虚拟主机)可用于封装配置详细信息,并在一台服务器上托管多个域。 我们将建立一个名为your_domain的域。 要了解有关使用DigitalOcean设置域名的更多信息,请参阅 。

Nginx on Debian 10 has one server block enabled by default that is configured to serve documents out of a directory at /var/www/html. While this works well for a single site, it can become unmanageable if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for the your_domain website, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

默认情况下,Debian 10上的Nginx启用了一个服务器块,该服务器块被配置为从/var/www/html目录中提供文档。 尽管这对于单个站点非常有效,但是如果您托管多个站点,则可能变得难以管理。 而不是修改的/var/www/html ,让我们创建中的目录结构/var/www您的网域网站,留下/var/www/html作为如果客户端的请求不符合任何要服务的默认目录的地方其他网站。

Create the directory for your_domain as follows, 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

Next, assign ownership of the directory with the $USER environment variable, which should reference your current system user:

接下来,使用$USER环境变量分配目录的所有权,该变量应引用您当前的系统用户:

  • sudo chown -R $USER:$USER /var/www/your_domain/html

    须藤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:

如果您尚未修改umask值,则Web根目录的权限应该正确,但是可以通过键入以下umask来确保:

  • sudo chmod -R 755 /var/www/your_domain

    须藤chmod -R 755 / var / www / your_domain

Next, 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
            Welcome to your_domain                

Success! Your Nginx server is successfully configured for your_domain.

This is a sample page.

Save and close the file when you are finished.

完成后保存并关闭文件。

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:

为了使Nginx提供此内容,我们需要创建一个服务器块,并使用指向我们自定义Web根的正确指令。 与其直接修改默认配置文件, /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, which is similar to the default, but 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.htm index.nginx-debian.html;        server_name your_domain www.your_domain;        location / {                try_files $uri $uri/ =404;        }}

Notice that we’ve updated the root configuration to our new directory, and the server_name to our domain name.

请注意,我们已将root配置更新到我们的新目录,并将server_name到我们的域名。

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:

接下来,我们通过在sites-enabled目录中创建指向我们的自定义配置文件的符号链接来启用此服务器块,Nginx在启动过程中从中读取:

  • 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 (you can read more about how Nginx processes these directives ):

现在已启用并配置了两个服务器块,以基于它们的listenserver_name指令响应请求(您可以阅读有关Nginx如何处理这些指令的更多信息):

  • your_domain: Will respond to requests for your_domain and www.your_domain.

    your_domain :将响应对your_domainwww. your_domain请求www. 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 to your configuration, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file:

为避免在配置中添加其他服务器名称而可能引起的哈希存储区内存问题,有必要调整/etc/nginx/nginx.conf文件中的单个值。 打开文件:

  • sudo nano /etc/nginx/nginx.conf

    须藤nano /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指令并删除#符号以取消注释该行:

/etc/nginx/nginx.conf
/etc/nginx/nginx.conf
...http {    ...    server_names_hash_bucket_size 64;    ...}...

Save and close the file when you are finished.

完成后保存并关闭文件。

Next, test to make sure that there are no syntax errors in any of your Nginx files:

接下来,测试以确保您的任何Nginx文件中都没有语法错误:

  • sudo nginx -t

    须藤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 oknginx: configuration file /etc/nginx/nginx.conf test is successful

Once your configuration test passes, restart Nginx to enable your changes:

一旦配置测试通过,请重新启动Nginx以启用您的更改:

  • sudo systemctl restart nginx

    sudo systemctl重启nginx

Nginx should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this:

Nginx现在应该为您的域名服务。 您可以通过导航到http:// your_domain ,您应该会在其中看到以下内容:

第6步–熟悉重要的Nginx文件和目录 (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.

既然您知道如何管理Nginx服务本身,那么您应该花几分钟时间来熟悉一些重要的目录和文件。

内容 (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.

    /var/www/html :实际的Web内容(默认情况下仅包含您之前看到的默认Nginx页面)从/var/www/html目录中提供。 这可以通过更改Nginx配置文件来更改。

服务器配置 (Server Configuration)

  • /etc/nginx: The Nginx configuration directory. All of the Nginx configuration files reside here.

    /etc/nginx :Nginx配置目录。 所有Nginx配置文件都位于此处。

  • /etc/nginx/nginx.conf: The main Nginx configuration file. This can be modified to make changes to the Nginx global configuration.

    /etc/nginx/nginx.conf :主要的Nginx配置文件。 可以对其进行修改以更改Nginx全局配置。

  • /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 the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory.

    /etc/nginx/sites-available/ :可以存储每个站点服务器块的目录。 Nginx不会使用在此目录中找到的配置文件,除非它们链接到sites-enabled目录。 通常,所有服务器块配置都在此目录中完成,然后通过链接到另一个目录来启用。

  • /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 the sites-available directory.

    /etc/nginx/sites-enabled/ :存储已启用的每个站点服务器块的目录。 通常,通过链接到可在sites-available目录中找到的配置文件来创建这些文件。

  • /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.

    /etc/nginx/snippets :此目录包含一些配置片段,这些片段可以包含在Nginx配置中的其他位置。 潜在的可重复配置段是重构为代码段的良好候选者。

服务器日志 (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/access.log :除非配置Nginx否则,对Web服务器的每个请求都记录在此日志文件中。

  • /var/log/nginx/error.log: Any Nginx errors will be recorded in this log.

    /var/log/nginx/error.log :任何Nginx错误都将记录在此日志中。

结论 (Conclusion)

Now that you have your web server installed, you have many options for the type of content you can serve and the technologies you can use to create a richer experience for your users.

现在,您已经安装了Web服务器,对于可以提供的内容类型以及可以用来为用户创造更丰富的体验的技术,您有很多选择。

翻译自:

debian安装nginx

转载地址:http://zvhgb.baihongyu.com/

你可能感兴趣的文章
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
人工智能暑期课程实践项目——智能家居控制(一)
查看>>
前端数据可视化插件(二)图谱
查看>>
kafka web端管理工具 kafka-manager【转发】
查看>>
获取控制台窗口句柄GetConsoleWindow
查看>>
Linux下Qt+CUDA调试并运行
查看>>
51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)
查看>>
OKMX6Q在ltib生成的rootfs基础上制作带QT库的根文件系统
查看>>
zabbix
查看>>
多线程基础
查看>>
完美解决 error C2220: warning treated as error - no ‘object’ file generated
查看>>
使用SQL*PLUS,构建完美excel或html输出
查看>>
SQL Server数据库笔记
查看>>