This article is a brief review of the steps to set up a local experimental Apache HTTP server with Django suitable for local development and testing. We don't address security concerns in this article. Before deploying a web server on a public interface, security topics should be thoroughly reviewed, and these can be found on both the Apache and Django project web sites (see links below).
In case you're not familiar with the open source technologies discussed in this article, a brief introduction of each component is provided below:
- Ubuntu Linux 18.04 is the most recent long-term release for a very popular and stable Linux desktop distribution. This operating system can be downloaded for free at https://www.ubuntu.com and will run on most PCs without too much trouble.
- The Apache HTTP Server Project is a common Web server for serving up dynamic content across the world. It may be overkill for personal network installations; however, it can be very useful for developing & testing new technologies locally before deploying to a data server in the cloud.
- Django is a Web framework for building world-class web sites. The framework uses Python, which is a very friendly & powerful scripting language used for an endless number of applications on local networks and data servers.
- WSGI (Web Server Gateway Interface) is a connector module that enables communication between the Apache web server and the Django web framework. The default data flow is one where a Web request via a browser is routed to the Apache web server. The server in turn makes a request call into Django via WSGI. Djagno processes the request and returns the response to Apache for transmission back across the Internet to the browser.
We're starting with a clean x86 64-bit Ubuntu 18.04 system with Python 3.6 but with neither Apache nor Django installed:
$ lsb_release -a Distributor ID: Ubuntu Description: Ubuntu 18.04.1 LTS Release: 18.04 Codename: bionic
note: lsb_release provides "certain LSB (Linux Standard Base) and distribution-specific information".
$ which apache2 $ dpkg -L apache2 dpkg-query: package 'apache2' is not installed ... envy:~$ python3 Python 3.6.6 (default, Sep 12 2018, 18:26:19) ... >>> import django ... ModuleNotFoundError: No module named 'django'
Use Ubuntu's apt (Advanced Packaging Tool) to install our base apache2 installation
$ sudo apt install apache2 Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap Suggested packages: apache2-doc apache2-suexec-pristine | apache2-suexec-custom The following NEW packages will be installed: apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap 0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded. Need to get 195 kB/1,605 kB of archives. After this operation, 6,496 kB of additional disk space will be used.
We want mod-wsgi, which is an Apache module for the implmenentation of python WSGI. This will enable us to run our Django application using Apache and also do cool things like SSE server push via WSGI.
Install mod-wsgi for python 3 making sure to use the Apache library for python3. Information on various Ubuntu HTTP server packages can be found on the Ubuntu package site.
$ sudo apt install libapache2-mod-wsgi-py3 Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: libapache2-mod-wsgi-py3 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 87.8 kB of archives. After this operation, 278 kB of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu bionic/universe amd64 libapache2-mod-wsgi-py3 amd64 4.5.17-1 [87.8 kB] Fetched 87.8 kB in 0s (740 kB/s) Selecting previously unselected package libapache2-mod-wsgi-py3. (Reading database ... 309617 files and directories currently installed.) Preparing to unpack .../libapache2-mod-wsgi-py3_4.5.17-1_amd64.deb ... Unpacking libapache2-mod-wsgi-py3 (4.5.17-1) ... Setting up libapache2-mod-wsgi-py3 (4.5.17-1) ... apache2_invoke: Enable module wsgi
Note the last line above that states the module was enabled. Although it's not necessary, let's check that it is indeed installed and where it's located:
$ dpkg -S mod_wsgi libapache2-mod-wsgi-py3: /usr/lib/apache2/modules/mod_wsgi.so libapache2-mod-wsgi-py3: /usr/lib/apache2/modules/mod_wsgi.so-3.6
Check if our Apache Web Server is running
$ ps -e | grep apache 3231 ? 00:00:00 apache2 3234 ? 00:00:00 apache2 3235 ? 00:00:00 apache2
Install Django from source
It's relatively easy to work with Django from source using the Git version control system. We'll install it under our /build tree:
$ mkdir -p /build; cd /build git clone https://github.com/django/django.git Cloning into 'django'... remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (4/4), done. remote: Total 423695 (delta 0), reused 0 (delta 0), pack-reused 423691 Receiving objects: 100% (423695/423695), 179.24 MiB | 8.50 MiB/s, done. Resolving deltas: 100% (307304/307304), done. Checking out files: 100% (6027/6027), done.
Checkout the latest production branch
$ cd django $ git tag -l | grep ^2 ... 2.1 2.1.1 2.1.2 2.1.3 ... $ git checkout 2.1.3 Note: checking out '2.1.3'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b <new-branch-name> HEAD is now at d6226e3a82 [2.1.x] Bumped version for 2.1.3 release.
Create our own development branch so we can make changes to the source, commit them, create patches, etc.
$ git checkout -b dev2.1.3 Switched to a new branch 'dev2.1.3'
Determine where to configure the django library:
$ python3 Python 3.6.6 (default, Sep 12 2018, 18:26:19) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import site >>> site.getsitepackages() ['/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.6/dist-packages'] >>> quit() $ cd /usr/lib/python3/dist-packages $ sudo sh -c "echo '/build/django' > django.pth"
Note that the last line above creates a path configuration file so python can find our Django library.
Verify it's installed in our python3 environment:
$ python3 Python 3.6.6 (default, Sep 12 2018, 18:26:19) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.VERSION (2, 1, 3, 'final', 0)
Now we have Apache running and Django installed so python3 can import it and use it.
Create an example Django test project: tstdj under /build
$ cd /build $ python3 /build/django/django/bin/django-admin.py startproject tstdj
Find your settings.py file under tstdj and modify your ALLOWED_HOSTS so it includes your interface (e.g., "192.168.1.17"). If you don't do this, you'll see an error when you visit your website explaining what you need to fix (courtesy of the Django developers).
If you want to take a break and verify your Django test project is OK, you can use:
$ cd /build/tstdj $ python3 manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK ... $ python3 manage.py runserver Performing system checks... System check identified no issues (0 silenced). ...
If you are unfamiliar with runserver and database migrations, head over to djangoproject.com and walk through their very good tutorial.
Configure the Apache server to serve Django
Now it's time to tie it all together and serve up your Django project using Apache and mod-wsgi.
Open two terminals side-by-side to modify Apache conf files and view Apache logs:
$ cd /etc/apache2 $ grep APACHE_LOG_DIR envvars export APACHE_LOG_DIR=/var/log/apache2$SUFFIX
Second terminal, making use of the path determined for APACHE_LOG_DIR above:
$ cd /var/log/apache2
This second terminal will let you view errors in error.log and accesses in access.log.
For completeness sake, make sure mod-wsgi is enabled:
$ sudo a2enmod wsgi Module wsgi already enabled
In the first terminal open /etc/apache2/apache2.conf with your favorite editor after making a copy of it:
$ sudo cp apache2.conf apache2.conf.orig
Add the following lines to the end of apache2.conf:
WSGIDaemonProcess <username> processes=2 threads=12 python-path=/build/tstdj WSGIProcessGroup <username> WSGIRestrictEmbedded On WSGILazyInitialization On WSGIScriptAlias / /build/tstdj/tstdj/wsgi.py <Directory /build/tstdj/tstdj> Require all granted </Directory>
Note that there are many options for configuring, tuning, and tweaking both Apache and mod-wsgi, but these are similar to the ones we use on a production site. Refer to the documentation of both projects for further information.
Now, restart Apache and give it a try. If you have errors, review error.log in /var/log/apache2 or submit a comment below and we'll try our best to help you.
sudo apachectl -k restart
Lastly, make sure it's running:
$ ps -e | grep apache 13172 ? 00:00:00 apache2 13173 ? 00:00:00 apache2 13174 ? 00:00:00 apache2 13175 ? 00:00:00 apache2 13176 ? 00:00:00 apache2
In order to try your installation, open your browser and type in the IP address you set in your Django settings.py file for ALLOWED_HOSTS above. Hopefully, you see something similar to what is shown below.