「我的記錄,確定有理解錯誤和理解不透的地方,小白老是有個成長的過程,但願你們多多指教。」 nginx
*書接上文:CentOS 7 安裝Nginx-1.9.2 web
*Nginx 安裝完後,開啓,關閉的操做實在是長,所以…… shell
傳送門:http://my.oschina.net/liucao/blog/470458 工具
二、乾貨: this
相信看了準備活動,基本上就會了,這個比起下面那個方法實在是簡單多了直接上指令: spa
$ sudo vi /usr/lib/systemd/system/nginx.service #輸入下面內容,並保存 [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 PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
#修改權限 $ sudo chmod +x /usr/lib/systemd/system/nginx.service #/etc/systemd/system/multi-user.target.wants/ $ sudo systemctl enable nginx.service #如今可使用下面的指令來控制nginx啦 $ sudo systemctl start nginx.service $ sudo systemctl reload nginx.service $ sudo systemctl restart nginx.service $ sudo systemctl stop nginx.service #日誌 $ journalctl -f -u nginx.service
注意上面的幾個路徑 .net
#下面這幾個路徑是你的nginx安裝的目錄,務必不要弄錯。 PIDFile=/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
$ vi /etc/init.d/nginx輸入如下內容(下面這段腳本網上不少,直接拷貝 ):
#!/bin/sh # # nginx - this script starts and stops the nginx daemin # # 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 # pidfile: /run/nginx/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" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" lockfile=/var/lock/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 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 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
注意幾個地方的配置,就是上在nginx中編譯時設置的那些目錄: unix
# config: /etc/nginx/nginx.conf
# pidfile: /run/nginx/nginx.pid
nginx="/usr/sbin/nginx"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx.lock
$ chmod a+x /etc/init.d/nginx $ chkconfig --add nginx $ chkconfig --list nginx nginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off
上面其實就是在/etc/rc.d/rc5.d/目錄下建立了一個連接。以下: rest
$ cd /etc/rc.d/rc5.d/ $ ll |grep nginx lrwxrwxrwx. 1 root root 15 6月 24 16:14 K15nginx -> ../init.d/nginx
$ service nginx start $ service nginx stop $ service nginx restart $ service nginx reload $ /etc/init.d/nginx start $ /etc/init.d/nginx stop $ /etc/init.d/nginx restart $ /etc/init.d/nginx reload這樣的操做就簡單多了。
若是有下面的錯誤提示,去看看目錄是否在該文件,若是實在是存在,且腳本無錯,則考慮文件的格式是否是正確,網上有用工具轉的好比:doc2unix 。 日誌
env: /etc/init.d/nginx: 沒有那個文件或目錄
個人作法是直接複製下內容:
$ rm /etc/init.d/nginx $ vi /etc/init.d/nginx再把內容複製進去,保存,
$ chmod a+x /etc/init.d/nginx記得修改權限。