Search code examples
nginxflaskgunicorncentos8

How do I setup files to run multiple flask (nginx, gunicorn) applications on one server? (specify separate ports)


After getting each application to run on their own you need to specify the port in the application, nginx and gunicorn/service files. I am using AWS with Centos 8 operating system.


Solution

  • This example below is running right now on an AWS free tier machine and I can access both websites. I just changed the names to a generic "App1" and "App2".

    To do this you actually only need to adjust the nginx .conf file and the .service file for gunicorn. No adjustments needed to my flask/python files. Here is the general folder structure of the files I’m referencing for 2 applications:

    enter image description here

    With this example I will run app1 on the default, which I thought was 80 but maybe its 8000, and app2 on 8001.

    File setup:

    App1Domain.com.conf

    server {
            listen       80;
            listen       [::]:80;
            server_name  App1Domain.com;
            client_max_body_size 30M;
    
        location /static {
            alias /home/ubuntu/environments/App1ProjectFolder/app_package/static;
        }
    
        location / {
            proxy_pass http://localhost:8000;
            include /etc/nginx/proxy_params;
            proxy_redirect off;
        }
    }
    

    App2Domain.com.conf

    server {
            listen       80;
            listen       [::]:80;
            server_name  App2Domain.com;
            client_max_body_size 30M;
    
        location /static {
            alias /home/ubuntu/applications/App2ProjectFolder/app_package/static;
        }
    
        location / {
            proxy_pass http://localhost:8001;
            include /etc/nginx/proxy_params;
            proxy_redirect off;
        }
    }
    

    App1.service

    [Unit]
    Description=Gunicorn instance to serve App1 in conda environment
    After=network.target
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/environments/App1ProjectFolder
    Environment="PATH=/home/ubuntu/miniconda3/envs/App1/bin"
    ExecStart=/home/ubuntu/miniconda3/envs/App1/bin/gunicorn -w 3 run:app --timeout 300
    
    [Install]
    WantedBy=multi-user.target
    

    App2.service

    
    [Unit]
    Description=Gunicorn instance to serve App2 in a venv environment
    After=network.target
    
    [Service]
    User=ubuntu
    Group=www-data
    WorkingDirectory=/home/ubuntu/applications/App2ProjectFolder
    Environment="PATH=/home/ubuntu/environments/App2/bin"
    ExecStart=/home/ubuntu/environments/App2/bin/gunicorn -w 3 -b 0.0.0.0:8001 run:app --timeout 300
    
    [Install]
    WantedBy=multi-user.target