# 查看nginx安装了哪些插件
nginx -V
# 停止并卸载老的nginx
systemctl stop nginx
ps -ef|grep nginx
# 备份老的配置
find / -name "nginx"
mv /etc/logrotate.d/nginx{,-bak}
mv /etc/nginx{,-bak}
mv /var/log/nginx
mv /var/cache/yum/x86_64/7/nginx{,-bak}
mv /var/cache/nginx{,-bak}
mv /usr/sbin/nginx{,-bak}
mv /usr/lib/debug/usr/lib64/nginx{,-bak}
mv /usr/lib/debug/usr/lib64/perl5/vendor_perl/auto/nginx{,-bak}
mv /usr/lib64/perl5/vendor_perl/auto/nginx{,-bak}
mv /usr/lib64/nginx{,-bak}
mv /usr/share/nginx{,-bak}
mv /usr/libexec/initscripts/legacy-actions/nginx{,-bak}
# mv /usr/lib/systemd/system/nginx-debug.service
# mv /usr/lib/systemd/system/nginx.service
mv /usr/share/doc/nginx-1.22.0{,-bak}
mv /usr/share/man/man8/nginx.8.gz{,-bak}
# 下载nginx源码,并解压,进入解压目录
wget http://nginx.org/download/nginx-1.22.0.tar.gz
tar -zxvf nginx-1.22.0.tar.gz
cd nginx-1.22.0
修改源码配置
vi +13 src/core/nginx.h
#define NGINX_VERSION "8.8.8"
#define NGINX_VER " /" NGINX_VERSION
vi +49 src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: " CRLF;
vi +36 src/http/ngx_http_special_response.c
"<hr><center> </center>" CRLF
# 编译新的nginx
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
make && make install
编译中报错修复:
yum -y install pcre-devel
# 配置新的nginx配置
mkdir -p /etc/nginx/conf.d/
cp /etc/nginx-bak/nginx.conf /etc/nginx/
cp /etc/nginx-bak/conf.d/default.conf /etc/nginx/conf.d/
cp -a /etc/nginx-bak/ssl /etc/nginx/
vi /etc/nginx/nginx.conf
# 配置文件新增隐藏版本号
http {
server_tokens off;
}
启动测试
systemctl start nginx
cat /usr/lib/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
ExecStartPre=/bin/mkdir -p /var/run/nginx/
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl restart nginx
systemctl enable nginx