环境:
centos7、nginx 1.10.1
在以上都已经安装完成,能正常启动之后
配置服务:
centos7 的服务管理 与 centos6 发生了一些变化 ,此处参考 http://www.ebanban.com/?p=476
启动、停止、重启、重载、检查服务:
6: service httpd start|stop|restart|reload|status
7: systemctl start|stop|restart|reload|status httpd.service允许、禁止服务自启动:
6: chkconfig httpd on|off
7: system enable|disable httpd.service列出服务:
6: chkconfig –list
7: systemctl list-unit-files –type=service 或 ls /etc/systemd/system/*.wants/添加服务:
6: chkconfig httpd –add
7: systemctl daemon-reload
可以先通过上述命令查询是否已经配置nginx服务,如果没有
centos7:
服务文件
#systemd 下的 system 或 usr 创建 nginx.service 文件
vim /usr/lib/systemd/system/nginx.service
#nginx服务配置到该文件中
#服务描述性的配置
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
#服务关键配置
[Service]
Type=forking
#pid文件位置
#要与nginx配置文件中的pid配置路径一致,这个很重要,否则会服务启动失败
PIDFile=/var/run/nginx.pid
#启动前检测 nginx配置文件 是否正确
ExecStartPre=/usr/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#启动
ExecStart=/usr/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#重启
ExecReload=/bin/kill -s HUP $MAINPID
#关闭
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
启动服务
systemctl start nginx.service
设置开机自启
systemctl enable nginx.service
centos6:
在 /etc/init.d 中新增 nginx 文件,内容如下
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
然后设置自启
#添加服务
chkconfig --add nginx
chkconfig nginx on
启动服务
service nginx start
来源:oschina
链接:https://my.oschina.net/u/2287173/blog/749134