tutorial | mezzanine

Installing Mezzanine v4+ on CentOS 6 with PostgreSQL and Apache using WSGI (I)

This is a comprehensive guide for installing the Django CMS distribution known as Mezzanine v4+ on CentOS 6.x. using the repositories provided by  PostgreSQL and the Rackspace team (IUS).

Mezzanine is a powerful, consistent, and flexible content management platform with  a WordPress-like editing and admin experience.  Built using the Django framework, Mezzanine provides a simple yet highly extensible architecture that encourages diving in and hacking on the code. Mezzanine is BSD licensed and supported by a diverse and active community.  It uses Bootstrap 3.0 and is fully responsive. ased on the Django framework it supports a wide range of databases (SQLite, mysql/mariadb, PostgreSQL and Oracle)

The provider’s virtual machine comes with CentOS 6.7. and no option to upgrade to CentOS 7 and Mezzanine v4+ requires newer versions of Python (>2.8) and Django (>1.7) than the  CentOS 6.7 stock packages.

The IUS Project as well as the PostgreSQL Project provide packages for Enterprise Linux that follow the latest upstream stable versions of specific software. Both are safe repositories using alternate package names that and do not obsoleting the stock package and preventing anything from automatically replacing a stock package or breaking dependencies.

Updating the System

Most of the operation require root privileges. Either put sudo in front of the relevant commands or change to a root account.

su -
yum update

Installing  Postgres (9.4)

The development files are needed later to build one of the python modules. The database will be initialized, added to the boot configuration and started.

yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1.noarch.rpm
yum install postgresql94-server postgresql94-contrib postgresql94-devel
service postgresql-9.4 initdb
chkconfig postgresql-9.4 on
service postgresql-9.4 start

Creating a database and a user

PostgreSQL runs with a user called postgres and psql as a terminal-based front-end.

to PostgreSQL.

su - postgres
psql

In the terminal will the database and  the user granted with the right to access this database created

CREATE DATABASE cms;
CREATE USER cms WITH PASSWORD 'yourPassword';
GRANT ALL PRIVILEGES ON DATABASE cms TO cms;

Check with:

-l 
\du

The first command list all databases and the second one all users than quit psql and return to root user with:

\q
exit

In order to allow access from a user with a password on the domain socket we need to modify the PostgreSQL access control configuration  file (pg_hba.conf) and restart the service.

vi /var/lib/pgsql/9.4/data/pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     md5
service postgresql-9.4 restart

Installing Python 3.3

CentOS 6 ships with Python 2.6.6 and several critical system utilities, for example yum, will break if the default Python interpreter is upgraded. The correct way is to install an alternative version of Python in /usr/local (or similar location).

IUS repo from the Rackspace team

yum install http://dl.iuscommunity.org/pub/ius/stable/CentOS/6/x86_64/ius-release-1.0-14.ius.centos6.noarch.rpm
yum install python33 python33-libs python33-setuptools python33-mod_wsgi python33-devel
alias python='python3.3'

Check with

python --version

Setting up Mezzanine

Installing pip from pip.pypa.io

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

Installing and Configuring required packages

yum install gcc zlib-devel libjpeg-devel

Installing database connector, Django and Mezzanine

The IUS-repository does not provide python33-psycopg2 and python33-django, therefore pip is used. As the database is not installed in the standard directory this path need to be added to PATH and also the development files need to be available (we installed them already with the database) to build the connector will be built against the database.

export PATH=/usr/pgsql-9.4/bin/:$PATH
pip install psycopg2

Django will be installed with a specified version as the latest version does not match the python version.

pip install Django==1.8
pip install mezzanine
cd /var/www
mezzanine-project testblog

Configuration for postgres connection

In the main directory, you will see a file called local_settings.py. Go ahead and open that file with a text editor (Vim, Emacs, Gedit, whatever; just don’t use a word processor). Fill in the main entries (engine, name, user, password, host, port) with the settings we created previously. Like this:

cd /var/www/testblog/testblog
vi local_settings.py
DATABASES = { 
 "default": { 
 # Ends with "postgresql_psycopg2", "mysql", "sqlite3" or "oracle". 
 "ENGINE": "django.db.backends.postgresql_psycopg2", 
 # DB name or path to database file if using sqlite3. 
 "NAME": "cms", 
 # Not used with sqlite3. 
 "USER": "cms", # Use your actual username. 
 # Not used with sqlite3. 
 "PASSWORD": "St1963echlin", # Use your actual password. 
 # Set to empty string for localhost. Not used with sqlite3. 
 "HOST": "localhost", # I have found that placing "localhost" here is required in some cases. 
 # Set to empty string for default. Not used with sqlite3. 
 "PORT": "5432", 
 # See below.

Creating database

cd /var/www/testblog
python manage.py createdb

Starting a the python-webserver

python manage.py runserver

Deploying to Apache

The python-webserver is easy to use for testing, it’s not suitable for production server. Mezzanine comes with tools for deployments on Nginx, the deployment on Apache need been done manually.

Installing mod_wsgi for Apache

yum install python33-mod_wsgi
service httpd restart

Configuration in a virtual host as daemon process

For this example we are keeping all adjustments in the Apache main configuration file. Open the main configuration file:

vi /etc/httpd/conf/httpd.conf

Doing the following changes on the end of the document in the virtual host section.

  • Uncommenting (removing the number sign) NameVirtualHost without making any changes. Further changes of this line would be needed if more than one IP-address or different ports are used for the virtual host. (please refer to the apache documentation)
  • Add the path for the wsgi folder
  • You can leave the rest of the number marks in place until you reach the line <VirtualHost *:80> . Uncomment everything from there through <VirtualHost>.
  • Leave <VirtualHost *:80> as is—its details must match with those in the NameVirtual Host section.
  • Adjust the server name, alias  and administrator email
  • Delete document root For this section, write in the extension of the new directory created in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.
  • Add the directives for static directory and wsgi file
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier 
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /var/www/testblog
    ServerName testblog.local

    WSGIDaemonProcess testblog.localhost python-path=/var/www/testblog/
    WSGIScriptAlias / /var/www/testblog/testblog/wsgi.py process-group=testblog.localhost

    <Directory /var/www/testblog/testblog>
        <Files wsgi.py>
            Order deny,allow
            Allow from all
        </Files>
    </Directory>

    Alias /static/ /var/www/testblog/static/

    <Directory /var/www/testblog/static>
        Options Indexes MultiViews FollowSymLinks
        AllowOverride all
        Order deny,allow
        Allow from all
    </Directory>

#    ErrorLog /var/www/testblog/logs/error.log
#    CustomLog /var/www/testblog/logs/access.log combined

</VirtualHost>

WSGISocketPrefix /var/run/wsgi

Create the directory for wsgi run

service httpd restart
mkdir /var/run/wsgi

Change to the project directory and put all static files in to the static directory

cd /var/www/testblog 
python manage.py collectstatic

Changing ownership and rights

chown -R apache:apache /var/www/testblog

In case you of using SELinux allow apache to connect to postgresql via unix-sockets. Reboot maybe needed.

setsebool -P httpd_can_network_connect_db 1
reboot
# export LC_ALL=en_US.UTF-8

Credits:

http://www.rosslaird.com/blog/first-steps-with-mezzanine/
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-ubuntu-14-04