nginx 文件服务器部署

系统环境:CentOS Linux release 7.9.2009 (Core)
本文适用于 centos、redhat(linux发行版)

1. nginx 源码安装脚本

#!/usr/bin/env bash

###################################################################################
# 控制台颜色
BLACK="\033[1;30m"
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
PURPLE="\033[1;35m"
CYAN="\033[1;36m"
RESET="$(tput sgr0)"
###################################################################################

printf "${BLUE}\n"
cat << EOF
###################################################################################
# 采用编译方式安装 Nginx, 并将其注册为 systemd 服务
# 默认下载安装 1.20.1 版本,安装路径为:/usr/local/nginx
# @system: 适用于 CentOS7+
# @author: Zhang Peng
###################################################################################
EOF
printf "${RESET}\n"

command -v yum > /dev/null 2>&1 || {
	printf "${RED}Require yum but it's not installed.${RESET}\n";
	exit 1;
}

printf "\n${GREEN}>>>>>>>> install nginx begin${RESET}\n"

if [[ $# -lt 1 ]] || [[ $# -lt 2 ]]; then
	printf "${PURPLE}[Hint]\n"
	printf "\t Usage: sh nginx-install.sh [version] \n"
	printf "\t Default: sh nginx-install.sh 1.20.1\n"
	printf "\t Example: sh nginx-install.sh 1.20.1 \n"
	printf "${RESET}\n"
fi

temp=/opt/nginx
version=1.20.1
if [[ -n $1 ]]; then
	version=$1
fi

# install info
printf "${PURPLE}[Install Info]\n"
printf "\t version = ${version}\n"
printf "${RESET}\n"

printf "${CYAN}>>>> install required libs${RESET}\n"
yum -y install pcre pcre-devel openssl openssl-devel zlib zlib-devel  gcc-c++ make wget &> /dev/null; if [ $? -eq 0 ];then echo "install completed"; else echo "install fail" ;fi

# download and decompression
printf "${CYAN}>>>> download nginx${RESET}\n"
mkdir -p ${temp}
wget -O ${temp}/nginx-${version}.tar.gz http://nginx.org/download/nginx-${version}.tar.gz
tar zxf ${temp}/nginx-${version}.tar.gz -C ${temp}

# configure and makefile
printf "${CYAN}>>>> compile nginx${RESET}\n"
cd ${temp}/nginx-${version}
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make && make install &> /dev/null; if [ $? -eq 0 ];then echo "compile  completed"; else echo "compile fail" ;fi
rm -rf ${temp}
cd -

# setting systemd service
printf "${CYAN}>>>> set nginx as a systemd service${RESET}\n"
wget -N https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/config/nginx/nginx.service -O /usr/lib/systemd/system/nginx.service
chmod +x /usr/lib/systemd/system/nginx.service

# boot nginx
printf "${CYAN}>>>> start nginx${RESET}\n"
systemctl enable nginx.service
systemctl start nginx.service

printf "\n${GREEN}<<<<<<<< install nginx end${RESET}\n"
printf "\n${PURPLE}nginx service status: ${RESET}\n"
systemctl status nginx

2 更改nginx配置文件

按照第一步脚本安装nginx后,默认nginx配置文件在:/usr/local/nginx/conf/nginx.conf
修改 http 下的 server 内容
源内容

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

修改后内容

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root /opt/packages;       # 指定访问主机目录
            autoindex on;             # 开启目录浏览功能
            autoindex_exact_size on;  # 开启详细文件大小统计,让文件大小显示MB,GB单位,默认为b
            autoindex_localtime on;   # 开启以服务器本地时区显示文件修改日期!  
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

3.效果

在这里插入图片描述

nginx常用命令

nginx –s stop  停止nginx的服务 
nginx –s reload  不停止nginx的服务,重新加载配置文件。
nginx –t  检测配置文件是否有错误。
nginx -s quit 关闭nginx,完整有序的停止nginx,保存相关信息
nginx -V 除版本信息外还显示配置参数信息(大写字母V)
nginx -s reopen 重新打开日志文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值