Home > Net >  Django apache doesn't work when using a virtual environment
Django apache doesn't work when using a virtual environment

Time:10-22

I am trying to deploy my django project on a linux ubuntu server using apache2. I can deploy it correclty when I dont use a virtual venv. I rebuilt my VM and did the same thing but with using a virtual env and it not working.

When I visit my website url it give me this error Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.

here is my /etc/apache2/sites-available/mysite.conf

<VirtualHost *:80>
    ServerName MYIPSERVER

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

    WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite/venv/lib/python3.10/site-packages
    WSGIProcessGroup mysite
    WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py

    Alias /robots.txt /var/www/mysite/static/robots.txt
    Alias /favicon.ico /var/www/mysite/static/favicon.ico
    Alias /static/ /var/www/mysite/static/
    Alias /static/ /var/www/mysite/media/

    <Directory /var/www/mysite/mysite>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    <Directory /var/www/mysite/static>
        Require all granted
    </Directory>

    <Directory /var/www/mysite/media>
        Require all granted
    </Directory>
</VirtualHost>

Before without the venv the WSGIDaemonProcess was setup like this

WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite but it wasn't working so I changed it for that:

WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite/venv/lib/python3.10/site-packages

and it still not working. I am not sure where the problem is coming from. my project folder is located into /var/www

CodePudding user response:

You need to set the python-home parameter to the folder that contains your virtualenv and the python-path parameter to the folder that contains your project. See https://docs.djangoproject.com/en/stable/howto/deployment/wsgi/modwsgi/#using-mod-wsgi-daemon-mode

WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com
WSGIProcessGroup example.com
  • Related