
Static hosting platforms are popular for Flutter web apps, but they are not the only option. Many organizations prefer to host applications on their own servers for reasons such as data residency, compliance requirements, integration with existing infrastructure, internal network access, or simply cost control. Because a Flutter web build produces static files, hosting it on a server is straightforward once you understand how to configure the web server correctly. This guide explains how to deploy a Flutter web app on a server running Linux, using Nginx or Apache, and covers routing, HTTPS, compression, caching, containerization, and automation.
How AAMAX.CO Supports Custom Hosting and Application Projects
Self-hosting brings flexibility, but it also requires careful configuration and ongoing maintenance. AAMAX.CO is a full-service digital marketing company offering web development, digital marketing, and SEO services worldwide. Their developers set up secure servers, configure web servers for optimal performance, and implement deployment pipelines that make updates effortless. They work closely with clients to align hosting decisions with business, security, and budget requirements. Organizations investing in professional website development can rely on them for robust solutions that perform well from day one.
What You Need
- A Linux server, such as Ubuntu LTS, from any VPS or cloud provider.
- SSH access with a sudo-enabled user.
- A domain name pointed to your server's IP address.
- A Flutter project ready for a production build.
Step 1: Build the Flutter Web App
On your development machine or CI environment, run flutter build web --release. If the app will be served from a subpath, such as example.com/portal/, include --base-href /portal/. The compiled files are placed in build/web. Building locally or in CI is recommended rather than installing the Flutter SDK on the production server, which keeps the server lean and secure.
Step 2: Prepare the Server
Connect via SSH, update system packages, and enable a firewall that allows SSH, HTTP, and HTTPS. Then install a web server. Nginx is the most common choice for serving static files thanks to its speed and low memory usage, but Apache works well too, especially if your organization already uses it. Create a directory for your app, such as /var/www/flutter-app, and assign ownership to your deployment user.
Step 3: Upload the Build Files
Transfer the contents of build/web to the server directory. Common methods include:
- rsync: Efficiently syncs only changed files and can delete old files with the --delete flag.
- scp: Simple secure copy for one-off uploads.
- SFTP clients: Graphical tools for those who prefer not to use the command line.
- CI pipelines: Automated uploads after each successful build.
Rsync is generally the best choice because it is fast and keeps the server directory in sync with your latest build.
Step 4: Configure Nginx
Create a server block configuration for your domain. Set the root to your app directory and the index to index.html. The most important directive for Flutter apps using path-based URLs is a try_files rule that serves the requested file if it exists, or falls back to index.html otherwise. This ensures deep links work when users refresh the page or open a shared URL. Enable the configuration, test it with nginx -t, and reload Nginx.
Step 5: Configure Apache (Alternative)
If you use Apache, create a virtual host pointing to your app directory and enable mod_rewrite. Add rewrite rules, either in the virtual host or an .htaccess file, that route all non-file requests to index.html. Restart Apache to apply the changes. The behavior is equivalent to the Nginx try_files approach.
Step 6: Enable HTTPS
Install Certbot and run it with the plugin for your web server to obtain a free Let's Encrypt certificate. Certbot updates your configuration automatically and sets up renewal. HTTPS is essential not only for security but also for features Flutter web may rely on, such as service workers, which browsers only allow on secure origins.
Step 7: Turn On Compression
Flutter web bundles benefit greatly from compression. Enable gzip in Nginx for JavaScript, CSS, JSON, SVG, and WebAssembly MIME types. If available, add the Brotli module for even better compression ratios. Ensure your server sends the correct MIME type for .wasm files so browsers can compile them efficiently.
Step 8: Configure Caching Correctly
Caching determines whether users see your latest release. Configure your web server to send no-cache headers for index.html, flutter_service_worker.js, and flutter_bootstrap.js, while setting long expiry times for other static assets. This combination delivers fast repeat visits without trapping users on outdated versions.
Step 9: Containerize with Docker (Optional)
For teams that use containers, you can package the app in a Docker image based on a lightweight Nginx image. A multi-stage Dockerfile can build the Flutter app in one stage and copy build/web into the Nginx stage along with your configuration file. This produces a small, portable image that runs consistently on any server or orchestration platform such as Kubernetes or Docker Compose.
Step 10: Automate Deployments
Create a CI workflow that installs Flutter, runs tests, builds the app, and uses rsync over SSH to upload the output. Store the SSH private key as an encrypted secret in your CI system. For zero-downtime releases, upload builds to timestamped directories and switch a symbolic link to the new version once the upload completes, which also makes rollbacks instant.
Troubleshooting
If the app loads a blank page, verify the base href and check the browser console for missing files. A 404 error on refresh means the fallback rule to index.html is missing. If WebAssembly builds fail to load, confirm the server sends the application/wasm MIME type. Permission errors usually indicate the web server user cannot read the app directory.
Conclusion
Deploying a Flutter web app on a server gives you full control over hosting, security, and performance. Build the app in release mode, upload it to your server, configure Nginx or Apache with proper routing fallback, enable HTTPS, compression, and smart caching, and automate the process with CI. The result is a fast, reliable, self-hosted Flutter web application tailored to your needs.