linux如何配置nginx全局变量

前段时间安装了nginx, 命令需要去sbin目录执行,比较麻烦,设置下全局命令,就无需进入nginx的sbin目录执行nginx命令了,过程如下:

1.创建文件

vim /etc/init.d/nginx

2.把下面的代码粘贴进去

  1 #!/bin/sh
  2 # nginx - this script starts and stops the nginx daemin
  3 #
  4 # chkconfig:   - 85 15
  5 
  6 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse 
  7 #               proxy and IMAP/POP3 proxy server
  8 
  9 # processname: nginx
 10 # config:      /usr/local/nginx/conf/nginx.conf
 11 # pidfile:     /usr/local/nginx/logs/nginx.pid
 12 
 13 # Source function library.
 14 
 15 . /etc/rc.d/init.d/functions
 16 
 17 # Source networking configuration.
 18 
 19 . /etc/sysconfig/network
 20 
 21 # Check that networking is up.
 22 
 23 [ "$NETWORKING" = "no" ] && exit 0
 24 
 25 nginx="/usr/local/nginx/sbin/nginx"
 26 
 27 prog=$(basename $nginx)
 28 
 29 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
 30 
 31 lockfile=/var/lock/subsys/nginx
 32 
 33 start() {
 34 
 35     [ -x $nginx ] || exit 5
 36 
 37     [ -f $NGINX_CONF_FILE ] || exit 6
 38 
 39     echo -n $"Starting $prog: "
 40 
 41     daemon $nginx -c $NGINX_CONF_FILE
 42 
 43     retval=$?
 44 
 45     echo
 46 
 47     [ $retval -eq 0 ] && touch $lockfile
 48 
 49     return $retval
 50 
 51 }
 52 
 53 
 54 stop() {
 55 
 56     echo -n $"Stopping $prog: "
 57 
 58     killproc $prog -QUIT
 59 
 60     retval=$?
 61 
 62     echo
 63 
 64     [ $retval -eq 0 ] && rm -f $lockfile
 65 
 66     return $retval
 67 
 68 }
 69 
 70 
 71 
 72 restart() {
 73 
 74     configtest || return $?
 75 
 76     stop
 77 
 78     start
 79 
 80 }
 81 
 82 
 83 reload() {
 84 
 85     configtest || return $?
 86 
 87     echo -n $"Reloading $prog: "
 88 
 89     killproc $nginx -HUP
 90 
 91     RETVAL=$?
 92 
 93     echo
 94 
 95 }
 96 
 97 force_reload() {
 98 
 99     restart
100 
101 }
102 
103 
104 configtest() {
105 
106   $nginx -t -c $NGINX_CONF_FILE
107 
108 }
109 
110 
111 
112 rh_status() {
113 
114     status $prog
115 
116 }
117 
118 
119 rh_status_q() {
120 
121     rh_status >/dev/null 2>&1
122 
123 }
124 
125 case "$1" in
126 
127     start)
128 
129         rh_status_q && exit 0
130         $1
131         ;;
132 
133     stop)
134 
135 
136         rh_status_q || exit 0
137         $1
138         ;;
139 
140     restart|configtest)
141         $1
142         ;;
143 
144     reload)
145         rh_status_q || exit 7
146         $1
147         ;;
148 
149 
150     force-reload)
151         force_reload
152         ;;
153     status)
154         rh_status
155         ;;
156 
157 
158     condrestart|try-restart)
159 
160         rh_status_q || exit 0
161             ;;
162 
163     *)
164 
165         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
166         exit 2
167 
168 esac

3.进入目录

cd /etc/init.d

执行

chmod 755 /etc/init.d/nginx

chkconfig --add nginx

4. 错误处理

现在执行nginx -s reload还会出现错误,显示

 -bash: nginx: command not found

接下来进入

vim /etc/profile,如下所述

添加这两行代码

PATH=$PATH:/home/nginx/sbin  #安装nginx的sbin目录
export PATHi

 执行:source /etc/profile           让配置文件重新生效一下即可

 没有报错,说明ok了

原文地址:https://www.cnblogs.com/fuzhengyi/p/14318255.html