Running multiple Flask applications on a single server lets you host several web services, APIs, or projects without needing a separate machine or virtual private server for each. This guide shows you how to configure a production-ready setup using Gunicorn and Nginx, so each app gets its own domain or subdomain and runs reliably. You’ll need basic familiarity with the command line and access to a Linux server (Ubuntu is assumed here).
What You’ll Need
- A Linux server (Ubuntu 20.04 or 22.04 recommended) with sudo access.
- Python 3 and pip installed.
- Two or more Flask applications (each in its own directory).
- Domain names or subdomains pointing to your server’s IP address (e.g., app1.example.com, app2.example.com).
- Nginx and Gunicorn installed (we’ll cover installation).
Step-by-Step
- Prepare your Flask apps. Ensure each app is a proper Python project with its own virtual environment. For example, create
/var/www/app1and/var/www/app2. Each should have anapp.py(or your entry point) that creates a Flask instance. Test each locally withflask runbefore proceeding. - Install Gunicorn and Nginx. Run
sudo apt updatethensudo apt install nginx python3-pip. Install Gunicorn inside each virtual environment:pip install gunicorn. - Create systemd service files for each app. For each app, create a service file, e.g.,
/etc/systemd/system/app1.service:[Unit] Description=Gunicorn instance for App1 After=network.target [Service] User=www-data Group=www-data WorkingDirectory=/var/www/app1 Environment="PATH=/var/www/app1/venv/bin" ExecStart=/var/www/app1/venv/bin/gunicorn --workers 3 --bind unix:/var/www/app1/app1.sock wsgi:app [Install] WantedBy=multi-user.target
Replace
wsgi:appwith the module name and variable name for your Flask app (e.g.,app:appif your file isapp.py). Repeat for app2. - Start and enable the services. Run
sudo systemctl start app1andsudo systemctl enable app1. Do the same for app2. Check they’re running withsudo systemctl status app1. - Configure Nginx as a reverse proxy. Create a site configuration for each app in
/etc/nginx/sites-available/. For app1, createapp1:server { listen 80; server_name app1.example.com; location / { include proxy_params; proxy_pass http://unix:/var/www/app1/app1.sock; } }Repeat for app2 with its own domain and socket path.
- Enable the sites and test Nginx. Symlink each config to
/etc/nginx/sites-enabled/withsudo ln -s /etc/nginx/sites-available/app1 /etc/nginx/sites-enabled/. Runsudo nginx -tto test syntax, thensudo systemctl reload nginx. - Set up SSL (HTTPS) for each domain. Install Certbot:
sudo apt install certbot python3-certbot-nginx. Then runsudo certbot --nginx -d app1.example.com -d app2.example.com(or run it separately for each domain). Certbot will automatically configure SSL and redirect HTTP to HTTPS. - Test both apps. Visit each domain in your browser. If you see your Flask app, everything works. If not, check the logs:
sudo journalctl -u app1andsudo tail /var/log/nginx/error.log.
Common Mistakes
- Wrong socket path: The path in the Nginx proxy_pass must exactly match the one in the systemd service file. A typo causes a 502 Bad Gateway.
- Missing virtual environment path: If Gunicorn isn’t installed in the venv, the service won’t start. Always use the full path to the venv’s gunicorn binary.
- Using the same port for multiple apps: If you try to bind Gunicorn to
0.0.0.0:5000for both, the second will fail. Using Unix sockets avoids this. - Forgetting to set
proxy_params: This file includes important headers likeHostandX-Real-IP. Without it, your app may see the wrong IP or hostname. - Not enabling the service: If you don’t run
systemctl enable, the app won’t start after a reboot.
Tips
- Use a separate virtual environment for each app to avoid dependency conflicts.
- Set the number of Gunicorn workers to
2 * CPU cores + 1for optimal performance (adjust the--workersflag). - Serve static files directly with Nginx for better performance. Add a
location /staticblock that points to your app’s static folder. - Monitor your apps with
systemctl statusand consider using a tool likehtopto watch resource usage. - Keep your system updated with
sudo apt update && sudo apt upgraderegularly for security patches.
Troubleshooting
- 502 Bad Gateway: This usually means Nginx can’t reach Gunicorn. Check that the socket file exists (
ls -l /var/www/app1/app1.sock) and that the systemd service is running. Also verify the socket path matches exactly. - 404 Not Found: If you see this, Nginx is running but the server_name doesn’t match the domain. Check your DNS settings and the Nginx config’s
server_name. - App loads but static files are missing: Add a
location /staticblock in Nginx pointing to your app’s static directory, and restart Nginx. - Service fails to start: Run
sudo journalctl -u app1 -n 50to see the error. Common causes are a wrong Python path or a missing module in the venv. - SSL certificate fails: Ensure your domain points to the server’s IP before running Certbot. Also, port 80 must be open for the HTTP-01 challenge.
Frequently Asked Questions
Can I run multiple Flask apps on different ports instead of domains?
Yes, you can configure Nginx to listen on different ports (e.g., 5001, 5002) and proxy each to a different Gunicorn socket. Just replace listen 80 with listen 5001 and omit server_name if you’re using IP access.
Do I need a separate Gunicorn instance for each app?
Yes, each Flask app should run its own Gunicorn process. This isolates dependencies and allows you to restart or scale apps independently.
Can I run Flask apps with different Python versions on the same server?
Yes, as long as each app has its own virtual environment created with the appropriate Python version. The systemd service should point to that venv’s Python and Gunicorn.
How do I add a third Flask app later?
Follow the same steps: create a new directory, set up a venv, create a systemd service file, start it, and add a new Nginx server block. Then reload Nginx.
What if I want to use Apache instead of Nginx?
Apache can also act as a reverse proxy using mod_proxy. The systemd and Gunicorn parts remain the same; only the web server configuration changes.
Is it safe to run multiple apps on one server?
Yes, with proper isolation (separate venvs, systemd services, and Nginx configs) it’s safe. Ensure you keep all software updated and monitor resource usage to avoid overloading the server.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Use Mac More Efficiently: 21 Proven Tips
- Best CPUs for Gaming and Streaming: Encoder Guide
- How to Compare AI Features and Performance in Phones
- Nginx Blocks in CentOS: Config Guide
- Create Google Meet Account: Step-by-Step Guide
- AI Frameworks: How Developers Choose and Ship Them