介绍
Nginx 是一种流行的高性能 Web 服务器。本教程将教您如何在 CentOS 7 服务器上安装和启动 Nginx。
先决条件
本教程中的步骤需要具有特权的root用户。
第 1 步 — 添加 EPEL 软件仓库
要添加 CentOS 7 EPEL 存储库,首先通过 SSH 连接到你的 CentOS 7 机器,然后使用yum命令安装扩展包存储库:
sudo yum install epel-release
接下来,您将安装实际的nginx软件包。
第 2 步 — 安装 Nginx
现在 EPEL 存储库已安装在您的服务器上,请使用以下yum命令安装 Nginx:
sudo yum install nginx
再次,对验证提示回答是,然后 Nginx 将完成安装。
第 3 步 — 启动 Nginx
Nginx 安装后不会自动启动。要运行 Nginx,请使用以下systemctl命令:
sudo systemctl start nginx
您可以使用以下 systemctl status 方式检查服务的状态:
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 Sat 2023-02-04 22:02:12 CST; 1s ago
Process: 30808 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 30802 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 30801 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 30811 (nginx)
CGroup: /system.slice/nginx.service
├─30811 nginx: master process /usr/sbin/nginx
└─30813 nginx: worker process
Feb 04 22:02:11 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 04 22:02:12 localhost.localdomain nginx[30802]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 04 22:02:12 localhost.localdomain nginx[30802]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 04 22:02:12 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
Nginx服务应该是active状态。
如果您开启了防火墙,请运行以下命令以允许 HTTP 和 HTTPS 流量:
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
访问服务器ip地址检查是否启动成功:
http://server_domain_name_or_IP/
本地访问示例:
http://127.0.0.1:80/
您将看到默认的 CentOS 7 Nginx 网页,该网页用于提供信息和测试目的。它应该看起来像这样:
如果您看到此页面,那么您的 Web 服务器现已正确安装。
第 4 步 — 探索和配置 Nginx
如果您想通过 Nginx 开始为您自己的页面或应用程序提供服务,您将想知道 Nginx 配置文件和默认服务器根目录的位置。
默认服务器根目录
默认的服务器根目录是/usr/share/nginx/html. 放置在那里的文件将在您的网络服务器上提供。该位置在 Nginx 附带的默认服务器块配置文件中指定,该文件位于/etc/nginx/conf.d/default.conf.
服务器块配置
任何额外的服务器块,在 Apache 中称为虚拟主机,可以通过在/etc/nginx/conf.d 文件夹中创建以 .conf 结尾的配置文件, 当 Nginx 启动时,将加载该目录中以 .conf 结尾的文件。
Nginx 全局配置
主要的 Nginx 配置文件位于/etc/nginx/nginx.conf. 在这里您可以更改设置,例如运行 Nginx 守护进程的用户,以及 Nginx 运行时生成的工作进程的数量等等。
本文暂时没有评论,来添加一个吧(●'◡'●)