Serving Arches with Apache¶
When you are putting your Arches app in production, you’ll need to serve it with a webserver. The following is a guide using Apache as an example, split into two sections:
For reference:
- Django Documentation
- How to Run Django with mod_wsgi and Apache with a virtualenv Python environment on a Debian VPS (Digital Ocean)
- Using mod_wsgi when developing Django sites (Graham Dumpleton)
Setup Apache¶
During development, it’s easiest to use the Django webserver to view progress on your app. However, once you are ready to put the app into production, you’ll have to use a more efficient webserver like Apache or nginx.
We have the most experience using Apache, which is very easy to install and configure. The following instructions work for Ubuntu 14.04, minor changes may be necessary for a different OS.
- Get apache2 and mod_wsgi
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi
- In order to properly configure Apache, we must:
- Create a python daemon process
- Set the path to your app’s wsgi.py file and reference to the python daemon process created above
- Give Apache access to the main app directory
All of these tasks are handled by adding a block of code to Apache’s ../sites-enabled/000-default.conf file. Use this command to open the file
$ sudo nano /etc/apache2/sites-enabled/000-default.conf
and in the appropriate <VirtualHost> stanza (<Virtualhost *:80> in the most basic configuration) paste the following code, changing directory and file paths where necessary:
WSGIDaemonProcess arches python-path=/home/ubuntu/Projects/arches/arches:/home/ubuntu/Projects/ENV/lib/python2.7/site-packages
WSGIScriptAlias / /home/ubuntu/Projects/arches/arches/wsgi.py process-group=arches
<Directory /home/ubuntu/Projects/arches>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Use ctrl+x to save the file.
You may find it helpful to read the Official Django Documentation on serving Django apps with Apache and mod_wscgi.
- Now we must give the Apache write-permission in a few locations. Do that by first changing the permissions of the necessary files and directories, and second by setting the Apache user as the group. Note that the Apache user on Ubuntu is www-data (used in the example below), and on CentOS it is httpd.
$ sudo chmod 664 /home/ubuntu/Projects/arches/arches/arches.log
$ sudo chgrp www-data /home/ubuntu/Projects/arches/arches/arches.log
$ sudo chmod 775 /home/ubuntu/Projects/arches/arches
$ sudo chgrp www-data /home/ubuntu/Projects/arches/arches
These commands should give Apache sufficient permissions to create and modify the arches/uploadedfiles directory (where user uploads are stored by default) and the arches/tileserver directory where Tilestache caches tiles that it renders.
Please post to the Arches forum if you find that more permissions need to be modified, or these directions can be simplified further.
- Finally, restart Apache.
Ubuntu
$ sudo service apache2 restart
CentOS
$ sudo /sbin/service httpd restart
You should now be able to view your app from any web browser by navigating directly to your IP address (you don’t need to run the Django dev server now).
Note that with Apache serving your app, any changes to a .py file will not be reflected in the app until you restart Apache (use the command shown above). If you want to avoid this manual restart/reload step, the following resources will get you started:
Using mod_wsgi when developing Django sites
mod_wsgi: Reloading Source Code
If you are still in development and just want to use Apache instead of the Django server (and keep getting the lengthy Django error messages instead of a 500 page), you can rejoice and stop here. Otherwise, continue on…
Handling Static Files¶
There are two cases in which you need to follow these directions to handle static files (js, css and images):
- You are going to set
DEBUG = False, at which point Django will no longer serve them, or- You are leaving
DEBUG = Truebut are developing and serving your app from a non-root location, say www.example.com/arches4 instead of www.example.com.
- Create a new directory to hold the static files. Place this within your app, adjacent to the
modelsandtemplatesdirectories.
$ sudo mkdir /home/ubuntu/Projects/arches/arches/static
Now open your
settings.py(orsettings_local.pyfile), and add these lines to it:STATIC_ROOT = os.path.join(PACKAGE_ROOT, 'static') STATIC_URL = "/static/"
This will point Django to your new static directory, and also tell it how to create a URL that points to that directory.
- From within the directory that holds your
manage.pyfile, run this command:
$ python manage.py collectstatic
Watch as all of your static files (including those that come standard with Django) are copied to the new directory. Now we are ready to tell Apache where to find them.
- Use
$ sudo nano /etc/apache2/sites-enabled/000-default.conf
to open the same configuration file as you modified above. Find your <VirtualHost> stanza with some familiar code in it. Below the original code you added, paste this block, changing paths as necessary.
Alias /static/ /home/ubuntu/Projects/arches/arches/static/
<Directory /home/ubuntu/Projects/arches/arches/static>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>p
The Alias line tells Apache where to look when Django sends it the /static/ URL, and the subsequent block allows Apache access to your newly created static directory.
- Finally, restart Apache.
$ sudo service apache2 restart
Important
You must now run python manage.py collectstatic any time you make any changes to static files (.js, .css, or images).