Nginx编译安装
目录
注意
本文最后更新于 2024-04-12,文中内容可能已过时。
摘要
1 编译安装Nginx
1.1 创建用户
groupadd --system nginx
useradd --system -g nginx -d /usr/lib/nginx -s /sbin/nologin -c "Nginx web server" nginx
1.2 下载Nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
1.3 解决依赖
# centos
yum -y install pcre-devel openssl-devel gcc make
# ubuntu
apt install -y zlib1g-dev libpcre3-dev libssl-dev gcc make
1.4 解压
tar zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0
1.5 生成配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-stream
# --add-module=/root/fastdfs-nginx-module/src #fastdfs模块
–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 #添加第三方外部模块
1.6 编译安装
make && make install
1.7 自启动
创建nginx.service
文件并插入以下内容
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
2 Alpine Linux
2.1 安装依赖
apk add gcc g++ make openssl-dev pcre-dev zlib-dev
2.2 生成配置
./configure \
--prefix=/usr/local/nginx \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-stream
2.3 编译
make && make install
2.4 创建nginx的openrc文件
cat /etc/init.d/nginx
#!/sbin/openrc-run
description="Nginx http and reverse proxy server"
extra_commands="checkconfig"
extra_started_commands="reload reopen upgrade"
cfgfile=${cfgfile:-/usr/local/nginx/conf/nginx.conf}
pidfile=/usr/local/nginx/logs/nginx.pid
command=${command:-/usr/local/nginx/sbin/nginx}
command_args="-c $cfgfile"
required_files="$cfgfile"
depend() {
need net
use dns logger netmount
}
start_pre() {
#checkpath --directory --owner nginx:nginx ${pidfile%/*}
$command $command_args -t -q
}
checkconfig() {
ebegin "Checking $RC_SVCNAME configuration"
start_pre
eend $?
}
reload() {
ebegin "Reloading $RC_SVCNAME configuration"
start_pre && start-stop-daemon --signal HUP --pidfile $pidfile
eend $?
}
reopen() {
ebegin "Reopening $RC_SVCNAME log files"
start-stop-daemon --signal USR1 --pidfile $pidfile
eend $?
}
upgrade() {
start_pre || return 1
ebegin "Upgrading $RC_SVCNAME binary"
einfo "Sending USR2 to old binary"
start-stop-daemon --signal USR2 --pidfile $pidfile
einfo "Sleeping 3 seconds before pid-files checking"
sleep 3
if [ ! -f $pidfile.oldbin ]; then
eerror "File with old pid ($pidfile.oldbin) not found"
return 1
fi
if [ ! -f $pidfile ]; then
eerror "New binary failed to start"
return 1
fi
einfo "Sleeping 3 seconds before WINCH"
sleep 3 ; start-stop-daemon --signal 28 --pidfile $pidfile.oldbin
einfo "Sending QUIT to old binary"
start-stop-daemon --signal QUIT --pidfile $pidfile.oldbin
einfo "Upgrade completed"
eend $? "Upgrade failed"
}
2.5 授权可执行
chmod +x /etc/init.d/nginx
3 升级Nginx
包括升级Nginx版本或者重新编译添加模块
如果忘记之前安装的配置参数
执行``nginx -V查看
configure arguments`
3.1 下载新版本Nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
3.2 解决依赖
yum -y install pcre-devel openssl-devel gcc make
3.3 解压
tar zxf nginx-1.24.0.tar.gz && cd nginx-1.24.0
3.4 重新配置
比如重新编译添加支持fastdfs的模块
./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
3.5 重新编译
注意:不进行make install操作
make
make[1]: *** [/usr/local/openssl/.openssl/include/openssl/ssl.h] Error 127
编译安装 nginx 时,--with-openssl
参数默认只支持OpenSSL的源代码,不支持已编译好的 OpenSSL。可以在nginx的解压目录下修改auto/lib/openssl/conf
sed -i 's/\.openssl\///' auto/lib/openssl/conf
将文件中的.openssl
去掉,就可以支持编译之后的openssl路径了
重新编译即可
make clean
./configure ....
make
3.6 替换新的Nginx
make完之后在objs目录下就多了个nginx,这个就是新版本的程序了
备份旧的Nginx程序
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
把新的Nginx程序覆盖旧的
cp objs/nginx /usr/local/nginx/sbin/nginx
如果提示cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy
建议使用如下语句 cp
cp -rfp objs/nginx /usr/local/nginx/sbin/nginx
测试新的Nginx程序是否正确
/usr/local/nginx/sbin/nginx -t
平滑重启Nginx
/usr/local/nginx/sbin/nginx -s reload
查看Nginx版本极其编译参数
/usr/local/nginx/sbin/nginx -V