下载源码包回来
编译安装。编译之前查看是否安装pcre-devel,openssl,openssl-devel。
装好之后进入nginx的目录下。默认是/usr/local/nginx
编辑一个shell脚本文件。vim /etc/init.d/nginx
1 #!/bin/bash 2 # nginx Startup script for the Nginx HTTP Server 3 # it is v.0.0.2 version. 4 # chkconfig: - 85 15 5 # description: Nginx is a high-performance web and proxy server. 6 # It has a lot of features, but it's not for everyone. 7 # processname: nginx 8 # pidfile: /var/run/nginx.pid 9 # config: /usr/local/nginx/conf/nginx.conf10 11 nginxd=/usr/local/nginx/sbin/nginx12 nginx_config=/usr/local/nginx/conf/nginx.conf13 nginx_pid=/var/run/nginx.pid14 RETVAL=015 prog="nginx"16 # Source function library.17 . /etc/rc.d/init.d/functions18 # Source networking configuration.19 . /etc/sysconfig/network20 # Check that networking is up.21 [ ${NETWORKING} = "no" ] && exit 022 [ -x $nginxd ] || exit 023 # Start nginx daemons functions.24 start() {25 if [ -e $nginx_pid ];then26 echo "nginx already running...."27 exit 128 fi29 echo -n $"Starting $prog: "30 daemon $nginxd -c ${nginx_config}31 RETVAL=$?32 echo33 [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx34 return $RETVAL35 }36 # Stop nginx daemons functions.37 stop() {38 echo -n $"Stopping $prog: "39 killproc $nginxd40 RETVAL=$?41 echo42 [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid43 }44 # reload nginx service functions.45 reload() {46 echo -n $"Reloading $prog: "47 #kill -HUP `cat ${nginx_pid}`48 killproc $nginxd -HUP49 RETVAL=$?50 echo51 }52 # See how we were called.53 case "$1" in54 start)55 start56 ;;57 stop)58 stop59 ;;60 reload)61 reload62 ;;63 restart)64 stop65 start66 ;;67 status)68 status $prog69 RETVAL=$?70 ;;71 *)72 echo $"Usage: $prog {start|stop|restart|reload|status|help}"73 exit 174 esac75 exit $RETVAL
保存之后,给定执行权限
1 chmod a+x /etc/init.d/nginx
设置开机启动
1 chkconfig /etc/init.d/nginx on
这样通过下面的方式就可以下了。
1 /etc/init.d/nginx start/stop/restart2 service nginx start/stop/restart