Python: How to install Django
In this article, we will show you the steps needed to install Django and setup a basic application.
For this article, you will need SSH access to your account.
1) First we need to edit our bashrc file to add our python path.
nano ~/.bashrc |
and add the following to the end
export PATH="$HOME/.local/bin:$PATH"
Once you have added it, we need to save the file. You can do this by pushing Ctrl+X on your keyboard. If prompted, press Y and then enter.
Then we need to source our bashrc file so the server picks up the changes. We can do this by running the following command.
. ~/.bashrc |
2) Next we need to create a tmpdownload folder so we can download all the required files for Django.
mkdir ~/tmpdownload |
cd ~/tmpdownload |
3) Now we need to download Django itself and extract it.
wget https://www.djangoproject.com/download/1.4.3/tarball/ |
tar xzf Django-*.tar.gz && rm Django-*.tar.gz |
cd Django-* |
4) Now its time to install Django
python2.6 setup.py install --user |
5) Now we need to create the folder our projects will reside in.
mkdir ~/projects |
cd ~/projects |
6) Next we need to create our project and start our application
django-admin.py startproject myproject |
cd myproject |
./manage.py startapp myapp |
7) Now we need to create the htaccess file that will point users to our fcgi script that we are about to create.
cd ~/public_html |
nano .htaccess |
add the following contents, then save the file (Ctrl+X)
AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]
8) Now we need to create the fcgi file that will link to our project.
nano index.fcgi |
now add the following
#!/usr/bin/env python2.6
import sys, os
# Don't edit above this line!
#
# Enter your username between the quotes here...
USERNAME="username"
# ...and your project's name here
PROJECTNAME="myproject"
#
# Don't edit below this line!
#
# Add a custom Python path.
sys.path.insert(0,
'/home/'+USERNAME+'/.local/lib/python2.6/site-packages')
sys.path.insert(0, '/home/'+USERNAME+'/projects/'+PROJECTNAME)
#
# Switch to the directory of your project.
os.chdir('/home/'+USERNAME+'/projects/'+PROJECTNAME)
#
# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = PROJECTNAME+'.settings'
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
Make sure you replace the items in bold with the correct information for your account/project. Once you are done, save the file (Ctrl+X)
now you need to run the following
chmod +x index.fcgi |
9) Now lets clean up our tmpdownload folder:
rm -r ~/tmpdownload |
That’s it! You now know how to install django and setup a project.
If you wish to setup Django outside of your public_html directory (e.g. http://example.com/django), simply cd to a different directory in step 7.
Please Note: In some situations, you may not notice changes made to your code/website. This can be resolved by killing/stopping the index.fcgi process over SSH. For more information on how to do this, please click here.
Amos January 2, 2013 at 4:30 pm
Thanks for this tech note. I got the django site up and running. When I go to access the Admin portal, I noticed that it does not display the standard DjangoAdmin look and feel. This is apparently common. Can you assist in how we need to modify the django project settings.py to reference the proper DjangoAdmin static files?
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#serving-the-admin-files
John Oliver at Site5 January 22, 2013 at 5:36 pm
Hello,
Unfortunately without more details I can’t provide the answer to your question. The link you provided is for sites using mod_wsgi, however we use FastCGI here at Site5. Because of this, that link you provided unfortunately does not apply to the way our services are setup.
Ryan January 7, 2013 at 9:35 pm
Next steps for me were:
create the database tables:
> cd ~/projects/myproject
> python2.6 manage.py syncdb
(need to reference python2.6 otherwise it uses python2.4 which uses django 1.1.1)
enable admin:
> cd ~/projects/myproject/myproject
> nano settings.py
(uncomment the line in INSTALLED_APPS for “django.contrib.admin”)
> nano urls.py
(uncomment the three lines where it says “uncomment … to enable the admin…”)
enable static files for admin:
> cd ~/public_html
> mkdir static
> cd ~/projects/myproject
> python2.6 manage.py collectstatic
(downloads the files into “admin” subfolder of current folder)
> mv admin ~/public_html/static
log into the admin center:
yourdomain.com/admin/
Ryan January 7, 2013 at 9:43 pm
I got an error: table django_admin_log doesn’t exist (I assume because I ran syncdb before enabling admin). Running this again fixed it:
> python2.6 manage.py syncdb
Joe March 3, 2013 at 5:54 pm
When I run step 4 (setup.py), the script ends with the warning message below. After completing all the steps on this page, when visiting my site, I see an “unhandled exception” message. Can these 2 things be related? And where can I see the error logs to get the details of the exception?
Thanks!
========
WARNING!
========
You have just installed Django over top of an existing
installation, without removing it first. Because of this,
your install may now include extraneous files from a
previous version that have since been removed from
Django. This is known to cause a variety of problems. You
should manually remove the
/usr/local/lib/python2.6/site-packages/django
directory and re-install Django.
John Oliver at Site5 March 8, 2013 at 4:01 pm
Hello Joe,
Thank you for bringing this to our attention.
Our server administrators will be looking into our setup soon to see how we can fix the warning that appears during the install process.
However, in the meantime, the warning itself does not actually mean something bad has happened, so you can ignore it. Your sites should still function as expected.
As for the unhandled exception message, please open a ticket with our support department so that we can take a better look into the issue.
You can open a ticket from the support tab in Backstage.
Devon March 10, 2013 at 1:38 am
Should I need to run `python2.6 manage.py runfcgi` or something like that to start it, or should the server just work?
John Oliver at Site5 March 11, 2013 at 4:36 pm
Hello Devon,
Because of the way the htaccess file calls the fcgi script, the server should work by itself. There is no need to start/stop the server as this is all automated when the person first tries to access the site.
If you have any questions, please feel free to leave a comment here, or you can contact our support staff directly by any of the methods found at the following link…
http://www.site5.com/contact/
Happy Hosting!
Miles April 22, 2013 at 6:55 am
My site keeps coming up with the default site5 page “cgi-sys/defaultwebpage.cgi” – is that normal? I’m sure I covered every step correctly and ws expecting the default Django page.
John Oliver at Site5 April 24, 2013 at 2:12 pm
Hello Miles,
I hope this comment finds you well!
Unfortunately without more details it can be hard to debug this type of issue. Normally you will only see the default webpage message if you have nothing on the account, or if the account is still new on the server. If you have completed all the steps in the articles (making sure to replace the username on step 8) and you are still getting the default message, I recommend that you clear your browser cache as well as restarting your browser to see if that corrects the problem.
For information on how to clear your cache, please see the following article:
http://kb.site5.com/browsers/how-to-clear-the-browser-cache/
If it still does not work, please contact our support department so that we can better assess the issue and help you resolve the problem.
You can contact our support department directly by opening a ticket through Backstage.
Devon April 24, 2013 at 3:35 pm
Miles, does your fcgi config point at the correct directory? I’d double check that.
Devon April 24, 2013 at 4:17 pm
Sorry, what I meant was that it sounds like a problem somewhere in .htaccess or index.fcgi
John June 8, 2013 at 10:16 pm
When you follow these instructions, what is the url one should use to access the myproject django site? I don’t seem to be able to find it.
Also, Django can co-exist with a wordpress install at the root, right?
John Oliver at Site5 June 11, 2013 at 3:43 pm
Hello John,
I hope this comment finds you well!
In regards to the URL, if you followed the article above, you should be able to access it by simply visiting the web/document root of your domain (www.example.com/).
The project that gets loaded depends on the variable set in the index.fcgi file in step 8 (e.g. PROJECTNAME=”myproject”).
In regards to running a WordPress site alongside the Django site, you may have issues because of the way WordPress handles its request (it’s also done though the htaccess file). You may be better off running one or the other in a sub-folder. If you want to move the Django site to a sub-folder, you can do this by changing “cd ~/public_html” to “cd ~/public_html/subfolder” (after you have created the folder) in step 7.
If you have any other questions, please feel free to reply here. We are always happy to help!
Homanp June 13, 2013 at 4:52 am
Hi there,
When I run the $ python manage.py syncdb to sync my database I get this error:
Error: No module named messages
I have set up a my database as such:
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.mysql’, # Add ‘postgresql_psycopg2’, ‘mysql’, ‘sqlite3’ or ‘oracle’.
‘NAME’: ‘********_django’, # Or path to database file if using sqlite3.
‘USER’: ‘********_django’, # Not used with sqlite3.
‘PASSWORD’: ‘********’, # Not used with sqlite3.
‘HOST’: ”, # Set to empty string for localhost. Not used with sqlite3.
‘PORT’: ”, # Set to empty string for default. Not used with sqlite3.
}
}
John Oliver at Site5 June 14, 2013 at 2:24 pm
Hello Homanp,
I hope this comment finds you well.
Could you try removing django.contrib.messages from INSTALLED_APPS section of the Django app configuration file?
If that does not work, I recommend that you open a ticket with our support department so that we can better assess the situation and work to find a solution.
You can open a ticket though Backstage by visiting the support tab.
We look forward to hearing from you!
Amos Brown June 14, 2013 at 10:41 pm
Hi all – I had a ton of trouble with this – likely my fault. Site5 support is great though.
I ended up ‘resetting’ my account back to scratch, and following this tutorial – http://kb.site5.com/python/how-to-setup-django-in-a-virtual-environment/ and it worked.
I’m a rookie when it comes to Django, Python, and virtual-env, but the tutorial walked me through it. I built out my site in a dev environment in Ubuntu, so I had some things to change around a little bit, but that tutorial I linked above worked.
You may have to ‘start over’ with your account – I had a few years of environmental cruft that I couldn’t clear out either because I couldn’t find it, or didn’t know what to look for. I’m happy to offer assistance on my experience in getting it set up, but this site5 support team is great.
homanp June 19, 2013 at 10:13 am
So I got the installation up and running. Just have one problem now. Seems that my admin stylesheet doesn’t run which is strange.
What could be the problem?
Thanks in advance
John Oliver at Site5 June 20, 2013 at 5:33 pm
Hello again Homanp,
In this situation, I would recommend that you contact our support team so that they can better assess the problem and help you resolve the issue.
You can contact our support team by opening a ticket though Backstage, or by emailing them directly at [email protected]
We look forward to hearing from you!
Aron January 23, 2014 at 11:28 am
Django 1.4 is very dated. Both 1.5 and 1.6 are out, but require a minimum version of Python 2.6.5 (you install 2.6.1). Better yet, Django 1.7 is coming in the next few months, and it drops support for Python 2.6 all together. Python 3.0 is not really an option, and was never really a serious release. 3.3, maybe, but it lacks library support.
Are there any plans to include Python 2.7 on your servers?
James Davey January 23, 2014 at 12:01 pm
Hi Aron,
We actually have a few versions of Python installed, as detailed at http://kb.site5.com/python/python-version-and-configuration-information/ :) 3.0 should work normally for anything you would need. We are always reviewing the versions of software like this, and updates are heavily reviewed and tested before they are pushed to the fleet. Any updates like this will be announced on our blog, and likely via email as well.
Aron January 23, 2014 at 12:08 pm
3.0 is a backwards-compatibility breaking branch of Python. 3.x wasn’t really viable until 3.2, and thus 3.2 is the minimum version of the 3.x branch that Django supports. Even so, there is no MySQL solution under 3.x, so it’s all hypothetical at this point.
All that is to say, supporting Python 3.0 does nobody any good.
James Davey January 23, 2014 at 12:52 pm
Hi Aron,
I am very sorry, but as mentioned these are the versions we have available at the moment. This is not to say that we will never update these, but any update to any software in a production hosting environment must be approached with extreme caution. Updating this on a production fleet is not like updating it on a local workstation – there are literally thousands of things that can go catastrophically wrong with this. Our Systems team is constantly monitoring the versions of various software like this, and testing things thoroughly. As a result, updates can be a little behind the curve, but when they do happen they are seamless, and are in the best interests of the majority of our clients.
If you absolutely require an update schedule outside of our own, you may want to consider an Unmanaged VPS, where you have root access. You can read more about this at http://www.site5.com/vps/unmanaged/
Devon January 23, 2014 at 2:19 pm
Thanks for this explanation. It’s good to know.
I’m also interested in the original question, though. Is there any testing of 2.7 in progress right now?
It’s definitely understandable that the upgrade process is complicated, but 2.7 has been available since 2010, and as Aron mentioned, django and other libraries are dropping support for 2.6.
I’ve ended up using other services to host my web applications, which is disappointing. I would much rather keep my web apps with the rest of my site.
James Davey January 23, 2014 at 4:03 pm
Hi Devon,
There are no plans for this that I am aware of, no. As 3.0 is available, the next version installed to the server fleet would likely be 3.x. Don’t quite me on that, but it would be the next step, I would imagine :)
James Davey January 23, 2014 at 4:07 pm
Devon and Aron,
I should point out that there is nothing stopping you from installing the required version to your local home directory, and using it. The server-wide versions are set, but you can certainly install Python locally, without root access.