Flask App Deyploy on Ubuntu 14.04. Apache2 with Python 3.7

Posted April 29, 2020 2.4k views
UbuntuMySQLApacheLAMP StackGitPythonMariaDBPython FrameworksDeployment

Hi all,

so I have a simple Flask application that I would like to deploy to my Ubuntu 14.04. that is already set as a LAMP server. I have the Apache default configuration with MySQL and both Python 2.7 and 3.7 set up. I have a specific subdomain where I want to deploy my Flask app. I followed this tutorial:

But at the end I get a 500 error and when I look at the Apache error log G get that the flask module is not installed which is not true because when I check my virtual enviroment every requirement is there. I made my virtual enviroment with ‘pipenv shell’ and then I installed my requirements with 'pip install -r requirements.txt’.

Error from the log:

mod_wsgi (pid=3501): Target WSGI script '/var/www/' cannot be loaded as Python module.
mod_wsgi (pid=3501): Exception occurred processing WSGI script '/var/www/'.
Traceback (most recent call last):
 File "/var/www/", line 12, in <module>
   from maktivnosti_backend import app as application
  File "/var/www/", line 1, in <module>
    from flask import Flask, request, jsonify
ImportError: No module named 'flask'

Here is my setup:

My directory tree:


My Apache configuration:

<IfModule mod_ssl.c>
<VirtualHost *:443>
        #referring the user to the recipes application
        DocumentRoot /var/www/
        WSGIScriptAlias / /var/www/

        <Directory /var/www/>
        Require all granted
                Order allow,deny
                Allow from all

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteEngine on
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/
SSLCertificateChainFile /etc/letsencrypt/live/

My wsgi file:

activate_this = '/home/(username)/.local/share/virtualenvs/maktivnosti_backend-tbfZiu-1/bin/'
with open(activate_this) as file_:
    exec(, dict(__file__=activate_this))
import sys
import logging
import site
sys.path.insert(0, '/var/www/')
from maktivnosti_backend import app as application
application.secret_key = '(my secret key)'

If anyone can help me solve this problem I would really apreciate it!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

I have the same issue with flask not being recognized as an installed dependency and I am using apache2 and ubuntu 20.04 as well as pipenv. Did you solve this issue?