Nginx源码包地址:http://nginx.org/download/nginx-1.20.1.tar.gz
🥇 参考文档:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
🥇 参考文档:https://cloud.tencent.com/developer/article/1619507
编译环境
1. OpenSSL库 yum install -y pcre pcre-devel openssl openssl-devel zlib zlib-devel 2. 编译软件 yum install -y gcc gcc-c++
编译安装nginx
1.创建用户及组 # useradd -s /bin/false -M nginx #-M(不创建主目录) -s(指定登入shell) groupaddd nginx #两种方法,选其中一个即可 useradd -g nginx nginx 2.解压缩并进入源码目录 tar -xzvf /root/nginx-1.20.1.tar.gz && cd nginx-1.20.1 3.配置编译环境 ./configure \ --prefix=/etc/nginx \ #nginx目录文件 --sbin-path=/usr/sbin/nginx \ #nginx启动文件 --conf-path=/etc/nginx/nginx.conf \ #nginx主配置文件 --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ #nginx用户 --group=nginx \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --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-mail \ --with-mail_ssl_module \ --with-stream \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module 4.编译:make 5.安装:make install 6.查看当前版本号:nginx -V 7.查看nginx安装的各个目录:whereis nginx nginx: /usr/sbin/nginx /etc/nginx
Nginx操作
开启nginx: /usr/sbin/nginx 关闭nginx: /usr/sbin/nginx -s stop 配置生效: /usr/sbin/nginx -s reload 配置文件目录:ls /etc/nginx/nginx.conf 启动目录:ls /sbin/nginx
配置nginx启动项
简单启动脚本
1. 创建启动目录:vim /sbin/nginx.sh #!/bin/bash #description #该脚本用户开启、关闭、重启nginx . /etc/rc.d/init.d/functions export NGINX_HOME=/usr/sbin case "$1" in start) $NGINX_HOME/nginx ;; stop) $NGINX_HOME/nginx -s stop ;; restart) $NGINX_HOME/nginx -s stop &>/dev/null sleep 1 $NGINX_HOME/nginx $NGINX_HOME/nginx -s reload ;; esac 2. 授权执行脚本:chmod +x /sbin/nginx.sh 3. 执行脚本:/sbin/nginx start #参数(start/stop/restart)
添加启动项
1. 创建目录:vim /etc/init.d/nginx -------------------------------------------------- #!/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="/usr/sbin/nginx" #填自己的nginx主目录 !!!!!! prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" #自己的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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then 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 fi } 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 $prog -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 ----------------------------------------------------------- 2. 授权:chmod a+x /etc/init.d/nginx 3. 添加开机启动项:chkconfig --add /etc/init.d/nginx 4. 开机自启: chkconfig nginx on 5. 查看状态:chkconfig --list ----------测试----------- systemctl start nginx systemctl status nginx systemctl stop nginx systemctl restart nginx
⛔注意:
- nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx执行程序的路径。
- NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。