Install Django CMS on Ubuntu 12.04 with MariaDB, Gunicorn and Nginx

Leave reply

django_cmsIn this blog post we will show you how to install the Django CMS on a Ubuntu VPS with MariaDB, Nginx and Gunicorn  .

Django CMS is open source content management system based on the Web framework Django. Some of the main features include: Plugin-based, Pretty URLs, Permission management, Apps, Frontend-Editing, Analytics, Blog ..etc.

 

The following command will install all necessary packages

apt-get update
apt-get install -y python-software-properties python python-dev

Install the the latest version of Nginx

add-apt-repository ppa:nginx/stable
apt-get update && sudo apt-get install nginx

Install MariaDB and create a database.

apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
add-apt-repository 'deb http://ftp.osuosl.org/pub/mariadb/repo/5.5/ubuntu precise main'
apt-get update
echo -e "Package: *\nPin: origin ftp.osuosl.org\nPin-Priority: 1000" | tee /etc/apt/preferences.d/mariadb
apt-get install mariadb-server libmariadbclient-dev
mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE dcms;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON dcms.* TO 'dcmsuser'@'localhost' IDENTIFIED BY 'pa33W0rd';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> \q

Install pip

curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python -

Create a virtualenv using pip

sudo pip install --upgrade virtualenv
mkdir -p ~/.virtualenvs/djangocms
virtualenv ~/.virtualenvs/djangocms
source ~/.virtualenvs/djangocms/bin/activate

Create a requirements.txt file with the following content

# Bare minimum
django-cms==2.4.1

#These dependencies are brought in by django-cms, but if you want to lock-in their version, specify them
Django==1.5.1
django-classy-tags==0.4
South==0.8.1
html5lib==1.0b1
django-mptt==0.5.2
django-sekizai==0.7
six==1.3.0
mysql-python==1.2.5

#Optional, recommended packages
Pillow==2.0.0
django-filer==0.9.4
cmsplugin-filer==0.9.5
django-reversion==1.7

Install django CMS

pip install --upgrade --download-cache=~/.pip-cache -r requirements.txt

Create your django CMS project

mkdir ~/projects
cd  ~/projects
django-admin.py startproject mynewproject

Open the file ~/projects/mynewproject/mynewproject/settings.py and add the following at the top of the file:

# -*- coding: utf-8 -*-
import os
gettext = lambda s: s
PROJECT_PATH = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]

Add your database details:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dcms', 
        'USER': 'dcms',
        'PASSWORD': 'pa33W0rd',
        'HOST': '',
        'PORT': '',
    }
}

Add the following apps to the INSTALLED_APPS.

'django.contrib.admin',
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'cms',
    'mptt',
    'menus',
    'south',
    'sekizai'
)

Add the following lines to the MIDDLEWARE_CLASSES list.

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.language.LanguageCookieMiddleware',
)

Add the following at the end of the file:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'cms.context_processors.media',
    'sekizai.context_processors.sekizai',
)

Change STATIC_ROOT and MEDIA_ROOT directives to:

STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = "/static/"

MEDIA_ROOT = os.path.join(PROJECT_PATH, "media")
MEDIA_URL = "/media/"

Modify the TEMPLATE_DIRS directive:

TEMPLATE_DIRS = (
    # The docs say it should be absolute path: PROJECT_PATH is precisely one.
    # Life is wonderful!
    os.path.join(PROJECT_PATH, "templates"),
)

Add at least one template to CMS_TEMPLATES

CMS_TEMPLATES = (
    ('template_1.html', 'Template One'),
    ('template_2.html', 'Template Two'),
)

Limit to English

LANGUAGES = [
    ('en', 'English'),
]

Finally save the settings.py file.

Define routes for our project

Open mynewproject/mynewproject/urls.py file and replace the content with the following

from django.conf.urls.defaults import *
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings

admin.autodiscover()

urlpatterns = patterns('',
  url(r'^admin/', include(admin.site.urls)),
  url(r'^', include('cms.urls')),
)
if settings.DEBUG:
    urlpatterns = patterns('',
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
    url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns

Create templates

Create a new directory templates

mkdir ~/projects/mynewproject/templates

Open your favorite editor and create the following files:

base.html

{% load cms_tags sekizai_tags %}
<html>
  <head>
      {% render_block "css" %}
  </head>
  <body>
      {% cms_toolbar %}
      {% placeholder base_content %}
      {% block base_content %}{% endblock %}
      {% render_block "js" %}
  </body>
</html>

template_1.html

{% extends "base.html" %}
{% load cms_tags %}

{% block base_content %}
  {% placeholder template_1_content %}
{% endblock %}

template_2.html

{% extends "base.html" %}
{% load cms_tags %}

{% block base_content %}
  {% placeholder template_2_content %}
{% endblock %}

Initialize the database and create a new superuser

python manage.py syncdb --all
python manage.py migrate --fake

Check if everything is set up correctly

python manage.py cms check

If you see “Installation okay”, it means everything is set up correctly.

Collect  static files

cd ~/projects/mynewproject
./manage.py collectstatic

Install and configure gunicorn

pip install gunicorn

create a new configuration file (~/.virtualenvs/djangocms/gunicorn_config.py) as follows

command = '~/.virtualenvs/djangocms/bin/gunicorn'
pythonpath = '~/projects/mynewproject'
bind = '127.0.0.1:8011'
workers = 3
user = nobody

Start the gunicorn with the following command:

cd ~/projects/mynewproject && gunicorn -c ~/.virtualenvs/djangocms/gunicorn_config.py mynewproject.wsgi

Configure Nginx

Create a new nginx virtual host

vim /etc/nginx/sites-available/your_domain.com
server {
 listen       80;
 server_name  your_domain.com;

    location /static/ {
        alias /<your_username>/projects/mynewproject/static/;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8011/;
    }
 }

ln -s /etc/nginx/sites-available/your_domain.com /etc/nginx/sites-enabled/your_domain.com
/etc/init.d/nginx restart

That’s it. Now open your browser and navigate to your domain.

For more info about the Django CMS please go to: http://docs.django-cms.org/

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

One Response to “Install Django CMS on Ubuntu 12.04 with MariaDB, Gunicorn and Nginx”

  1. Silvio

    Hello,

    Thank you for Howto.

    but what is with admin panel? The admin panel come without style. Is there another folder which must be have instuction of location, root?

    Reply

Leave a Reply