centos安装nginx_如何在CentOS 8上安装Nginx

centos安装nginx

介绍 (Introduction)

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and most popular 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 a CentOS 8 server.

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

先决条件 (Prerequisites)

To follow this guide, you’ll need access to a CentOS 8 server as a non-root user with sudo privileges, and an active firewall installed on your server. To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

要遵循本指南,您需要以具有sudo特权的非root用户身份访问CentOS 8服务器,并在服务器上安装了活动防火墙。 要进行设置,您可以遵循我们的《 CentOS 8初始服务器设置指南》

第1步-安装Nginx Web服务器 (Step 1 — Installing the Nginx Web Server)

In order to install Nginx, we’ll use the dnf package manager, which is the new default package manager on CentOS 8.

为了安装Nginx,我们将使用dnf软件包管理器,它是CentOS 8上新的默认软件包管理器。

Install the nginx package with:

使用以下命令安装nginx软件包:

  • sudo dnf install nginx

    须藤dnf安装nginx

When prompted, enter y to confirm that you want to install nginx. After that, dnf will install Nginx and any required dependencies to your server.

出现提示时,输入y确认要安装nginx 。 之后, dnf将为您的服务器安装Nginx和所有必需的依赖项。

After the installation is finished, run the following commands to enable and start the server:

安装完成后,运行以下命令来启用和启动服务器:

  • sudo systemctl enable nginx

    sudo systemctl启用nginx
  • sudo systemctl start nginx

    sudo systemctl启动nginx

This will make Nginx start at system boot.

这将使Nginx在系统启动时启动。

步骤2 —调整防火墙规则 (Step 2 — Adjusting Firewall Rules)

In case you have enabled the firewalld firewall as instructed in our initial server setup guide for CentOS 8, you will need to adjust the firewall settings in order to allow external connections on your Nginx web server, which runs on port 80 by default.

如果您按照CentOS 8初始服务器设置指南中的说明启用了firewalld防火墙,则需要调整防火墙设置,以允许Nginx Web服务器上的外部连接(默认情况下在端口80上运行)。

Run the following command to permanently enable HTTP connections on port 80:

运行以下命令以永久启用端口80上的HTTP连接:

  • sudo firewall-cmd --permanent --add-service=http

    sudo firewall-cmd-永久--add-service = http

To verify that the http firewall service was added correctly, you can run:

要验证是否正确添加了http防火墙服务,可以运行:

  • sudo firewall-cmd --permanent --list-all

    sudo firewall-cmd-永久--list-all

You’ll see output like this:

您将看到如下输出:


   
   
Output
public target: default icmp-block-inversion: no interfaces: sources: services: cockpit dhcpv6-client http ssh ports: protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:

To apply the changes, you’ll need to reload the firewall service:

要应用更改,您需要重新加载防火墙服务:

  • sudo firewall-cmd --reload

    sudo firewall-cmd-重新加载

Now your Nginx server is fully installed and ready to be accessed by external visitors.

现在,您的Nginx服务器已完全安装,并可供外部访问者访​​问。

步骤3 —检查您的Web服务器 (Step 3 — Checking your Web Server)

You can now test if your Nginx web server is up and running by accessing your server’s public IP address or domain name from your web browser.

现在,您可以通过从Web浏览器访问服务器的公共IP地址或域名来测试Nginx Web服务器是否已启动并正在运行。

Note: In case you are using DigitalOcean as your DNS hosting provider, you can check our product docs for detailed instructions on how to set up a new domain name and point it to your server.

注意 :如果您将DigitalOcean用作DNS托管提供商,则可以查看我们的产品文档,以获取有关如何设置新域名并将其指向服务器的详细说明。

If you do not have a domain name pointed at your server and you do not know your server’s public IP address, you can find it by running the following command:

如果没有指向服务器的域名,并且您不知道服务器的公共IP地址,则可以通过运行以下命令来找到它:

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

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

This will print out a few IP addresses. You can try each of them in turn in your web browser.

这将打印出一些IP地址。 您可以依次在网络浏览器中尝试使用它们。

As an alternative, you can check which IP address is accessible, as viewed from other locations on the internet:

作为替代方案,您可以检查从互联网上其他位置查看的哪个IP地址可访问:

  • curl -4 icanhazip.com

    卷曲-4 icanhazip.com

Type the address that you receive in your web browser and it will take you to Nginx’s default landing page:

输入您在网络浏览器中收到的地址,它将带您到Nginx的默认登录页面:

If you see this page, then your web server is now correctly installed.

如果看到此页面,则表明您的Web服务器现在已正确安装。

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

Now that you have your web server up and running, we’ll review how to manage the Nginx service through systemctl.

现在,您的Web服务器已启动并正在运行,我们将回顾如何通过systemctl管理Nginx服务。

Whenever you need to stop your web server, you can use:

每当需要停止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, you can use:

要停止然后再次启动该服务,可以使用:

  • sudo systemctl restart nginx

    sudo systemctl 重启 nginx

Nginx can also reload configuration changes 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 wish, you can disable this behavior by typing:

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

  • sudo systemctl disable nginx

    sudo systemctl 禁用 Nginx

To re-enable the service and make Nginx start at boot again, you can use:

要重新启用该服务并使Nginx重新启动,可以使用:

  • sudo systemctl enable nginx

    sudo systemctl 启用 nginx

第5步–熟悉重要的Nginx文件和目录 (Step 5 – Getting Familiar with Important Nginx Files and Directories)

Now that you know how to manage the Nginx service, you should take a few minutes to familiarize yourself with a few important directories and files.

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

内容 (Content)

  • /usr/share/nginx/html: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /usr/share/nginx/html directory. This can be changed by altering Nginx configuration files.

    /usr/share/nginx/html :实际的Web内容(默认情况下仅由您先前看到的默认Nginx页面组成)从/usr/share/nginx/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/conf.d/: This directory contains server block configuration files, where you can define the websites that are hosted within Nginx. A typical approach is to have each website in a separate file that is named after the website’s domain name, such as your_domain.conf.

    /etc/nginx/conf.d/ :此目录包含服务器块配置文件,您可以在其中定义Nginx中托管的网站。 一种典型的方法是将每个网站放在一个单独的文件中,该文件以该网站的域名(例如your_domain.conf命名。

服务器日志 (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错误都将记录在此日志中。

步骤6 –设置服务器块(可选) (Step 6 – Setting Up Server Blocks (Optional))

In case you’d like to host multiple websites within the same Nginx web server, you’ll need to set up server blocks. Nginx server blocks work in a similar way to Apache virtual hosts, allowing a single server to respond to multiple domain names and serving different content for each of them. On CentOS 8, server blocks are defined in .conf files located at /etc/nginx/conf.d.

如果您想在同一Nginx Web服务器上托管多个网站,则需要设置服务器块 。 Nginx服务器块的工作方式与Apache虚拟主机类似,允许单个服务器响应多个域名并为每个域名提供不同的内容。 在CentOS 8上,服务器块在/etc/nginx/conf.d中的.conf文件中定义。

We will set up a server block for a domain called your_domain. To learn more about setting up a domain name with DigitalOcean, see our introduction to DigitalOcean DNS.

我们将为名为your_domain的域设置服务器块。 要了解有关使用DigitalOcean设置域名的更多信息,请参阅DigitalOcean DNS简介

By default, Nginx on CentOS 8 is configured to serve documents out of a directory at /usr/share/nginx/html. While this works well for a single site, it can become unmanageable if you are hosting multiple sites. Instead of modifying /usr/share/nginx/html, we’ll create a directory structure within /var/www for the your_domain website, leaving /usr/share/nginx/html in place as the default directory to be served if a client request doesn’t match any other sites.

默认情况下,CentOS 8上的Nginx配置为从/usr/share/nginx/html目录下的文档提供服务。 尽管这对于单个站点非常有效,但是如果您托管多个站点,则可能变得难以管理。 我们将在/var/wwwyour_domain网站创建目录结构,而不是修改/usr/share/nginx/html ,如果有客户端,则将/usr/share/nginx/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

Next, we’ll create a sample index.html page to test the server block configuration. The default text editor that comes with CentOS 8 is vi. vi is an extremely powerful text editor, but it can be somewhat obtuse for users who lack experience with it. You might want to install a more user-friendly editor such as nano to facilitate editing configuration files on your CentOS 8 server:

接下来,我们将创建一个示例index.html页面来测试服务器块配置。 CentOS 8随附的默认文本编辑器是vivi是一个功能非常强大的文本编辑器,但对于缺乏使用经验的用户而言,它可能会有些迟钝。 您可能需要安装更加用户友好的编辑器(例如nano以方便在CentOS 8服务器上编辑配置文件:

  • sudo dnf install nano

    须藤dnf安装纳米

Now you can use nano to create the sample index.html file:

现在,您可以使用nano创建示例index.html文件:

  • nano /var/www/your_domain/html/index.html

    纳米/ var / www / your_domain /html/index.html

Inside that file, add the following HTML code:

在该文件内,添加以下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! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>
<p>This is a sample page.</p>
    </body>
</html>

Save and close the file when you are finished. If you used nano, you can do so by pressing CTRL + X, Y, then ENTER.

完成后保存并关闭文件。 如果您使用过nano ,则可以通过按CTRL + XY ,然后按ENTER

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. We’ll create a new server block at /etc/nginx/conf.d/your_domain.conf:

为了使Nginx提供此内容,我们需要创建一个服务器块,并使用指向我们自定义Web根的正确指令。 我们将在/etc/nginx/conf.d/ your_domain.conf创建一个新的服务器块:

  • sudo nano /etc/nginx/conf.d/your_domain.conf

    须藤nano /etc/nginx/conf.d/ your_domain.conf

Paste in the following configuration block:

粘贴以下配置块:

/etc/nginx/conf.d/your_domain.conf
/etc/nginx/conf.d/your_domain.conf
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’re done editing its content.

编辑完文件内容后,保存并关闭文件。

To make sure that there are no syntax errors in any of your Nginx files, run:

为了确保任何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 ok nginx: 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

Before you can test the changes from your browser, you’ll need to update your server’s SELinux security contexts so that Nginx is allowed to serve content from the /var/www/your_domain directory.

在可以通过浏览器测试更改之前,您需要更新服务器的SELinux安全上下文,以便允许Nginx提供/var/www/ your_domain目录中的内容。

The following command will allow your custom document root to be served as HTTP content:

以下命令将您的自定义文档根目录用作HTTP内容:

  • chcon -vR system_u:object_r:httpd_sys_content_t:s0 /var/www/your_domain/

    chcon -vR system_u:object_r:httpd_sys_content_t:s0 / var / www / your_domain /

Now you can test your custom domain setup by navigating to http://your_domain, where you will see something like this:

现在,您可以通过导航到http:// your_domain来测试自定义域设置,您将在其中看到以下内容:

This page is rendering the HTML code we’ve defined in the custom document root created for the server block. If you’re able to see this page, it means your Nginx server is correctly configured to serve your domain.

该页面正在呈现我们在为服务器块创建的自定义文档根目录中定义HTML代码。 如果您能够看到此页面,则意味着您的Nginx服务器已正确配置为服务您的域。

结论 (Conclusion)

In this guide, we’ve seen how to install and set up Nginx, a high-performance web server and reverse proxy. We reviewed how to manage the Nginx service running on your server, and what are the main directories used by Nginx to store configuration files, content, and logs.

在本指南中,我们已经了解了如何安装和设置高性能Web服务器和反向代理Nginx。 我们回顾了如何管理服务器上运行的Nginx服务,以及Nginx用于存储配置文件,内容和日志的主要目录是什么。

From here, you have many options for the type of content and the technologies that you might want to use in the websites hosted within your web server.

从这里开始,您可以选择许多内容类型和技术,以供在Web服务器中托管的网站中使用。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-8

centos安装nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值