First you'll need some dependencies:
pip install supervisor virtualenv
Next you need to make a directory for your application:
cd ~
git clone [email protected]:Username/flask-app flask-app
cd flask-app
# Setup Virtualenv
virtualenv .flaskapp
Assume flask-app
hierarchy is like so:
flask-app/
app/
__init__.py
resources/
controllers/
models/
views/
app.py
app.py should have:
from app import app
if __name__ == "__main__":
app.run(0.0.0.0)
Next you will want to make your source the virtualenv:
source .flaskapp/bin/activate
Make sure you have a requirements.txt file in your project
pip install -r requirements.txt
# Install uwsgi for serving the app
pip install uwsgi
Next we need to make a supervisor config file, below is an example config file:
[program:flask-app]
directory=/home/user/flask-app
command=/home/user/flask-app/.flaskapp/bin/uwsgi --http :9230 --file ./app.py --callable app
autostart=true
autorestart=true
stderr_logfile=/home/user/flask-app/flask-app.err.log
stdout_logfile=/home/user/flask-app/flask-app.out.log
Save this at /etc/supervisor/conf.d/flask-app.conf
Lets go through this config line by line:
[program:flask-app]
: Registers this app as flask-app with supervisor. Allows for commands like supervisorctl restart flask-app
directory=/home/user/flask-app
: Home directory of app
command=/home/user/flask...
: This will run the uwsgi
command with the port 9320
and file being the app.py
file in our flask-app project. --callable
is for the application folder of the app. If it's not called flask-app/app
then rename it to whatever folder is inside flask-app
The rest is just for supervisor and debug logs. They are selfexplainatory.
Now we need to setup an nginx config file:
Run:
sudo nano /etc/nginx/conf.d/virtual.conf
Add:
server {
listen 80;
server_name sub.domain.com;
location / {
proxy_pass http://127.0.0.1:9230
}
}
Next run nginx -t
to confirm config file is okay.
Then run
supervisorctl reread
systemctl restart supervisor
systemctl restart nginx
Now try to go to your domain set in server_name
you should see your flask app running! If you get a 500 or something check the ~/flask-app/flask-app.*.log
files.