天天看點

阿裡雲centos 安裝nginx,并配置檔案伺服器

第一步:登入官網,檢視最新的穩定版本,官網的位址:​​https://nginx.org/en/download.html​​

第二步:将下載下傳的檔案,通過winscp放到伺服器的目錄下

阿裡雲centos 安裝nginx,并配置檔案伺服器

第三步:解壓安裝檔案,tar -zxvf nginx-1.14.0.tar.gz

阿裡雲centos 安裝nginx,并配置檔案伺服器

第四步:安裝nginx之前需要安裝pcre,pcre讓nginx有了rewrite功能。

安裝需要執行以下的步驟

阿裡雲centos 安裝nginx,并配置檔案伺服器

第五步:執行編譯的指令,./configure –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module –with-pcre=/usr/local/src/pcre-8.35

如果編譯過程中出現這兩個錯誤,請執行相應的指令,然後再執行編譯的指令

阿裡雲centos 安裝nginx,并配置檔案伺服器

指令為:yum install -y zlib-devel

阿裡雲centos 安裝nginx,并配置檔案伺服器

指令為:yum -y install openssl openssl-devel

注:為了将來擴充https的接口,–with-http_ssl_module 需要帶上。而–with-http_stub_status_module 添加監控狀态配置。

第六步:啟動nginx

阿裡雲centos 安裝nginx,并配置檔案伺服器

第六步:将nginx做成服務,并且設定為開啟自啟動

6.1 建立執行檔案

touch /etc/init.d/nginx

6.2 nginx裡面的内容修改,執行和配置檔案部分,原檔案來自官網​​https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/​​

#!/bin/sh

#nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  NGINX is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -n "$user" ]; then
      if [ -z "`grep $user ]; then
         useradd -M -s /bin/nologin $user
      fi
      options=`$nginx -V 2>&1 | grep 'configure arguments:'`
      for opt in $options; do
          if [ `echo $opt | grep '.*-temp-path'` ]; then
              value=`echo $opt | cut -d "=" -f 2`
              if [ ! -d "$value" ]; then
                  # echo "creating" $value
                  mkdir -p $value && chown -R $user $value
              fi
          fi
       done
    fi
}

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0
        exit 2
esac      

6.3 付給執行權限,chomd a+x /etc/init.d/nginx

6.4 通過service來管理,chkconfig –add /etc/init.d/nginx

6.5 設定開機自啟動,chkconfig nginx on

注:service nginx restart 這個是重新開機指令,service nginx reload 這是重新加載指令,改完配置檔案使用這個指令,不是restart。

第七步、配置檔案伺服器

7.1 因為是檔案伺服器,是以要走CDN,是以開始CDN的配置

阿裡雲centos 安裝nginx,并配置檔案伺服器
阿裡雲centos 安裝nginx,并配置檔案伺服器

7.2 配置DNS解析

阿裡雲centos 安裝nginx,并配置檔案伺服器
server{
        client_max_body_size 4G;
        listen 80;
        server_name img.static.geexek.com;
        root /geexek/nginx/nginx_data;
        location /files {
                autoindex on;
                autoindex_exact_size on;
                autoindex_localtime on;
        }
        location ~* \.(eot|ttf|ttc|otf|eot|woff|woff2|svg)$ {
                 add_header Access-Control-Allow-Origin *;
        }
}      

繼續閱讀