Flask is one of the most popular micro web frameworks for Python, prized for its simplicity and flexibility. By deploying Flask on a VPS, you can create a tailored environment optimized for your Python applications, ideal for development and production alike. This guide covers the complete process of installing and configuring Flask on a VPS, along with setting up essential tools to keep it running smoothly.
Step 1: Select a VPS Provider and Set Up Your Server
-
Choose a Compatible VPS Provider
- Look for VPS providers like DigitalOcean, Linode, Vultr, or AnonVM that support custom Python installations. Select a plan that meets the resource needs of your Flask app (generally, 1GB of RAM and a single CPU core are sufficient for small applications).
-
Set Up the Operating System
- Most VPS providers offer the option to install popular distributions like Ubuntu or Debian. For this tutorial, we’ll use Ubuntu 20.04 as it’s widely supported and suitable for Flask deployments.
-
Update the Server
- After logging in via SSH, update your server’s packages:
Step 2: Install Required Dependencies
-
Install Python and pip
- Flask requires Python and pip. Install Python 3 and pip if they’re not already installed:
-
Install Virtualenv (Optional but Recommended)
- It’s a best practice to create a virtual environment for your Flask applications to isolate dependencies:
Step 3: Set Up Flask Application
-
Create a Directory for Your Flask App
- Organize your Flask application by creating a dedicated directory:
-
Set Up a Virtual Environment
- Inside your project directory, create a virtual environment:
- Activate the virtual environment:
-
Install Flask
- With the virtual environment activated, install Flask:
-
Create a Basic Flask Application
- Create a file named
app.py
in your project directory: - This simple application will respond with a "Hello, Flask on VPS!" message when accessed.
- Create a file named
Step 4: Test Flask Application Locally
-
Run the Flask Application
- Start the Flask app to test it locally:
- Visit your VPS IP address with port 5000 in the browser to verify it’s working:
-
Stop the Application
- Once confirmed, stop the application by pressing
Ctrl+C
.
- Once confirmed, stop the application by pressing
Step 5: Configure Gunicorn for Production
-
Install Gunicorn
- Gunicorn is a popular WSGI server for deploying Python applications. Install it within your virtual environment:
-
Test Running Flask with Gunicorn
- Test Gunicorn by running your Flask app with it:
- Here,
-w 3
indicates using three worker processes. Adjust this based on your application’s needs and server capabilities.
-
Verify Application is Running on Gunicorn
- You should see that the application is running on
http://127.0.0.1:8000
. Stop the server withCtrl+C
after verifying.
- You should see that the application is running on
Step 6: Set Up Nginx as a Reverse Proxy
-
Install Nginx
- Nginx will serve as a reverse proxy to forward incoming HTTP requests to Gunicorn. Install Nginx:
-
Configure Nginx for Flask
- Create a new configuration file for your Flask application:
- Add the following configuration:
- Replace
your_domain_or_IP
with your server's IP address or domain name.
-
Enable the Configuration
- Enable your new Nginx configuration by creating a symbolic link:
-
Restart Nginx
- Restart Nginx to apply the changes:
Step 7: Set Up Supervisor for Process Management
-
Install Supervisor
- Supervisor will manage your Gunicorn processes, ensuring they run continuously:
-
Create a Supervisor Configuration for Flask
- Add a configuration file for your Flask application:
- Add the following:
- Replace
your_username
with your VPS username.
-
Update and Start Supervisor
- Reload Supervisor to apply the new configuration:
-
Start Your Flask Application
- Ensure that Supervisor is running your application:
Step 8: Configure Firewall Settings (Optional)
- Allow HTTP and HTTPS Traffic
- Open ports 80 (HTTP) and 443 (HTTPS) to allow incoming web traffic:
Conclusion
By following these steps, you now have a fully operational Flask application on a VPS, secured and managed by Nginx and Supervisor. This setup provides a production-grade environment, ready for handling lightweight Python applications, APIs, and more.