Hosting Python Flask Applications on Google Cloud Platform for Free
Flask, a robust Python web framework, serves as an excellent choice for developers seeking simplicity and power. During development, Flask applications are typically accessible via localhost:5000, facilitated by Flask's built-in development web server. However, transitioning to making your Flask application available on the web for others to use requires a few additional steps.
In this guide, tailored for aspiring hobbyist coders aiming to showcase their projects while keeping costs minimal, we'll explore deploying Flask applications using Google Cloud Platform's free virtual machine, Gunicorn as the WSGI web server, and nginx as a reverse proxy. Assuming your Flask app is already hosted on a Git repository on GitHub, let's dive into the process step by step.
Setting up the Virtual Machine on Google Cloud Platform
Begin by creating a f1-micro virtual machine on Google Cloud Platform, opting for Ubuntu 18.04.2 LTS as the operating system. Log in to the machine, generate an SSH key, and add the SSH public key to your GitHub repository to enable the VM to pull your code. Clone the repository onto the VM and install necessary dependencies using pip. Ensure your Flask app runs smoothly on the VM before proceeding.
Making the Virtual Machine Accessible via a Domain
If you own a domain, it's recommended to configure it to resolve to your virtual machine's IP address. This can typically be achieved by setting the A-record of your domain or subdomain to point to the VM's IP address.
Gunicorn as the Web Server
To prepare your Flask app for production, it's advised to use Gunicorn instead of Flask's built-in development server. Gunicorn acts as a web application server, adhering to the WSGI standard. Add Gunicorn to your project dependencies and start it up using appropriate configurations.
Nginx as a Reverse Proxy
To enable your app to listen on port 80 (HTTP), which is commonly used for web traffic, nginx is employed as a reverse proxy. This involves installing nginx on your VM and configuring it to route external requests from port 80 to the internal port where Gunicorn is running.
Always-On Web Server
To ensure your web server remains active even after closing your command line interface, set up a background service using systemd. This service will automatically start Gunicorn on boot and keep it running in the background.
Securing Your Website with HTTPS
For enhanced security, consider securing your website with HTTPS by obtaining an SSL/TLS certificate. Let's Encrypt and its Certbot client offer a straightforward way to obtain and configure free SSL certificates, ensuring encrypted communication between your users and your website.
Conclusion
By following these steps, you can host your Python Flask web application for free on Google Cloud Platform, utilizing GitHub for version control, Gunicorn as the web server, and nginx as the reverse proxy server. With each push to your GitHub repository, simply perform a git pull on your VM to update your website effortlessly. Hosting your Flask app has never been easier or more cost-effective. ๐
Subscribe to my newsletter
Read articles from Joseph Lweya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Joseph Lweya
Joseph Lweya
I am a developer trained and mentored by ALX-Africa and building solutions at Datalytika. Always learning, always building.