一、看看/etc/inid.d下面有没有Nginx文件,没有就创建 vi /etc/inid.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 注意红色加粗部分,需要将路径改为自己机器的相应路径。 接着,设置文件的访问权限: chmod a+x /etc/init.d/Nginx (a+x参数表示 ==> all user can execute 所有用户可执行) 二、建立服务文件 vi /lib/systemd/system/Nginx.service 内容如下: [Unit] Description=Nginx After=network.target [Service] Type=forking ExecStart=/etc/init.d/Nginx start ExecReload=/etc/init.d/Nginx restart ExecStop=/etc/init.d/Nginx stop PrivateTmp=true [Install] WantedBy=multi-user.target 三、 设置开机自启动 执行命令: systemctl enable Nginx.service 参考文章: http://blog.163.com/qsc0624@126/blog/static/140324073201312734548701/ https://yq.aliyun.com/articles/66323
原文链接:https://www.f2er.com/centos/377063.html