Serving Arches with Apache¶
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.
The following is a guide using Apache as an example, split into two sections:
If you want to use nginx + uWSGI instead of Apache + mod_wsgi, this tutorial should get you started.
Configure Apache¶
The following instructions work for Ubuntu 16.04 - 20.04; minor changes may be necessary for a different OS. This is a very basic Apache configuration, and more fine tuning will benefit your production installation.
Install Apache.
$ sudo apt-get install apache2
Install
mod_wsgi
There are two ways to install
mod_wsgi
. Both of the require you to start by installing the Apache and Python development headers.$ sudo apt install apache2-dev python3-dev
Note
You may need to install the Python dev package specific to your Python version, e.g.
python3.8-dev
.Now follow one of the following two options:
Install mod_wsgi directly into your Python virtual environment
$ source /home/ubuntu/Projects/ENV/bin/activate (ENV)$ pip install mod_wsgi
Generate the link path to
mod_wsgi
.(ENV)$ mod_wsgi-express module-config
This command will produce two lines that look like
LoadModule wsgi_module "<your venv path>/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so" WSGIPythonHome "<your venv path>"
Copy these two lines, you will use them in step 3.
Install mod_wsgi system-wide
Alternatively, you can use
apt
to install at the system level:$ sudo apt install libapache2-mod-wsgi-py3
Note that the version of Python 3 installed at the system-level may need to match the version used to create the virtual environment pointed to in the config. For example, if
libapache2-mod-wsgi-py3
is compiled against Python 3.8, use Python 3.8 for your virtual environment. Installingmod-wsgi
this way means you will not need to load it as a module in the Apaache .conf file.Create a new Apache .conf file
Here is a basic Apache configuration for Arches. If using a domain like
heritage-inventory.org
, name this fileheritage-inventory.org.conf
, otherwise, use something simple likearches-default.conf
.The paths below are based on an example project in
/home/ubuntu/Projects/my_project
.sudo nano /etc/apache2/sites-available/arches-default.conf
Complete new file contents:
# If you have mod_wsgi installed in your python virtual environment, paste the text generated # by 'mod_wsgi-express module-config' here, *before* the VirtualHost is defined. LoadModule wsgi_module "/home/ubuntu/Projects/ENV/lib/python3.7/site-packages/mod_wsgi/server/mod_wsgi-py37.cpython-37m-x86_64-linux-gnu.so" WSGIPythonHome "/home/ubuntu/Projects/ENV" <VirtualHost *:80> WSGIApplicationGroup %{GLOBAL} WSGIDaemonProcess arches python-path=/home/ubuntu/Projects/my_project WSGIScriptAlias / /home/ubuntu/Projects/my_project/my_project/wsgi.py process-group=arches # May be necessary to support integration with possible 3rd party mobile apps WSGIPassAuthorization on ## Uncomment the ServerName directive and fill it with your domain ## or subdomain if/when you have your DNS records configured. # ServerName heritage-inventory.org <Directory /home/ubuntu/Projects/my_project/> Require all granted </Directory> # This section tells Apache where to find static files. This example uses # STATIC_URL = '/media/' and STATIC_ROOT = os.path.join(APP_ROOT, 'static') # NOTE: omit this section if you are using S3 to serve static files. Alias /media/ /home/ubuntu/Projects/my_project/my_project/static/ <Directory /home/ubuntu/Projects/my_project/my_project/static/> Require all granted </Directory> # This section tells Apache where to find uploaded files. This example uses # MEDIA_URL = '/files/' and MEDIA_ROOT = os.path.join(APP_ROOT) # NOTE: omit this section if you are using S3 for uploaded media Alias /files/uploadedfiles/ /home/ubuntu/Projects/my_project/my_project/uploadedfiles/ <Directory /home/ubuntu/Projects/my_project/my_project/uploadedfiles/> Require all granted </Directory> ServerAdmin webmaster@localhost DocumentRoot /var/www/html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn # Recommend changing these file names if you have multiple arches # installations on the same server. ErrorLog /var/log/apache2/error-arches.log CustomLog /var/log/apache2/access-arches.log combined </VirtualHost>
Disable the default Apache conf, and enable the new one.
$ sudo a2dissite 000-default $ sudo a2ensite arches-default $ sudo service apache2 reload
Replace
arches-default
with the name of your new .conf file if needed.
At this point, you can try accessing your Arches installation in a browser, but you’re likely to get some kind of file permissions error. Continue to the next section.
Important
With Apache serving Arches, any changes to a .py
file (like settings.py
)
will not be reflected until you reload Apache.
Prepare the Arches Project¶
Set all file and directory permissions.
Apache runs as the user
www-data
, and this user must have write access to some portions of your Arches project.Note
On CentOS, Apache runs as is
httpd
, so substitute that forwww-data
herein.The
arches.log
file…$ 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
The
uploadedfiles
directory…$ sudo chmod 775 /home/ubuntu/Projects/my_project/my_project/uploadedfiles $ sudo chgrp www-data /home/ubuntu/Projects/my_project/my_project/uploadedfiles
Or, if either
arches.log
oruploadedfiles
doesn’t yet exist, you can just allowwww-data
to create them at a later point by giving write access to your project directory.$ sudo chmod 775 /home/ubuntu/Projects/my_project/my_project $ sudo chgrp www-data /home/ubuntu/Projects/my_project/my_project
You should now be able to access your Arches installation in a browser, but there is one more important step.
Run
collectstatic
.This Django command places all of the static files (CSS, JavaScript, etc.) used in Arches into a single location that a webserver can find. By default, they are placed in
my_project/my_project/static
, based onSTATIC_ROOT
.Note
You can change
STATIC_ROOT
all you want, but be sure to update the Alias and Directory info in the Apache conf accordingly.(ENV)$ python manage.py collectstatic
The first time this runs it will take a little while (~20k files), and may show errors/warnings that you can safely ignore.
Finally, make sure Apache has write access to this static directory because django-compressor needs to update the CACHE contents inside it:
$ sudo chmod 775 /home/ubuntu/Projects/my_project/my_project/static $ sudo chgrp www-data /home/ubuntu/Projects/my_project/my_project/static
Important
from now on, any time you change a CSS, JavaScript, or other static asset you must rerun this command.
You should now be able to view your Arches installation in a browser without any issues.