0
点赞
收藏
分享

微信扫一扫

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


第一步:登录官网,查看最新的稳定版本,官网的地址:​​https://nginx.org/en/download.html​​

第二步:将下载的文件,通过winscp放到服务器的目录下

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


第三步:解压安装文件,tar -zxvf nginx-1.14.0.tar.gz

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


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

安装需要执行以下的步骤

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


第五步:执行编译的命令,./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,并配置文件服务器_nginx_04


命令为:yum install -y zlib-devel

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


命令为:yum -y install openssl openssl-devel

注:为了将来扩展https的接口,–with-http_ssl_module 需要带上。而–with-http_stub_status_module 添加监控状态配置。

第六步:启动nginx

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


第六步:将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,并配置文件服务器_nginx_07

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

7.2 配置DNS解析

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


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 查看是否成功。


举报

相关推荐

0 条评论