Nginx编译安装

bash

groupadd --system nginx
useradd --system -g nginx -s /sbin/nologin -c "Nginx web server" nginx

bash

wget http://nginx.org/download/nginx-1.24.0.tar.gz

bash

# centos
yum -y install pcre-devel openssl-devel gcc make
# ubuntu
apt install -y zlib1g-dev libpcre3-dev libssl-dev gcc make

text

tar zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0

bash

./configure \
--prefix=/usr/local/nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-stream

–with-http_auth_basic_module #认证

–with-http_access_module #访问控制

–with-http_ssl_module #ssl支持

–with-http_v2_module #启用http2支持

–with-http_realip_module #获取真实ip

–with-stream #四层代理

–add-module=PATH #添加第三方外部模块

bash

make && make install

创建nginx.service文件并插入以下内容

bash

cat > /usr/lib/systemd/system/nginx.service <<'EOF'
[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
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

包括升级Nginx版本或者重新编译添加模块

如果忘记之前安装的配置参数

执行``nginx -V查看configure arguments`

bash

wget http://nginx.org/download/nginx-1.24.0.tar.gz

bash

yum -y install pcre-devel openssl-devel gcc make

bash

tar zxf nginx-1.24.0.tar.gz && cd nginx-1.24.0

比如重新编译添加支持fastdfs的模块

bash

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-file-aio \
--with-http_realip_module \
--add-module=/root/fastdfs-nginx-module/src

注意:不进行make install操作

bash

make

make[1]: *** [/usr/local/openssl/.openssl/include/openssl/ssl.h] Error 127

image-20220913172304001

编译安装 nginx 时,--with-openssl参数默认只支持OpenSSL的源代码,不支持已编译好的 OpenSSL。可以在nginx的解压目录下修改auto/lib/openssl/conf

bash

sed -i 's/\.openssl\///' auto/lib/openssl/conf

将文件中的.openssl去掉,就可以支持编译之后的openssl路径了

重新编译即可

bash

make clean
./configure ....
make

make完之后在objs目录下就多了个nginx,这个就是新版本的程序了

备份旧的Nginx程序

bash

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

把新的Nginx程序覆盖旧的

bash

cp objs/nginx /usr/local/nginx/sbin/nginx

如果提示cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy

建议使用如下语句 cp

bash

cp -rfp objs/nginx /usr/local/nginx/sbin/nginx

测试新的Nginx程序是否正确

bash

/usr/local/nginx/sbin/nginx -t

平滑重启Nginx

bash

/usr/local/nginx/sbin/nginx -s reload

查看Nginx版本极其编译参数

bash

/usr/local/nginx/sbin/nginx -V

相关内容