Flask is a popular and lightweight Python web framework that enables developers to build web applications with ease. One of the key aspects of deploying a Flask application is choosing the right web server and gateway interface. In this article, we will discuss how to deploy a Flask app using Apache and WSGI (Web Server Gateway Interface) on a Linux-based system. Apache is a powerful and widely-used web server, while WSGI is a standard interface between web servers and Python applications.

Advertisement

This comprehensive guide will walk you through the entire process of deploying a Flask app with Apache and WSGI on a Linux machine. By the end of this tutorial, you will have a functional Flask application running on an Apache web server with WSGI as the interface.

Prerequisites

To deploy a Flask application on Ubuntu, you will need to have the following prerequisites installed:

  • Python 3: Flask is a Python web framework, so you will need to have Python installed on your server.
  • Flask: You will need to install Flask using pip, the Python package manager.
  • Apache2: Apache is a popular web server that can be used to host Flask applications.
  • mod_wsgi: This Apache module is used to interface with WSGI-compatible web applications like Flask.

Step 1: Begin Packages Installation

Let’s begin with the required packages installation on your system using the following steps:

  1. Update the package list and upgrade any outdated packages:
    sudo apt-get update 
    sudo apt-get upgrade 
    
  2. Install Python 3 and pip:
    sudo apt-get install python3 python3-pip python3-venv 
    
  3. Install Apache2 and mod_wsgi:
    sudo apt-get install apache2 libapache2-mod-wsgi-py3
    

Step 2: Set up your Flask application

Create a directory for your application and copy all files to it. Then create a virtual environment to isolate your Flask application and install the required packages.

  1. Create a directory for your Flask application:
    mkdir /var/www/flask-app 
    cd /var/www/flask-app 
    
  2. Copy all the files of the Flask application to the directory you just created
  3. Create the virtual environment for your application and active it:
    python3 -m venv venv 
    source venv/bin/activate 
    
  4. Install the required libraries in the virtual environment:
    pip install flask 
    
  5. Create a new Python file named app.py in the flaskapp directory and add the following code:

    This creates a simple Flask app that returns “Hello, World!” when you access the root URL.

Step 3: Test the Flask app

To test the Flask app, run the following command in the terminal:

 export FLASK_APP=app.py 
 flask run 

This should start the Flask development server. Open your web browser and go to http://localhost:5000. You should see “Hello, World!” displayed in your browser.

Step 4: Create WSGI Configuration file

  1. Create a WSGI entry point for your Flask application:
    nano /var/www/flaskapp/flaskapp.wsgi 
    
  2. Paste the following code into the file, replacing “flaskapp” with the name of your application:

  3. Save the file and close it.

Step 5: Configuring Apache

  1. Create an Apache configuration file for your application:
    sudo nano /etc/apache2/sites-available/flaskapp.example.com.conf 
    
  2. Paste the following code into the file, replacing “flaskapp” with the name of your application and the paths with the correct paths for your setup:

  3. Enable the new Apache configuration:
    sudo a2ensite flaskapp.example.com 
    
  4. Restart Apache to apply the changes:
    sudo service apache2 restart 
    

Your Flask application should now be up and running on your Ubuntu server. You can access it by visiting the URL of your server in a web browser.

Step 6: Test the Flask app with Apache

To test the Flask app with Apache, open your web browser and go to http://flaskapp.example.com (replace flaskapp.example.com with your domain name). You should see “Hello, World!” displayed in your browser.

Congratulations! You have successfully deployed a Flask app with Apache and WSGI on Linux.

Conclusion

Deploying Flask apps with Apache and WSGI on Linux can be challenging, but it is a crucial step in making your app available on the internet. This comprehensive guide has shown you how to deploy a Flask app with Apache and WSGI on Linux, from creating a Flask app to configuring Apache and creating a WSGI script.

Remember to always test your app after deployment and make sure it is running smoothly. With this knowledge, you can now deploy your Flask apps with confidence and take your web development skills to the next level.

Share.
Leave A Reply

Exit mobile version