How to Install Flask on VPS for Lightweight Python Applications

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

  1. 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).
  2. 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.
  3. Update the Server

    • After logging in via SSH, update your server’s packages:
       
      sudo apt update && sudo apt upgrade -y

Step 2: Install Required Dependencies

  1. Install Python and pip

    • Flask requires Python and pip. Install Python 3 and pip if they’re not already installed:
       
      sudo apt install python3 python3-pip -y
  2. Install Virtualenv (Optional but Recommended)

    • It’s a best practice to create a virtual environment for your Flask applications to isolate dependencies:
       
      sudo pip3 install virtualenv

Step 3: Set Up Flask Application

  1. Create a Directory for Your Flask App

    • Organize your Flask application by creating a dedicated directory:
       
      mkdir ~/my_flask_app cd ~/my_flask_app
  2. Set Up a Virtual Environment

    • Inside your project directory, create a virtual environment:
       
      virtualenv venv
    • Activate the virtual environment:
       
      source venv/bin/activate
  3. Install Flask

    • With the virtual environment activated, install Flask:
       
      pip install Flask
  4. Create a Basic Flask Application

    • Create a file named app.py in your project directory:
      python
       
      # app.py from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Flask on VPS!" if __name__ == '__main__': app.run(host='0.0.0.0')
    • This simple application will respond with a "Hello, Flask on VPS!" message when accessed.

Step 4: Test Flask Application Locally

  1. Run the Flask Application

    • Start the Flask app to test it locally:
       
      python app.py
    • Visit your VPS IP address with port 5000 in the browser to verify it’s working:
      plaintext
       
      http://[your-vps-ip]:5000
  2. Stop the Application

    • Once confirmed, stop the application by pressing Ctrl+C.

Step 5: Configure Gunicorn for Production

  1. Install Gunicorn

    • Gunicorn is a popular WSGI server for deploying Python applications. Install it within your virtual environment:
       
      pip install gunicorn
  2. Test Running Flask with Gunicorn

    • Test Gunicorn by running your Flask app with it:
       
      gunicorn -w 3 app:app
    • Here, -w 3 indicates using three worker processes. Adjust this based on your application’s needs and server capabilities.
  3. Verify Application is Running on Gunicorn

    • You should see that the application is running on http://127.0.0.1:8000. Stop the server with Ctrl+C after verifying.

Step 6: Set Up Nginx as a Reverse Proxy

  1. Install Nginx

    • Nginx will serve as a reverse proxy to forward incoming HTTP requests to Gunicorn. Install Nginx:
       
      sudo apt install nginx -y
  2. Configure Nginx for Flask

    • Create a new configuration file for your Flask application:
       
      sudo nano /etc/nginx/sites-available/my_flask_app
    • Add the following configuration:
      nginx
       
      server { listen 80; server_name your_domain_or_IP; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
    • Replace your_domain_or_IP with your server's IP address or domain name.
  3. Enable the Configuration

    • Enable your new Nginx configuration by creating a symbolic link:
       
      sudo ln -s /etc/nginx/sites-available/my_flask_app /etc/nginx/sites-enabled
  4. Restart Nginx

    • Restart Nginx to apply the changes:
       
      sudo systemctl restart nginx

Step 7: Set Up Supervisor for Process Management

  1. Install Supervisor

    • Supervisor will manage your Gunicorn processes, ensuring they run continuously:
       
      sudo apt install supervisor -y
  2. Create a Supervisor Configuration for Flask

    • Add a configuration file for your Flask application:
       
      sudo nano /etc/supervisor/conf.d/my_flask_app.conf
    • Add the following:
      plaintext
       
      [program:my_flask_app] command=/home/your_username/my_flask_app/venv/bin/gunicorn -w 3 -b 127.0.0.1:8000 app:app directory=/home/your_username/my_flask_app user=your_username autostart=true autorestart=true stopasgroup=true killasgroup=true
    • Replace your_username with your VPS username.
  3. Update and Start Supervisor

    • Reload Supervisor to apply the new configuration:
       
      sudo supervisorctl reread sudo supervisorctl update
  4. Start Your Flask Application

    • Ensure that Supervisor is running your application:
       
      sudo supervisorctl start my_flask_app

Step 8: Configure Firewall Settings (Optional)

  1. Allow HTTP and HTTPS Traffic
    • Open ports 80 (HTTP) and 443 (HTTPS) to allow incoming web traffic:
       
      sudo ufw allow 'Nginx Full'

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.

Was this answer helpful? 0 Users Found This Useful (0 Votes)

Powered by WHMCompleteSolution