CentOS7 安装 Nginx

   操作系统:CentOS 7.9; 安装方式:在线安装和离线安装;操作说明:下面的命令基本都是在通过命令 su 切换成 root 用户执行的。


一、在线 Yum 命令安装

1. 执行 Yum 安装命令

1
2
3
yum install nginx
# 可以使用参数 -y,表示在安装过程中自动应答所有问题
yum install nginx -y

2. 可能遇到的错误

2.1 错误:“没有可用软件包 nginx”。解决方式,安装 epel 并更新源:

1
2
3
4
# 安装 epel
yum install epel-release
# 更新源
yum update

2.2 错误:“Another app is currently holding the yum lock; waiting for it to exit…”。解决方式,杀掉运行的 yum:

1
2
# yum 程序正在运行,通过命令杀掉
rm -f /var/run/yum.pid

3. 查看及启动 Nginx

3.1 查看安装情况:

1
rpm -qa | grep nginx

3.2 启动 Nginx

1
2
3
4
# 启动服务 
systemctl start nginx
# 查看服务
systemctl status nginx

4. 访问 Nginx

在浏览器输入“http://localhost”,默认端口 80。

5. 默认安装地址

1
2
3
/etc/nginx/nginx.conf # Yum 安装 Nginx 默认主配置文件
/usr/share/nginx/html # Nginx 默认存放目录
/usr/share/nginx/html/index.html # Nginx 默认主页路径

二、离线源码安装

1. 下载 Nginx 及依赖包

1.1 依赖包:

  • ssl 功能需要 openssl 库 (openssl-fips-2.0.16.tar.gz,下载 )
  • gzip 模块需要 zlib 库 (zlib-1.3.tar.gz, 下载 )
  • rewrite 模块需要 pcre 库 (pcre-8.45.tar.gz,下载 )

1.2 Nginx (nginx-1.24.0.tar.gz,下载 )

2. 安装依赖包

2.1 先依次安装依赖包:openssl、zlib、pcre。

2.2 安装 openssl:

1
2
3
4
5
6
7
8
9
10
# 解压 
tar -zxvf openssl-fips-2.0.16.tar.gz
# 进入目录
cd openssl-fips-2.0.16/
# 检测、编译
./config
# 编译
make
# 安装
make install

2.3 安装 zlib:

1
2
3
4
5
tar -zxvf zlib-1.3.tar.gz
cd zlib-1.3/
./configure
make
make install

2.4 安装 pcre:

1
2
3
4
5
tar -zxvf pcre-8.45.tar.gz
cd pcre-8.45/
./configure
make
make install

3. 安装 Nginx:

3.1 执行如下命令安装:

1
2
3
4
5
6
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0/
# "--with-xxx="的值是解压目录,不是安装目录
./configure --prefix=/usr/local/nginx --with-pcre=../pcre-8.45 --with-zlib=../zlib-1.3 --with-openssl=../openssl-fips-2.0.16
make
make install

3.2 查看安装情况,执行如下命令:

1
2
3
# 进入安装目录
cd /usr/local/nginx/sbin/
./nginx -t

会出现如下结果,表示成功。
执行结果

4. 基本操作命令:

  • 启动服务:nginx
  • 退出服务:nginx -s quit
  • 强制关闭服务:nginx -s stop
  • 重载服务:nginx -s reload  # 重载服务配置文件,类似于重启,但服务不会中止
  • 验证配置文件:nginx -t
  • 使用配置文件:nginx -c “配置文件路径”
  • 使用帮助:nginx -h
  • 查看版本:nginx -v
  • 查看详细信息:nginx -V

5. 可能遇到的错误

5.1 错误,在编译依赖包时出现make[1]: gcc:命令未找,解决方式,安装 gcc:

1
2
yum -y install gcc
yum -y install gcc-c++

说明:这里属于作弊操作,是通过命令在线安装的;本来下载了源码包,想要离线安装,但是操作下来发现它也依赖很多其他包,比在线操作麻烦太多,就放弃了;希望以后有机会填上这个坑。

5.2 错误,安装完 Nginx,执行 nginx -t 时遇到bash: nginx: 未找到命令...,原因是未加入到环境变量中,解决方式,配置环境变量:

1
2
3
4
# 编辑配置文件,在文件末尾增加:export PATH=/usr/local/nginx/sbin:$PATH
vim /etc/profile
# 重新加载,使配置文件生效
source /etc/profile

6. 注册系统服务

6.1 CentOS7 的服务 systemctl 脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分。我们一般存在系统服务里,即:/usr/lib/systemd/system 目录下。
6.2 编写 service 脚本,一般会分为 3 部分:[Unit]、[Service] 和[Install]:

1
vim /usr/lib/systemd/system/nginx.service

脚本内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

注意:PIDFile、ExecStart、ExecReload、ExecStop 的值和安装路径有关,根据自己的实际情况修改。
6.3 启动服务及查看服务:

1
2
systemctl start nginx
systemctl status nginx

三、服务基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#  启动 Nginx
systemctl start nginx

# 停止 Nginx
systemctl stop nginx

# 重载 Nginx
systemctl reload nginx

# 重启 Nginx
systemctl restart nginx

# 查询 Nginx 运行状态
systemctl status nginx

# 查询 Nginx 进程
ps -ef |grep nginx

# 查询 Nginx 监听端口
netstat -lntup |grep nginx

# 卸载 Nginx
yum -y remove nginx

# 开机启动 Nginx
systemctl enable nginx

# 关闭开机启动
systemctl disable nginx

# 查看是否开机启动
systemctl is-enabled nginx