Deploy a Django Project with Apache Server
Note: This guide is intended for Ubuntu 16.04
Project Directory
First of all, let’s make sure the django project is structured like this:
.
├── my-site-virtualenv/
|   ├── pip-selfcheck.json
|   ├── bin/
|   ├── ...
|   └── my-site-repository/
|       ├── .git/
|       ├── requirements.txt
|       ├── README.md
|       ├── app1/
|       |   ├── models.py
|       |   ├── views.py
|       |   ├── ...
|       ├── my_site/
|       |   ├── settings.py
|       |   ├── wsgi.py
|       |   ├── ...
└── another-project/
Django
Settings
Make sure the Django project is connected to a MySQL server, and has the following line in settings.py
STATIC_ROOT = os.path.abspath('/var/www/[my-project]/static/')
WSGI
Add the following to wsgi.py:
import sys
sys.path.append('/[path-to-my-project-vir]/my-project-repo')
sys.path.append('/[path-to-my-project-vir]/lib/python3.5/site-packages')
Virtualenv, Git Clone and Install
$ virtualenv my-project-vir
$ cd my-project-vir
$ mkdir my-project-repo
$ cd my-project-repo
$ git clone [git-repo-address] .
$ pip install -r requirements.txt
See “Django - Connect to Remote MySQL Database” about how to install mysqlclient
Collect Static Files
$ python manage.py collectstatic
Change Owner
$ sudo chown -R :www-data /var/www/[my-project]
Apache2
Install
$ sudo apt-get install apache2-dev libapache2-mod-wsgi apache2
Config
Append the following to /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
    . . .
    Alias /static /var/www/[my-project]/static
    <Directory /[path-to-my-project-repo]/[my-project]>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    WSGIDaemonProcess my-project python-path=[path-to-my-project-repo]:[path-to-my-project-vir]/lib/pythonx.x/site-packages
    WSGIProcessGroup my-project
    WSGIScriptAlias / [path-to-my-project-repo]/[my-project]/wsgi.py
    WSGIPassAuthorization On
</VirtualHost>
mod_wsgi
Download Source
Go to https://github.com/GrahamDumpleton/mod_wsgi/releases and find the latest release .tar file.
$ cd ~/
$ wget <url-to-.tar-file>
Unpack .tar file:
$ tar xvfz <.tar-file-name>
Build
cd into source directory, then
$ ./configure --with-python=<path-to-virtualenv>/bin/python3
Build:
$ make
Back to Apache2
Modify Apache2 Config
Find any mod_wsgi.so, and copy the path:
$ sudo find / -name "mod_wsgi.so"
By default, it should be at /usr/lib/apache2/modules/mod_wsgi.so
Then, find the main Apache2 configuration file. By default, it should be at /etc/apache2/apache2.conf. Add the followling line into it:
LoadModule wsgi_module <path-to-mod_wsgi.so>
Restart Apache2
Try
$ sudo apachectl restart
or
$ sudo service apache2 restart
or
$ sudo /etc/init.d/apache2 start
How to Check Apache Error Log
$ sudo less /var/log/apache2/error.log