How to deploy a Django web application
How to deploy a Django web application on Mochahost.com
Note: Our python version is 2.7 which should be compatible with all django releases and the latest django release.
At a glance
To deploy a Django web application at MochaHost, you need to:
· request activation of mod_python for your account through tech support
· upload all Django files
· upload your application files and modify settings.py accordingly
· setup the Django handler in .htaccess file using SetHandler python-program
· upload static media files to ~/www/media and ~/www/adminmedia, and create .htaccess files in these folders containing “SetHandler None”
And you're done! :-)
Here is a step-by-step tutorial for deploying a Django web application called “HelloDjango”.
The web application on your local machine
Let's assume the following setup is in place and working on your local machine:
Directory structure of the example application on your local machine is:
. . . .
. . .
<other files and folders>
The web application connects to a MySQL database on your local machine with:
database name: hellodjango
Also, all necessary tables have been created using
$ python manage.py syncdb
Assuming the above, let's move to the step-by-step tutorial.
Set up the environment
Login to your account at http://www.mochahost.com/support.php and go to "Get Support" / "Support Desk".
Create a ticket in the support application, and state that you need mod_python activated for your account. The support guys are pretty quick, so you'll receive a confirmation soon that mod_python has been activated for your account.
Login to the Control panel at http://cpanel.yourdomain.com (the actual URL is in the confirmation email that you received when you created your account)
Open File Manager and make it show your Home folder - /home/youraccount. Make sure to check the "Show Hidden Files" checkbox.
Create a folder outside of your web root - "/home/youraccount/webapps”.
Deploy the Django framework
Upload all Django files to /home/youraccount/webapps/django. If you installed Django locally on your Linux machine, usually you can find all Django files in /usr/lib/python2.5/site-packages/django .
Deploy your application
Upload your application to /home/youraccount/webapps/hellodjango.
Change the TEMPLATE_DIRS variable in settings.py to /home/youraccount/webapps/hellodjango/templates
Setup the web server
Create/edit a .htaccess file in your web root folder with the following contents:
SetEnv DJANGO_SETTINGS_MODULE hellodjango.settings
PythonPath "['/home/youraccount/webapps'] + sys.path"
Static (media) files
Django recommends serving static files (.js, .css, .jpg, etc.) to be left up to the web server. Create a new folder in your web root - /home/youraccount/www/media , and upload all your media files there. In this folder, create a .htaccess file containing:
If you are using the admin feature of Django, you should do the same for the static files for the admin interface. Create a new folder /home/youraccount/www/adminmedia, and copy all the files from /home/youraccount/webapps/django/contrib/admin/media into this new folder. Create a .htaccess files as in the ../media folder. Also make sure you set the ADMIN_MEDIA_PREFIX variable in settings.py to “/adminmedia/”.
Create your database via MySQL Databases in Control Panel. For this example, we'll use:
Database name: hellodjango
Export your database from your local machine using mysqldump:
$ mysqldump -u your_db_user -p -l -e -Q your_database > ~/db.sql
Import your database via phpMyAdmin in Control Panel from the exported db.sql.
Setup the database access in settings.py
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'youraccount_hellodjango'
DATABASE_USER = 'youraccount_hellodjango'
DATABASE_PASSWORD = 'hellodjango'
DATABASE_HOST = 'localhost'
DATABASE_PORT = ''
Test your application
Point your browser to “www.yourdomain.com” or “http://servername/~youraccount/ and you should see your application running
If everything runs fine, make sure that:
· the DEBUG variable in settings.py is set to False
· the PythonDebug setting in /home/youraccount/www/.htaccess is set to Off.
If you see a Error 500 page, you can edit the .htaccess file in your web root and change
Now mod_python will show you detailed information about the error that occurred.
Make sure you turn off the debug setting after you solve the problem, because leaving this debug setting to “on” is considerable security vulnerability.
If you get an error “Cannot find module xxxx”, make sure the paths are correct, and all Python files (both Django's and your app's files) have “read” and “execute” access rights for the Apache user.