安装nginx
下载网址:http://nginx.org/en/download.html
cd 解压后的nginx目录
./configure --prefix=/usr/local/nginx -- 安装配置
弹出 需要安装的依赖
yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
安装完毕不需要其他依赖后进行编译
make
make install
到/usr/local/nginx
目录下去
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Yo2fijm-1673160885399)(E:\desktop\笔记\nginx\image\Snipaste_2023-01-02_20-15-51.jpg)]
sbin目录下就是nginx的执行文件
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置
启动后,需要开启防火墙放心规则或者关闭防火墙
关闭防火墙centos7+
systemctl stop firewalld
systemctl disable firewalld 关闭防火墙自启
systemctl enable firewalld 开启防火墙自启
新增防火墙入境规则
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload 重启防火墙
systemctl restart firewalld 重启防火墙
安装成系统服务
创建服务脚本
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
重新加载系统服务
systemctl daemon-reload
如果脚本编辑失败,重新审视以上的脚本内容,并重新执行以上内容
先关闭开启的nginx服务,再用系统级命令操作nginx
systemctl restart nginx 重启nginx
systemctl reload nginx 优雅停机,重新加载配置,再启动服务