Skip to content

Commit

Permalink
general update to serving arches with apache section
Browse files Browse the repository at this point in the history
  • Loading branch information
mradamcox committed May 21, 2019
1 parent 530d989 commit 3adf6c7
Showing 1 changed file with 98 additions and 85 deletions.
183 changes: 98 additions & 85 deletions docs/serving-arches-with-apache.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,167 +2,180 @@
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:
.. sidebar:: Further Reference

+ :ref:`Setup Apache`
+ `Django Documentation <https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/#how-to-use-django-with-apache-and-mod-wsgi>`_
+ `How to Run Django with mod_wsgi and Apache with a virtualenv Python environment on a Debian VPS (Digital Ocean) <https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps>`_
+ `Using mod_wsgi when developing Django sites (Graham Dumpleton) <http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html>`_

+ :ref:`Handling Static Files`

For reference:
When you are putting your Arches project 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:

+ `Django Documentation <https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/#how-to-use-django-with-apache-and-mod-wsgi>`_
+ `How to Run Django with mod_wsgi and Apache with a virtualenv Python environment on a Debian VPS (Digital Ocean) <https://www.digitalocean.com/community/tutorials/how-to-run-django-with-mod_wsgi-and-apache-with-a-virtualenv-python-environment-on-a-debian-vps>`_
+ `Using mod_wsgi when developing Django sites (Graham Dumpleton) <http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html>`_
+ :ref:`Setup Apache`

+ :ref:`Handling Static Files`

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.
During development, it's easiest to use the Django webserver to view your Arches installation. However, once you are ready to put the project 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.

1. Get apache2 and mod_wsgi

.. code-block:: bash

$ sudo apt-get install apache2

$ sudo apt-get install libapache2-mod-wsgi


2. In order to properly configure Apache, we must:
.. code-block:: bash

+ Create a python daemon process
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi

+ 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
2. Create the Python process

In order to properly configure Apache, we must:

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
+ Create a python daemon process

.. code-block:: bash
+ Set the path to your project's wsgi.py file and reference to the python daemon process created above

$ sudo nano /etc/apache2/sites-enabled/000-default.conf
+ Give Apache access to the main project 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

and in the appropriate ``<VirtualHost>`` stanza (``<VirtualHost *:80>`` in the most basic configuration) paste the following code, changing directory and file paths where necessary: ::
.. code-block:: bash

$ sudo nano /etc/apache2/sites-enabled/000-default.conf

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
and paste the following code into the ``<VirtualHost *:80>`` stanza, changing directory and file paths where necessary::

<Directory /home/ubuntu/Projects/arches>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

Use `ctrl+x` to save the file.
WSGIDaemonProcess arches python-path=/home/ubuntu/Projects/my_project:/home/ubuntu/Projects/ENV/lib/python2.7/site-packages
WSGIScriptAlias / /home/ubuntu/Projects/my_project/my_project/wsgi.py process-group=arches

You may find it helpful to read the `Official Django Documentation <https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/#how-to-use-django-with-apache-and-mod-wsgi>`_ on serving Django apps with Apache and mod_wscgi.
<Directory /home/ubuntu/Projects/my_project>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

3. 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`.
Use `ctrl+x` to save the file.

You may find it helpful to read the `Official Django Documentation <https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/modwsgi/#how-to-use-django-with-apache-and-mod-wsgi>`_ on serving Django apps with Apache and mod_wscgi.

.. code-block:: bash
3. Set file system permissions for Apache

Now we must give Apache write-permission in a few locations. We'll do that by first changing the permissions of the necessary files and directories, and second by setting the Apache user as the group.

$ sudo chmod 664 /home/ubuntu/Projects/arches/arches/arches.log
.. code-block:: bash

$ sudo chgrp www-data /home/ubuntu/Projects/arches/arches/arches.log
$ sudo chmod 775 /home/ubuntu/Projects/my_project/my_project
$ sudo chgrp www-data /home/ubuntu/Projects/my_project/my_project
$ sudo chmod 664 /home/ubuntu/Projects/my_project/my_project/arches.log
$ sudo chgrp www-data /home/ubuntu/Projects/my_project/my_project/arches.log

.. note::

On Ubuntu the Apache user is ``www-data`` (used in the example below), but on CentOS it is ``httpd``.

$ sudo chmod 775 /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.

$ sudo chgrp www-data /home/ubuntu/Projects/arches/arches
.. note::
Please post to the `Arches forum <https://groups.google.com/forum>`_ if you find that more permissions need to be modified, or that these directions can be simplified further.

4. Restart Apache.

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.
**Ubuntu**

Please post to the `Arches forum <https://groups.google.com/forum>`_ if you find that more permissions need to be modified, or these directions can be simplified further.
.. code-block:: bash

4. Finally, restart Apache.
$ sudo service apache2 restart

**Ubuntu**
**CentOS**

.. code-block:: bash
.. code-block:: bash

$ sudo service apache2 restart
$ sudo /sbin/service httpd restart

**CentOS**

.. code-block:: bash
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).

$ sudo /sbin/service httpd restart
.. important::
With Apache serving Arches, any changes to a ``.py`` file will not be reflected until you restart Apache:


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 <http://blog.dscpl.com.au/2008/12/using-modwsgi-when-developing-django.html>`_

`mod_wsgi: Reloading Source Code <https://modwsgi.readthedocs.io/en/develop/user-guides/reloading-source-code.html>`_

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...
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 stop here. Otherwise, you'll need to continue on to handle your app's static files.

Handling Static Files
=======================


There are two cases in which you need to follow these directions to handle static files (js, css and images):
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 = True`` but are developing and serving your app from a non-root location, say www.example.com/arches4 instead of www.example.com.
* You are leaving ``DEBUG = True`` but are developing and serving your project from a non-root location, say www.example.com/arches4 instead of www.example.com.

1. Create static files directory

This directory can be placed anywhere. In the example below we are putting it inside of your project.

.. code-block:: bash

1. Create a new directory to hold the static files. Place this within your app, adjacent to the ``models`` and ``templates`` directories.
$ mkdir /home/ubuntu/Projects/my_project/my_project/static

.. code-block:: bash

$ sudo mkdir /home/ubuntu/Projects/arches/arches/static
2. Configure your Arches project settings

Now open your ``settings.py`` (or ``settings_local.py``) file, and add these lines to it.

2. Now open your ``settings.py`` (or ``settings_local.py`` file), and add these lines to it: ::
.. code-block:: bash

STATIC_ROOT = os.path.join(PACKAGE_ROOT, 'static')
STATIC_ROOT = os.path.join(PACKAGE_ROOT, 'static')

STATIC_URL = "/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.

This will point Django to your new static directory, and also tell it how to create a URL that points to that directory.
3. Collect the static files

3. From within the directory that holds your ``manage.py`` file, run this command:
With your virtual environment activated, enter your project's top directory and run this command:

.. code-block:: bash
.. code-block:: bash

$ python manage.py collectstatic
$ 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.

.. important::

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.
With Apache serving your app you must run ``python manage.py collectstatic`` any time you make any changes to static files.

4. Use
4. Configure Apache settings

.. code-block:: bash
Use

.. code-block:: bash

$ sudo nano /etc/apache2/sites-enabled/000-default.conf
$ sudo nano /etc/apache2/sites-enabled/000-default.conf

to edit the default Apache configuration file. Find your ``<VirtualHost *:80>`` stanza with some familiar code in it. Below the original code you added, paste this block, changing paths as necessary.

.. code-block:: bash

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/my_project/my_project/static/

Alias /static/ /home/ubuntu/Projects/arches/arches/static/
<Directory /home/ubuntu/Projects/my_project/my_project/static>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>p

<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.

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.
4. Restart Apache.

4. Finally, restart Apache.
.. code-block:: bash

.. code-block:: bash
$ sudo service apache2 restart

$ 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).

0 comments on commit 3adf6c7

Please sign in to comment.