1
votes

I am hosting my django app on Linode with Ubuntu OS and i have configured apache webserver. When I try to access the site i get 500 Internal Server error Apache logs show the following error

Traceback (most recent call last):
File "/home/mosajan/artistry/artistry/wsgi.py", line 12, in <module>
from django.core.wsgi import get_wsgi_application<br>
ImportError: No module named 'django'
Target WSGI script '/home/mosajan/artistry/artistry/wsgi.py' cannot be loaded as Python module.

Exception occurred processing WSGI script '/home/mosajan/artistry/artistry/wsgi.py'.

wsgi.py

import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('home/mosajan/artistry/')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'artistry.settings')
application = get_wsgi_application()

apache2 conf file artistry.conf

Alias /static /home/mosajan/artistry/static
<Directory /home/mosajan/artistry/static>
    Require all granted
</Directory>
<Directory /home/mosajan/artistry/artistry>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>
WSGIScriptAlias / /home/mosajan/artistry/artistry/wsgi.py
WSGIDaemonProcess artistry python-path=/home/mosajan/artistry python-home=/home/mosajan/artistry/venv
WSGIProcessGroup artistry
WSGIPythonHome /home/mosajan/artistry/venv
WSGIPythonPath /hom/mosajan/artistry

File structure
enter image description here

1
When you activate you manually activate your venv and do something like python manage.py runserver 0.0.0.0:8000 in your project folder, are you able to see the project and run it without any errors?Tiago Martins Peres
Thanks for the edit and the link. But i have installed the correct version of wsgi python. Is there anything incorrect in my code?ms394
when i run the server using python manage.py runserver i see no errors. I cant access the site with the ip though as the port 8000 is disabled. But there are no errors when i run the command.ms394

1 Answers

1
votes

This answer assumes that you manually activated the venv and that when you do something like python manage.py runserver 0.0.0.0:8000 in your project folder, are you able to see the project and run it without any errors. If that's the case, this means you have installed django as well as other needed project requirements / packages, so you can deactivate the venv for now.

Let's start by making sure that the server's IP address was added to ALLOWED_HOSTS in your settings.py file.

Now, with the venv deactivated, make sure to install Apache 2.4 with the service httpd with the module wsgi which is gonna help the Django app behave as a web app totally compatible with Apache 2.4. If you're using yum as a package manager, then you'd run something like this

yum install -y httpd python36u-mod_wsgi

Let's now add a group www

groupadd www

and edit the group

vim /etc/group

and add in the end of the file

www:x:10000:root,apache

Go to the root directory and run the following commands to make the www group owner of this directory

chown root.www -R /home/mosajan
chmod 775 -R /home/mosajan

Now in your Apache virtual host configuration file artistry.conf, the version you're presenting has an error in the last line (WSGIPythonPath /hom/mosajan/artistry). Fix it by using WSGIPythonPath /home/mosajan/artistry instead, save the file and check if the Apache config file if fine by running

httpd -t

You should get Syntax OK if all is well. I'm assuming you have that code inside a <VirtualHost *:8000></VirtualHost> block and would add as well an ErrorLog to it as well and probably would structure the file differently. Check if it works, if it doesn't then I would put some more thought into it. You can use this documentation page as reference (How to use Django with Apache and mod_wsgi).

Then, in your wsgi.py file I would change the sys.path.append line to have '/home/mosajan/artistry', so you would have something like this

import os, sys
from django.core.wsgi import get_wsgi_application

sys.path.append('/home/mosajan/artistry')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'artistry.settings')
application = get_wsgi_application()

Then, open port 80, enable and start httpd and check the result in the browser

enable httpd
systemctl start httpd
systemctl status httpd

You should see that it's starting fine and now when you go to the browser you should be able to see the project working just fine.