编译安装Nginx
系统版本
1 2 # cat /etc/system-release CentOS Linux release 7.6.1810 (Core)
编译安装Nginx 1.20.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 # !/bin/bash # Close selinux setenforce 0 &> /dev/null # Install the compilation environment in advance yum install -y wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel gd gd-devel # Download nginx source code package # --->>> Only modify the following parameters <<<--- nginx_version=1.20.1 nginx_prefix_dir=/usr/local/nginx install_package_dir=/usr/local/src install_log_dir="./install_nginx.log" # --->>> Only modify the above parameters <<<--- cd ${install_package_dir} && \ ls ./${nginx_version}.tar.gz || wget http://nginx.org/download/nginx-${nginx_version}.tar.gz &&\ tar xf ./${nginx_version}.tar.gz -C ${install_package_dir} && cd ./nginx-${nginx_version} && \ useradd -s /sbin/nologin nginx # Compile and install ./configure --prefix=${nginx_prefix_dir} \ --user=nginx \ --group=nginx \ --with-pcre \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-http_image_filter_module \ --with-http_slice_module \ --with-mail \ --with-threads \ --with-file-aio \ --with-stream \ --with-mail_ssl_module \ --with-stream_ssl_module if [[ $? -ne 0 ]];then echo "Compilation failed. Please check!";exit 2 fi make && make install # Configure nginx daemons cat >/usr/lib/systemd/system/nginx.service <<EOF [Unit] Description=nginx - high performance web server Documentation=https://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=${nginx_prefix_dir}/logs/nginx.pid ExecStartPre=${nginx_prefix_dir}/sbin/nginx -t -c ${nginx_prefix_dir}/conf/nginx.conf ExecStart=${nginx_prefix_dir}/sbin/nginx -c ${nginx_prefix_dir}/conf/nginx.conf ExecReload=${nginx_prefix_dir}/sbin/nginx -s reload ExecStop=${nginx_prefix_dir}/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target EOF chmod +x /usr/lib/systemd/system/nginx.service systemctl daemon-reload # Set Nginx to boot automatically systemctl enable nginx --now # systemctl restart nginx && systemctl enable nginx # Add to PATH # Add Soft Connection ln -s ${nginx_prefix_dir}/sbin/nginx /usr/sbin/nginx # Add environment variables # cat >/etc/profile.d/nginx.sh<<EOF # export NGINX_HOME=${nginx_prefix_dir} # export PATH=\$PATH:\$NGINX_HOME/sbin # EOF # source /etc/profile# Print installation information cat > ${install_log_dir} <<EOF [Info] Binary: ln -s ${nginx_prefix_dir}/sbin/nginx /usr/sbin/nginx Daemon: /usr/lib/systemd/system/nginx.service [Detail] EOF nginx -V &>> ./${install_log_dir} clear; cat ./${install_log_dir}
Nginx添加模块
基于编译安装方式的Nginx新增模块、版本升级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # 查看nginx路径 rpm -ql nginx | grep sbin # 查看当前Nginx已安装模块,记录configure argument: # nginx -V nginx version: nginx/1.20.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-stream_ssl_module # 编译安装Nginx依赖 yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel cd /usr/local/src wget http://nginx.org/download/nginx-1.20.1.tar.gz tar xf nginx-1.20.1.tar.gz cd nginx-1.20.1
查看需要开启的内置模块 添加内置模块
./configure + 已编译参数 + 新增模块
1 2 3 4 5 6 7 8 9 10 # 查看nginx内置模块 ./configure --help | grep xxx_module # 重新编译nginx ./configure \ # 旧二进制 configure arguments \ --with-xxx_module // 根据./configure --help 中查看的方式添加 # 禁止make install,否则会覆盖现有的nginx make
添加第三方模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # 下载第三方模块 wget http://xxx.tgz # 复制模块到指定目录 cp xxx_module /usr/local/src/nginx-1.20.1/xxx_module # 重新编译 cd /usr/local/src/nginx-1.20.1 ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-pcre \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-http_image_filter_module \ --with-http_slice_module \ --with-mail \ --with-threads \ --with-file-aio \ --with-stream \ --with-mail_ssl_module \ --with-stream_ssl_module \ --add-module=/usr/local/src/nginx-1.20.1/xxx_module # 禁止make install,否则会覆盖现有的nginx make # 查看模块是否开启/添加 /usr/local/src/nginx-1.18.0/objs/nginx -V
Nginx平滑升级 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # 备份当前Nginx二进制文件 cp -a /usr/local/nginx/sbin/nginx{,.bak`date +%Y%m%d`} # 记录旧 nginx master进程号 ps -ef | grep nginx ng_master_pid_old=$(ps -ef | grep nginx | awk '/master process/{print $2}') # 覆盖旧二进制文件 mv /usr/local/src/nginx-1.18.0/objs/nginx /usr/local/nginx/sbin/nginx # 记录新 nginx master进程号 ps -ef | grep nginx ng_master_pid_new=$(ps -ef | grep nginx | awk '/master process/{print $2}') # 挂起老进程 kill -USR2 ng_master_pid_old # 保存老master进程 kill -WINCH ng_master_pid_old # 验证无误后升级完成 # 回滚方式 kill -HUP ng_master_pid_old //重新拉起老master进程的子进程 kill -QUIT ng_master_pid_new //优雅退出新master进程 mv /usr/local/nginx/sbin/nginx.bak /usr/local/nginx/sbin/nginx 回滚完成