You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过命令行在Apache服务器上部署Flutter Web应用?

通过命令行将Flutter Web应用部署到Apache服务器的步骤

1. 构建Flutter Web生产包

先确保Flutter环境支持Web开发,再构建生产版本的Web包:

  • 切换到稳定版Flutter(推荐):
    flutter channel stable
    flutter upgrade
  • 启用Web支持:
    flutter config --enable-web
  • 进入你的Flutter项目目录:
    cd /path/to/your/flutter/project
  • 构建生产版本:
    flutter build web --release
    构建完成后,所有静态文件会生成在项目根目录的build/web文件夹下。

2. 准备Apache服务器环境

以Ubuntu系统为例,通过命令行完成Apache的安装与基础配置:

  • 更新软件包列表并安装Apache:
    sudo apt update && sudo apt install apache2
  • 启动Apache服务:
    sudo systemctl start apache2
  • 设置Apache开机自启:
    sudo systemctl enable apache2
  • 允许Apache通过防火墙(若开启UFW):
    sudo ufw allow 'Apache'

3. 部署Web文件到Apache根目录

Apache默认网站根目录为/var/www/html,将构建好的Web文件复制到该目录:

  • 备份默认首页文件(可选):
    sudo mv /var/www/html/index.html /var/www/html/index.html.bak
  • 复制Flutter Web文件到Apache目录:
    sudo cp -r /path/to/your/flutter/project/build/web/* /var/www/html/
    注意替换命令中的实际项目路径。

4. 配置Apache支持Flutter Web单页路由

Flutter Web是单页应用(SPA),直接刷新非首页路由会出现404错误,需配置URL重写:

  • 编辑Apache默认站点配置文件:
    sudo nano /etc/apache2/sites-available/000-default.conf
  • 在<VirtualHost *:80>块内添加以下配置:
    <Directory /var/www/html>
      Options Indexes FollowSymLinks
      AllowOverride All
      Require all granted
    </Directory>
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
    
  • 启用Apache的rewrite模块:
    sudo a2enmod rewrite
  • 重启Apache使配置生效:
    sudo systemctl restart apache2

5. 验证部署结果

打开浏览器,输入服务器IP地址或绑定的域名,即可访问部署好的Flutter Web应用。测试页面跳转与刷新功能,确认路由正常工作。

内容的提问来源于stack exchange,提问作者gauhun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 11:52:09