felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

CentOS 7 环境下 yum 安装 Nginx #78

Open felix-cao opened 6 years ago

felix-cao commented 6 years ago

之前有一篇文章是源码安装 centos 7 下 Nginx 的下载安装 #75, 本篇主要介绍 CentOS 7 环境下的yum 安装, CentOS 7 中的 yum 没法直接使用 yum install nginx 这个指令去安装 Nginx,因为 Nginx 这个服务不是 yum 库中自带的。

一、配置 yum 源

这里是 官方提供的 nginx 安装教程, 或者看看这个 Linux 系统下 Nginx 稳定的安装包

http://nginx.org/packages/centos/7/x86_64/RPMS/

$ vi  /etc/yum.repos.d/nginx.repo

输入如下内容

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1

$releasever 替换为7,将 $basearch 替换成x86_64。 通过下面这条指令获取你自己系统的 $releasever 和 $basearch

$ rpm -qa | grep centos-release

结果显示:

centos-release-7-4.1708.el7.centos.x86_64

二、安装、启动及检测

2.1、安装 Nginx

$ yum list nginx
$ yum install nginx

2.2、启动 Nginx

$ systemctl start nginx

2.3、检测是否启动

$ systemctl status nginx

2.4、查看 nginx 版本

$ nginx -v
nginx version: nginx/1.14.0

2.5、查看安装目录

$ rpm -ql nginx

2.6、检查配置文件是否正确

$ nginx -t

2.7、平滑重启 nginx

$ nginx -s reload

2.8、本地测试

测试一下在本地是否可以访问。

$ curl -i localhost

2.9、其他

启动:systemctl start nginx.service

停止:systemctl stop nginx.service

重启:systemctl restart nginx.service

设置开机自启动:systemctl enable nginx.service

停止开机自启动:systemctl disable nginx.service

查看当前状态:systemctl status nginx.service

查看所有已启动的服务:systemctl list-units --type=service