学堂 学堂 学堂公众号手机端

使用nginx搭建简易的文件共享服务器

lewis 1年前 (2024-04-29) 阅读数 17 #技术
前期准备

使用yum的方法安装nginx,提供以下脚本供安装使用。

#!/bin/bash
. /etc/os-release

#捕获ctrl+c信号
trap "exit" INT

#日志处理
color(){
if [[ "$1" == "error" ]]
then

printf "\e[1;31m[Error] $(date +"%F %T"): $2\n\e[0m"
elif [[ "$1" == "info" ]]
then
printf "\e[1;32m[info] $(date +"%F %T"): $2\n\e[0m"
elif [[ "$1" == "warning" ]]
then
printf "\e[1;33m[warning] $(date +"%F %T"): $2\n\e[0m"

fi
}

#安装nginx,目前在rocky8,ubnutu20.4运行无问题,其他版本未试过
_install_nginx(){
REPLY=y
color warning "reinstall will uninstall and install again,deep think about...."
[[ -f "/lib/systemd/system/nginx.service" ]] && read -p "your already installed nginx,do you want install again[y|n]?"
case $REPLY in
y|Y)
if [[ "$ID" == "ubuntu" ]]
then
color info "uninstall nginx"
sudo apt autoremove nginx -y
color info "install nginx"
#ubuntu
sudo apt update -y
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install nginx -y
systemctl restart nginx
systemctl status nginx
curl -I 127.0.0.1:80|grep "HTTP/1.1 200 OK" &>/dev/null
color info "nginx server is running ...."
elif [[ "$ID" == "rocky" ]]
then
color info "uninstall nginx"
yum install yum-utils expect -y
yum autoremove nginx -y
color info "install nginx"
[ -f "/etc/yum.repos.d/nginx.repo" ] && mv /etc/yum.repos.d/nginx.repo /etc/yum.repos.d/nginx.repo.bak.$(date +%s) &>/dev/null
cat >/etc/yum.repos.d/nginx.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/8/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/8/x86_64/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
#yum-config-manager --enable nginx-mainline
yum install nginx -y
systemctl restart nginx
expect <<EOF
spawn systemctl status nginx
expect {
"(END)" {send "q";exp_continue}
}

EOF

curl -I 127.0.0.1:80|grep "HTTP/1.1 200 OK" &>/dev/null
color info "nginx server is running ...."
fi
;;
n|N)
exit 0
;;
*)
echo "input error,exit after 2 second"
sleep 2
exit 1
esac
}
新建目录
mkdir -p /myrepo/epel   #路径可随意
关闭selinux

如果不关闭会出现403的报错,关闭完成后重启机器生效

sed '/^SELINUX=/ s/enforcing/disabled/' -i  /etc/selinux/config

临时关闭


[root@localhost yum.repos.d]# setenforce 0
[root@localhost yum.repos.d]# getenforce
Permissive
​​将安装脚本保存为.sh文件,然后使用bash 脚本名.sh​​执行脚本修改nginx配置文件,添加如下配置
location /repo  {
alias /myrepo/epel;
autoindex on;
}
重启nginx
systemctl restart  nginx
访问http://ip/repo,出现如下界面说明配置成功

版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门