0
votes

I am following this tutorial to Deploy Flask on Ubuntu with WSGI Apache2. This is how my directory looks like:

FlaskApp
    FlaskApp
        static
        templates
        __init__.py
    flaskapp.wsgi

I created a virtualenv. I installed Flask in the virtualenv. When I activate the virtualenv, __ini__.py starts the webserver.

When I try to connect to my live server, I get an internal server error.

The problem is that WSGI does not find Flask. But Flask is installed. When I attempt to install Flask in the virtualenv, I get "Requirement already satisfied: flask in /usr/lib/python2.7/dist-packages (0.10.1)". Is that a problem?

I changed the compiled version to python 2.7

[Tue Oct 30 15:16:23.863324 2018] [mpm_event:notice] [pid 29283:tid 139720393185152] AH00491: caught SIGTERM, shutting down
[Tue Oct 30 15:16:23.943705 2018] [wsgi:warn] [pid 29915:tid 140238218004352] mod_wsgi: Compiled for Python/2.7.11.
[Tue Oct 30 15:16:23.943747 2018] [wsgi:warn] [pid 29915:tid 140238218004352] mod_wsgi: Runtime using Python/2.7.12.
[Tue Oct 30 15:16:23.944430 2018] [mpm_event:notice] [pid 29915:tid 140238218004352] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
[Tue Oct 30 15:16:23.944448 2018] [core:notice] [pid 29915:tid 140238218004352] AH00094: Command line: '/usr/sbin/apache2'
[Tue Oct 30 15:17:25.679253 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] mod_wsgi (pid=29918): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Oct 30 15:17:25.679276 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] mod_wsgi (pid=29918): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Tue Oct 30 15:17:25.679292 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] Traceback (most recent call last):
[Tue Oct 30 15:17:25.679304 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]   File "/var/www/FlaskApp/flaskapp.wsgi", line 19, in <module>
[Tue Oct 30 15:17:25.679340 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]     from FlaskApp import app as application
[Tue Oct 30 15:17:25.679349 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Tue Oct 30 15:17:25.679371 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003]     from flask import Flask
[Tue Oct 30 15:17:25.679388 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 198.167.223.52:14003] ImportError: No module named flask
[Tue Oct 30 15:17:38.089586 2018] [wsgi:error] [pid 29918:tid 140237992269568]     [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Oct 30 15:17:38.089614 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Tue Oct 30 15:17:38.089629 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699] Traceback (most recent call last):
[Tue Oct 30 15:17:38.089644 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/flaskapp.wsgi", line 19, in <module>
[Tue Oct 30 15:17:38.089663 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]     from FlaskApp import app as application
[Tue Oct 30 15:17:38.089670 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Tue Oct 30 15:17:38.089679 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699]     from flask import Flask
[Tue Oct 30 15:17:38.089693 2018] [wsgi:error] [pid 29918:tid 140237992269568] [remote 95.91.245.250:11699] ImportError: No module named flask
[Tue Oct 30 15:17:38.417619 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module.
[Tue Oct 30 15:17:38.417638 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699] mod_wsgi (pid=29918): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Tue Oct 30 15:17:38.417660 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699] Traceback (most recent call last):
[Tue Oct 30 15:17:38.417674 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/flaskapp.wsgi", line 19, in <module>
[Tue Oct 30 15:17:38.417696 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]     from FlaskApp import app as application
[Tue Oct 30 15:17:38.417702 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 1, in <module>
[Tue Oct 30 15:17:38.417711 2018] [wsgi:error] [pid 29918:tid 140238109865728] [remote 95.91.245.250:11699]     from flask import Flask
[Tue Oct 30 15:17:38.417723 2018] [wsgi:error] [pid 29918:tid 140238109865728]     [remote 95.91.245.250:11699] ImportError: No module named flask

__init__.py

from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
    return "Hello, I love Digital Ocean!"
if __name__ == "__main__":
    app.run()

FlaskApp/flaskapp.wsgi

activator = '/var/www/FlaskApp/FlaskApp/venv/bin/activate_this.py'  # Looted from virtualenv; should not require modification, since it's defined relatively
with open(activator) as f:
    exec(f.read(), {'__file__': activator})

import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'

/etc/apache2/sites-enabled/FlaskApp.conf

<VirtualHost *:80>
                ServerName 18.196.125.229
                ServerAdmin [email protected]
#               WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/lib/python2.7/
# WSGIDaemonProcess FlaskApp python-home=flask in /usr/local/lib/python2.7/dist-packages/
WSGIDaemonProcess FlaskApp python-home=/var/www/FlaskApp/FlaskApp/venv/FlaskApp
                 WSGIProcessGroup FlaskApp
                WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
                <Directory /var/www/FlaskApp/FlaskApp/>
                        Order allow,deny
                        Allow from all
                </Directory>
                Alias /static /var/www/FlaskApp/FlaskApp/static
                <Directory /var/www/FlaskApp/FlaskApp/static/>
                        Order allow,deny
                        Allow from all
                </Directory>
                ErrorLog ${APACHE_LOG_DIR}/error.log
                LogLevel warn
                CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I tried to uninstall flask - reinstall flask in virtualenvironment:

sudo pip uninstall flask

/usr/local/lib/python2.7/dist-packages/pip-18.1- 
py2.7.egg/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old         version of cryptography ([1, 2, 3]) may cause slowdown.
  warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want     sudo's -H flag.
Uninstalling Flask-0.10.1:
  Would remove:
    /usr/lib/python2.7/dist-packages/Flask-0.10.1.egg-info
    /usr/lib/python2.7/dist-packages/flask
Proceed (y/n)? y
  Successfully uninstalled Flask-0.10.1

source venv/bin/activate

sudo pip install Flask

(venv) ubuntu@ip-172-26-9-95:/var/www/FlaskApp/FlaskApp$ sudo pip install Flask
/usr/local/lib/python2.7/dist-packages/pip-18.1-py2.7.egg/pip/_vendor/requests/__init__.py:83: RequestsDependencyWarning: Old version of cryptography ([1, 2, 3]) may cause slowdown.
warnings.warn(warning, RequestsDependencyWarning)
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not     owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Flask
  Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB)
100% |████████████████████████████████| 92kB 17.8MB/s
Requirement already satisfied: Jinja2>=2.10 in /usr/local/lib/python2.7/dist- 
packages (from Flask) (2.10)
Requirement already satisfied: itsdangerous>=0.24 in     /usr/local/lib/python2.7/dist-packages (from Flask) (1.1.0)
Requirement already satisfied: Werkzeug>=0.14 in /usr/local/lib/python2.7/dist-packages (from Flask) (0.14.1)
Requirement already satisfied: click>=5.1 in /usr/local/lib/python2.7/dist-packages (from Flask) (7.0)
Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/dist-packages (from Jinja2>=2.10->Flask) (1.0)
Installing collected packages: Flask
Successfully installed Flask-1.0.2

still same ImportError: No module named flask

1
Apache is compiling for Python 3.5.2, while you use 2.7. Maybe that caused the problem?Ruben Helsloot
can you uninstall flask, activate virtualenv and install flask in virtual environment ?VillageMonkey
@Ruben Helsloot I changed the compiled version to 2.7 still same errorTim Kecks
@VillageMonkey I tried to uninstall - reinstall flask as described but still same ImportErrorTim Kecks
once you activate your virtualenv you should not need to use sudo pip, just do pip install Flask once your virtualenv is active.arunkumar

1 Answers

0
votes

Your problem is that you are executing pip with sudo.

When in a virtualenv, you have access and control over all the files that pip needs to modify. By running with sudo, you execute as the superuser, and thus execute pip outside of the context of the virtualenv.

Generally you should be cautious about using pip with sudo, as when installing packages from less trustworthy sources, you may find yourself installing malware.

To install successfully,

# Must be in virtualenv.
pip install flask