Linux(Ubuntu)下如何设置nginx开机自启动?

Nginx源码包安装成功后并修改好nginx.conf(还有设设置好fastcgi),本人安装在/usr/local/目录下,照这个进行。

首先在 /etc/init.d/目录下创建nginx脚本(也可采用终端输入 vi /etc/init.d/nginx)  ;然后存入以下内容:

#! /bin/sh
# nginx version 1.3.0
# Author: xxxxx
# Modified: xxxxxxxxxxxxxxxxx
# Modified: http://www.histriver.com
# Reproduced with express authorization from its contributors
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC=”nginx daemon”
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME

# If the daemon file is not found, terminate the script.
test -x $DAEMON || exit 0
d_start() {
$DAEMON || echo -n ” already running”
}
d_stop() {
$DAEMON –s quit || echo -n ” not running”
}
d_reload() {
$DAEMON –s reload || echo -n ” could not reload”
}
case “$1” in
start)
echo -n “Starting $DESC: $NAME”
d_start
echo “.”
;;
stop)
echo -n “Stopping $DESC: $NAME”
d_stop
echo “.”
;;
reload)
echo -n “Reloading $DESC configuration…”
d_reload
echo “reloaded.”
;;
restart)
echo -n “Restarting $DESC: $NAME”
d_stop
# Sleep for two seconds before starting again, this should give the
# Nginx daemon some time to perform a graceful stop.
sleep 2
d_start
echo “.”
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart|reload}” >&2
exit 3
;;
esac
exit 0

保存后然后打开终端 ,先获取超级用户权限(可参考本站文章“(linux)Ubuntu下如何获取超级权限”),然后输入:

[root@localhost root]# chmod  +x  /etc/init.d/nginx

[root@localhost root]# update-rc.d  -f  nginx  defaults

[root@localhost root]# ln -s /etc/init.d/nginx  S99nginx

大功告成,开机nginx即可自动启动。停止自动启动,终端输入:

[root@localhost root]# update-re.d  -f  nginx  remove

当然要想网站开机即可运行php文件,还需先运行php-fpm ,参考本站文章“Linux(Ubuntu)下如何设置php-fpm开机自启动?”)

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>