一、引言
在生产环境中,Nginx 通常以源码编译的方式来安装,一则生产环境网络要求苛刻,二则可以自选模块,比如在 复用443 端口时候,需要用到 stream_ssl_module 模块。
本文以 CentOS 7 和 Debian 11 操作系统为例,文中第四部分是如何新增模块,重新编译已经安装好的 Nginx 服务
二、安装
1.安装依赖包
########## 以下适用于 CentOS 7 | RadHat 7 ##########
yum install -y libtool autoconf cmake curl vim \
gcc gcc-c++ \
openssl openssl-devel \
pcre pcre-devel \
zlib zlib-devel \
libxml2 libxml2-devel \
libxslt libxslt-devel \
gd gd-devel \
GeoIP GeoIP-devel GeoIP-data \
gperftools
########## 以上适用于 CentOS 7 | RadHat 7 ##########
########## 以下适用于 Debian 11 ##########
apt-get -y install google-perftools libgdchart-gd2-noxpm libgdchart-gd2-noxpm-dev -y
########## 以下适用于 Debian 11 ##########
2.下载 Nginx | openssl 包
curl -LO https://nginx.org/download/nginx-1.20.2.tar.gz
curl -LO https://ftp.openssl.org/source/old/1.1.1/openssl-1.1.1k.tar.gz
3.解压
tar -zxvf nginx-1.20.2.tar.gz -C /usr/local
tar -zxvf openssl-1.1.1k.tar.gz -C /usr/local/src
cd /usr/local/nginx-1.20.2
useradd www
4.生成侦测文件
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_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_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-mail --with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-google_perftools_module \
--with-pcre \
--with-openssl=/usr/local/src/openssl-1.1.1k \
--with-openssl-opt=enable-weak-ssl-ciphers
5.编译并安装
make && make install
三、守护进程脚本
vim /etc/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP (/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM(/bin/cat /usr/local/nginx/logs/nginx.pid)"
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start nginx.service
四、重新编译已存在的 nginx
1.下载 Nginx
2.解压
3.生成侦测文件
在这里添加你想要添加的模块,比如 –with-stream_ssl_module,根据需求来
4.编译(这里千万不要 make install)
make
5.备份并复制
# 备份原有的 nginx 可执行文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.default.bak
# 复制全新编译的 nginx
cp /usr/local/src/nginx-1.20.2/objs/nginx /usr/local/nginx/sbin/nginx
6.重新启动 Nginx
systemctl restart nginx