-
Notifications
You must be signed in to change notification settings - Fork 2
Run Django on Apache
Alrighty, I took the time to set up my django installation to run on Apache. Here it is documented for you all to see. As a disclaimer, this is all written based on my Gentoo installation. Gentoo handles Apache very differently (much nicer) than other distro’s. It shouldn’t be hard to migrate to whatever you’re using. Also, you can most likely just deal with the built in development server as documented run server
Edit So, I cleaned this up a little bit to make it nicer. This will keep you able to run the development server or apache (or both).
- Setting up a subdomain that runs mod_python 127.0.0.2
- Linking the media directory in Django up to regular Apache for serving the files
- The original howto about mod_python on Apache
- Install mod-python
- `emerge -v mod_python` or `apt-get install mod-python` (i’m just guessing about that, btw)
- Tell Apache to use mod_python
- Add “-D PYTHON” to your APACHE2_OPTS (in this file /etc/conf.d/apache2 on Gentoo)
- if you don’t know where this is specified, try `grep -ir “APACHE2_OPTS” /etc/*`
- Edit your hosts file (/etc/hosts) and add your new subdomain to 127.0.0.1
127.0.0.1 localhost python.localhost
- Make the VirtualHost Directory
mkdir /var/www/python.localhost
- Link the concert project
ln -s /home/elserj/projects/Concert /var/www/python.localhost/htdocs
- Make the new VirtualHost (probably /etc/apache2/httpd.conf )
<IfDefine DEFAULT_VHOST> <VirtualHost 127.0.0.2:80> ServerName python.localhost DocumentRoot "/var/www/python.localhost/htdocs" SetEnv DJANGO_SETTINGS_MODULE concertapp.settings <Directory "/var/www/python.localhost/htdocs"> Options Indexes FollowSymLinks AllowOverride All # Controls who can get stuff from this server. Order allow,deny Allow from all </Directory> <Location /> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE concertapp.settings PythonOption django.root /concert PythonDebug On PythonPath "['/home/elserj/projects/Concert/'] + sys.path" </Location> </VirtualHost> </IfDefine>
- Restart apache
# /etc/init.d/apache2 restart
Link media directory to normal Apache  
One thing that mod_python doesn’t do well on its own is serve static files. Since we have Apache to work with, let’s us that. This one is simple:
- Symlink the media directory of Concert (‘/Concert/concertapp/media/’) to your webroot
# ln -s /home/elserj/projects/Concert/concertapp/media /var/www/localhost/media
This will allow your documents to be served normally by Apache.
As a note, the address that Django will use as the URL is defined in /Concert/settings.py. By default, it is using ‘http://localhost/media’.
Disclaimer This didn’t actually work as intended. The entire root was being served by mod_python for some reason. I came up with the earlier solution instead.
- Install mod_python
- `emerge -v mod_python` or `apt-get install mod-python` (i’m just guessing about that, btw)
- Tell Apache to use mod_python
- Add “-D PYTHON” to your APACHE2_OPTS
- if you don’t know where this is specified, try `grep -ir “APACHE2_OPTS” /etc/*`
- Symlink your project to your project
- `ln -s /home/elserj/projects/Concert /var/www/localhost/htdocs/concert`
- Tell Apache about the location you want served by mod_python
- Add the following code to ‘/etc/apache2/modules.d/16_mod_python.conf’
<Location /concert> SetHandler python-program PythonHandler django.core.handlers.modpython SetEnv DJANGO_SETTINGS_MODULE concertapp.settings PythonOption django.root /concert PythonDebug On PythonPath "['/home/elserj/projects/Concert/'] + sys.path" </Location>
- Again, your mileage may vary when trying to find this file or figure out where your distribution puts this file.
- Add the following code to ‘/etc/apache2/modules.d/16_mod_python.conf’
- Tell Apache about virtual host for this directory
- On Gentoo this would go into a file in /etc/apache2/vhosts.d/ but in Ubuntu or Mint it would most likely just be put into /etc/apache2/httpd.conf (or wherever httpd.conf is located. i.e. `locate httpd.conf`)
<IfDefine DEFAULT_VHOST>
<VirtualHost *:80>
ServerName localhost
SetEnv DJANGO_SETTINGS_MODULE concertapp.settings
<Location /concert/media>
SetHandler None
</Location>
</VirtualHost>
</IfDefine>
- On Gentoo this would go into a file in /etc/apache2/vhosts.d/ but in Ubuntu or Mint it would most likely just be put into /etc/apache2/httpd.conf (or wherever httpd.conf is located. i.e. `locate httpd.conf`)
- Restart Apache
- At this point, you need to restart Apache to have it reparse its configuration files
- On gentoo `/etc/init.d/apace2 restart`