如何在Ubuntu 18.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 discuss how to install Nginx on your Ubuntu 18.04 server.

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

先决条件 (Prerequisites)

Before you begin this guide, you should have a regular, non-root user with sudo privileges configured on your server. You can learn how to configure a regular user account by following our initial server setup guide for Ubuntu 18.04.

在开始本指南之前,您应该在服务器上配置具有sudo特权的常规非root用户。 您可以按照我们针对Ubuntu 18.04的初始服务器设置指南,了解如何配置常规用户帐户。

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 Ubuntu’s default repositories, it is possible to install it from these repositories using the apt packaging system.

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

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 most recent package listings. Afterwards, we can install nginx:

由于这是我们在此会话中与apt打包系统的第一次交互,因此我们将更新本地包索引,以便我们可以访问最新的包清单。 之后,我们可以安装nginx

  • sudo apt update

    sudo apt更新
  • sudo apt install nginx

    sudo apt安装nginx

After accepting the procedure, apt will install Nginx and any required dependencies to your server.

接受该过程后, 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 registers itself as a service with ufw upon installation, making it straightforward to allow Nginx access.

在测试Nginx之前,需要调整防火墙软件以允许访问该服务。 Nginx在安装时将自己注册为ufw服务,这使得直接允许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 OpenSSH

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 on port 80.

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

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: 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)

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

在安装过程结束时,Ubuntu 18.04将启动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 Fri 2018-04-20 16:08:19 UTC; 3 days ago Docs: man:nginx(8) Main PID: 2369 (nginx) Tasks: 2 (limit: 1153) CGroup: /system.slice/nginx.service ├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─2380 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, you can get it a few different ways.

您可以通过导航到服务器的IP地址来访问默认的Nginx登录页面,以确认软件是否正常运行。 如果您不知道服务器的IP地址,则可以通过几种不同的方式获取它。

Try typing this at your server’s command prompt:

尝试在服务器的命令提示符下键入以下内容:

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

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

An alternative is typing this, which should give you your public IP address as seen from another location on the internet:

另一种方法是键入此内容,它应该为您提供从Internet上另一个位置看到的公共IP地址:

  • curl -4 icanhazip.com

    卷曲-4 icanhazip.com

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

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 example.com, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, see our Introduction to DigitalOcean DNS.

使用Nginx Web服务器时, 服务器块 (类似于Apache中的虚拟主机)可用于封装配置详细信息,并在一台服务器中托管多个域。 我们将建立一个名为example.com的域,但是您应该用自己的域名替换它 。 要了解有关使用DigitalOcean设置域名的更多信息,请参阅我们的DigitalOcean DNS简介 。

Nginx on Ubuntu 18.04 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 unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for our example.com site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Ubuntu 18.04上的Nginx默认情况下启用了一个服务器块,该服务器块被配置为提供/var/www/html目录中的文档。 尽管这对于单个站点非常有效,但是如果您托管多个站点,则可能变得笨拙。 与其修改/var/www/html ,不如在example.com站点的/var/www创建目录结构,如果客户请求未得到响应,则将/var/www/html保留为默认目录匹配其他任何网站。

Create the directory for example.com as follows, using the -p flag to create any necessary parent directories:

使用-p标志创建所有必需的父目录,如下所示为example.com创建目录:

  • sudo mkdir -p /var/www/example.com/html

    须藤mkdir -p / var / www / example.com / html

Next, assign ownership of the directory with the $USER environment variable:

接下来,使用$USER环境变量分配目录的所有权:

  • sudo chown -R $USER:$USER /var/www/example.com/html

    须藤chown -R $ USER:$ USER / var / www / example.com / 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/example.com

    须藤chmod -R 755 / var / www / example.com

Next, create a sample index.html page using nano or your favorite editor:

接下来,使用nano或您喜欢的编辑器创建一个示例index.html页面:

  • nano /var/www/example.com/html/index.html

    纳米/ var / www / example.com /html/index.html

Inside, add the following sample HTML:

在其中,添加以下示例HTML:

/var/www/example.com/html/index.html
/var/www/example.com/html/index.html

    
        Welcome to Example.com!
    
    
        

Success! The example.com server block is working!

Save and close the file when you are finished.

完成后保存并关闭文件。

In order for Nginx to serve this content, it’s necessary to create a server block with the correct directives. Instead of modifying the default configuration file directly, let’s make a new one at /etc/nginx/sites-available/example.com:

为了使Nginx可以提供此内容,必须使用正确的指令创建一个服务器块。 与其直接修改默认配置文件, /etc/nginx/sites-available/ example.com/etc/nginx/sites-available/ example.com创建一个新文件:

  • sudo nano /etc/nginx/sites-available/example.com

    须藤纳米/ etc / nginx / sites-available / example.com

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/example.com
/etc/nginx/sites-available/example.com
server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        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 the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:

接下来,让我们通过创建从文件到sites-enabled目录的链接来sites-enabled文件,该目录在启动过程中从Nginx读取:

  • sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

    sudo ln -s / etc / nginx / sites-available / example.com / 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):

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

  • example.com: Will respond to requests for example.com and www.example.com.

    example.com :将响应对example.comwww.example.com请求。

  • 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文件中的单个值。 打开文件:

  • 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, 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://example.com, where you should see something like this:

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

第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 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 18.04.

如果您想构建更完整的应用程序堆栈,请查看有关如何在Ubuntu 18.04上配置LEMP堆栈的本文。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-18-04

你可能感兴趣的:(python,nginx,linux,java,docker)