#安装gcc环境yum install -y gcc-c++#安装PCRE库,用于解析正则表达式yum install -y pcre pcre-devel#zlib压缩和解压缩依赖yum install -y zlib zlib-devel#SSL 安全的加密的套接字协议层,用于HTTPSyum install -y openssl openssl-devel
下载并解压缩下载地址:http://nginx.org/en/download.html
#用命令下载wget -c https://nginx.org/download/nginx-1.20.2.tar.gz#解压缩tar -zxf nginx-1.20.2.tar.gz
新增账号#新增账号useradd -s /sbin/nologin nginx
创建临时目录#创建临时目录mkdir -p /var/tmp/nginx
修改代码#修改代码,去掉Header中的Server信息#解压后的nginx目录vim src/http/ngx_http_header_filter_module.c#第49行至第51行修改前:static u_char ngx_http_server_string[] = "Server: nginx" CRLF;static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;#第49行至第51行修改后:static u_char ngx_http_server_string[] = "" CRLF;static u_char ngx_http_server_full_string[] = "" NGINX_VER CRLF;static u_char ngx_http_server_build_string[] = "" NGINX_VER_BUILD CRLF;
配置#使用默认配置./configure#使用自定义配置./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --user=nginx --group=nginx --with-http_ssl_module --with-pcre
编译和安装#编译make#安装make install
创建系统服务#创建文件vim /usr/lib/systemd/system/nginx.service#内容如下[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service]Type=forkingPIDFile=/run/nginx.pid# Nginx will fail to start if /run/nginx.pid already exists but has the wrong# SELinux context、This might happen when running `nginx -t` from the cmdline.# https://bugzilla.redhat.com/show_bug.cgi?id=1268621ExecStartPre=/usr/bin/rm -f /run/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -tExecStart=/usr/local/nginx/sbin/nginxExecReload=/usr/local/nginx/sbin/nginx -s reloadKillSignal=SIGQUITTimeoutStopSec=5KillMode=process#不拿掉会开不起来#PrivateTmp=true[Install]WantedBy=multi-user.target
启动停止#启动systemctl start nginx#停止systemctl stop nginx#重启systemctl restart nginx