linux 安装 nginx,并且注册成一个服务
原创
©著作权归作者所有:来自51CTO博客作者sky_nan的原创作品,请联系作者获取转载授权,否则将追究法律责任
* 一键安装四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
* 安装 nginx【如果报:-bash: wget: command not found,先安装 wget:yum -y install wget】
$ cd /usr/local/
$ wget http://nginx.org/download/nginx-1.10.3.tar.gz
$ tar -zxvf nginx-1.10.3.tar.gz
$ cd nginx-1.10.3
$ ./configure
$ make && make install
* 启动 nginx
/usr/local/nginx/sbin/nginx
* 查看是否启动:
ps aux | grep nginx //能看到 nginx 进程表示启动成功
或者直接访问:
看到 nginx 欢迎页的 html 代码表示成功
* nginx 的启动停止重启操作:
启动:/usr/local/nginx/sbin/nginx
停止:/usr/local/nginx/sbin/nginx -s stop
重启:/usr/local/nginx/sbin/nginx -s reload
* 设置 nginx 开机自启
vi /etc/rc.local
底部加:
/usr/local/nginx/sbin/nginx
* 把 nginx 配置成一个服务:
* 服务脚本:
复制代码
#!/bin/sh
#chkconfig: - 85 15
PATH=/usr/local/nginx/sbin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
复制代码
保存后赋予权限:
添加服务:
重开一下终端窗口,然后再测试:(有时候要等一会才生效,具体原因不明)
启动:systemctl start nginx.service
停止:systemctl stop nginx.service
重启:systemctl restart nginx.service