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.).
- First step is pretty obvious -- register an account -- plain & simple.
- 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.
-
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 $LOGINBut 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 $LOGINYou 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 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: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'], } }
install_requires=['Django>=1.3','MySQL-python'], - 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:
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:
#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
And now execute these 3 commands:ssh uuid@domain # for example ssh a01e3eebf93c947d33e862e20c41f43b@django-username.rhcloud.com
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