Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Deploying Flask Applications


Introduction

Deploying Flask applications involves configuring a production-ready web server and hosting the application on a reliable platform. In this article, we will go through the steps to deploy Flask applications using Gunicorn, Nginx, and hosting platforms like Heroku, AWS, or DigitalOcean.

Step 1: Setting Up the Flask Application

Create a simple Flask application and save it as app.py:

            from flask import Flask

            app = Flask(__name__)

            @app.route('/')
            def home():
                return "Welcome to the deployed Flask app!"
        

Ensure all dependencies are listed in requirements.txt:

            Flask==2.3.0
            gunicorn==20.1.0
        

Step 2: Installing Gunicorn

Gunicorn is a Python WSGI HTTP server for running Flask in production. Install Gunicorn:

            pip install gunicorn
        

Run the application using Gunicorn:

            gunicorn -w 4 -b 0.0.0.0:8000 app:app
        

Here, -w 4 specifies 4 worker processes, and -b sets the bind address and port.

Step 3: Configuring Nginx

Nginx is used as a reverse proxy to forward requests to Gunicorn. Install and configure Nginx:

            sudo apt update
            sudo apt install nginx
        

Edit the Nginx configuration file:

            sudo nano /etc/nginx/sites-available/flask_app
        

Add the following configuration:

            server {
                listen 80;
                server_name yourdomain.com;

                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;
                }
            }
        

Enable the configuration and restart Nginx:

            sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled
            sudo systemctl restart nginx
        

Step 4: Hosting on Heroku

Heroku is a platform-as-a-service (PaaS) for hosting applications. Install the Heroku CLI:

            curl https://cli-assets.heroku.com/install.sh | sh
        

Create a Procfile for Heroku:

            web: gunicorn app:app
        

Deploy the application:

            heroku login
            heroku create
            git init
            git add .
            git commit -m "Initial commit"
            git push heroku master
        

Step 5: Hosting on AWS

To deploy on AWS, create an EC2 instance and configure it:

Access the instance's public IP address to view the application.

Step 6: Hosting on DigitalOcean

Create a Droplet on DigitalOcean and set up the server:

Point your domain to the Droplet's IP address to access the application.

Conclusion

Deploying Flask applications can be done using Gunicorn and Nginx for production. Hosting platforms like Heroku, AWS, or DigitalOcean provide reliable solutions for deploying and scaling Flask applications. By following these steps, you can have your Flask application live and ready for users.

Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java