centos7.6安装nginx并设置开机自启

1、简单安装nginx-1.6版本

安装依赖:

yum -y install gcc pcre-devel zlib-devel openssl-devel

解压下载的nginx-1.6

tar  -zxvf  nginx-1.16.1.tar.gz

安装

cd   nginx-1.16.1

#安装时--prefix 指定路径,根据自己的需求来。

./configure --prefix=/www/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module

make  && make install

安装完成。

启动nginx

cd   /www/server/nginx/sbin

执行    ./nginx

或者:/www/server/nginx/sbin/nginx

关闭nginx:/www/server/nginx/sbin/nginx   -s   stop

2、设置nginx开机自启动

vim  /etc/init.d/nginx 

内容如下:

#! /bin/bash
# chkconfig: - 85 15
PATH=/www/server/nginx       ###这里是你安装的路径,替换成自己安装的路径
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/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

3、加入系统设置开机自启动

chmod +x /etc/rc.d/init.d/nginx (设置可执行权限)

chkconfig --add nginx (添加系统服务)

chkconfig --level 35 nginx on (开机自启动)

可以使用systemctl   start nginx  启动 

               systemctl   stop nginx   关闭

猜你喜欢

转载自www.cnblogs.com/sxshaolong/p/12766534.html