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.txtfile from the local directory.- This can alternatively be done with other environment tools like
venvandvirutalenv, but I’ve usedpipenv. - Setting Up A Virtual Env For Development 1
- 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
gitinstead of SFTP.
- Install
Python3,pip,Apache2andmod_wsgimodules.
sudo apt-get install python3 python3-pip python3-venv
sudo apt-get install apache2 libapache2-mod-wsgi-py3- Upload your Flask files to the server, in the apache folder directory
/var/www. Make sure that you havesudoadmin 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 cloneto the respective directory also.
- In the project’s created folder, create a
logsfolder./var/www/project-name/logs
- Using Requirements.txt file, install all required dependencies
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.
# 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:
pip install gunicorn- Create
wsgi.pyfile in same directory as yourapp.py(main flask app code) file. - Test the
wsgiand gunicorn are running properly with the following command, testing public access again.gunicorn --bind 0.0.0.0:5000 wsgi:app - Create a
systemdservice to allow web serving to recover in the event of service interruptions/intermittent host states.
Nginx
- Install Nginx
- Create
.conffile 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_wsgiconfiguration - 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