如何在CentOS Linux 7.5上安装Nginx

安装Nginx

Nginx包可在EPEL存储库中找到。

如果您没有安装EPEL存储库,可以运行以下命令:

sudo yum install epel-release

如何在CentOS Linux 7.5上安装Nginx

我们现在可以通过执行以下命令来安装nginx包:

sudo yum install nginx

如果这是您第一次从EPEL存储库安装包装,yum可能会提示您导入EPEL GPG密钥:

如果是这种情况,请键入y并按Enter键。

安装完成后,启用并启动Nginx服务:

sudo systemctl enable nginx
sudo systemctl start nginx

如果您正在运行防火墙,则还需要打开端口80和443:

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

如何在CentOS Linux 7.5上安装Nginx

我们现在可以检查Nginx服务的状态和版本:

sudo systemctl status nginx

示例输出:

● nginx.service - The nginx HTTP and reverse proxy server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
  Active: active (running) since 二 2018-05-15 10:09:59 CST; 57s ago
  Process: 7149 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 7147 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 7142 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 7151 (nginx)
  CGroup: /system.slice/nginx.service
          ├─7151 nginx: master process /usr/sbin/nginx
          └─7152 nginx: worker process

5月 15 10:09:58 localhost.localdomain systemd[1]: Starting The nginx HTTP an...
5月 15 10:09:59 localhost.localdomain nginx[7147]: nginx: the configuration ...
5月 15 10:09:59 localhost.localdomain nginx[7147]: nginx: configuration file...
5月 15 10:09:59 localhost.localdomain systemd[1]: Failed to read PID from fi...
5月 15 10:09:59 localhost.localdomain systemd[1]: Started The nginx HTTP and...
Hint: Some lines were ellipsized, use -l to show in full.

sudo nginx -v

nginx version: nginx/1.12.2

如何在CentOS Linux 7.5上安装Nginx

最后,我们可以通过在您选择的浏览器中打开http//YOUR_IP来验证安装,并且您应该能够看到默认的Nginx欢迎页面,如下所示:

如何在CentOS Linux 7.5上安装Nginx

使用systemctl管理Nginx服务

我们可以像任何其他系统单元一样管理Nginx服务。

要停止Nginx服务,请运行:

sudo systemctl stop nginx

要再次启动,请键入:

sudo systemctl start nginx

重新启动Nginx服务:

sudo systemctl restart nginx

在进行一些配置更改后重新加载Nginx服务:

sudo systemctl reload nginx

如果你想禁用Nginx服务在启动时启动:

sudo systemctl disable nginx

并重新启用它:

sudo systemctl enable nginx

如何在CentOS Linux 7.5上安装Nginx

阅读推荐:

如果您想用免费的LetsEncrypt SSL证书保护您的网站,可以查看以下指南:

猜你喜欢

转载自www.linuxidc.com/Linux/2018-05/152399.htm