Deploying Flask Applications on DigitalOcean

beginner
17 min

Deploying Flask Applications on DigitalOcean

Welcome to our comprehensive guide on deploying your Flask applications on DigitalOcean! This tutorial is designed to be beginner-friendly, yet detailed enough for intermediate learners. Let's dive into the world of web deployment!

🎯 Understanding the Basics

Before we begin, let's make sure you have the following prerequisites:

  • A Flask project you'd like to deploy
  • A DigitalOcean account
  • SSH keys set up for your DigitalOcean account

What is Flask?

Flask is a micro web framework written in Python. It provides an easy-to-use and flexible environment for creating web applications.

What is DigitalOcean?

DigitalOcean is a cloud platform that offers services like virtual private servers, load balancers, and storage solutions to host your web applications.

📝 Setting Up Your Flask Application

First, let's make sure your Flask application is ready to deploy.

Creating a New Flask Application

To create a new Flask application, use the following command in your terminal:

bash
pip install flask flask --version

Then, create a new file named app.py and add the following code:

python
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello, World!' if __name__ == '__main__': app.run(debug=True)

Now, you can run your application by executing python app.py in your terminal.

💡 Pro Tip:

Remember to include a requirements.txt file that lists all the Python packages your project depends on. This file will help when installing your project's dependencies on the server.

🎯 Deploying to DigitalOcean

Now that our Flask application is ready, it's time to deploy it on DigitalOcean.

Creating a Droplet

  1. Log in to your DigitalOcean account and navigate to the Control Panel.
  2. Click "Create" and select "Droplets."
  3. Choose your preferred image (Ubuntu 20.04 is a good starting point), size, region, and SSH key.
  4. Give your Droplet a hostname and click "Create."

Connecting to Your Droplet

  1. Once your Droplet is ready, you can connect to it using SSH.
bash
ssh root@your_droplet_ip_address

Installing Git

We will use Git to transfer our Flask application to the server.

bash
sudo apt-get update sudo apt-get install git

Transferring Your Application

  1. Navigate to the directory where you want to store your project:
bash
cd /var/www/your_project_name
  1. Clone your repository:
bash
git clone git@github.com:your_username/your_repo.git

Installing Dependencies

We will use pip3 to install the dependencies listed in our requirements.txt file.

bash
cd your_project_name pip3 install -r requirements.txt

Running Your Application

Now, you can run your Flask application using the following command:

bash
gunicorn -b 0.0.0.0:8000 app:app

💡 Pro Tip:

Gunicorn is a Python WSGI HTTP Server for UNIX that's well-suited for deploying Flask applications.

🎯 Configuring the Firewall and Nginx

To make our application accessible via a domain, we'll configure the firewall and set up Nginx.

Configuring the Firewall

bash
sudo ufw allow 8000/tcp

Installing Nginx

bash
sudo apt-get install nginx

Creating a New Nginx Configuration File

bash
sudo nano /etc/nginx/sites-available/your_project_name

Add the following configuration:

bash
server { listen 80; server_name your_domain_or_ip; location / { include proxy_params; proxy_pass http://127.0.0.1:8000; } }

Enabling and Testing Nginx Configuration

bash
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx

📝 Note:

Remember to replace your_project_name, your_domain_or_ip, and your_username with appropriate values throughout this guide.

🎯 Conclusion

Congratulations! You've now deployed your Flask application on DigitalOcean. This tutorial covered setting up a Flask application, creating a Droplet, connecting to it, installing Git, transferring the application, and configuring the firewall and Nginx.

Quick Quiz
Question 1 of 1

What does Flask provide for creating web applications?