1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
| #!/bin/bash
LOG_FILE="/var/log/FileBrowser.log"
log_echo() { local message="$1" local timestamp=$(date +"%Y-%m-%d %H:%M:%S") echo "$timestamp - $message" | tee -a $LOG_FILE }
RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' NC='\033[0m'
print_title() { local message="$1"
echo -e "${GREEN}==================================================${NC}" echo -e "${GREEN}$message${NC}" echo -e "${GREEN}==================================================${NC}"
log_echo "$message" }
print_title "设置存储位置" log_echo "您想手动设置存储位置的用户名还是使用系统默认的存储位置?" log_echo "手动设置的位置为 /root/(您设置的用户名),自动生成的位置是 /root/admin" read -p "输入 1 手动设置,输入 2 使用系统默认: " choice
if [ "$choice" == "1" ]; then read -p "请输入存储位置的用户名: " username echo
log_echo "用户手动设置了存储位置的用户名。" else username=admin log_echo "使用默认用户名:admin" fi
log_echo "正在创建用户 ${username} ..." sudo useradd -s /usr/sbin/nologin ${username} log_echo "用户 ${username} 创建成功。"
log_echo "存储位置的用户名为: $username" log_echo "存储内容的位置在: /home/$username"
run_yum() { local yum_command="$1" local service_name="$2" local max_retries=5 local retry_count=0 local yum_pid_file="/var/run/yum.pid" while [ $retry_count -lt $max_retries ]; do if [ -f "$yum_pid_file" ]; then local yum_pid=$(cat "$yum_pid_file") if ps -p $yum_pid > /dev/null 2>&1; then log_echo "另一个应用程序(PID: $yum_pid)正在持有 yum 锁,正在强制终止该进程..." sudo kill -9 "$yum_pid" sudo rm -f "$yum_pid_file" log_echo "已强制终止持有 yum 锁的进程并移除锁文件。" else log_echo "检测到 yum 锁文件,但进程 $yum_pid 已不存在,移除锁文件..." sudo rm -f "$yum_pid_file" fi fi if $yum_command; then log_echo "$service_name 安装成功。" return 0 else retry_count=$((retry_count+1)) log_echo "yum 命令 '$yum_command' 失败,重试第 $retry_count 次..." sleep 5 fi done
log_echo "yum 命令 '$yum_command' 多次失败,请检查网络连接或其他问题。" exit 1 }
print_title "检查防火墙和SELinux"
log_echo "正在检查防火墙状态..." firewalld_status=$(systemctl is-active firewalld)
if [ "$firewalld_status" == "active" ]; then log_echo "防火墙正在运行,正在停止防火墙..." sudo systemctl stop firewalld sudo systemctl disable firewalld log_echo "防火墙已停止并禁用。" else log_echo "防火墙是关闭的。" fi
log_echo "正在检测 SELinux 状态..." selinux_status=$(getenforce)
if [ "$selinux_status" == "Enforcing" ]; then log_echo "SELinux 当前状态为 Enforcing,正在将其设置为 Permissive 模式..." sudo setenforce 0 log_echo "SELinux 已临时设置为 Permissive 模式。" elif [ "$selinux_status" == "Permissive" ]; then log_echo "SELinux 已经处于 Permissive 模式。" else log_echo "SELinux 当前状态为 Disabled,无需修改。" fi
print_title "检查 yum 源" yum_repolist=$(yum repolist | grep "mirrors.aliyun.com")
if [ -z "$yum_repolist" ]; then log_echo "未找到阿里云源,让我们配置阿里云 yum 源..."
epel_installed=$(rpm -qa | grep epel-release)
sudo mkdir -p /etc/yum.repos.d/backup sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ sudo touch /etc/yum.repos.d/CentOS7-Aliyun.repo sudo bash -c 'cat > /etc/yum.repos.d/CentOS7-Aliyun.repo << EOF [base] name=CentOS-\$releasever - Base - mirrors.aliyun.com baseurl=http://mirrors.aliyun.com/centos/\$releasever/os/\$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[updates] name=CentOS-\$releasever - Updates - mirrors.aliyun.com baseurl=http://mirrors.aliyun.com/centos/\$releasever/updates/\$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[extras] name=CentOS-\$releasever - Extras - mirrors.aliyun.com baseurl=http://mirrors.aliyun.com/centos/\$releasever/extras/\$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 EOF'
run_yum "sudo yum clean all" "一键清除所有 yum 源" run_yum "sudo yum repolist" "阿里 yum 源"
if [ -n "$epel_installed" ]; then log_echo "检测到已安装epel-release,让我们先移除它..." run_yum "sudo yum remove -y epel-release" "EPEL 卸载成功,并没有" fi
run_yum "sudo yum install -y epel-release" "epel-release" run_yum "sudo yum repolist" "所有的 yum 源都" else log_echo "阿里云源已存在,跳过阿里yum源的配置。"
epel_installed=$(rpm -qa | grep epel-release)
if [ -n "$epel_installed" ]; then log_echo "检测到已安装epel-release" else log_echo "未检测到EPEL源,正在为您安装..." run_yum "sudo yum install -y epel-release" "EPEL源" fi fi
print_title "部署nginx" log_echo "让我们来安装Nginx..." run_yum "sudo yum install -y nginx" "nginx"
log_echo "正在获取主机的IP地址..." ipaddress=$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1 | head -n 1) log_echo "检测到的IP地址为: $ipaddress"
log_echo "配置 Nginx..." cat << EOF > /etc/nginx/conf.d/filebrowser.conf server { listen 80;
client_max_body_size 100M; proxy_read_timeout 300s;
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host \$host; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; } } EOF
log_echo "检测 Nginx 配置..." nginx_test_output=$(nginx -t 2>&1)
if echo "$nginx_test_output" | grep -q 'successful'; then log_echo "Nginx 配置检测成功。" log_echo "$nginx_test_output" log_echo "启动并启用 Nginx 服务..." sudo systemctl start nginx sudo systemctl enable nginx else log_echo "Nginx 配置检测失败!" log_echo "$nginx_test_output" exit 1 fi
print_title "部署FileBrowser" log_echo "安装 FileBrowser..." curl -fsSL https://moka.anitsuri.top/images/shell/get.sh | bash
log_echo "正在检测 FileBrowser 是否安装成功..." filebrowser_installed=$(ls /usr/local/bin/ | grep filebrowser)
if [ -z "$filebrowser_installed" ]; then log_echo "FileBrowser 下载失败,请重新尝试运行脚本。" exit 1 else log_echo "FileBrowser 安装成功。" fi
log_echo "正在后台启动 FileBrowser" log_echo "FileBrowser 输出日志: /var/log/filebrowser.out" log_echo "FileBrowser 错误日志: /var/log/filebrowser_error.log" nohup filebrowser -r /home/${username} > /var/log/filebrowser.out 2> /var/log/filebrowser_error.log &
print_title "部署 FileBrowser 成功!" log_echo "恭喜你,FileBrowser 部署成功!" log_echo "快来使用 http://${ipaddress}/ 来进行访问吧" log_echo "默认用户名是 admin" log_echo "默认密码是 admin" log_echo "请及时进入 FileBrowser 修改默认密码!" print_title "备注" log_echo "PS:如果服务器还部署其他服务的情况下,请去 /etc/nginx/conf.d/filebrowser.conf 的第 3 行添加上 server_name (你的IP或者域名);"
|