VPS一键部署Docker版Firefox浏览器完美教程

本教程是在Debian 13 VPS 上一键部署 Docker + Firefox 浏览器的完整方案。
目标是让你能通过浏览器(VNC 或 Web 界面)在远程 VPS 上直接使用 Firefox 图形界面,非常适合做网页自动化、在线浏览或隐私上网。

一、一键安装脚本(完整可直接复制)

下面脚本支持 Debian 12 / 13(Trixie),自动安装:
  • Docker + docker-compose
  • Firefox(在容器内运行)
  • Web VNC(noVNC + Xfce 桌面)
  • 自动开机启动
  • 使用 https://<你的IP>:6901 打开图形界面
点击查看脚本代码
#!/bin/bash
# Debian 12/13 一键安装 Firefox 浏览器环境(Docker 版)
# 适用于已安装 Docker 的环境

set -e

# 颜色定义(可选,如果支持颜色)
if [ -t 1 ]; then
    RED='\033[0;31m'
    GREEN='\033[0;32m'
    YELLOW='\033[1;33m'
    NC='\033[0m'
else
    RED=''
    GREEN=''
    YELLOW=''
    NC=''
fi

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查 Docker 是否安装和运行
check_docker() {
    if ! command -v docker >/dev/null 2>&1; then
        log_error "Docker 未安装,请先安装 Docker"
        exit 1
    fi
    
    if ! docker info >/dev/null 2>&1; then
        log_error "Docker 守护进程未运行,请启动 Docker 服务"
        log_info "尝试执行: sudo systemctl start docker"
        exit 1
    fi
    
    log_info "Docker 已安装并运行"
}

# 检查 Docker Compose 是否可用
check_docker_compose() {
    if ! command -v docker-compose >/dev/null 2>&1; then
        if ! docker compose version >/dev/null 2>&1; then
            log_warn "Docker Compose 未安装,将使用 docker run 方式运行"
            return 1
        else
            log_info "Docker Compose Plugin 可用"
            return 0
        fi
    else
        log_info "Docker Compose 可用"
        return 0
    fi
}

# 使用 Docker Compose 部署 Firefox
setup_firefox_compose() {
    local CONTAINER_DIR="/opt/firefox"
    
    log_info "设置 Firefox 容器..."
    
    # 创建目录
    mkdir -p "$CONTAINER_DIR"
    cd "$CONTAINER_DIR"

    # 生成 docker-compose.yml
    cat > docker-compose.yml <<'EOF'
version: "3.8"
services:
  firefox:
    image: lscr.io/linuxserver/firefox:latest
    container_name: firefox
    restart: unless-stopped
    shm_size: "2gb"
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=Asia/Shanghai
      - FIREFOX_CLI=false
    volumes:
      - ./config:/config
      - ./downloads:/config/Downloads
    ports:
      - "6901:3000"
    security_opt:
      - seccomp:unconfined
EOF

    # 创建数据目录
    mkdir -p config downloads
    chmod -R 755 config downloads

    log_info "拉取 Firefox 镜像..."
    docker compose pull

    log_info "启动 Firefox 容器..."
    docker compose up -d
    
    # 等待容器启动
    log_info "等待容器启动..."
    sleep 10
    
    # 检查容器状态
    if docker compose ps | grep -q "Up"; then
        log_info "Firefox 容器启动成功"
    else
        log_error "Firefox 容器启动失败,请检查日志"
        docker compose logs
        exit 1
    fi
}

# 显示安装信息
show_info() {
    log_info "Firefox 安装完成 ✅"
    
    IP=$(hostname -I | awk '{print $1}')
    
    echo "========================================="
    echo "           访问信息"
    echo "========================================="
    echo "Web 界面: http://${IP}:6901"
    echo "默认用户: abc"
    echo "默认密码: abc"
    echo ""
    echo "========================================="
    echo "           文件目录"
    echo "========================================="
    echo "配置目录: /opt/firefox/config"
    echo "下载目录: /opt/firefox/downloads"
    echo "========================================="
}

# 显示管理命令
show_commands() {
    echo ""
    echo "========================================="
    echo "           管理命令"
    echo "========================================="
    echo "查看状态: docker ps | grep firefox"
    echo "查看日志: docker logs firefox"
    echo "停止服务: docker stop firefox"
    echo "启动服务: docker start firefox"
    echo "重启服务: docker restart firefox"
    echo ""
    
    if [ -f "/opt/firefox/docker-compose.yml" ]; then
        echo "使用 Docker Compose 管理:"
        echo "cd /opt/firefox"
        echo "docker compose ps    # 查看状态"
        echo "docker compose logs  # 查看日志"
        echo "docker compose down  # 停止服务"
        echo "docker compose up -d # 启动服务"
    fi
}

# 主函数
main() {
    echo "========================================="
    echo "   Firefox 浏览器一键安装脚本"
    echo "   适用于已安装 Docker 的环境"
    echo "========================================="
    
    # 检查 Docker
    check_docker
    
    # 检查 Docker Compose
    if check_docker_compose; then
        setup_firefox_compose
    else
        log_warn "使用 docker run 方式部署(Docker Compose 不可用)"
        setup_firefox_run
    fi
    
    show_info
    show_commands
    
    echo ""
    log_warn "重要提示:首次登录后请立即修改默认密码!"
    log_warn "如需修改端口,请编辑 /opt/firefox/docker-compose.yml"
    
    echo ""
    log_info "安装脚本可以安全删除: rm -f /tmp/install-firefox.sh"
}

main "$@"

      保存为:

      nano install-firefox.sh
      chmod +x install-firefox.sh
      ./install-firefox.sh

      安装完成后

      访问:

      http://你的服务器IP:6901

      或使用 HTTPS:

      https://你的服务器IP:6901

      登录界面输入:

      用户名:admin(或abc)<br>密码:admin(或abc)

      打开即可看到桌面 + Firefox 浏览器。

      可选优化

      修改登录密码

      docker exec -it firefox passwd

      绑定域名 + HTTPS(推荐)

      你可以用 Nginx 反代 localhost:6901 实现 HTTPS 安全访问。

      增加 GPU 加速(可选)

      如果你的 VPS 支持核显直通(如 Proxmox + Intel GPU):

      devices:
        - /dev/dri:/dev/dri

      然后

      docker compose up -d

      常用命令

      功能命令
      查看运行状态docker ps
      重启 Firefoxdocker restart firefox
      停止docker stop firefox
      更新镜像docker pull lscr.io/linuxserver/firefox:latest && docker compose up -d
      删除容器docker compose down

      二、完整反代 + HTTPS 部署步骤

      这里使用acme.sh + Firefox 容器 + Nginx 反代的方案

      整体架构

      浏览器访问 → https://fx.example.com
      ↓
      Cloudflare (CDN / Proxy)
      ↓
      Nginx (监听443, SSL由acme.sh签发)
      ↓
      反代到 → http://127.0.0.1:6901 (Firefox 容器)

      (一)申请 SSL 证书(使用 acme.sh)

      1️⃣ 安装 acme.sh(如果未安装)

      curl https://get.acme.sh | sh

      2️⃣ 配置 Cloudflare API(域名解析到CF)

      nano .bashrc
      export CF_Account_ID="你的Cloudflare CF_Account_ID"
      export CF_Token="你的Global API Key或API Token"
      export CF_Zone_ID="你的Cloudflare CF_Zone_ID"  # 可选
      source ~/.bashrc

      3️⃣ 申请证书

      acme.sh --issue --dns dns_cf -d fx.example.com

      成功后,会提示证书保存路径,如:

      /root/.acme.sh/fx.example.com/
      包含:
      fullchain.cer
      fx.example.com.key

      4️⃣ 安装证书到系统路径(方便 Nginx 使用)

      mkdir -p /etc/nginx/ssl/fx.example.com
      acme.sh --install-cert -d fx.example.com \
      --key-file /etc/nginx/ssl/fx.example.com/fx.example.com.key \
      --fullchain-file /etc/nginx/ssl/fx.example.com/fullchain.cer \
      --reloadcmd "systemctl reload nginx"

      这样当证书续期时,Nginx 会自动 reload。

      (二)Nginx 反代配置

      nano /etc/nginx/sites-available/firefox.conf
      点击查看Nginx 反代配置代码
      server {
          listen 80;
          listen [::]:80;  # 添加 IPv6 支持
          server_name fx.example.com;
          
          # 安全头即使在 HTTP 跳转时也添加
          add_header X-Frame-Options DENY always;
          add_header X-Content-Type-Options nosniff always;
          add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
          return 301 https://$host$request_uri;
      }
      
      server {
          listen 443 ssl;
          listen [::]:443 ssl;  # 添加 IPv6 SSL 支持
          http2 on
          server_name fx.example.com;
      
          # SSL 证书路径
          ssl_certificate /etc/nginx/ssl/fx.example.com/fullchain.cer;
          ssl_certificate_key /etc/nginx/ssl/fx.example.com/fx.example.com.key;
      
          # SSL 优化配置
          ssl_session_cache shared:SSL:10m;
          ssl_session_timeout 1h;
          ssl_session_tickets off;
          ssl_protocols TLSv1.2 TLSv1.3;
          ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
          ssl_prefer_server_ciphers off;
          
          # DH 参数(增强安全性)
          ssl_dhparam /etc/nginx/dhparam.pem;  # 可选:生成命令见下文
      
          # 安全头
          add_header X-Frame-Options DENY always;
          add_header X-Content-Type-Options nosniff always;
          add_header Referrer-Policy no-referrer-when-downgrade always;
          add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
          add_header X-XSS-Protection "1; mode=block" always;
          add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
      
          # 反向代理到 Firefox 容器
          location / {
              proxy_pass http://127.0.0.1:6901;
              proxy_http_version 1.1;
              
              # 基础代理头
              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;
              proxy_set_header X-Forwarded-Host $host;
              proxy_set_header X-Forwarded-Port $server_port;
      
              # WebSocket 支持(VNC 必需)
              proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection "upgrade";
      
              # 超时设置(VNC 需要较长超时)
              proxy_connect_timeout 7d;
              proxy_send_timeout 7d;
              proxy_read_timeout 7d;
              
              # 缓冲区优化
              proxy_buffering off;
              proxy_request_buffering off;
          }
      
          # 阻止常见漏洞扫描
          location ~* /(\.git|\.env|backup|admin) {
              deny all;
              return 404;
          }
      
          # 日志配置
          access_log /var/log/nginx/fx.example.com.access.log;
          error_log /var/log/nginx/fx.example.com.error.log;
      }

      保存后执行:

      ln -s /etc/nginx/sites-available/firefox.conf /etc/nginx/sites-enabled/
      nginx -t
      systemctl reload nginx

      (三)需要执行的优化步骤

      1.生成 DH 参数(可选但推荐)

      sudo mkdir -p /etc/nginx
      sudo openssl dhparam -out /etc/nginx/dhparam.pem 2048

      2.创建日志目录

      sudo mkdir -p /var/log/nginx
      sudo touch /var/log/nginx/fx.example.com.{access,error}.log
      sudo chown www-data:www-data /var/log/nginx/fx.example.com.*

      3.优化 Nginx 主配置(未尝试)

      sudo nano /etc/nginx/nginx.conf

      在 http 块中添加:

      # WebSocket 代理连接映射
      map $http_upgrade $connection_upgrade {
          default upgrade;
          '' close;
      }
      
      # 共享 SSL 缓存
      ssl_session_cache shared:SSL:10m;
      ssl_session_timeout 1h;

      4.测试并重载配置

      # 测试配置
      sudo nginx -t
      
      # 重载配置
      sudo systemctl reload nginx

      (四)验证配置

      测试 SSL 配置

      # 测试 SSL 配置
      curl -I https://fx.example.com
      
      # 详细 SSL 测试
      openssl s_client -connect fx.example.com:443 -servername fx.example.com
      
      # 在线测试
      # https://www.ssllabs.com/ssltest/analyze.html?d=fx.example.com

      测试 WebSocket 连接

      # 测试 WebSocket 连接
      curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: fx.example.com" -H "Origin: https://fx.example.com" https://fx.example.com

      防火墙配置建议

      # 确保只开放必要端口
      sudo ufw allow 80/tcp
      sudo ufw allow 443/tcp
      sudo ufw deny 6901/tcp  # 禁止直接访问容器端口,无需放行 6901 端口,因为现在只有 Nginx 本地反代能访问。
      
      # 查看状态
      sudo ufw status

      访问验证

      https://fx.example.com

      完结,撒花!🌸🌸🌸🌸🌸

      发表评论

      您的邮箱地址不会被公开。 必填项已用 * 标注

      滚动至顶部