Deploying a Flask application on Ubuntu using Apache and mod_wsgi involves several steps. Here’s a step-by-step guide:
Step 1: Install Apache and mod_wsgi
First, you need to install Apache and mod_wsgi. You can do this using the following commands:
sudo apt-get update sudo apt-get install apache2 sudo apt-get install libapache2-mod-wsgi-py3
Step 2: Install Flask
Next, you need to install Flask. You can do this using pip:
sudo apt-get install python3-pip pip3 install flask
Step 3: Create Your Flask Application
Now you can create your Flask application. For example, you might create a directory for your application and a simple “Hello, World!” application like this:
mkdir ~/myflaskapp nano ~/myflaskapp/app.py
In the app.py
file, you might write something like this:
from flask import Flask app = Flask(name)
@app.route("/") def hello(): return "Hello, World!"
if name == "main": app.run()
Step 4: Configure Apache
Next, you need to configure Apache to serve your Flask application. You can do this by creating a new configuration file in the **/etc/apache2/sites-available/**
directory:
sudo nano /etc/apache2/sites-available/myflaskapp.conf
In this file, you might write something like this:
ServerName mydomain.com ServerAdmin admin@mydomain.com WSGIScriptAlias / /var/www/myflaskapp/myflaskapp.wsgi Order allow,deny Allow from all ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combinedStep 5: Create the WSGI File
Next, you need to create the WSGI file that Apache will use to serve your Flask application. You can do this with the following commands:
mkdir /var/www/myflaskapp nano /var/www/myflaskapp/myflaskapp.wsgi
In the **myflaskapp.wsgi**
file, you might write something like this:
import sys sys.path.insert(0, '/home/username/myflaskapp')
from app import app as application
Step 6: Enable the Site
Finally, you can enable the site and restart Apache to start serving your Flask application:
sudo a2ensite myflaskapp sudo service apache2 restart
Now, if you navigate to your server’s IP address or domain name in a web browser, you should see your Flask application running.
Please replace mydomain.com
, admin@mydomain.com
, and **/home/username/myflaskapp**
with your actual domain name, admin email, and the actual path to your Flask application respectively.
Also, ensure that the user under which Apache is running has the necessary permissions to access the directories and files of your Flask application.
The post How to Deploying Flask Application on Ubuntu (Apache+WSGI) appeared first on Abhay Singh.