nginx编译安装

生产环境中一般不建议使用yum方法来安装nginx,通常情况下需要通过编译安装方式来安装,二进制包下载地址:

http://nginx.org/en/download.html                 #二进制包
https://nginx.org/packages/centos/7/x86_64/RPMS/  #rpm包

先安装基础编译环境,执行命令如下:

yum -y install gcc gcc-c++ autoconf automake make zlib* pcre*  #centos
apt update && apt install build-essential  libpcre3 libpcre3-dev  openssl libssl-dev  #ubuntu

1、首先将nginx安装包上传到linux服务器上,解压,进入解压后的目录中,执行命令如下:

上图中的安装目录可以根据需要自定义,如果添加模块,可以这样:./configure –prefix=/usr/local/nginx –with-stream,表示编译模块stream(此模块可以代理tcp或者udp)

添加多个功能包的编译如下(下面是基于nginx1.26):

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_sub_module --with-http_addition_module --with-stream_ssl_module --with-stream --with-http_secure_link_module --with-http_gzip_static_module --with-threads --with-file-aio
  • --with-http_ssl_module:启用 HTTPS/SSL 支持(必备)
  • --with-http_v2_module:支持 HTTP/2 协议,nginx1.9.5版本后才支持
  • --with-http_realip_module: 获取客户端真实 IP
  • --with-http_addition_module:响应内容追加文本
  • --with-http_sub_module:响应内容替换
  • --with-stream_ssl_module:加密的 TCP/UDP 代理,1.9.4版本后支持
  • --with-stream:TCP/UDP 代理支持(四层负载均衡),1.9.0版本后支持
  • --with-http_secure_link_module:资源访问权限控制(通过加密链接)
  • --with-http_gzip_static_module:预压缩静态文件(减少 CPU 开销)
  • --with-threads:启用线程池(提高性能)
  • --with-file-aio:启用异步文件 I/O(提升高并发性能)

2、编译完成后执行命令make && make install 安装,如图:

3、进入/usr/local/nginx/sbin目录执行命令./nginx启动nginx ,浏览器访问,如图:

4、配置服务自启动文件,编辑文件vim /lib/systemd/system/nginx.service,添加内容如图:

[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target


[Service]
Type=forking
#ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target

配置完成后即可通过服务方式启动和停止nginx,命令如下:

systemctl restart nginx
systemctl status nginx
systemctl enable nginx
systemctl start nginx
systemctl stop nginx
systemctl relaod nginx

备注:如果修改了nginx配置文件后,想查看配置是否正确,可执行命令nginx -t查看,如图:

一键安装脚本内容如下:

#!/bin/bash
: '
 @function: 安装nginx,本例子中版本为nginx-1.21.6.tar.gz
 @author: gongguan
 @date: 2022-03-07
'
echo "<<<<<<<<<<<<<<<<<<首先安装基础编译环境>>>>>>>>>>>>>>>>>"
yum -y install gcc gcc-c++ autoconf automake make pcre* zlib*
[ $? -eq 0 ] && echo "<<<<<<<<<<<<<环境安装完成>>>>>>>>>>>>>"
tar -xf $(dirname $0)/nginx-1.21.6.tar.gz
cd nginx-1.21.6
./configure --prefix=/usr/local/nginx --with-stream --with-http_secure_link_module
[ $? -eq 0 ] && echo "<<<<<<<<<<<<<<<编译完成>>>>>>>>>>>>>"
make && make install
[ $? -eq 0 ] && echo "<<<<<<<<<<<<<安装完成>>>>>>>>>>>>>"
echo "<<<<<<<<<<<<<<<<添加软链接>>>>>>>>>>>>>>>>>>>>>>>>"
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
echo "<<<<<<<<<<<<<<<<<<<编写服务器托管文件>>>>>>>>>>>>>"
cat > /lib/systemd/system/nginx.service <<EOF
[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target


[Service]
Type=forking
#ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop

[Install]
WantedBy=multi-user.target
EOF
echo "添加开机自启动"
systemctl enable nginx
systemctl start nginx
systemctl status nginx
  • –with-http_secure_link_module:用于配置防盗链
  • –with-stream:用于代理TCP和UDP

注意:如果nginx出现中文乱码,可以设置编码为UTF-8,在nginx.conf配置中添加如下:

charset utf-8;

要在日志中输出获取的真实IP,需要在http块中添加如下配置(需要http_realip_module模块):

# 确保设置$http_x_forwarded_for,该变量用于记录X-Forwarded-For的值
  log_format  main  '$remote_addr- $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

标签