Transferred from: http://binyan17.iteye.com/blog/1688308
The following code is on the basis of predecessors, combined with the actual situation of their own server modification, my server environment is: CentOS 6.3
1. Create a startup script,
Root user executes Vi/etc/init.d/nginx and saves the following script
#!/bin/SH# # Nginx-This script starts and stops the Nginx Daemon # # Chkconfig:- - the# Description:nginx is an HTTP (s) server, HTTP (s) reverse # Proxy and IMAP/POP3 Proxy Server # processname:nginx # chkconfig:2345 - the# Description:nginx Web server# processname:nginx# config:/opt/nginx/conf/nginx.conf# pidfile:/opt/nginx/nginx.pid# SourcefunctionLibrary:/etc/init.d/functions# Source Networking configuration:/etc/sysconfig/Networkif[-f/etc/sysconfig/nginx]; Then. /etc/sysconfig/Nginxfi# Check that networking are up. [ "$NETWORKING"="No"] && exit0Nginx="/opt/nginx/sbin/nginx"Prog=$(basename$nginx) Nginx_conf_file="/opt/nginx/conf/nginx.conf"[ -f/etc/sysconfig/nginx] &&. /etc/sysconfig/NginxLockfile=/var/lock/subsys/Nginxstart () {[-X $nginx] | | Exit5 [ -F $NGINX _conf_file] | | Exit6 Echo-N $"starting $prog:"Daemon $nginx #-c $NGINX _conf_file retval=$?Echo[$retval-eq0] &&Touch$Lockfilereturn $retval}stop () {Echo-N $"stopping $prog:"Killproc $prog-QUIT retval=$?Echo[$retval-eq0] &&RM-F $Lockfilereturn $retvalKillall-9Nginx}restart () {configtest|| Return $?StopSleep 1start}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 " $" inchstart) Rh_status_q&& exit0 $1;; stop) Rh_status_q|| Exit0 $1;; restart) $1;; test) configtest;; Reload) Rh_status_q|| Exit7 $1;; force-reload) force_reload;; status) Rh_status;; Condrestart|try-restart) rh_status_q|| Exit0 ;; *) Echo$"Usage: $ {start|stop|status|restart|condrestart|try-restart|reload|force-reload|test}"Exit2 Esac
2. Add permissions for Nginx scripts
chmod A+x/etc/init.d/nginx
3. Add Nginx Service
Chkconfig--add nginxchkconfig nginx on
4. Testing
Service Nginx startservice nginx stop service Nginx Reload
Go Nginx system service configuration script in Redhat