centos7下安装nginx和设置开机启动

首先安装一些必须的库

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

安装 PCRE

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

安装nginx

##获取nginx最新的安装包
wget http://nginx.org/download/nginx-1.11.10.tar.gz
##解压缩
tar zxvf nginx-1.11.10.tar.gz
##进入目录
cd nginx-1.11.10
##检测系统配置, 生成make相关文件
./configure
编译并安装
            make && make install
创建nginx启动命令脚本
            vi /etc/init.d/nginx
插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径 (这段是从网上copy的)
            #! /bin/bash
            # chkconfig: - 85 15
            PATH=/usr/local/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
设置执行权限
            chmod a+x /etc/init.d/nginx
注册成服务
            chkconfig --add nginx
设置开机启动
            chkconfig nginx on
重启, 查看nginx服务是否自动启动
            shutdown -h 0 -r
            netstat -apn|grep nginx
对nginx服务执行停止/启动/重新读取配置文件操作
            #启动nginx服务
            systemctl start nginx.service
            #停止nginx服务
            systemctl stop nginx.service
            #重启nginx服务
            systemctl restart nginx.service
            #重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
            systemctl reload nginx.service
文章来源: centos7下安装nginx和设置开机启动

人吐槽 人点赞

猜你喜欢

发表评论

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:centos7下安装nginx和设置开机启动