keepcalculator

06 Numbers and Powers

Run Multiple Flask Apps on One Server

Card No. 06·72259 · Updated · Standard formula, shown below

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

  1. Prepare your Flask apps. Ensure each app is a proper Python project with its own virtual environment. For example, create /var/www/app1 and /var/www/app2. Each should have an app.py (or your entry point) that creates a Flask instance. Test each locally with flask run before proceeding.
  2. Install Gunicorn and Nginx. Run sudo apt update then sudo apt install nginx python3-pip. Install Gunicorn inside each virtual environment: pip install gunicorn.
  3. 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:app with the module name and variable name for your Flask app (e.g., app:app if your file is app.py). Repeat for app2.

  4. Start and enable the services. Run sudo systemctl start app1 and sudo systemctl enable app1. Do the same for app2. Check they’re running with sudo systemctl status app1.
  5. Configure Nginx as a reverse proxy. Create a site configuration for each app in /etc/nginx/sites-available/. For app1, create app1:
    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.

  6. Enable the sites and test Nginx. Symlink each config to /etc/nginx/sites-enabled/ with sudo ln -s /etc/nginx/sites-available/app1 /etc/nginx/sites-enabled/. Run sudo nginx -t to test syntax, then sudo systemctl reload nginx.
  7. Set up SSL (HTTPS) for each domain. Install Certbot: sudo apt install certbot python3-certbot-nginx. Then run sudo 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.
  8. 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 app1 and sudo 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:5000 for both, the second will fail. Using Unix sockets avoids this.
  • Forgetting to set proxy_params: This file includes important headers like Host and X-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 + 1 for optimal performance (adjust the --workers flag).
  • Serve static files directly with Nginx for better performance. Add a location /static block that points to your app’s static folder.
  • Monitor your apps with systemctl status and consider using a tool like htop to watch resource usage.
  • Keep your system updated with sudo apt update && sudo apt upgrade regularly 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 /static block in Nginx pointing to your app’s static directory, and restart Nginx.
  • Service fails to start: Run sudo journalctl -u app1 -n 50 to 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

The whole Numbers and Powers drawer →