ManagedCloud Servers

High performance handled and monitored by us 24/7/365. A complete solution to provide you with our in house expertise 24/7 tailored to your specific needs. We'll setup a bespoke server for your site using the latest tehnologies so you can get the most out of your hardware and get your website loading quickly and reliably. Find out more..

cPanelCloud Servers

Recommended - High performance cloud servers with no technical knowledge required. If you're hosting multiple websites already and you're looking to consolidate, or if you're looking to isolate yourself from the shared hosting environment but you don't have the time or knoweldge to manage a server, then the Managed cPanel Servers are for you. Find out more..

UnmanagedCloud Servers

Our unmanaged range gives you complete control at rock bottom prices and our cloud platform boasts super fast multipath 40Gb/s network, the latest Intel Xeon V3 CPUs and enterprise grade redundant SSDs. If you're a sysadmin look no further, we offer some of the best specification to price ratio servers available. Find out more..

Need Django Hosting? Get our easy to use Django Hosting from £1.60 / month.

View Plans

How To Setup a Python Django Website on HostPresto!

This tutorial will guide you through the process of publishing your Python Django website in your HostPresto! account.

Note that the supported flavour of Python Django web sites deployment is based on VirtualEnv and the glue between the web server and your Django site is WSGI.

An example Django Website

As an example Django website we will be using a simple "Hello World" one. The structure of the Django project is as follows:

Figure 1 - Our sample Django project

Notice that the root directory of the website is called "HelloWord". The same name was used for the Python Django app that holds and serves the website. It does not make a difference what the root directory is called, as you will see further down.

The words App and Website are used interchangeably and both refer to the same thing, your Django website.

By convention, your site's root folder/directory is the main directory that contains all your project's files.

Your Django root directory is the one that contains the Python package (which is a directory itself) that contains your Django app's specific files. Those files are your settings, views, controllers, urls, etc.

In the example above, the outer "HelloWorld" directory is the the project's root foler while the the inner HelloWorld is the Django root one.

Upload your files

Use your favourite FTP client or your IDE's "Publish" feature or even cPanel's File Manager, to upload your Django project site files into your HostPresto account. Avoid uploading them into your www or public_html folder. This poses security risks and may expose your site's source code and data, especially if you are using a SQLite database. Your Django website can reside anywhere within your accounts home directory; /home/. Your site's home directory can be viewed on the left hand side column of your account's main cPanel page:

Figure 2 - Your home directory

It is highly recommended that you create a subfolder directly into your home directory and name it as you wish. This will be your app root folder. (Don't worry if it has the same name as your Django app's directory; this is perfectly valid). In our example website in Figure 1 this is the outter "HelloWorld" folder.

To clarify this even better, your root folder is the one that contains your .wsgi file, manage.py, your website's/app's main folder, etc.

Configure the Web Server

This step takes care of all that is needed to tell the web server about your Django web site, set it up and configure it.

Adding your Django website/App

In your cPanel's main page, navigate to the "SOFTWARE" section and select the option Setup Python App:

Figure 3 - Your Python Django starting point

In the page that will appear:

Figure 4 - Set up new Python App

Select your version of Python. It is essential that this is the same version of Python you used to develop your website, or a later version. Do not set this to an earlier version than the one you are using.

In the App Directory type the name of your project's root folder, as created in the previous section; the one relative your account's home folder. In our example app, this is the "django" folder.

In the App URI you must specify your Django website's main URI. This is usually the URI path of your Django website's home page. Unless your Django website is a subpart of another web site, this value must be set to / If you already have a main website, e.g. myhostprestowebsite.com and you want to add a Django one within it, then you need to type in the URI your Django website will runs at, e.g. myhostprestowebsite.com/djangoSite. In this case you need to enter /djangoSite. NB in such a case, make sure your urls.py file contains the right mappings. If you do not know what this is or none of above applies and this is your main website, just set this to / and it will be your main website.

Note also that the value of this fields corresponds to either your webroot directory and a subdirectory of it; www or www/django. If www/django does not exist in your www directory, it will be created by cPanel. cPanel will also create or amend an existing .htaccess file with the data it needs for the Django website to work. No matter what you do, do not touch the .htaccess file's section that manages your Django app. In the HelloWorld example app, we set this to /django, so that it will not interfere with the main website.

Figure 5 - New Python App filled in

Click Setup

The values you enter are case sensitive. So make sure the case matches your files and URLs

Configuring your Django Website

The previous step has created an entry such as:

Figure 6 - Your new Python Django App

App Directory is as you have set it and it is relative your account's home folder. App URI shows you the full URL of your Django website.

WSGI file location is the name of your .wsgi file. Do not touch this option. Do not Edit or Save it. Saving it might replace your configuration with default values. The next section explains how to configure WSGI.

Python modules, including Django itself

What we are missing, so far, are the Python and Django modules that both Python and your app use!

And yes you guessed it right, we need to install Django itself, as a module! Click on the "Modules" "Show" button of Figure 6 Your new Python Django Appand the following will appear

Figure 7 - Adding the Django module

Type Django in the search box and select the first Django option that appears on the drop down box, below the search text box.

Then you will be prompted to select the version of Django (the module) you wish to install. Avoid any Alpha and Beta versions and go for the version of Django you use during the development of your website.

Then click on the Add button. This will install the Django module into your Python application's VirtualEnv.

Then you need to click the Update and then the Restart buttons, to apply the changes and make them take effect.

Figure 8 - Django module installed
It is good practice (and required) to Update & Restart every time you make any changes to your set up Python app.

Django is the one and the main module you need. However, if you app/website uses other external (to the Python and Django core) modules, you will need to install those too. Follow the steps described in this section and install all other modules that are required; you have done this already, via pip or other methods, in your development environment!

If required modules are missing from your VirtualEnv, you will keep getting error messages when accessing your website.

The .wsgi file (the glue)

Finally, your .wsgi file tells Python where your app resides, adds your app's path to the Python runtime environment as well as it is essential and necessary. Thus you must get it right or your website will not show. Our Python Django hosting software has been configured such that WSGI files must reside within the app root folder and be named "passenger_wsgi.py". So in our example the .wsgi file's path is: /home/zisoftco /django/passenger_wsgi.py. In some circumstances the file is created by cPanel. If it is not there yet, feel free to create one yourself.

If you already have a .wsgi that works, all you need to do is copy its contents into your new passenger_wsgi.py file, overwitting all the latter's previous contents. If you do not have one already, then our sample apps .wsgi file looks like this:

Figure 9 - Sample WSGI file

Note that the setting os.environ.setdefault("DJANGO_SETTINGS_MODULE", "HelloWorld.settings")*** defines an environment variable that must be pointing to your Django app's **settings.py file, without the .py extension. Ensure that the directory name of your Django app is correct ("HelloWorld.") as well as that the settings.py file resides in that directory and its name is settings.py.

Following is the sample WSGI's file code. Feel free to copy and use it.

Figure 10 - Sample WSGI Config

Troubleshooting & Best Practises

Before you publish you application, or even better before you upload it, make 100% sure that the SPython modules, including Django itself.

Turn of Debug

Don't run with debug turned on in production! ENsure the DEBUG flag is set to false! If you have DEBUG = True in a live website, the chances are it will be hacked! Please refer to the Python Django documentation regarding this fact.

Restrict Hosts if Possible

The ALLOWED_HOST setting, controls who can view your website. * means any client can view your website. If you restrict this value to a hostname/domain/IP address range/etc then only the hosts in this group will be able to access your site.

Check your Static and Media URLs

Static files: if your website has problems showing correctly as a result of .css and .js files missing, you must ensure your STATIC_URL and MEDIA_URL settings do not clash.

Moreover, your static files must be servable from a location served by a web server. Django and Python do not serve images, css and js files. And it would be a total overkill to get to do that. A good approach is to have your .css and .js files in another directory residing within your www (public_html) directory which is served by our web servers. Then make your STATIC_ROOT setting point to the absolute path of that location. If your images are not displaying, then you need to ensure that your MEDIA_ROOT setting has the correct value. It is a good idea to keep STATIC_ROOT and MEDIA_ROOT separate. The main and most common reason being that your site admins won't be uploading their images into the same location as the .css and .js code. The MEDIA_ROOT location is identical to set up and use, as the STATIC_ROOT, one i.e. you will need a directory within servable web space; served by the main web server.

STATIC_URL and MEDIA_URL common problems:

They must not share the same location or clash.

Ensure your urls.py file is aware of those locations and does not include them in the Django app URL space. url(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), url(r'^media/(?P.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), STATIC_ROOT is paired with STATIC_URL and MEDIA_ROOT is paired with MEDIA_URL. If those two pairs clash in any way, then your app will not work.

Other Notes

Ensure your SECRET_KEY settings is long and unique and contains a lot of random characters and symbols. Ensure the TEMPLATE_DEBUG settings (if used) is set to False, too. Ensure you TIME_ZONE setting has the right and desired value. Ensure that you provide names and emails of yourself or other administrators in the ADMINS setting.

If none of the above makes sense or seems incomplete, you are strongly encouraged to consult the Python Django documentation and ensure security and functionality of your Django web site.

Conclusion

After following this tutorial you should be able to setup your Django App on a HostPresto! Django hosting package easily and securely. Good luck and may the source be with you!

Need Django Hosting? Get our easy to use Django Hosting from £1.60 / month.

View Plans