操作系统:CentOS 7.9;
安装方式:在线安装和离线安装;
操作说明:下面的命令基本都是在通过命令su
切换成root用户执行的。
一、在线Yum命令安装
1. 执行Yum安装命令
yum install nginx
# 可以使用参数 -y,表示在安装过程中自动应答所有问题
yum install nginx -y
2. 可能遇到的错误
2.1 错误:“没有可用软件包 nginx”。解决方式,安装epel并更新源:
# 安装epel
yum install epel-release
# 更新源
yum update
2.2 错误:“Another app is currently holding the yum lock; waiting for it to exit…”。解决方式,杀掉运行的yum:
# yum程序正在运行,通过命令杀掉
rm -f /var/run/yum.pid
3. 查看及启动Nginx
3.1 查看安装情况:
rpm -qa | grep nginx
3.2 启动Nginx
# 启动服务
systemctl start nginx
# 查看服务
systemctl status nginx
4. 访问Nginx
在浏览器输入“http://localhost:80”,默认端口80。
5. 默认安装地址
/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:
# 解压
tar -zxvf openssl-fips-2.0.16.tar.gz
# 进入目录
cd openssl-fips-2.0.16/
# 检测、编译
./config
# 编译
make
# 安装
make install
2.3 安装zlib:
tar -zxvf zlib-1.3.tar.gz
cd zlib-1.3/
./configure
make
make install
2.4 安装pcre:
tar -zxvf pcre-8.45.tar.gz
cd pcre-8.45/
./configure
make
make install
3. 安装Nginx:
3.1 执行如下命令安装:
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 查看安装情况,执行如下命令:
# 进入安装目录
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:
yum -y install gcc
yum -y install gcc-c++
说明:这里属于作弊操作,是通过命令在线安装的;本来下载了源码包,想要离线安装,但是操作下来发现它也依赖很多其他包,比在线操作麻烦太多,就放弃了;希望以后有机会填上这个坑。
5.2 错误,安装完Nginx,执行nginx -t
时遇到bash: nginx: 未找到命令...
,原因是未加入到环境变量中,解决方式,配置环境变量:
# 编辑配置文件,在文件末尾增加: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]:
vim /usr/lib/systemd/system/nginx.service
脚本内容如下:
[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 启动服务及查看服务:
systemctl start nginx
systemctl status nginx
三、服务基本操作
# 启动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