Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Automated deployment of RPM-packaged django applications with Red Hat Software Collections

September 3, 2014
Tomas Tomecek
Related topics:
Linux
Related products:
Red Hat Enterprise Linux

Share:

    Introduction

    There has been already two (first and second) guides which covered installing django using Red Hat Software Collections (RHSCL). While very popular, none of them went far enough to show a real world deployment example. This guide will try to provide full guidance together with sample project.

    I chose django 1.7 (as of time writing this guide, it's still in RC) because in a couple of months, it will be used by many and in a year or so, it will be used widely. I also picked Red Hat Enterprise Linux 7 (RHEL 7) instead of Red Hat Enterprise Linux 6 (RHEL 6), because I feel more comfortable in our new Enterprise Linux release (I will mention differences between 6 and 7 in this guide). As a web server I chose nginx+uWSGI. I feel like this combination is very popular lately (probably because it is so fast! and flexible and easy to configure). For the interpreter I picked python 3. It's the successor of python 2 basically in every way and since collections have it, why would you not use it?

    The application I created is simple django application (it just prints in what environment it is running) and the whole project with all scripts can be found on GitHub. There is a Makefile so you can built it very easily and ansible playbook to deploy it. All you need to do is edit some variables and it should work pretty smoothly. The app itself consists of 2 rpms:

    1. application code (stored in sitelib)
    2. application configuration (nginx, uwsgi, uwsgi unit file and django configuration)

    You can easily swap between different configurations with this layout: devel, staging and production.

    The app itself is packaged as a collection in its own prefix.

    Also, I sort of copied first part of Slavek's guide, so I won't describe it as succinctly as he did.

    Enough background, let's start!

    Guide

    I'm assuming you have set up RHSCL repositories for yum. Let's install needed packages: python 3, postgres 9 and psycopg2, adapter for postgres:

    $ yum install python33 postgresql92 python33-python-psycopg2

    We will configure postgres first.

    PostgreSQL

    We have to initialize the database first:

    $ scl enable postgresql92 'postgresql-setup initdb'

    And start it:

    RHEL 7

    $ systemctl start postgresql92-postgresql

    RHEL 6

    $ service postgresql92-postgresql start

    Is it working?

    RHEL 7

    $ systemctl status postgresql92-postgresql
    
    postgresql92-postgresql.service - PostgreSQL database server
    Loaded: loaded (/usr/lib/systemd/system/postgresql92-postgresql.service; disabled)
    Active: active (running) since Ut 2014-08-12 13:33:54 BST; 5s ago
    Process: 1415 ExecStart=/opt/rh/postgresql92/root/usr/bin/scl-service $POSTGRESQL92_SCLS_ENABLED /opt/rh/postgresql92/root/usr/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS)
    Process: 1405 ExecStartPre=/opt/rh/postgresql92/root/usr/bin/scl-service $POSTGRESQL92_SCLS_ENABLED /opt/rh/postgresql92/root/usr/bin/postgresql-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
    Main PID: 1423 (postgres)
    CGroup: name=systemd:/system/postgresql92-postgresql.service
    ├─1423 /opt/rh/postgresql92/root/usr/bin/postgres -D /opt/rh/postgresql92/root/var/lib/pgsql/data -p 5432
    ├─1424 postgres: logger process
    ├─1427 postgres: checkpointer process
    ├─1428 postgres: writer process
    ├─1429 postgres: wal writer process
    ├─1430 postgres: autovacuum launcher process
    └─1431 postgres: stats collector process

    RHEL 6

    service postgresql92-postgresql status

    We will now create a user role, django, for our application:

    $ su - postgres
    # you have to enable postgresql92 collection so your shell environment is set up correctly
    $ scl enable postgresql92 bash
    $ createuser -P django
    Enter password for new role:
    Enter it again:
    $ createdb -O django djangodb

    Time to configure pg_hba.conf so we can actually connect to our database with our new django role. By default, postgres is asking the operating system for your username if you try to connect locally using unix socket. Let's configure postgres to require a password and use it for authentication:

    $ vim /opt/rh/postgresql92/root/var/lib/pgsql/data/pg_hba.conf
    --- <snip> ---
    local all postgres peer
    local all all md5

    We will proceed now with virtualenv.

    virtualenv

    We have to enable python33 collection so our virtualenv is correctly hooked to python 3.

    $ scl enable python33 bash
    # I put my virtualenv to /var/lib/, put yours wherever you want
    $ virtualenv --system-site-packages /var/lib/simple-django-project
    $ source /var/lib/simple-django-project/bin/activate

    Time to install packages from PyPI.

    # uwsgi is written in C, so you have to compile it (unless you install binary wheel packages)
    $ yum install gcc
    $ pip install uwsgi

    Lets install nginx webserver now (as of time writing this guide, there is nginx 1.6 in EPEL 7, but let's just stick with RHSCL rather).

    $ yum install nginx14-nginx

    We have to enable firewall if we want to access the app remotely:

    RHEL 7

    $ firewall-cmd --add-service=http
    success
    $ firewall-cmd --permanent --add-service=http
    success

    RHEL 6

    $ iptables -A INPUT -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT

    This part may be done from your workstation (I was deploying from my Fedora box).

    Deployment

    # mock is tool for building rpms in chroot
    $ yum install git mock ansible

    We will clone the project and build it:

    $ git clone https://github.com/TomasTomecek/simple-django-project.git
    $ cd simple-django-project

    But first, we need to update configuration files for mock (just write URLs of yum repos of RHEL 7, RHEL 7 optional and RHSCL)

    $ vim rhscl-python3.cfg
    $ vim rhscl-python3-sdp.cfg
    $ sudo cp rhscl-python3.cfg rhscl-python3-sdp.cfg /etc/mock/

    Firing build...

    $ make

    ...if still not there, ansible is not configured...

    $ vim /etc/ansible/hosts
    
    # rhel7-sdp is target in ansible playbook, update your credentials accordingly
    rhel7-sdp ansible_ssh_user=root ansible_ssh_host=192.168.122.62

    And deploy!

    $ ansible-playbook ansible.yml

    One command deployments. It can't be easier.

    Let's get back to the server.

    Configuration for nginx is stored in our collection, we have to configure nginx to load our file:

    $ vim /opt/rh/nginx14/root/etc/nginx/nginx.conf
    --- <snip> ---
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /opt/rh/nginx14/root/etc/nginx/conf.d/*.conf;
    include /opt/example_provider/sdp/root/etc/simple-django-project/nginx/*.conf;

    And finally...

    RHEL 7

    $ systemctl start nginx14-nginx uwsgi

    RHEL 6

    $ service nginx14-nginx start
    $ uwsgi --ini /opt/example_provider/sdp/root/etc/simple-django-project/uwsgi/uwsgi.ini

    Hopefully, everything is set up correctly. We may proceed with writing schema to the database. syncdb is deprecated in django 1.7, we'll use migrate instead:

    $ python manage.py migrate
    Operations to perform:
    Apply all migrations: admin, sessions, auth, contenttypes
    Running migrations:
    Applying contenttypes.0001_initial... OK
    Applying auth.0001_initial... OK
    Applying admin.0001_initial... OK
    Applying sessions.0001_initial... OK

    It didn't ask us to create a superuser. Weird. Lets create it:

    $ python manage.py createsuperuser

    Grand finale!

    $ yum install elinks
    $ elinks -dump http://localhost
    
    * Python version: 3.3.2 (default, Mar 20 2014, 18:55:17) [GCC 4.8.20140120 (Red Hat 4.8.2-16)]
    * django version: 1.7c3
    * django path:
    /var/lib/simple-django-project/lib/python3.3/site-packages/django
    * kernel: 3.10.0-123.el7.x86_64
    * distribution: ('Red Hat Enterprise Linux Server', '7.0', 'Maipo')

    And that's it! You have your django application packaged as an RPM (so you can easily manage it) and deployed in python 3. Aren't collections just wonderful?

    I would like to thank the python maintenance team (especially Slavek) for helping me with writing this blog post.

    Last updated: November 2, 2023

    Recent Posts

    • A deep dive into Apache Kafka's KRaft protocol

    • Staying ahead of artificial intelligence threats

    • Strengthen privacy and security with encrypted DNS in RHEL

    • How to enable Ansible Lightspeed intelligent assistant

    • Why some agentic AI developers are moving code from Python to Rust

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue