一、下载安装包
# 下载nginx_upstream_check_module模块
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
# 解压
unzip master
# 下载nginx 1.21.6
wget https://github.com/nginx/nginx/archive/refs/tags/release-1.21.6.tar.gz
# 解压
tar xf release-1.21.6.tar.gz
二、安装依赖包
apt-get update
apt-get install gcc libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl libssl-dev libtool build-essential -y
sudo apt-get install libxslt-dev libgd-dev libgeoip-dev -y
三、编译安装
# 移动到安装包目录下
cd /opt/nginx-release-1.21.6/
# 自定义要安装的插件和module
./auto/configure --prefix=/usr/local/nginx --add-module=/opt/nginx_upstream_check_module-master/ --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-lUTckl/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
# 开始编译
make
# 安装
make install
# 将编译后的nginx 拷贝到
cp /usr/local/nginx/sbin/nginx /usr/sbin/.
四、配置systemd守护
# 创建nginx守护进程文件
vim /usr/lib/systemd/system/nginx.service
# 文件内容如下
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
# 启动nginx服务
systemctl start nginx.service
# 查看nginx状态
systemctl status nginx.service
# 设置nginx开机自启
systemctl enable nginx.service
# 停止nginx服务
systemctl stop nginx.service
脚本解释:
Unit: 服务的启动顺序和依赖关系
Description:对该服务的描述;
After:在b.target服务组启动后,再启动本服务;
Service:服务具体执行的方式
ExecStart,ExecStop,ExecReload等:启动命令组,分别是服务启动时,停止时,重启时,启动前,启动后,停止后执行的命令;
Type:服务启动类型。默认simple表示ExecStart为主进程,notify类似于simple,启动结束后会发出通知信号。另外还有forking,oneshot,dbus,idle等类型;
Install: 把服务放在哪个服务组
WantedBy:服务所在的服务组。
五、检查安装结果
# 查看nginx版本
nginx -v
# 查看nginx安装的模块
nginx -V
# 查看nginx端口
netstat -nltp