如何在 CentOS 7 上安装 Nginx

介绍

Nginx 是一个流行的高性能 Web 服务器,本教程将教你如何在 CentOS 7 服务器上安装和启动 Nginx。

前提条件

本教程中的步骤需要具有sudo特权的非root 用户,请参阅我们的 CentOS 7初始服务器设置教程以了解如何设置此用户。

步骤 1 – 添加 EPEL 软件存储库

要添加 CentOS 7 EPEL 存储库,首先通过 SSH 连接到您的 CentOS 7 机器,然后使用yum命令来安装扩展包存储库:

1sudo yum install epel-release

您将被要求验证您想要安装该软件. 输入y然后ENTER以继续。

接下来,您将安装实际的nginx软件包。

第2步:安装 Nginx

现在,EPEL存储库已安装在您的服务器上,请使用以下yum命令安装 Nginx:

1sudo yum install nginx

再次,对验证提示回答,然后 Nginx 将完成安装。

步骤 3 – 启动 Nginx

安装后 Nginx 不会自动启动. 若要运行 Nginx,请使用 systemctl 命令:

1sudo systemctl start nginx

您可以通过systemctl status检查服务的状态:

1sudo systemctl status nginx
 1[secondary_label Output]
 2 nginx.service - The nginx HTTP and reverse proxy server
 3   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
 4   Active: active (running) since Mon 2022-01-24 20:14:24 UTC; 5s ago
 5  Process: 1898 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
 6  Process: 1896 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
 7  Process: 1895 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 8 Main PID: 1900 (nginx)
 9   CGroup: /system.slice/nginx.service
10           ├─1900 nginx: master process /usr/sbin/nginx
11           └─1901 nginx: worker process
12
13Jan 24 20:14:24 centos-updates systemd[1]: Starting The nginx HTTP and reverse proxy server...
14Jan 24 20:14:24 centos-updates nginx[1896]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
15Jan 24 20:14:24 centos-updates nginx[1896]: nginx: configuration file /etc/nginx/nginx.conf test is successful
16Jan 24 20:14:24 centos-updates systemd[1]: Started The nginx HTTP and reverse proxy server.

服务应该是积极的

如果您正在运行防火墙,请执行以下命令以允许 HTTP 和 HTTPS 流量:

1sudo firewall-cmd --permanent --zone=public --add-service=http
2sudo firewall-cmd --permanent --zone=public --add-service=https
3sudo firewall-cmd --reload

您可以立即进行点击检查,通过访问您的服务器的公共 IP 地址在您的 Web 浏览器中,以确认一切按计划进行:

1http://server_domain_name_or_IP/

您将看到默认的 CentOS 7 Nginx 网页,该网页用于信息和测试。

CentOS 7 Nginx Default

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

<$>[note] 注: 若要查找服务器的公共IP地址,请通过键入:

1ip addr
1[secondary_label Output]
21. lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
3
4. . .
52: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
6
7. . .

您可能會看到一系列的接口,這取決於您的伺服器上可用的硬體。「lo」接口是本地的 loopback 接口,而不是我們想要的接口。

一旦你有界面名称,你可以运行以下命令,揭示你的服务器的公共IP地址。

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

美元

在继续之前,您可能希望在系统启动时允许 Nginx 启动。

1sudo systemctl enable nginx

Nginx 已安装并运行。

步骤 4 – 探索和配置 Nginx

如果您想通过 Nginx 开始服务自己的页面或应用程序,您将需要知道 Nginx 配置文件和默认服务器根目录的位置。

默认服务器根

默认的服务器 root 目录是 /usr/share/nginx/html. 在那里放置的文件将在您的 Web 服务器上服务. 此位置在默认的服务器块配置文件中指定,它与 Nginx 交付,该文件位于 /etc/nginx/conf.d/default.conf

服务器区块配置

任何其他服务器块,在Apache中被称为虚拟主机,可以通过在 `/etc/nginx/conf.d 中创建新的配置文件来添加。

Nginx 全球配置

主要的 Nginx 配置文件位于 /etc/nginx/nginx.conf. 在这里,您可以更改设置,例如运行 Nginx 示范流程的用户,以及在 Nginx 运行时生长的工人流程的数量。

结论

一旦您在 CentOS 7 服务器上安装了 Nginx,您可以继续到 在 CentOS 7 上安装完整的 LEMP Stack

Published At
Categories with 技术
Tagged with
comments powered by Disqus