Deploying Django with XAMPP or Apache
I was just ready to deploy my first Django project when I realized I had never ever deployed anything written in Python. I turned to Django’s official deploying documentation, but I think it was obviously lacking some user-friendliness and maybe most of all - rookie-friendliness. In any case, I was still not sure if and how it all could be applied to my current setup with XAMPP, virtual hosts and Windows. So here are a few steps that maybe should be taken more as guidelines than literally, if your environment is anything like mine.
1. Download mod_wsgi
Download the 32-bit version of mod_wsgi for your version of Python / Apache. There are also 64-bit binaries out there. In any case, it’s very important that Python, Apache and mod_wsgi all use the same processor architecture. Before I realized this I ran into problems when starting Apache -
Cannot load mod_wsgi.so into server: The specified module could not be found.. If you really put the file in the right place (step 2), then the process architecture problem is to be taken seriously. I had to reinstall Python to 32-bit before I got around the problem.
2. Install mod_wsgi
Simply copy mod_wsgi.so to <your xampp folder>\modules.
3. Configure Apache
First of all, add the following line at the bottom of the current module load list in httpd.conf, maybe somewhere around line 130+.
LoadModule wsgi_module modules/mod_wsgi.soThen, let’s pretend you have the following file structure of your Django project:
- A fresh Django project, initiated using
django-admin.py startproject mysite, in c:/mysite. This will probably have generated a file in c:/mysite/mysite/wsgi.py, which is a file we will need.
- A virtualenv (optional) in c:/virtualenv. Otherwise, this should be the Python path where you have Django libraries and dependencies installed.
Well, then you can go ahead and edit your httpd-vhosts.conf to look something like this:
# Path for virtualenv, cannot be within <VirtualHost> WSGIPythonPath c:/virtualenv/Lib/site-packages <VirtualHost *:80> # Site name ServerName mysite.com # Setup WSGI WSGIScriptAlias / "c:/mysite/mysite/wsgi.py" <Directory "c:/mysite/mysite"> <Files wsgi.py> Order deny,allow Allow from all </Files> </Directory> # Static files AliasMatch ^/([^/]*\.css) c:/mysite/static/styles/$1 Alias /media/ c:/mysite/media/ Alias /static/ c:/mysite/static/ <Directory c:/mysite/static> Order deny,allow Allow from all </Directory> <Directory c:/mysite/media> Order deny,allow Allow from all </Directory> </VirtualHost>
4. Modify wsgi.py
In my case, Django had troubles finding the files for my project, so I had to insert the following lines at the top of c:/mysite/mysite/wsgi.py:
import sys sys.path.append('c:/mysite/mysite') sys.path.append('c:/mysite')
5. Start apache
Start apache and hold your thumbs. If it starts successfully you should now be able to browse to your new Django site at mysite.com. If you get some kind of Apache error, like a 500 internal error, look for clues in the Apache error log. It was really helpful for me to find mistakes I had done along the way.
It’s fully possible to keep developing against c:/mysite. One problem though, that I have yet to resolve, is that the wsgi_mod will not detect source changes to the Python files on the fly. If you make changes to the source files, you will have to restart Apache. If you run Linux, it’s possible to use wsgi’s daemon mode. I’m sure there are ways also for Windows if one needs it!