Nginx安装及配置

创建nginx管理用户

先添加应用工作组,再创建nginx工作目录

# 创建app用户组ID为6666 
groupadd -g 6666 app 
# 添加nginx用户指定工作目录为/app/nginx 
useradd -g app -u 6660 nginx -d /app/nginx

 

下载nginx

在等待下载文件的过程中可以先配置后面的快捷命令和环境变量

wget http://nginx.org/download/nginx-1.23.1.tar.gz -O nginx-1.23.1.tar.gz
# 解压压缩包

tar -zxf nginx-1.23.1.tar.gz

 

下载pcre2

下载文件并保存为pcre2-10.40.tar.gz

wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.40/pcre2-10.40.tar.gz -O pcre2-10.40.tar.gz
# 解压压缩包
tar -zxf pcre2-10.40.tar.gz

 

 

下载zlib

下载文件并保存为zlib-1.2.12.tar.gz

wget http://www.zlib.net/zlib-1.2.12.tar.gz -O zlib-1.2.12.tar.gz
# 解压压缩包
tar -zxf zlib-1.2.12.tar.gz

 

 

下载openssl

下载文件并保存为openssl-3.0.5.tar.gz

wget http://artfiles.org/openssl.org/source/openssl-3.0.5.tar.gz -O openssl-3.0.5.tar.gz
# 解压压缩包
tar -zxf openssl-3.0.5.tar.gz

 

 

配置nginx编译参数

主要调整--prefix=path即可

cd nginx-1.23.1
# 配置编译参数
./configure \
--prefix=/app/nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-mail=dynamic \
--with-stream \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-pcre=../pcre2-10.40 \
--with-zlib=../zlib-1.2.12 \
--with-openssl=../openssl-1.1.1q
# 执行编译和安装
make && make install

 

 

配置快捷命令

配置sudo权限

# 在/etc/sudoers.d/路径添加nginx命令文件
cd /etc/sudoers.d/ && vi nginx
# 添加如下内容后保存退出
nginx ALL= NOPASSWD:/app/nginx/sbin/run.sh
nginx ALL= NOPASSWD:/app/nginx/sbin/nginx -s reload
nginx ALL= NOPASSWD:/app/nginx/sbin/nginx -t

 

 

添加nginx管理脚本

在/app/nginx/sbin/目录下添加run.sh脚本,在/app/nginx/目录下添加nginx.sh脚本

#在/app/nginx/sbin/目录下添加run.sh脚本
cd /app/nginx/sbin/ && touch run.sh && chmod a+x run.sh

# 将下面内容完整复制到run.sh中

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by jackbillow at 2007.10.15.
# it is v.0.0.2 version.
# if you find any errors on this scripts,please contact jackbillow.
# and send mail to jackbillow at gmail dot com.
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

nginxd=/app/nginx/sbin/nginx
nginx_config=/app/nginx/conf/nginx.conf
nginx_pid=/app/nginx/logs/nginx.pid

RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING}x = "no"x ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.
start() {

if [ -e $nginx_pid ];then
   status $prog | grep pid
   RETVAL=$?
   if [ $RETVAL != 0 ];then
        status $prog
   else
        echo "nginx already running...."
        exit 1
   fi
fi

   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch $nginx_pid
   return $RETVAL

}

# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f $nginx_pid
}

# reload nginx service functions.
reload() {

    echo -n $"Reloading $prog: "
    #kill -HUP cat ${nginx_pid}
    killproc $nginxd -HUP
    RETVAL=$?
    echo

}

# See how we were called.
case "$1" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac

# 在 /app/nginx/ 目录 添加nginx.sh 文件
cd /app/nginx/ && touch nginx.sh && chmod a+x nginx.sh

# 将下面内容完整复制到nginx.sh文件中
#!/bin/bash
# Start nginx daemons functions.
start() {
        sudo $NGX_HOME/sbin/run.sh start
}

# Stop nginx daemons functions.
stop() {
        sudo $NGX_HOME/sbin/run.sh stop
}

# reload nginx service functions.
reload() {
        sudo $NGX_HOME/sbin/run.sh reload
}

#
status() { 
       sudo $NGX_HOME/sbin/run.sh status
}
#
check() { 
       sudo $NGX_HOME/sbin/nginx -t
}

# See how we were called.
case "$1" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

status)
        status
        ;;
check)
        check
        ;;
*)
        echo $"Usage: {start|stop|restart|reload|status|check|help}"
        exit 1
esac

 

添加环境变量

在.bash_profile中添加如下生效语句

# User specific environment and startup programs
if [ -f ~/.profile ]; then
. ~/.profile
fi

 

在 /app/nginx目录新建.profile文件,添加下面内容到.profile中

#命令设置
alias start="sh ~/nginx.sh start"
alias stop="sh ~/nginx.sh stop"
alias restart="sh ~/nginx.sh restart"
alias reload="sh ~/nginx.sh reload"
alias status="sh ~/nginx.sh status"
alias check="sh ~/nginx.sh check"
alias conf="/bin/vi ~/conf/nginx.conf"
export NGX_HOME="/app/nginx"
export NGINX_CONF="$NGX_HOME/conf/nginx.conf"
alias log="cd ~/logs"
alias l='ls -l'
alias lf='ls -F'
alias rm="hrm"
hrm() {
echo "警告:生产系统禁止使用rm命令!!!"
}

#系统环境变量
export PS1='\h:''$PWD>'
set -o vi

echo "#######################################"
echo "# Nginx快捷命令 #"
echo "#######################################"
echo "  start 启动nginx"
echo "   stop 关闭nginx"
echo "restart 重启nginx"
echo " reload 重新加载nginx"
echo " status 查看nginx运行状态"
echo "  check 检查nginx.conf配置是否正确"
echo "   conf 修改nginx配置文件nginx.conf"
echo "    log 跳转到nginx日志目录"
echo "#######################################"

 

添加环境变量后需要source .bash_profile进行生效,或者重新登录系统会自动生效

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录