# 1、首先安装nginx要有gcc的环境和第三方库 pcre-devel可以使用以下命令进行安装。
yum install -y gcc
yum install -y pcre pcre-devel
# 2、下载nginx,在/usr/local目录下
wget https://nginx.org/download/nginx-1.21.6.tar.gz
# 3、解压安装
tar zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
./configure --prefix=/usr/local/nginx
make && make install
# 4、启动nginx
cd /usr/local/nginx/sbin
./nginx
# 提示:建议将nginx.config(在/usr/local/nginx/conf下)的第一行“user noboday”改成“user root”或者实际使用的用户,不然有些代理的资源路径由于权限问题导致访问不了
# 也可以直接注释掉这一行(推荐)
# nginx一些常用命令
./nginx // 启动nginx
./nginx -s stop // 停止ngxin
./nginx -s quit // 优雅的关闭nginx,在退出前完成已经接受的连接请求
./nginx -s reload // 重新加载ngxin配置
./nginx -s reopen // 打开nginx日志文件
./nginx -t // 检测配置文件是否有语法错误
./nginx -v // 显示版本信息
./nginx -c [配置文件路径] // 指定配置文件
killall nginx // 杀死所有nginx的进程
ps -aux | grep nginx // 查看nginx的进程
# 5、将nginx安装成系统服务
vim /usr/lib/systemd/system/nginx.service
# 将以下内容复制到文件中
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 设置文件权限
chmod 755 /usr/lib/systemd/system/nginx.service
# 干掉启动的服务
./nginx -s stop
或者
ps -ef|grep nginx
kill -9
# 启动服务
systemctl start nginx.service
# 设置为开机启动
systemctl enable nginx.service
# 常用命令
systemctl start nginx.service // 启动nginx服务
systemctl stop nginx.service // 停止nginx服务
systemctl restart nginx.service // 重启nginx服务
systemctl reload nginx.service // 重载nginx服务 (推荐使用)
systemctl status nginx.service // 查看nginx状态
systemctl enable nginx.service // 设置为开机启动nginx服务
03-02
1万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-27
3537
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-15
484
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-06
1484
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交