deploy django application with pipenv on apache
Asked Answered
S

1

8

I have created a python web application in Django 2.0 using pipenv virtualenv

Now, I have to host it on apache server. I have installed libapache2-mod-wsgi-py3 and python-setuptools in the server.

The structure of my application is like

myapp_dir
 |- myapp
    |- settings
       |- __init__.py
       |- production.py
    |- __init__.py
    |- urls.py
    |- wsgi.py
 |- otherapp
 |- templates
 |- static_my_project
 |- manage.py
 |- Pipfile
 |- Pipfile.lock

the path of the application to put on

/home/user/app.application.com/

I have moved all files to the directory and installed all dependencies from Pipfile by running in the directory

pipenv install

This has created a virtualenv and installed all required modules and the path of pipenv --venv gives

# pipenv --venv
/home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_

My VirtualHost configuration looks like

ServerName app.application.com
ServerAlias app.application.com

ErrorLog /home/user/error.log
CustomLog /home/user/custom.log combined

Alias /static /home/user/app.application.com/static_my_project
<Directory /home/user/app.application.com/static_my_project>
    Require all granted
</Directory>

<Directory /home/user/app.application.com/pricearbitrase>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIScriptAlias / /home/user/app.application.com/myapp/wsgi.py

VirtualInclude contains

<IfModule mod_wsgi>

    WSGIPythonHome /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_

</IfModule>

But on accessing http://app.application.com it gives Internal server error and the log file generated contains

[wsgi:error] [pid 60730] mod_wsgi (pid=60730): Target WSGI script '/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py' cannot be loaded as Python module.
[wsgi:error] [pid 60730] mod_wsgi (pid=60730): Exception occurred processing WSGI script '/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py'.
[wsgi:error] [pid 60730] Traceback (most recent call last):
[wsgi:error] [pid 60730]   File "/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py", line 12, in <module>
[wsgi:error] [pid 60730]     from django.core.wsgi import get_wsgi_application
[wsgi:error] [pid 60730] ImportError: No module named django.core.wsgi

Edit 2

app/wsgi.py modified wsgi.py file to activate virtual environment

activate_this = '/home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_/bin/activate_this.py'
exec(compile(open(activate_this,"rb").read(),activate_this, 'exec'), dict(__file__=activate_this))

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "application.settings")

application = get_wsgi_application()

Also, ls -l /path_to_pipenv_venv/bin gives

enter image description here

Septa answered 22/5, 2018 at 9:53 Comment(8)
#14927845Indomitable
removed /bin still same errorSepta
@Alasdair do I need to rerun server after removing /bin?Septa
You also appear to be missing WSGIPythonPath (see the example in the Django docs), but that would cause issues loading myapp, not django.Octet
If I set WSGIPythonPath what will be the path of this configurationSepta
removed /bin, restarted server. still getting same errorSepta
It’s the path to the project directory (the one that contains manage.py).Octet
If removing /bin didn’t help, I’m afraid I don’t have any other suggestions. Hope you manage to get it working.Octet
S
5

First of all I made a symbolic link to pipenv virtualenv to myapp_dir/venv just for convenience when using in apache files. Main problem I encountered are permissions.

If your virtualenv path is /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_ then try this:

sudo -u www-data cat /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_/bin/activate_this.py`

Where www-data is your apache user.

If you get permission denied it's probably the same problem. Just try changing permissions of ~/.local and ~/.local/share that apache process can access them.

Just be advised that this is a development setup and in production I wouldn't use pipenv. I would just generate requirements.txt and setup a normal virtualenv or use gunicorn or something similar.

Sheronsherourd answered 19/8, 2018 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.