ubuntu设置nginx为系统服务,ubuntunginx


先编译安装nginx

./configure 
--sbin-path=/usr/local/nginx/nginx   //默认的路径是 --prefix/sbin/nginx
--conf-path=/usr/local/nginx/nginx.conf  
--pid-path=/usr/local/nginx/nginx.pid  
--with-http_ssl_module  
--with-pcre=/usr/local/src/pcre-8.37  
make
make install

进入/etc/init.d目录下创建nginx脚本

#!/bin/sh

### BEGIN INIT INFO
# Provides:    nginx
# Required-Start:
# Required-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:        0 1 6
# Short-Description: nginx
# Description: nginx server
### END INIT INFO

#. /lib/lsb/init-functions

PROGRAM=/usr/local/nginx/nginx


test -x $PROGRAM || exit 0

case "$1" in
  start)
    log_begin_msg "Starting Nginx server"
    /usr/local/nginx/nginx
    log_end_msg 0
    ;;
  stop)
    PID=`cat /usr/local/nginx/nginx.pid`
    log_begin_msg "Stopping Nginx server"
    if [ ! -z "$PID" ]; then
        kill -15 $PID
    fi
    log_end_msg 0
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    log_success_msg "Usage: service nginx {start|stop|restart}"
    exit 1
esac

exit 0

然后运行下面的命令:
sudo chmod +x nginx
sudo update-rc.d nginx defaults
现在可以使用下面的命令了,重新启动nginx会自动启动
sudo service nginx start
sudo service nginx stop

相关内容

    暂无相关文章