0

I was using django with Apache2 when I encountered an error in the localhost server. Apache would throw a 500 Internal Server Error, with no logs or terminal feedback. I have mod_wsgi installed with official Apache2. My apache2.conf looks like this:

WSGIPythonHome "/usr"
WSGIPythonPath var/www/mysite

<VirtualHost *:80> 
    WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
    LoadModule wsgi_module "/home/server/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"

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

    WSGIDaemonProcess 127.0.1.1 python-path=/var/www/mysite python-home=/usr
    WSGIProcessGroup 127.0.1.1

    Alias /file.zip /var/www/mysite/static/file.zip
    Alias /static/ /var/www/mysite/static/

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

I do have some logs from way back when I just started out with apache. They told me that it failed to import django from wsgi.py, but i do not think these will be that helpful becuase they were generated such a long time ago. I am leaving it in, just in case. I have tried for two days of looking on multiple Stack Exchange sites, and have not found a reasonable explanation. I use sudo apachectl start to start my server. Could someone help me find why this is happening and how to prevent it? Thanks!

EDIT:

I deleted the old error logs and ran the program again. The error log shows that it could not import django from wsgi.py, which was the same in the previous error logs that were created when I started out.

DKH
  • 452
  • 7
  • 15
BoxifyMC
  • 43
  • 10
  • usually apache has special folder for files `access.log` and `error.log` and it uses `error.log` to save information about all problems. I uses `nginx` and I have `/var/log/nginx/` and apache should have similar folder `/var/log/apache2/`. BTW: in config you can create separated files `access.log` and `error.log` for every domain. – furas Jul 09 '20 at 11:46
  • @furas I know, and I have loooked in those folders. access.log and error.log are both missing. – BoxifyMC Jul 09 '20 at 12:24
  • you can define own [error log per VirtualHost](https://stackoverflow.com/questions/176/error-log-per-virtual-host) and then you will know where they should be. OR maybe apache has problem to access standard folder to save data. – furas Jul 09 '20 at 12:30
  • @furas it still does not give me error logs, however way back when I was just starting out with apache, error logs were given. If you want to know the contents of those, they say they cannot import django from wsgi.py, however i do not think they will be helpful due to them being generated long ago. – BoxifyMC Jul 13 '20 at 08:59
  • I am however now going to edit the question so that it includes the error logs, in case it is helpful. – BoxifyMC Jul 13 '20 at 09:05
  • remove all from logs (or rename log files) and run code again to see only new errors. – furas Jul 13 '20 at 12:37
  • The new error still says 'could not import django from wsgi.py', however I will edit the question now to include this. – BoxifyMC Jul 13 '20 at 16:24

1 Answers1

1

There is a nice serve django with apache tutorial. It is old yet it should still be well applicable to your situation.

From your config file, your problem seems to be in one of these two lines:

LoadModule wsgi_module "/home/server/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIDaemonProcess 127.0.1.1 python-path=/var/www/mysite python-home=/usr

You might be using the wrong module or wrong paths. I don't know about the module, but you can check for paths, specifically for the one to use with python-home.

First check to see you have django installed globally: python -m django --version. (use python3 if you have both v2 and v3)

If it is in the global, then check Find where python is installed (if it isn't default dir) to see where your python is installed and use that path.

If you get an error that it is not installed, you are probably using a virtual environment for your project. Then use that path instead. Different virtual environment programs exist, so you need to check their docs to find the path you need. For example, it might be under /var/www/mysite/venv. then it will be:

WSGIDaemonProcess 127.0.1.1 python-path=/var/www/mysite python-home=/var/www/mysite/venv

Warning: Some virtual environments cannot be moved. For example, if your project was under "/home/user/webproject", you used "python -m venv venv" in it, and tried copying the folder under apache, this path for the virtual environment won't work. In such a case, you will need pip freeze procedure.


I could see what was nagging me with your Loadmodule line.

First, it shows you have your local python installation in /home/server/.local/lib/python3.8/ so you can use this as your python-home but I don't recommend using a location under the user folder.

Then, use /usr/sbin/httpd -M to see if apache ever starts and loads the module. It is possible you haven't set it to read user directories or not from that location. It might also simply be a matter of quotes, remove quotes.

In short, you worked on your project in private but apache will serve the public. Make sure you use a separate installation in another folder apache reads/writes freely without compromising users.

Yılmaz Durmaz
  • 2,374
  • 12
  • 26
  • `pip freeze` procedure I am referring is the process of getting a list of current installed packages into a file, mostly called `requirements.txt`, edit it if needed, move it to new location and/or creating a new virtual environment and finally `pip install` from that file. you may already know the procedure, else you can find it on the internet – Yılmaz Durmaz Jan 20 '22 at 12:22