Mezzanine Local Test Environment

(Comments)

Objective:

This is a tutorial to setup mezzanine local dev environment. If you have created website using mezzanine or even django and have had hair pulling experience with setting up a local dev environment then continue reading.

Here I will assume:

1) You know Git (I will skip setting up git repo)

2) You know basics of Mezzanine or Django

The Setup:

Okay so you already have a mezzanine website running on a VPS or a dedicated server running inside a virtualenv. Somehow you have managed to get it up and running. Now you have decided how to setup a local dev environment where you can make some changes and push them to production via git. Here is how you do it.

How To:

VIRTUALENV:

Create a virtualenv on your local machine .Git repo should be your project directory . Clone your repo  inside the virutalenv you just created. If you maintain a requirements.txt file, then activate the virtualenv and install pacakages from there like this

$ cd my_venv/
$ pip install -r requirements.txt

If you dont have a requirements.txt file then simply 

$ pip install mezzanine

TEMPLATES:

Editing templates and pushing them to production is a straightforward thing.

SETTINGS.PY

A default mezzanine project looks like this

$ ls -alF proj

total 72
drwxrwxr-x  4 mayur mayur  4096 Oct 18 20:09 ./
drwxrwxr-x 11 mayur mayur  4096 Oct 18 20:09 ../
drwxrwxr-x  2 mayur mayur  4096 Oct 18 20:09 deploy/
-rw-rw-r--  1 mayur mayur 16008 Jul 31 12:12 fabfile.py
-rw-rw-r--  1 mayur mayur    63 Jul 31 12:12 .gitignore
-rw-rw-r--  1 mayur mayur    93 Jul 31 12:12 .hgignore
-rw-rw-r--  1 mayur mayur     0 Jul 31 12:12 __init__.py
-rw-rw-r--  1 mayur mayur   548 Jul 31 12:12 local_settings.py
-rw-rw-r--  1 mayur mayur   898 Jul 31 12:12 manage.py
drwxrwxr-x  2 mayur mayur  4096 Oct 18 20:09 requirements/
-rw-rw-r--  1 mayur mayur 12759 Oct 18 20:09 settings.py
-rw-rw-r--  1 mayur mayur  3856 Jul 31 12:12 urls.py
-rw-rw-r--  1 mayur mayur   288 Jul 31 12:12 wsgi.py

Make sure that .gitignore file contains local_settings.py. This file should used only on local dev environment. On production all the settings should be handled by settings.py alone.

In local_settings.py , set DEBUG = True or else your static file (css, js ,images) wont be served properly

DATABASE:

For database setup I would highly recommend that which ever database you use in production use the same on your local machine. If you use postgresql on your website I suggest you install postgresql on your local machine as well.

For simplicity reasons create a database with same name, same user and same password as that you have on your production server.

Also we will need get a sql dump of you DB from production server. We will be using it in this guide.

To backup / restore your database use django-dbbackup package https://pypi.python.org/pypi/django-dbbackup .This package is really handy for DB backups n restores. Follow the "DBBackup to Local Disk" part of the documentation.

On production machine, setup django-dbbackup, take a dump of sql using this command

$ python manage.py dbbackup

and transfer it to local machine

On local machine, setup django-dbbackup, use the dump and restore it in your local database.Use this command to restore database

$ python manage.py dbrestore -f "my_production_DB_dump.sql"

Since your local DB now contains same data as your production, your database is ready to test n run locally.

Runserver Hack:

As root, edit your /etc/hosts like this

$  echo -e "127.0.0.2 \twww.mywebsite.com mywebsite.com" >> /etc/hosts

As root user run the server on port 127.0.0.2:80 like this

$ python manage.py runserver 127.0.0.2:80

You might think whats wrong with running server on 127.0.0.1:8000 like a normal user. Here is why

1) There is a default website in Mezzanine that runs on 127.0.0.1:8000. You do not want your traffic to be served from the default website or spend time figuring out that why aren't your changes reflecting on your website when in fact you are looking at the default website

2) Running server on 127.0.0.2:80 as root guarantees that we will be on the safer side , away from troubles n confusion.

The only thing that will annoy you is while viewing the live website you will have to disable the entry ( 127.0.0.2:80 ) that we just did in /etc/hosts and when you want to test again, you will have to put the 127.0.0.2:80 entry back. For a effortless switching between live and test website do this

1) Make test website host entry like this

$ echo -e "127.0.0.2 \twww.mywebsitetest.com mywebsitetest.com" >> /etc/hosts

2) Run the current website and go into admin/sites . Change the name of your website from mywebsite.com to mywebsitetest.com

3) Delete the mywebsite.com entry from /etc/hosts

4) Done !! Now you can access your live website at mywebsite.com and access your test website at mywebsitetest.com

Conclusion:

I hope this post gives you an idea as well as guides you as to what needs to be done to setup you local dev environment. For Heroku users, the database setup will need more tweaking that this. For rest of the people, have a less painful day.

Comments