Deploying Flask applications to Vultr can be a powerful and cost-effective solution for your web projects. Vultr is a cloud computing provider offering high-performance cloud servers with a global footprint. In this article, we will walk you through a step-by-step process of deploying a Flask application on Vultr, covering every key aspect from server setup to making your application publicly accessible.

Prerequisites

Before starting, ensure that you have the following:

  • A Vultr account
  • Basic knowledge of Linux and Flask
  • A domain name (optional but recommended)
  • SSH client (e.g., PuTTY for Windows or the terminal for Linux/Mac)

Create a Vultr Account and Deploy an Instance

  1. Sign up and log in to Vultr: If you don’t have an account, head over to Vultr and sign up for an account.
  2. Deploy a new instance: Once logged in, click on the “Deploy New Server” button on your dashboard. Vultr will give you a variety of options:
    • Server Type: Choose “Cloud Compute” (regular or high-frequency depending on your needs).
    • Server Location: Choose a location that is closest to your target audience.
    • Server OS: Select a Linux distribution, such as Ubuntu 22.04 LTS (or the latest stable version).
    • Server Size: Choose an appropriate plan. For small Flask applications, a 1 vCPU, 1 GB RAM instance should suffice.
  3. Start your instance: After selecting your options, deploy the instance. Vultr will set up the server and provide you with its IP address and root login credentials.

Connect to Your Vultr Instance via SSH

Once your instance is up and running, you can connect to it using SSH.

  1. On Linux or macOS: Open a terminal and run:
    bash
    ssh root@your_server_ip
  2. On Windows: Use an SSH client like PuTTY. Enter your server’s IP address and connect using the root credentials provided by Vultr.

You will be prompted to enter the root password. After logging in, it is a good practice to update your system.

bash
sudo apt update && sudo apt upgrade -y

Set Up the Python Environment

To run Flask on your server, you’ll need to install Python, pip, and virtualenv. We’ll be using Python 3, which comes pre-installed on Ubuntu 22.04. However, let’s ensure the latest version is installed.

  1. Install required packages:
    bash
    sudo apt install python3 python3-pip python3-venv nginx -y
  2. Create a directory for your Flask application:
    bash
    mkdir /var/www/myflaskapp
    cd /var/www/myflaskapp
  3. Set up a virtual environment:
    bash
    python3 -m venv venv
  4. Activate the virtual environment:
    bash
    source venv/bin/activate

Now that the virtual environment is activated, install Flask and Gunicorn (WSGI HTTP server).

bash
pip install flask gunicorn

Create a Simple Flask Application

Next, create a simple Flask application. Inside the /var/www/myflaskapp directory, create a file named app.py:

bash
nano app.py

Paste the following basic Flask application code:

python

from flask import Flask

app = Flask(__name__)

@app.route(‘/’)
def hello_world():
return ‘Hello, Vultr!’

if __name__ == ‘__main__’:
app.run(host=‘0.0.0.0’)

Save the file and exit the editor.

Test the Flask Application Locally

You can now test your Flask application locally by running it:

bash
python app.py

You should see something like this:

bash
* Running on http://127.0.0.1:5000/

To access the app, open a browser and navigate to http://your_server_ip:5000. You should see “Hello, Vultr!” displayed. If everything works as expected, press Ctrl+C to stop the server.

Configure Gunicorn to Serve the Flask App

Gunicorn is a Python WSGI HTTP server that will help serve your Flask application. To run your app with Gunicorn, execute the following command:

bash
gunicorn --workers 3 app:app

This command will start your Flask application with Gunicorn using three worker processes. However, we don’t want to run this manually every time the server restarts, so let’s create a systemd service to manage it.

  1. Create a Gunicorn service file:
    bash
    sudo nano /etc/systemd/system/myflaskapp.service
  2. Add the following content:
    ini
    [Unit]
    Description=Gunicorn instance to serve myflaskapp
    After=network.target
    [Service]
    User=root
    Group=www-data
    WorkingDirectory=/var/www/myflaskapp
    Environment=“PATH=/var/www/myflaskapp/venv/bin”
    ExecStart=/var/www/myflaskapp/venv/bin/gunicorn –workers 3 –bind unix:/var/www/myflaskapp/myflaskapp.sock -m 007 app:app[Install]
    WantedBy=multi-user.target
  3. Start and enable the service:
    bash
    sudo systemctl start myflaskapp
    sudo systemctl enable myflaskapp

Configure Nginx as a Reverse Proxy

To make your application accessible over the internet, you’ll need to set up Nginx as a reverse proxy. This will forward web traffic to Gunicorn.

  1. Create an Nginx configuration file:
    bash
    sudo nano /etc/nginx/sites-available/myflaskapp
  2. Add the following content:
    nginx
    server {
    listen 80;
    server_name your_domain_or_ip;
    location / {
    proxy_pass http://unix:/var/www/myflaskapp/myflaskapp.sock;
    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;
    }
    }
  3. Enable the configuration:
    bash
    sudo ln -s /etc/nginx/sites-available/myflaskapp /etc/nginx/sites-enabled
  4. Test Nginx for configuration errors:
    bash
    sudo nginx -t
  5. Restart Nginx:
    bash
    sudo systemctl restart nginx

Set Up a Firewall (Optional but Recommended)

You should also set up a firewall to secure your server. First, allow traffic on port 80 (HTTP) and port 22 (SSH):

bash
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Test Your Application

Now your Flask app should be accessible via your server’s IP address or domain name. Open a browser and go to:

arduino
http://your_domain_or_ip

If everything was set up correctly, you should see the “Hello, Vultr!” message.

Set Up a Domain Name (Optional)

If you have a domain name, point it to your Vultr server’s IP address by updating your DNS records with your domain registrar. Once the DNS has propagated, you can access your Flask app through your domain name.

To automatically manage SSL certificates, you can use Certbot to install Let’s Encrypt:

bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain

This will automatically install an SSL certificate and configure Nginx to use HTTPS.

Conclusion

In this guide, we’ve covered the complete process of deploying a Flask application on Vultr, from setting up the server to configuring Nginx and Gunicorn. By leveraging Vultr’s cloud platform, you can deploy scalable and high-performance applications with minimal hassle. With Flask running behind a Gunicorn WSGI server and Nginx acting as a reverse proxy, your application is ready for production.

This setup is secure, scalable, and easily maintainable. You can further improve it by setting up continuous integration and deployment pipelines, adding database support, and monitoring your application for performance and errors. By mastering this deployment process, you’re on your way to deploying complex Flask applications that can serve users worldwide.