1. 程式人生 > >centos7下安裝nginx和設定開機啟動

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