Django on RedHat 7
Django on RHEL7
Here is how I chose to set up Django on RHEL7. This is somewhat painful because the default Python in RHEL7 is still version 2. Using this to create any new programs seems foolish as it has already been de-supported by the Python project, so I really have to use Python 3.
Set up Linux
We need Python 3, Django, Apache, and Postgresql. See the instructions linked in the next section if the Postgresql packages aren’t found. The version that comes with RHEL7 is too old for the version of Django we want to install.
Create the database.
Postgresql has instructions for installing on RedHat.
Set up the database
Make sure the database accepts passwords:
Edit /var/lib/pgsql/data/pg_hba.conf
and change the following lines:
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
To:
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Reload the service:
|
|
Set up Django
The Django website has instructions on how to set up Django to run under wsgi.
Create the Project Directory
Set up urls.py
Add the URL to the urls.py
, and add the required imports as follows:
Edit settings.py
Add the project and restframework to the INSTALLED_APPS
variable.
Also add TokenAuthentication to the REST_FRAMEWORK
setting:
Change the database to use postgresql by replacing the DATABASES
definition
with the following:
The HOST: Localhost
makes the connection use the host, because local connections are set
(in pg_hba.conf
) to identify as peer, which means they have the same username in the database as in the OS.
Add the location for the static files. These just work in debug mode with runserver
, but
need to have a place to be served from when running from Apache:
Another change required is the ALLOWED_HOSTS
line:
|
|
Where hostname the hostname of the VM that is running Django.
Lastly, take the Django out of debug mode.
|
|
Run required utilities
Run the migrations:
|
|
Build the static files directory:
I check here if the environment works.
python3 manage.py runserver 0.0.0.0:8000
This isn’t suitable for production use though, I need to set up Apache.
Set up the Django Application Users
Now I can log in to my Django website, and with this username I can create any other users I require.
I am using token authentication for the API, so I need to create a token for the user.
|
|
The token is echoed out to the terminal. This can be copied to the configuration of the API consumer.
Set up Apache
The Digital Ocean documentation is really useful here as is the official Django documentation.
WSGI
Apache uses WSGI to run Python processes. It seems that there is a Python 2 version already installed in the operating system. I couldn’t work out where it was from, but in any case the new one needed to be configured as follows:
|
|
The configuration for the old version was removed as follows:
|
|
Apache Configuration
An Apache configuration for Django was created by creating /etc/httpd/conf.d/django.conf
with
the following contents:
|
|
The static directory is the one that was built earlier using collectstatic
.
Note that I haven’t used a virtual environment. Doing so seems quite straightforward, so this might have been a better thing to do. As things stand I had to add the location where Django and the rest framework were installed to the python-path. Should Python be upgraded this will need to change.
A quick test shows this runs on Apache.
Using a self signed certificate.
This VM is inside a NATted nework. Nothing on the internet can see us, so we can’t get a Let’s Encrypt certificate. To make sure passwords to login to the admin site aren’t sent in the clear over the network, I will use a self-signed certificate.
There was already a self signed certificate, so just pointing the browser at the website using https worked. I had to accept the warnings that the certificate authority was untrusted. Also the certificate had expired. My understanding is that this means the traffic is encrypted over the network, but we can’t use the certificate to protect from man in the middle attacks, i.e. it won’t verify the server the request comes from. Since this is an internal network I think I am happy with this.
Prevent unencrypted traffic
This requires mod_rewrite which was already installed. I just had to add the following
file: /etc/httpd/conf.d/http.conf
Conclusion
This turned out to be a lot of effort, but now I have an application hosted that uses JSON REST APIs. This can be used by various scripts to keep information about the environments I maintain up to date.