Reputation: 10862
I have a Django project nice and running at 127.0.0.1:8000. I normally run it via:
$ python manage.py runserver
Now, what I want is to run it under Apache
.
This is what I tried:
Installed and enabled mod_wsgi. It is working indeed, because when I installed it I saw in the console: "apache2_invoke: Enable module wsgi"
Edited 000-default.conf
file in this way:
<VirtualHost *:80>
WSGIScriptAlias / /home/jacobian/django/apps/apps/wsgi.py
<Directory /home/jacobian/django/apps/apps/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
That's it. I'm not sure what I'missing, but after I restart apache
and go to localhost
, in my browser I see a page from /var/www
folder. Whereas I expect to see a django application. I guess I need to edit apache2.conf
(how?) and do some other magic tricks (like download and install some extra magic stuff, make some magic apache configuration etc).
I tried dozens of tutorial, but none of them helped - either apache is not restarted or django project is not rendered in browser. So, I need some basic steps to follow to make it work.
EDIT
When I try to start apache without the tag Files
inside the tag Directory
, then apache fails to do that, but at the same time I see no additional messages in error.log
. If, however, I use this tag like:
<Directory /home/jacobian/django/apps/apps/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
then I get a bunch of error messages in the error.log
. This is the whole list:
[Mon Sep 21 13:55:45.901935 2015] [wsgi:warn] [pid 39222:tid 140543251101568] mod_wsgi: Compiled for Python/2.7.8.
[Mon Sep 21 13:55:45.901995 2015] [wsgi:warn] [pid 39222:tid 140543251101568] mod_wsgi: Runtime using Python/2.7.9.
[Mon Sep 21 13:55:45.902149 2015] [mpm_event:notice] [pid 39222:tid 140543251101568] AH00489: Apache/2.4.10 (Ubuntu) mod_wsgi/4.3.0 Python/2.7.9 configured -- resuming normal operations
[Mon Sep 21 13:55:45.902186 2015] [core:notice] [pid 39222:tid 140543251101568] AH00094: Command line: '/usr/sbin/apache2'
[Mon Sep 21 13:55:46.851442 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] mod_wsgi (pid=41314): Target WSGI script '/home/jacobian/django/apps/apps/wsgi.py' cannot be loaded as Python module.
[Mon Sep 21 13:55:46.851498 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] mod_wsgi (pid=41314): Exception occurred processing WSGI script '/home/jacobian/django/apps/apps/wsgi.py'.
[Mon Sep 21 13:55:46.851526 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] Traceback (most recent call last):
[Mon Sep 21 13:55:46.851557 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] File "/home/jacobian/django/apps/apps/wsgi.py", line 14, in <module>
[Mon Sep 21 13:55:46.851696 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] application = get_wsgi_application()
[Mon Sep 21 13:55:46.851734 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] File "/usr/lib/python2.7/dist-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Sep 21 13:55:46.851800 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] django.setup()
[Mon Sep 21 13:55:46.851818 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] File "/usr/lib/python2.7/dist-packages/django/__init__.py", line 20, in setup
[Mon Sep 21 13:55:46.851874 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Sep 21 13:55:46.851892 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, in __getattr__
[Mon Sep 21 13:55:46.851994 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] self._setup(name)
[Mon Sep 21 13:55:46.852013 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
[Mon Sep 21 13:55:46.852038 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] self._wrapped = Settings(settings_module)
[Mon Sep 21 13:55:46.852053 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] File "/usr/lib/python2.7/dist-packages/django/conf/__init__.py", line 98, in __init__
[Mon Sep 21 13:55:46.852072 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] % (self.SETTINGS_MODULE, e)
[Mon Sep 21 13:55:46.852097 2015] [wsgi:error] [pid 41314:tid 140543032010496] [client 127.0.0.1:50601] ImportError: Could not import settings 'apps.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named apps.settings
I'm not sure what can be wrong with wsgi.py
file. It's a standard file created with $ django-admin startproject apps
. Probably we miss WSGIPythonPath
variable in the apache conf
file. But I'm not sure where it should point to in the real world case and not in some abstract case. If I follow some tutorials and set it like WSGIPythonPath /home/jacobian/django/apps/
, then apache is unable to start. So, I do not know what can I do.
Upvotes: 0
Views: 3058
Reputation: 830
Usually its very easy to configure apache2 + wsgi for django; but if you miss out any step it can be cumbersome to trace out the issue; just try to iterate over the steps mentioned below, it will help you trace out the issue.
Please find the django documenatation on this here.
Here are the steps you might want to try:
[Edit to fit user needs]
*RENAMED certain fields to make it more easier to understand
Lets say you created you django project in /home/jacobian/django/
django_project/
|-- django_project
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- wsgi.py
`-- manage.py
It should be similar to above tree your path to wsgi specified in apache will be:
/home/jacobian/django/django_project/django_project/wsgi.py
or in other terms
PROJECT_PATH would mean /home/jacobian/django/
PROJECT_NAME would mean django_project
[Specific to Ubuntu]
Install Apache
apt-get install apache2
Install mod_wsgi
apt-get install libapache2-mod-wsgi
Make a WSGI handler script; usaully go like this depending on the version of django installation:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Put it all in apache conf by writing it to a new file say sitename.conf
in /etc/apache2/sites-available/
<VirtualHost *:80>
WSGIScriptAlias / PROJECT_PATH/PROJECT_NAME/PROJECT_NAME/wsgi.py
Alias /static PROJECT_PATH/PROJECT_NAME/static/
Alias /media PROJECT_PATH/PROJECT_NAME/media/
ServerName www.example.com
<Directory />
Options -Indexes
</Directory>
<Directory PROJECT_PATH/PROJECT_NAME/>
Order allow,deny
Allow from all
Options -Indexes
</Directory>
</VirtualHost>
Enable that site
sudo a2ensite sitename.conf
Restart Apache
sudo service apache2 restart
Upvotes: 2