阿里云centos 安装nginx,并配置文件服务器

第一步:登录官网,查看最新的稳定版本,官网的地址:https://nginx.org/en/download.html
第二步:将下载的文件,通过winscp放到服务器的目录下
这里写图片描述
第三步:解压安装文件,tar -zxvf nginx-1.14.0.tar.gz
这里写图片描述
第四步:安装nginx之前需要安装pcre,pcre让nginx有了rewrite功能。
安装需要执行以下的步骤
这里写图片描述
第五步:执行编译的命令,./configure –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module –with-pcre=/usr/local/src/pcre-8.35
如果编译过程中出现这两个错误,请执行相应的命令,然后再执行编译的命令
这里写图片描述
命令为:yum install -y zlib-devel
这里写图片描述
命令为:yum -y install openssl openssl-devel
注:为了将来扩展https的接口,–with-http_ssl_module 需要带上。而–with-http_stub_status_module 添加监控状态配置。
第六步:启动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 /etc/passwd`" ]; 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 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        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的配置
这里写图片描述

这里写图片描述

7.2 配置DNS解析
这里写图片描述
7.3 修改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 *;
        }
}

7.5访问img.static.geexek.com 查看是否成功。

猜你喜欢

转载自blog.csdn.net/sunyuhua_keyboard/article/details/80940846
今日推荐