扩展知识:
1)设置nginx开机自动启动,将nginx启动命令添加到/etc/rc.local
[root@test01 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local
[root@test01 ~]# cat /etc/rc.local | grep nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
2)通过设置System V 脚本,使用server命令启动nginx服务
[root@test01 init.d]# vim nginx //在/etc/init.d/下创建nginx启动脚本文件
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
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 "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@test01 init.d]# chmod 755 nginx //修改脚本文件nginx的权限
[root@test01 init.d]# chkconfig --add nginx //将脚本文件加入chkconfig中
[root@test01 init.d]# chkconfig --level 35 nginx on //设置nginx开机在3和5级别自动启动
[root@test01 init.d]# chkconfig --list | grep nginx
nginx 0:off 1:off 2:off 3:on 4:off 5:on 6:off
测试nginx脚本文件是否能够正常使用
[root@test01 init.d]# /etc/init.d/nginx restart //重新启动
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@test01 init.d]# /etc/init.d/nginx reload //不间断服务平滑重启
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Reloading nginx: [ OK ]
[root@test01 ~]# cat /usr/local/nginx/logs/nginx.pid //存储了nginx运行的进程号
15799
[root@test01 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` //不间断服务重新启动nginx
[root@test01 init.d]# /etc/init.d/nginx stop
Stopping nginx: [ OK ]
[root@test01 init.d]# killall -9 nginx //结束nginx的全部经常
[root@test01 html]# pwd //安装nginx完成后,默认网站的路径
/usr/local/nginx/html
[root@test01 html]# ll //可以查看到默认网站为index.html,内容为以上测试内容。
total 8
-rw-r--r--. 1 root root 537 Feb 27 11:40 50x.html
-rw-r--r--. 1 root root 612 Feb 27 11:40 index.html
