一.安装部署
1.安装依赖包
因为nginx基于c语言开发,所以需要安装C语言的编译环境及正则表达式库等第三方依赖库
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
2. 下载源码包
我这里下载的 nginx-1.22.0.tar.gz
上传到虚拟机
-rw-r--r-- 1 root root 1073322 Aug 3 15:40 nginx-1.22.0.tar.gz
3.解压并指定安装目录
tar xvf nginx-1.22.0.tar.gz
创建目录:
mkdir /usr/local/nginx
进入解压的文件:
[root@localhost ~]# cd nginx-1.22.0
useradd nginx -s /sbin/nologin -M ./configure --user=nginx --group=nginx \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module# ./configure --prefix=/usr/local/nginx/用于设置 Nginx 的配置选项,并指定安装目录为
/usr/local/nginx/
。在执行这个命令之后,可以编译和安装 Nginx 到指定的目录。
4.编译&安装
[root@localhost nginx-1.22.0]# make & make install
5 为nginx提供SysV init脚本
# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6.设置开机自启并检查服务启动
[root@localhost nginx-1.22.0]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@localhost nginx-1.22.0]#
[root@localhost nginx-1.22.0]#
[root@localhost nginx-1.22.0]# systemctl list-unit-files | grep nginx
nginx.service enabled