Serving a Flask Web App on AWS using Gunicorn And Nginx
# Summary:
In this example, I’ve hosted a flask web app on a personal domain, accessible to the public.
- The web server is hosted on an AWS Free Tier instance.
- I’ve used Gunicorn here as it makes Python web-serving much easier.
- Gunicorn allows web server requests to interact with the flask app.
- Nginx is the web server of choice here
- I used Cloudflare as my domain registrar, providing added benefit of free SSL certificates.
# Creating Instance and Installing Dependencies:
- Ensure you have created a
requirements.txt
file from the local directory.- This can alternatively be done with other environment tools like
venv
andvirutalenv
, but I’ve usedpipenv
. - Setting Up A Virtual Env For Development
- This can alternatively be done with other environment tools like
- Create Ubuntu EC2 instance.
- If looking to stay in free tier, choose the instance type highlighted as such (currently T2 Micro).
- I chose Ubuntu Server 20.04 LTS.
- Set-up SSH/SFTP, if not yet already configured.
- I generally use Filezilla for SFTP.
- Strictly speaking this can be optional, as you can use EC2 Instance Connect in your browser, and
git
instead of SFTP.
- Install
Python3
,pip
,Apache2
andmod_wsgi
modules.
|
|
- Upload your Flask files to the server, in the apache folder directory
/var/www
. Make sure that you havesudo
admin access to allow upload here, in whichever method you are using.- You can do this through your SFTP connection (filezilla, sftp in terminal).
- Alternatively, if you have uploaded your flask app to github, you can
git clone
to the respective directory also.
- In the project’s created folder, create a
logs
folder./var/www/project-name/logs
- Using Requirements.txt file, install all required dependencies``
1
sudo pip install -r requirements.txt
# Gunicorn:
- Test all your required dependencies are installed properly. Run flask locally to make sure it’s all working as it should.``
1 2
# If your instance network settings are allowing public access, this should allow the app to be accessible from http://[your-instance-ip]:5000 flask run --host=0.0.0.0
WIP - from here on
- Install Gunicorn:
|
|
- Create
wsgi.py
file in same directory as yourapp.py
(main flask app code) file. - Test the
wsgi
and gunicorn are running properly with the following command, testing public access again.``1
gunicorn --bind 0.0.0.0:5000 wsgi:app
- Create a
systemd
service to allow web serving to recover in the event of service interruptions/intermittent host states.
# Nginx
- Install Nginx
- Create
.conf
file to any custom domains. - Ensure permissions to flask app dir is accessible by the user, by which Gunicorn will be run.
# Resources:
- How to Deploy a Flask App to Linux (Apache and WSGI) - luke peters
- Deploying a Flask Application via the Apache Server- opensourceforu
- Deploying Flask Application on Ubuntu (Apache+WSGI) - tecaadmin
- How to Install Apache Web Server on Amazon Linux 2 - dev.to
- Flask documentation to
mod_wsgi
configuration - Flask - Creating a Flask Web Server in EC2 on the AWS Free Tier from scratch! (Gunicorn) - Vincent Stevenson
- How to Deploy Flask with Gunicorn and Nginx (on Ubuntu)- Tony Teaches Tech