Pages

Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

27 March, 2012

How to configure Django and MySQL on OpenShift

Not a long time ago I wanted to try OpenShift. After I found out that it supports Python and then even Django I instantly knew, it might be home for my Django project.
When I tried to configure my instance I realized, it will be pretty tough fight (even with couple of tutorials out there). I will try to write down some handful tips (mainly for myself -- this means that there might be some incomprehensible terms, grammatical errors (English is not my native language) and lots of elementary stuff -- I consider myself still a linux & python & django & web development newbie. Despite these flaws I can imagine that these tips might be useful for somebody.).
  1. First step is pretty obvious -- register an account -- plain & simple.
  2. Now it is time to create an application. I followed this awesome tutorial & used that sample project, so it would be counterproductive to repeat it again.
  3. It could be nice to have some database -- I chose MySQL. Applications (like MySQL) are based in a form of cartridges, so you just have to add it:
    rhc-ctl-app -e add-mysql-5.1 -a $APP_NAME -l $LOGIN
    But this isn't just enough. Some sort of database client would be nice. Let's add phpMyAdmin:
    rhc-ctl-app -e add-phpmyadmin-3.4 -a $APP_NAME -l $LOGIN
    You should note the login credentials for both services.
    For first we have to set up MySQL database. Log in to phpMyAdmin, on main screen go to tab Privileges and click here on link Add a new user. Fill username and password (save these, we will use them in settings.py) and then check value Create database with same name and grant all privileges. In this step we have new database with dedicated user, who is going to be used by django. There is one thing, which may caused you to have nightmares in sleep. It is collation. By default it is set to latin1_swedish_ci, but I am using utf8_general_ci. I advise you to set it right now, because later you would have to convert data from one collation to the another or even wiping some tables.
    Now we have to set up django application to use MySQL. We have to edit settings.py:
    if ON_OPENSHIFT:
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.mysql',
                'NAME': 'username', #username specified in step before
                'USER': 'username', #username specified in step before
                'PASSWORD': 'password', #password specified in step before
                'HOST': os.environ['OPENSHIFT_DB_HOST'],
                'PORT': os.environ['OPENSHIFT_DB_PORT'],
            }
        }
    
    If we try syncdb now, it won't work, because there is not database driver for MySQL. Open setup.py and add module MySQL-python to install-require and pip will install it. It will look like this:
    install_requires=['Django>=1.3','MySQL-python'],
  4. Database is created, but is empty. We have to execute syncdb to fill it. It might done from command like, but that's not very practical. Better way is to add this command to action hook. Open file .openshift/action_hooks/deploy and edit it to look like this:
    #Activate VirtualEnv in order to use the correct libraries
    source $OPENSHIFT_GEAR_DIR/virtenv/bin/activate
    export PYTHON_EGG_CACHE=$OPENSHIFT_GEAR_DIR/virtenv/lib/python-2.6
    
    echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py syncdb --noinput'"
    python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py syncdb --noinput
    
    echo "Executing 'python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py collectstatic --noinput'"
    python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py collectstatic --noinput
    
    python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py createsuperuser --username=admin --noinput --email me@example.org
    #python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py changepassword admin
    You have to use correct path, so change it accordingly. This script does syncdb, collectstatis and last command creates superuser. I didn't find out how to change superuser's password through script (mainly because bash is not really my friend) so I did it by connecting to server and executing it manually:
    ssh uuid@domain # for example ssh a01e3eebf93c947d33e862e20c41f43b@django-username.rhcloud.com 
    And now execute these 3 commands:
    source $OPENSHIFT_GEAR_DIR/virtenv/bin/activate
    export PYTHON_EGG_CACHE=$OPENSHIFT_GEAR_DIR/virtenv/lib/python-2.6
    python $OPENSHIFT_REPO_DIR/wsgi/openshift/manage.py changepassword admin
And that's it. Enjoy your django application.