-1

I'm trying to deploy my first flask application and I'm running into some issues. I had my app working on my local machine with the build in flask development server, and all my dependencies were managed by pipenv. I uploaded my app to /var/www/directory_printer and ran pipenv install. Then I created a apache vhost file and pointed it to my .wsgi file: WSGIScriptAlias / /var/www/directory_printer/directory.wsgi

In directory.wsgi, I import my app. At the beginning of my main app file, I import flask. When I try to access my app, I get a 500 error. In the apache error log I get: ModuleNotFoundError: No module named 'flask'

If I start an interactive python shell in the directory_printer folder with the pipenv shell activated, I can import flask just fine. I tried putting the path to my virtual env at the beginning of my directory.wsgi file: #!/path/to/venv but that doesn't seem to help. I'm sure I'm missing something simple, but I can't seem to see what it is. Any help would be appreciated. Thanks!

davidism
  • 121,510
  • 29
  • 395
  • 339
user1432738
  • 101
  • 1
  • 5
  • would you mind to show how the entire script looks like? Change your variable names and show only generic paths and name. It maybe easier to help looking at the script – Laenka-Oss Mar 11 '22 at 12:32
  • When I put `import flask` on line 1 of my .wsgi file, I get the error. my scripts never get run – user1432738 Mar 11 '22 at 15:18

2 Answers2

0

Often on Linux systems a home directory is not accessible to other users so the Apache user will not be able to read anything under the directory. Permissions can also be wrong on Python packages that have been installed making them inaccessible as well.

davidism
  • 121,510
  • 29
  • 395
  • 339
Tony Frank
  • 248
  • 1
  • 2
  • 8
  • Ya, I was wondering if this was the issue. I tried running the pipenv install command as www-data, but that doesn't work. I'm assuming it is a common use case for people to use pipenv with wsgi, so there must be some way to make the permissions work – user1432738 Mar 11 '22 at 15:21
0

Ok, not sure if this is the correct answer, but it is now working for me.

First create a .venv folder in the project root folder then change permissions: sudo chown www-data:www-data .venv

Then create a virtual environment and install your requirements from Pipfile as the user wsgi will run as:

sudo -su www-data python3 -m virtualenv -p python3 .venv pipenv install

This will install your virtual environment in the project folder. Check out this answer for more: How to set PIPENV_VENV_IN_PROJECT on per-project basis

Then add this to your .wsgi folder:

with open(activate_this) as file_:
    exec(file_.read(), dict(__file__=activate_this))

Bottom of this page for more info: https://flask.palletsprojects.com/en/2.0.x/deploying/mod_wsgi/

And now it works! Hopefully this will help somebody else out as well.

user1432738
  • 101
  • 1
  • 5