centos7 配置nginx服務、開機自動啓動

環境:

centos七、nginx 1.10.1nginx

在以上都已經安裝完成,能正常啓動以後web

配置服務:

centos7 的服務管理 與 centos6 發生了一些變化 ,此處參考 http://www.ebanban.com/?p=476vim

啓動、中止、重啓、重載、檢查服務:
6: service httpd start|stop|restart|reload|status
7: systemctl start|stop|restart|reload|status httpd.servicecentos

容許、禁止服務自啓動:
6: chkconfig httpd on|off
7: system enable|disable httpd.servicebash

列出服務:
6: chkconfig –list
7: systemctl list-unit-files –type=service 或 ls /etc/systemd/system/*.wants/centos7

添加服務:
6: chkconfig httpd –add
7: systemctl daemon-reloadspa

能夠先經過上述命令查詢是否已經配置nginx服務,若是沒有rest

centos7:code

服務文件orm

#systemd 下的 system 或 usr 建立 nginx.service 文件
vim /usr/lib/systemd/system/nginx.service 

#nginx服務配置到該文件中
#服務描述性的配置
[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
#pid文件位置 
#要與nginx配置文件中的pid配置路徑一致,這個很重要,不然會服務啓動失敗
PIDFile=/var/run/nginx.pid
#啓動前檢測 nginx配置文件 是否正確
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#啓動
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#重啓
ExecReload=/bin/kill -s HUP $MAINPID
#關閉
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

啓動服務

systemctl start nginx.service

設置開機自啓

systemctl enable nginx.service

centos6:

在 /etc/init.d 中新增 nginx 文件,內容以下

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# 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=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/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} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/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
exit $RETVAL

而後設置自啓

#添加服務
chkconfig --add nginx
chkconfig nginx on

啓動服務

service nginx start
相關文章
相關標籤/搜索