如何在 CentOS 7 上安装 Nginx如何在 CentOS 7 上安装 Nginx如何在 CentOS 7 上安装 Nginx如何在 CentOS 7 上安装 Nginx
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2025年2月28日
类别
  • 未分类
标签

如何在 CentOS 7 上安装 Nginx

介绍

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

先决条件

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

第 1 步 — 添加 EPEL 软件仓库

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

  1. sudo yum install epel-release

系统将提示您确认是否要安装该软件。键入 y 然后 ENTER 继续。

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

第 2 步 — 安装 Nginx

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

  1. sudo yum install nginx

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

第 3 步 — 启动 Nginx

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

  1. sudo systemctl start nginx

您可以使用 systemctl status 检查服务的状态:

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

该服务应该是 active。

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

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

您可以立即进行抽查,以通过在网络浏览器中访问服务器的公共 IP 地址来验证一切是否按计划进行:

http://server_domain_name_or_IP/

您将看到默认的 CentOS 7 Nginx 网页,该网页用于提供信息和测试目的。它应该看起来像这样:

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

注意:要查找服务器的公共 IP 地址,请键入以下命令查找计算机上的网络接口:

  1. ip addr
Output
1. lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN . . . 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 . . .

根据服务器上可用的硬件,您可能会在此处看到许多接口。 lo接口是本地环回接口,不是我们想要的。在我们上面的示例中,eth0 接口就是我们想要的。

获得接口名称后,您可以运行以下命令来显示服务器的公共 IP 地址。替换您在上面找到的接口名称:

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

在继续之前,您可能希望在系统启动时启用 Nginx。为此,请输入以下命令:

  1. sudo systemctl enable nginx

Nginx 现已安装并运行。

第 4 步 — 探索和配置 Nginx

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

默认服务器根目录

默认的服务器根目录是/usr/share/nginx/html。放置在那里的文件将在您的网络服务器上提供。此位置在 Nginx 附带的默认服务器块配置文件中指定,该文件位于 /etc/nginx/conf.d/default.conf。

服务器块配置

可以通过在 /etc/nginx/conf.d 中创建新的配置文件来添加任何额外的服务器块,在 Apache 中称为虚拟主机。 Nginx 启动时将加载该目录中以 .conf 结尾的文件。

Nginx 全局配置

主要的 Nginx 配置文件位于 /etc/nginx/nginx.conf。在这里您可以更改设置,例如运行 Nginx 守护进程的用户,以及 Nginx 运行时生成的工作进程的数量等等。

结论

在 CentOS 7 服务器上安装 Nginx 后,您可以继续在 CentOS 7 上安装完整的 LEMP Stack。

©2015-2025 艾丽卡 support@alaica.com