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:

  1. Ensure you have created a requirements.txt file from the local directory.
  2. 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.
  3. 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.
  4. Install Python3, pip, Apache2 and mod_wsgi modules.
sudo apt-get install python3 python3-pip python3-venv 
sudo apt-get install apache2 libapache2-mod-wsgi-py3
  1. Upload your Flask files to the server, in the apache folder directory /var/www. Make sure that you have sudo 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.
  2. In the project’s created folder, create a logs folder.
    • /var/www/project-name/logs
  3. Using Requirements.txt file, install all required dependencies
    sudo pip install -r requirements.txt 

Gunicorn:

  1. 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

  1. Install Gunicorn:
pip install gunicorn
  1. Create wsgi.py file in same directory as your app.py (main flask app code) file.
  2. Test the wsgi and gunicorn are running properly with the following command, testing public access again.
    gunicorn --bind 0.0.0.0:5000 wsgi:app
  3. Create a systemd service to allow web serving to recover in the event of service interruptions/intermittent host states.

Nginx

  1. Install Nginx
  2. Create .conf file to any custom domains.
  3. Ensure permissions to flask app dir is accessible by the user, by which Gunicorn will be run.

Resources: