How to Deploy Flask Applications on Vultr — SitePoint


Introduction

Flask is a Python framework for building web applications. With Flask, developers can easily define routes, handle HTTP requests and responses, and render dynamic content using Jinja2 templates. It also supports extensions allowing developers to integrate features like database access, form handling, and user authentication.

In this article, we are going to deploy a Flask application using Gunicorn, along with CSRF protection and Content Security Policies implementation. We are also going to set up a reverse proxy using Nginx and implement HTTPS by requesting a free SSL certificate from Let’s Encrypt.

Deploy Vultr Optimized Cloud Instance

  1. Sign up and log in to the Vultr Customer Portal.
  2. Navigate to the Products page.
  3. From the side menu, select Compute.
  4. Click the Deploy Server button in the center.
  5. Choose Optimized Cloud Compute as the server type.
  6. Choose a server Location.
  7. Choose Ubuntu 24.04 as the operating system.
  8. Choose a suitable plan.
  9. Choose any Additional Features if required.
  10. Click Deploy Now.

Add Your Demo Application Code Files

Set Up a Python Virtual Environment

  1. Install the python3-venv package
  2. Create a virtual environment.
  3. Activate the virtual environment.

Add Demo Application Code Files

  1. Clone the Github repository.
  2. Navigate to the project directory.
  3. Install Flask and Gunicorn packages.
  4. Allow incoming connections to port 5000.

Apply CSRF Protection and Content Security Policies

Cross-Site Request Forgery (CORS) is a vulnerability that allows someone to exploit and take advantage of a user’s logged-in session on a trusted website to make unauthorized requests to a different website that the user trusts. WTForms is a Flask extension that integrates CSRF protection by default to prevent CORS attacks.

Content Security Policy (CSP) is an extra layer of security for web applications that protects them from malicious attacks. CSP instructs the browser on what resources are allowed to load on a specific webpage. Usually, the website administrator configures a CSP by adding a special header to the server’s response for a webpage, then the browser receives the CSP header and understands what resources are allowed to load. Talisman is an extension for Flask that simplifies the process of adding CSP to Flask applications.

  1. Install flask-wtf and talisman packages
  2. Open the app.py file.
  3. Edit the app.py file to include Content Security Policies.
  4. In the app.py file, import and configure CSRF protection.

    Save and close the file.

  5. Navigate to the templates directory.
  6. Open the index.html file.
  7. Edit the HTML page to include CSRF configuration.

    Save and close the file.

  8. Exit the templates directory.

Configuring Nginx as a Reverse Proxy

Nginx acts as a reverse proxy between your web server and clients. It directs incoming requests based on your request configuration settings. In this section, we are going to configure our application for reverse proxy for efficient request handling and load balancing. We are also going to request a free SSL certificate from Let’s Encrypt to implement HTTPS that secures the communication between a user and a web server for our domain.

  1. Log in to the Vultr Customer Portal.
  2. Navigate to the Products page.
  3. From the side menu, expand the Network drop-down, and select DNS.
  4. Click the Add Domain button in the center.
  5. Follow the setup procedure to add your domain name by selecting the IP address of your server.
  6. Set the following hostnames as your domain’s primary and secondary nameservers with your domain registrar.
  7. Install Nginx.
  8. Create a new Nginx configuration file.
  9. Paste the following configuration into the file.
  10. Activate the virtual host configuration.
  11. Test the Nginx configuration.
  12. Reload Nginx.
  13. Allow incoming connections to port 80 and 443.
  14. Install the certbot package.
  15. Request an SSL certificate.
  16. Start a Gunicorn server.
  17. You can now access your Flask application at https://.

Do More With Python Apps on Vultr

Conclusion

In this article, we explored how to deploy a Flask application using Nginx reverse proxy and Gunicorn with CSRF protection and CSPs. We also secured the application by adding an SSL certificate and enabling HTTPS access.

This is a sponsored article by Vultr. Vultr is the world’s largest privately-held cloud computing platform. A favorite with developers, Vultr has served over 1.5 million customers across 185 countries with flexible, scalable, global Cloud Compute, Cloud GPU, Bare Metal, and Cloud Storage solutions. Learn more about Vultr



Source link

case studies

See More Case Studies

Contact us

Partner with Us for Comprehensive IT

We’re happy to answer any questions you may have and help you determine which of our services best fit your needs.

Your benefits:
What happens next?
1

We Schedule a call at your convenience 

2

We do a discovery and consulting meting 

3

We prepare a proposal 

Schedule a Free Consultation