Page 1 of 4 123 ... LastLast
Results 1 to 10 of 37

Thread: DJANGO (not-supported)

  1. #1
    Join Date
    May 2006
    Posts
    7

    Default DJANGO (not-supported)

    Here is a sample set of instructions for getting Django on Bluehost. Try this, it is not supported but it is at least a try and gives you a place to start working on getting Django running on Bluehost.

    You may get CPU usage errors on the site or this may not work but hopefully people can try it and post any changes that need to be made to it.

    *****************************************

    Installation

    To install Django on Bluehost ...

    * Download the latest Django source into your home directory

    http://code.djangoproject.com/svn/django/trunk/ django_src (SUBVERSION WILL NOT WORK FOR THIS BECAUSE BLUEHOST DOES NOT SUPPORT IT)

    * Edit your .bash_profile with vars PYTHONPATH and PATH so that Django is available from your shell.

    export PATH=$PATH:$HOME/django_src/django/bin
    export PYTHONPATH=$PYTHONPATH:$HOME/django_src:$HOME/django_projects

    * Reload .bash_profile to get the updated PATH and PYTHONPATH.

    source ~/.bash_profile

    * Create a Django projects directory and create your first Django project.

    mkdir django_projects
    cd django_projects
    django-admin.py startproject myproject

    * Change the permissions on your myproject.settings file so that ONLY YOUR USER can read them. (This is important for security! Otherwise all other users on the server will be able to access your database!)

    chmod 600 myproject/settings.py

    * Edit the myproject.settings file to add your database connection parameters.

    * Initialise the database with the Django tables.

    ./manage.py syncdb

    * Get http://www.saddi.com/software/py-lib/py-lib/fcgi.py and put it in your /home/username/django.mydomain.com directory.

    cd ~/django.mydomain.com
    wget http://www.saddi.com/software/py-lib/py-lib/fcgi.py
    chmod 755 fcgi.py

    * Create a django.fcgi file in /home/username/django.mydomain.com with the following text. Make sure to replace 'username' with your username!

    #!/usr/bin/env python
    import sys
    sys.path += ['/home/username/django_src']
    sys.path += ['/home/username/django_projects']
    from fcgi import WSGIServer
    from django.core.handlers.wsgi import WSGIHandler
    import os
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
    WSGIServer(WSGIHandler()).run()

    * Set the correct permissions on django.fcgi.

    chmod 755 django.fcgi

    * To view the admin site, go to http://django.mydomain.com/django.fcgi/admin/
    * To view the normal site, go to http://django.mydomain.com/django.fcgi/

    (At this point things may or may not work. Handle the rewrite rules below before panicing.)
    [edit]
    mod_rewrite

    To be able to login and actually use the django admin pages you will need to set up a mod_rewrite rule.

    If you want everything to be run by django.fcgi, you can use the following set of rewrite rules. Order is important!

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(media/.*)$ - [L]
    RewriteCond %{REQUEST_URI} !(django.fcgi)
    RewriteRule ^(.*)$ django.fcgi/$1 [L]

    All directories and files not managed by Django, should go before the last line. E.g., if you want to use Awstats web stats application (supplied by Bluehost), you should add following three lines before Django instruction (taken from Mod_rewrite):

    RewriteCond %{REQUEST_URI} ^/stats/(.*)$ [OR]
    RewriteCond %{REQUEST_URI} ^/failed_auth.html$
    RewriteRule ^.*$ - [L]

    If you want to add favicon.ico, which is actually located in appmedia/favicon.ico:

    RewriteRule ^(favicon\.ico)$ appmedia/favicon.ico [L]

    And so on.

    1st part of rewrite rule is a regex pattern to be matched. 2nd part is a replacement ("-" means don't rewrite the url). 3rd part is a command ("L" means "last" => "this is the last rule, quit rewriting after successful application of this rule").
    [edit]
    Media Files

    For the admin pages to be able to access the correct CSS and JavaScript files, you'll need to set up a media directory.

    ln -s $HOME/django_src/django/contrib/admin/media $HOME/django.mydomain.com/media

    Now surfing to http://django.myproject.com/media/ should give you a directory listing:

    css
    img
    js

    (It is possible to rename the effective name of the media directory for the admin app through a conf setting. This will be required if you decide that you want your own media in a subdirectory named 'media'.)
    [edit]
    Troubleshooting

    Here's what to try if it doesn't work.

    * Double-check your usernames, passwords, database names, and hostnames in the settings file.
    * Verify that FastCGI is working. Create a file named hello.fcgi with the following text.

    #!/usr/bin/python2.3
    from fcgi import WSGIServer
    def test_app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    yield 'Hello, world!\n'

    WSGIServer(test_app).run()

    Make sure to set the correct permissions.

    chmod 755 hello.fcgi

    Then try to load http://django.mydomain.com/hello.fcgi.

    * Once you've proven that FastCGI is working, check your sys.path settings in django.fcgi. Make sure you added the directory that contains "django", and not the django directory itself! In other words, put /home/username/django_src in your sys.path, not /home/username/django_src/django.

    * If you make changes to the code, such as working through the official tutorials, and they don't seem to work, make sure to kill any existing python processes and reload the page.

  2. #2
    Join Date
    Jul 2006
    Posts
    2

    Default Got it working!

    Thanks for the post, Flavio. Looks very similar to the post on Dreamhost:D

    Anywho, you might want to change the part that talks about making a folder named 'django.mydomain.com' because it seems that the subdomains are created thru the control panel and are placed in the $HOME/public_html directory.

    So, I got this working last night after looking at this post, one from site5, and the fastcgi docs from django's website. I ended up using a config like the one in this post, however, not like the django docs.

    I have the django svn tree in $HOME/django_src, so under that are the folders django, docs, examples, etc.

    I have $HOME/djangoapps as my folder for my django apps. In there I have my first project, called 'myapps'.

    I setup my .bash_profile like in the post, and sourced it, then created my project.

    Code:
    cd $HOME/djangoapps
    django-admin.py startproject myapps
    Then thru the mysql admin from the control panel, I created a new database, username_django, with a user, and entered that into my myapps/settings.py file. I also changed the perms like in the above post to 600 on the settings.py file.

    I already had a subdomain created thru the control panel, and that folder lives in $HOME/public_html/subdomain.

    I downloaded fcgi.py from the above site, and also flup, as per the django docs. Not sure if I really needed that, but I had downloaded it to try setting up django as per their fastcgi docs. Let's place them in our django_src folder, since we will be adding that to the sys.path for python anyways.

    So now, in my $HOME/public_html/subdomain folder I created two files, django.fcgi and .htaccess.

    Django.fcgi --
    Code:
    #!/usr/bin/env python
    import sys, os
    sys.path.insert(0,"/home/username/django_src")
    sys.path.insert(0,"/home/username/djangoapps")
    from fcgi import WSGIServer
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myapps.settings'
    from django.core.handlers.wsgi import WSGIHandler
    WSGIServer(WSGIHandler()).run()
    .htaccess --
    Code:
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(media/.*)$ - [L]
    RewriteCond %{REQUEST_URI} !(django.fcgi)
    RewriteRule ^(.*)$ django.fcgi/$1 [L]
    Before I run anything, I made sure my settings.py file in $HOME/djangoapps/myapps was setup with the correct database info, time zone, installed apps, template directory. I had to add 'django.contrib.admin' to the list of installed apps in order to see the admin interface. When doing that, you also need to edit the urls.py file and uncomment the admin line. This is documented on django's website.

    I also linked in the media directory to my $HOME/public_html/subdomain directory like in the above post.

    Once that was all setup, It worked! I was able to go to subdomain.domain.com/admin/ and login as the superuser I had created and view the nice admin interface.

    Thanks again!

  3. #3
    Join Date
    Dec 2006
    Posts
    2

    Unhappy How to use psycopg?

    The post is very interesting but psycopg is essential for database legacy and you, Flavio, don't speak about that.
    Help please.

  4. #4
    Join Date
    Dec 2006
    Posts
    2

    Default

    ok, making a subdomain it works.
    But if I want to make the same in the root (/ = '/home/user/public_html') this is the message
    ------------------
    Forbidden
    You don't have permission to access / on this server.

    Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
    -------------------------------------
    Is it possible to fix that?

  5. #5
    Join Date
    Jul 2006
    Posts
    4

    Default Don't forget settings.pyc

    When django runs, python creates a file called settings.pyc, which is the compiled version of settings.py. You need to chmod 600 this file as well or people sharing the server with you will be able to see your database password.

  6. #6
    Join Date
    May 2007
    Posts
    1

    Default psycopg

    to get psycopg working I did the following
    mkdir ~/lib
    mkdir ~/lib/python2.3
    mkdir ~/lib/python2.3/site-packages

    psycopg-1.1.21.tar.gz
    ./cofigure --postgres-includes=/root/include/pgsql --install-dir=/home/mydomain/

    make
    make install (this may not work so manually move psycopgmodule.so into site-packages

    I did the same think for egenix-mx-base-2.0.6.tar.gz
    and
    flup-r2311.tar.gz

    With flup I had to manually move flup to site-packages because I could not get sys.path to correctly find the flup egg

    I hope this helps

  7. #7
    Join Date
    May 2007
    Posts
    13

    Default my setup, and also with sqlite3

    Ok here's my copy of the (working) above instructions. Note this is about the same as everyone's instructions, but they worked for me!
    I will list my steps (the first complete steps, note they have no unnecessary steps).

    1) download the django source "trunk" to /home/username/django_src [cd ~; svn co http://code.djangoproject.com/svn/django/trunk/ django_src]

    1a) make a directory for your apps [mkdir ~/django_apps]

    add the following to .bashrc (or .bash_profile) to tell Python about its new install

    export PATH=$PATH:$HOME/django_src/django/bin
    export PYTHONPATH=$PYTHONPATH:$HOME/django_src:$HOME/django_apps

    get those in your path thus [source ~/.bashrc]

    2) setup a new subdomain in cPanel "django.whatever.com"
    3) create or svn in (copy in) your django app to somewhere -- for us we'll call it myproject (/home/username/django_apps/myproject)
    3a) to create it from scratch, [cd ~/django_apps; django-admin.py startproject myproject]
    3b) svn just copy it there :)


    4) get python's fcgi wrapper [wget http://svn.saddi.com/py-lib/trunk/fcgi.py] Put it somewhere that it can be found by Python -- for us it would be convenient to put it in /home/username/django_src [cp fcgi.py /home/username/django_src]

    5) in public_html/django create django.fcgi thus

    #!/usr/bin/python
    import sys, os
    sys.path.insert(0,"/home/username/django_src")
    sys.path.insert(0,"/home/username/django_apps")
    from fcgi import WSGIServer
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
    from django.core.handlers.wsgi import WSGIHandler
    WSGIServer(WSGIHandler()).run()
    </code>

    make it executable
    [chmod 755 django.fcgi]

    And it works at that point (I think)! http://subdomain.domain.com
    Now you could use this as is (I believe), but url's end up transforming themselves to look a litle ugly except for the root page. So let's add an .htaccess file to make it pretty.

    6) in ~/public_html/django create a .htaccess file thus

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !(django.fcgi)
    RewriteRule ^(.*)$ django.fcgi/$1 [L]

    and it should work! Note that the .htaccess just says "use django.fcgi" and django.fcgi calls django's runner.

    Note also that to get your "root" to act as a django app, copy .htaccess and django.fcgi to ~/public_html.

    Note that this doesn't setup a database or anything, also.
    Now for the kicker -- how to install it with sqlite3.

    Turns out that it's not installed on bluehost by default, and is tedious. Ah well.

    So some notes for sqlite3
    install tcl to a local drive (hereafter "install local" -- for my system I created and use a directory called "private_installations_only", for no good reason)
    download sqlite3, install local
    download pysqlite2, install local thusly:
    edit setup.py's following lines:

    include_dirs = ["/home/username/private_installations_only/include"]
    library_dirs = ["/home/username/private_installations_only/lib"]

    then run
    python setup.py build
    then run
    python setup.py install --prefix=/home/username/private_installations_only/

    now enable python to see those pysqlite2 shtuffs we installed..
    edit ~/.bashrc to include them, as well as tthe others

    export PYTHONPATH=$PYTHONPATH:$HOME/src_django:$HOME/django_apps:/home/username/private_installations_only/lib64/python2.3/site-packages
    # the last part is what I added, extra to what was existing

    now create the directory where you specified the database should exist in settings.py (edit settings.py to your liking), and run manage.py syncdb. viola.

    Now get it to work with fcgi:

    add in django.fcgi

    sys.path.insert(0,"/home/username/private_installations_only/lib64/python2.3/site-packages")

    Change settings.py to not use internationalzation, or it will give you an error of undefined symbol: PyUnicodeUCS4_AsUTF8String
    (i.e. set line UseI18N = False) as for some reason pysqlite compiles with the wrong unicode format. I didn't fight it.
    And cross your fingers and hope it works.

    As a note to check your fcgi setup you could go to http://subdomain.domain.com/django.fcgi and see if that works. Also note that as you make changes sometimes "old" fcgi processes may respond to your queries, so don't be surprised if sometimes it works and sometimes it doesn't. Good luck!
    Last edited by rogerdpack; 06-06-2007 at 11:56 AM.

  8. #8
    Join Date
    May 2007
    Posts
    13

    Default oops

    Looks like to 'really' fix it you need to delete references in the source code that say "UTF8" anywhere (i.e. nuke your ability to have unicode support), then compile. You may also need to download the Python 2.4.4 source and link it against that to compile.
    His remark below fixed it.
    Note that I have never had to use plug, ever :)
    Last edited by rogerdpack; 06-06-2007 at 11:57 AM.

  9. #9

    Default

    I started following this thread today to get my django project up and working on bluehost. I, too, started to seeing errors releated to Unicode and experienced problems importing psycopg2.

    Quote Originally Posted by rogerdpack View Post
    Looks like to 'really' fix it you need to delete references in the source code that say "UTF8" anywhere (i.e. nuke your ability to have unicode support), then compile. You may also need to download the Python 2.4.4 source and link it against that to compile.
    I'm still not entirely sure what's going on, but I was able to solve my problems without having to do this last bit. I noticed that if I used the following in my fcgi files:
    #!/usr/bin/env python
    then, the python pages were somehow using python 2.4.3 though I don't see where this version is installed on the system.

    Normal python scripts using this header invoke the python 2.3.4 interpreter which seems to be the system's default and is the version that was used when I built psycopg2 and my other python modules.

    So after running into errors again and again, I changed to the following header for my fcgi files:
    #!/usr/bin/python

    I verified that now the fcgi processes were using python 2.3.4. This adjustment fixed all the problems that I was experiencing.

  10. #10
    Join Date
    Jun 2007
    Posts
    3

    Default

    I get MySQLdb module errors when I run ./manage.py syncdb.

    It tells me there's no module named MySQLdb.

    Is there any way for me to install MySQLdb? What can I do to fix this?

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •