User Guide - Django
- 03/11/2008 5:18 PM
Applicable Plans: All Standard VPS, Advanced VPS, and Premier VPS Plans (CentOS 5 plans)
“Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. With Django, you can build high-performing, elegant Web applications quickly.” - from http://djangoproject.com
This user guide will provide the minimum steps necessary for deploying your Django application in your eApps VPS. Examples are given for both Control Panel and command line setup. For configurations beyond the scope of this User Guide please see the Resources section at the end of the guide.
INDEX
mod_python
Django
Links to Other Information
Prerequisites
Django requires that a database, a database driver, mod_python, and Django are installed. You can use Django with MySQL, PostgreSQL, or SQLite, all of which are supported on your eApps VPS.
MySQL or PostgresSQL
To use MySQL or PostgreSQL with Django you will need to install either the MySQL or the PosrgreSQL server from your Control Panel. You will also need to install mysql-python or python-psycopg from the Power Panel or the command line using Yum.
To install MySQL or PostgreSQL, follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Add Application link. Select the box next to either MySQL-5 or PostgreSQL-8, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
To install mysql-python or python-psycopg follow one of these steps:
To install from the System tab:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Add Application link. Select the box next to mysql-python or python-psycopg, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
To install from the Parallels Power Panel:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Parallels Power Panel link. In the next screen, click the link for the Control Panel URL. If prompted, accept the SSL Certificates offered. These are self-signed SSL Certificates, so they are not always recognized by the browser.
Log into the Power Panel as the root user, with the root user's password. In the Container Management Menu, click Packages (the process for getting the list of packages may take quite some time, please wait for it to complete). Click Install New Packages (this process can also take some time to complete, please wait for it to finish), and then in the Name field type MySQL-python or python-psycopg and click Search. Select the check box next to your search result and click Install.
To install with Yum:
First make sure Yum has been installed on your VPS. Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to All Applications, and you should see Yum installed. If it is not installed, go to Applications, and click on the Add Application link. Select the box next to Yum, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
Using SSH, connect to your VPS and login as root. From the command line use either:
yum install MySQL-python
or
yum install python-psycopg
to install the correct application. The User Guides for Yum and SSH are located here:https://support.eapps.com/kb
SQLite
SQLite is installed in your VPS when it is created. All you will have to do is install python-sqlite2 from the Power Panel or the command line using yum.
To install python-sqlite2 follow one of these steps:
To install from the System tab:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Add Application link. Select the box next to python-sqlite2, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The applications should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
To install from the Parallels Power Panel:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Parallels Power Panel link. In the next screen, click the link for the Control Panel URL. If prompted, accept the SSL Certificates offered. These are self-signed SSL Certificates, so they are not always recognized by the browser.
Log into the Power Panel as the root user, with the root user's password. In the Container Management Menu, click Packages (the process for getting the list of packages may take quite some time, please wait for it to complete). Click Install New Packages (this process can also take some time to complete, please wait for it to finish), and then in the Name field type python-sqlite2 and click Search. Select the check box next to your search result and click Install.
To install with Yum:
First make sure Yum has been installed on your VPS. Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to All Applications, and you should see Yum installed. If it is not installed, go to Applications, and click on the Add Application link. Select the box next to Yum, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
Using SSH, connect to your VPS and login as root. From the command line use:
yum install python-sqlite2
to install the correct application. The User Guides for Yum and SSH are located here:https://support.eapps.com/kb
mod_python
To install mod_python, follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Add Application link. Select the box next to mod_python, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
Django
To install Django, follow these steps:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Go to Applications, and click on the Add Application link. Select the box next to Django, and then scroll down and click the Next button.
This takes you back to the All Applications screen. Wait for five minutes, then click on the Refresh link at the upper right, just under the word Parallels. The application should now show as installed. If it is still in a Scheduled state, wait another five minutes, and click Refresh again. If you see it in Error state, or it still shows as Scheduled, please contact eApps Technical Support.
Deploying Django Applications with Apache and mod_python
Apache and mod_python is the recommended way for deploying your Django application on a production server. This section of the User Guide will cover how to deploy an application with Django, Apache and mod_python.
You can use these steps to deploy an existing Django application in your eApps VPS. Make certain that you have met all the Prerequisites before you begin. To create a Django application on your VPS, see the section titled Using your VPS for Django development.
In the example below we assume that you have added a site from the Control Panel with a domain yourdomain.com, the site admin as webadmin and the Apache document root is /home/webadmin/yourdomain.com/html. Your Django project is called django_project and your application is called cmsapp, and your media files are in a directory called media. This also assumes that all command line commands are executed by the root user connected via SSH. Remember, this is only an example. Your actual domain, project, application and media files will have different names. Please substitute accordingly.
Uploading your application
Using either FTP or SFTP, upload your Django project to /home/webadmin/yourdomain.com/html
Serving media files
Django does not serve media files so you will have configure Apache to serve the media files in your Django application. You will need to either create a symbolic link in your Apache document root or copy the media directory from your application to your Apache document root. The symbolic link option can only be used from the command line, the copy option can be done from either the command line or the Control Panel.
Symbolic link option:
From the command line execute the commands below:
cd /home/webadmin/yourdomain.com/html
ln -s /home/webadmin/yourdomain.com/html/django_protjec/cmsapp/media media
Copy option:
From your control panel:
Login to your Control Panel, and click on the System Tab. If necessary, click the Select Another System (Subscription) link on the left and choose the correct system.
Click on File Manager, then navigate to home → webadmin → yourdomain.com → html → django_project → cmsapp
Click the box to the left of the media directory, and then click Copy. In the next screen, click Select, and in the next screen, click the '+' next to the home directory to navigate to the home → webadmin → yourdomain.com directory, then click on the html directory and in the next screen, click OK.
From the command line:
cd /home/webadmin/yourdomain.com/html/django_project/cmaapp
cp -r media /home/webadmin/yourdomain.com/html
Modifying settings.py to server media files:
You have to now modify your django_project settings.py file to set your MEDIA_ROOT and MEDIA_URL to match the location in your Apache document root. Using a text editor of you choice open /home/webadmin/yourdomain.com/html/django_project/settings.py and set the parameters as follows.
MEDIA_ROOT = '/home/webadmin/yourdomain.com/html/media/'
MEDIA_URL = 'http://www.yourdomain.com/media/'
Remember, this is just an example, please substitute your actual file names and directories accordingly.
Add mod_python directives to your Custom Settings
Login to the Control Panel and click on the Site Tab. If necessary, click the Select Another Site link on the left and choose the correct site.
Click on Website Settings, and then on the Custom Settings tab. Click on Edit, and add the lines below. Once you are finished, click Update. Again, make sure to substitute your actual domain, project name and directory names accordingly.
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE django_project.settings
PythonDebug On
PythonPath "['/home/webadmin/yourdomain.com/html'] + sys.path"
</Location>
<Location "/media/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png)$">
SetHandler None
</LocationMatch>
</Location>
Your application should now be available at http://www.yourdomain.com and http://yourdomain.com.
Using your VPS for Django development
With Django installed you can also create new projects and build an application on your VPS. This section of the User Guide will cover how to create a project called hello with a helloworld application. This section again assumes that the project will be built in the /home/webadmin/yourdomain.com/html directory.
Unlike the previous section for deploying an already existing Django application, this section requres that all work be done from the command line as the root user. This also assumes that you are very familiar with working from the Linux command line and editing files in a text editor such as vi. If these concepts are not very familiar to you, this section is beyond your abilities. None of what follows can be done from the Control Panel, with the exception of the very last section where entries are added to the Custom Settings Tab of the site. It is assumed that you are now connected to your VPS as root.
Creating a Django helloworld application:
cd /home/webadmin/yourdomain.com/html
django-admin.py startproject hello
A directory called hello will be created and should include the files below:
hello/
__init__.py
manage.py
settings.py
urls.py
Change to the hello directory, and run the command to create the application:
cd hello
python manage.py startapp helloworld
A directory called helloworld will be created and should include the files below:
helloworld/
__init__.py
models.py
views.py
This will configure the application to serve a simple Hello World! content. Using a text editor of your choice edit open helloworld/views.py and add the lines below and save the file.
from django.http import HttpResponse
def helloworld(request):
return HttpResponse('Hello, World!')
Edit /home/webadmin/yourdomain.com/html/hello/urls.py and add the line below after the line above (that will already be in the file), and save the file.
#(r'^admin/(.*)', admin.site.root),
(r'^$', 'hello.helloworld.views.helloworld'),
Testing the helloworld application
You can now use the Django development Web server to test the hellowold application. Change directories to the project root directory, and start the Web server.
cd /home/webadmin/yourdomain.com/html/hello/
python manage.py runserver 0.0.0.0:8000
If the start is successful you will see the output below.
Validating models...
0 errors found
Django version 1.0-final-SVN-unknown, using settings 'hello.settings'
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
You can now view the application at http://yourdomain.com:8000
Add the project directives to your Custom Settings
This application can be served with Apache by adding the lines below to your custom settings.
Login to the Control Panel and click on the Site Tab. If necessary, click the Select Another Site link on the left and choose the correct site.
Click on Website Settings, and then on the Custom Settings tab. Click on Edit, and add the lines below. Once you are finished, click Update. Again, make sure to substitute your actual domain, project name and directory names accordingly.
<Location "/">
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE hello.settings
PythonDebug On
PythonPath "['/home/webadmin/yourdomain.com/html'] + sys.path"
</Location>
<Location "/media/">
SetHandler None
</Location>
<LocationMatch "\.(jpg|gif|png)$">
SetHandler None
</LocationMatch>
</Location>
Links to Other Information
Django Documentation - http://docs.djangoproject.com/en/dev/
Django Tutorial - http://docs.djangoproject.com/en/dev/intro/tutorial01/