Nginx开源版安装
-
下载
安装wget下载工具
# yum install wget
下载nginx最新安装包
# wget https://nginx.org/download/nginx-1.25.2.tar.gz
-
解压
# tar -zxvf nginx-1.25.2.tar.gz
-
查询依赖
# cd nginx-1.25.2.tar.gz # ./configure --prefix=/usr/local/nginx checking for OS + Linux 3.10.0-1160.el7.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
提示缺少C编译器,安装gcc
# yum install -y gcc //参数-y为自动回答yes
-
配置安装路径
# ./configure --prefix=/usr/local/nginx ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
安装perl库
# yum install -y pcre pcre-devel
再来配置安装路径
# ./configure --prefix=/usr/local/nginx ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option.
安装zlib库
# yum install -y zlib zlib-devel
再来配置安装路径
# ./configure --prefix=/usr/local/nginx
-
编译并安装
# make && make install
-
启动nginx
# /usr/local/nginx/sbin/nginx //启动 # /usr/local/nginx/sbin/nginx -s stop //快速停止 # /usr/local/nginx/sbin/nginx -s quit //优雅关闭,在退出前完成已经接受的链接请求 # /usr/local/nginx/sbin/nginx -s reload //重新加载配置
关于防火墙
# systemctl stop firewalld.service //关闭防火墙
# systemctl disable firewalld.service //禁止防火墙开机启动
# firewall-cmd --zone=public --add-port=80/tcp --permanent //放行端口
# firewall-cmd --reload //重启防火墙
安装成系统服务
- 创建服务脚本
# vi /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进程
# ps -ef|grep nginx
root 7126 1 0 11:59 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 7127 7126 0 11:59 ? 00:00:00 nginx: worker process
root 7282 1314 0 14:40 pts/0 00:00:00 grep --color=auto nginx
- 关闭正在运行的nginx进程(先关闭此进程,否则会与下面服务冲突)
# /usr/local/nginx/sbin/nginx -s stop
- 再查看正在运行的nginx进程
# ps -ef | grep nginx
root 7286 1314 0 14:46 pts/0 00:00:00 grep --color=auto nginx
- 开启nginx服务
# systemctl start nginx
- 查看nginx服务状态
# systemctl status nginx
- 设置成开机启动
# systemctl enable nginx.service
参考
https://www.bilibili.com/video/BV1yS4y1N76R