忿惺噱 发表于 2025-6-1 20:44:24

Nginx 离线安装与介绍

一、安装

1.1 离线安装


[*]准备源代码包
#从项目的官方网站或代码仓库(如 GitHub)下载源代码
wget https://nginx.org/download/nginx-1.24.0.tar.gz   #下载
tar -xzvf nginx-1.24.0.tar.gz       #解压
cd nginx-1.24.0
[*]安装编译工具和依赖项
#正则表达式库(pcre-devel)、 数据压缩库(zlib-devel)和 https模块库(openssl-devel)
sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-develzlib与zlib-devel关系:zlib-devel提供编译环境,zlib提供运行时环境。其他同理,*-devel 库只支持编译,并不支持运行。
[*]安装(3步曲)
#配置构建环境
./configure --prefix=/usr/local/nginx \
            --with-http_ssl_module

#编译
make

#安装
sudo make install./configure --help 查看./configure 支持哪些参数

[*]--with-xxx_xxx:#表示默认不安装该模块。如果需要安装,则添加到 ./configure 参数中;
[*]--without-xxx_xxx: #表示默认会安装该模块。如果不需要安装,则添加到 ./configure 参数中。
[*]--prefix    #指定了Nginx的安装目录;
[*]--with-http_ssl_module    #启用 SSL 支持,确保Nginx编译时包含SSL模块;
[*]--with-http_stub_status_module    #启用Nginx状态信息模块(监控 Nginx 的性能和健康状态)

[*]验证
#启动验证
sudo /usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -version
[*]卸载
sudo rm -rf /usr/local/nginx
#编译安装只需清理编译目录即可,即删除 --prefix 参数目录。    更多配置内容查看这篇:《Nginx 配置与实战》
1.2 升级(安装缺失模块)


[*]检查已安装模块,命令:/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
[*]安装模块
由于已经安装了 --with-http_ssl_module 模块,所以以安装 --with-http_stub_status_module 为例:
#1. 进入安装包 nginx-1.24.0 目录
cd nginx-1.24.0

#2. 重新配置构建环境
./configure --prefix=/usr/local/nginx \
            --with-http_ssl_module \
            --with-http_stub_status_module

#3. 编译(make成功后,不用make install,否则nginx会重新安装,会将原来的配置覆盖)
make如果不知道需要哪些模块,可以按照官方yum安装的模块,基本能满足95%以上的生产需求。yum安装模块如下,可自行参考:
./configure --prefix=/usr/local/nginx --user=$USER --group=$(id -gn) --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --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-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --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执行后,nginx-1.24.0/objs 目录下会重新生成一个 nginx 文件,这个就是新版本的程序了。
为了清晰体现“新nginx”文件位置,以下操作均在 nginx-1.24.0 上级目录操作
查看新nginx的编译模块: nginx-1.24.0/objs/nginx -V
nginx version: nginx/1.24.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module检查模块无误后更新
#1. 停止 nginx 服务
sudo /usr/local/nginx/sbin/nginx -s stop

#2. 覆盖旧 nginx 启动文件
mv nginx-1.24.0/objs/nginx /usr/local/nginx/sbin/nginx

#3. 启动测试
sudo /usr/local/nginx/sbin/nginx
1.3 常用命令


[*]软链接(可选)
#查看 PATH 环境
echo $PATH

#创建软链接
sudo ls -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

#测试软链接
nginx -version
sudo nginx
sudo nginx -s stop点击此处查看更多软链接介绍
[*]服务操作
nginx -s reload#热重启:向主进程发送信号,重新加载配置文件。
nginx -s reopen#重启 Nginx
nginx -s stop    #快速关闭
nginx -s quit    #等待工作进程处理完成后关闭
nginx -T         #查看当前 Nginx 最终的配置
nginx -t         #检查配置是否有问题
1.4 Nginx 开机自启

1.4.1 Centos 6.x 配置


[*]新建 nginx 服务脚本文件(官方)
#1. 新建 nginx 文件
sudo touch /etc/init.d/nginx

#2. 设置文件权限
sudo chmod a+x /etc/init.d/nginx

#3. 配置文件脚本
sudo tee /etc/init.d/nginx <<-EOF
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:   /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

#根据实际情况,修改nginx的安装路径和配置文件
nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

prog=$(basename $nginx)
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
    useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
    if [ `echo $opt | grep '.*-temp-path'` ]; then
      value=`echo $opt | cut -d "=" -f 2`
      if [ ! -d "$value" ]; then
            # echo "creating" $value
            mkdir -p $value && chown -R $user $value
      fi
    fi
done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case "$1" in
    start)
      rh_status_q && exit 0
      $1
      ;;
    stop)
      rh_status_q || exit 0
      $1
      ;;
    restart|configtest)
      $1
      ;;
    reload)
      rh_status_q || exit 7
      $1
      ;;
    force-reload)
      force_reload
      ;;
    status)
      rh_status
      ;;
    condrestart|try-restart)
      rh_status_q || exit 0
            ;;
    *)
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
      exit 2
esac
EOF此脚本是nginx官方提供的,地址:http://wiki.nginx.org/RedHatNginxInitScript
注意:如果是自定义安装的nginx, 修改根据实际情况修改命令程序路径和配置文件路径。
[*]服务脚本命令
/etc/init.d/nginx start      #启动服务
/etc/init.d/nginx stop       #停止服务
/etc/init.d/nginx restart    #重启服务
/etc/init.d/nginx status   #查看服务的状态
/etc/init.d/nginx reload   #刷新配置文件
[*]系统管理(chkconfig)
上面的方法完成了用脚本管理nginx服务的功能,但是还是不太方便,比如要设置nginx开机启动等。
这个时候我们可以使用chkconfig来进行管理。
#1. 将nginx服务加入chkconfig管理列表
sudo chkconfig --add /etc/init.d/nginx

#设置终端模式开机启动
sudo chkconfig nginx on

#2. 服务管理
service nginx start   #启动服务
service nginx stop      #停止服务
service nginx restart   #重启服务
service nginx status    #查询服务的状态
service nginx relaod    #刷新配置文
1.4.2 Centos 7.x 配置

<ol>新建 nginx 服务文件
#1. 新建 nginx 文件sudo touch /lib/systemd/system/nginx#2. 配置文件脚本sudo tee /lib/systemd/system/nginx
页: [1]
查看完整版本: Nginx 离线安装与介绍