How to create a Django project using virtualenv

Initial setup

First, make sure you've installed a custom version of Python and created a virtual environment in your website's directory:

Next, install Django into your virtualenv:

Finally, enable Passenger on your domain.

Creating a Django project

To run the commands in this article, you must log into your server via SSH with your Shell user. View the following articles for more information:

The following instructions assume you've created a virtual environment titled 'my_project' using the links above.

  1. Run the following commands in order to create your Django project within this environment:
    [server]$ cd $HOME/
    [server]$ source $HOME/
    [server]$ python my_project/bin/ startproject <projectname>
    • Replace '' with your domain name.

    When you run the 'startproject' command, it creates a new folder in your site directory named whatever your Django project name is.

  2. In order for Passenger to pick up your project, create a file within your site's top level directory (/home/username/ Add the following:
    import sys, os
    INTERP = "/home/<username>/local/bin/python"
    #INTERP is present twice so that the new python interpreter knows the actual executable path 
    if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
    cwd = os.getcwd()
    sys.path.append(cwd + '/projectname')  #You must add your project here
    os.environ['DJANGO_SETTINGS_MODULE'] = "projectname.settings"
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

    Be sure to replace the following in the file:

    • The path to your local python version in line #2. Run which python to confirm:
      [server]$ which python
    • The two instances of 'projectname' with your actual project name in lines 8 and 14.
  3. Set up Django's static file settings in order to correctly serve images, CSS, and JavaScript as you will need this for the admin interface to work. For example:
    • Open the projects file found at Scroll to the bottom and you will find that the STATIC_URL is probably configured to /static/.
    • Add another line to set the location on the server of the actual static directory:
    STATIC_ROOT = os.path.dirname(BASE_DIR) + '/static/'
  4. In your /home/username/ directory, make sure to create this /static directory.
    [server]$ cd $HOME/
    [server]$ mkdir static
    • This will be the location where Django will put all of your static files – you shouldn't put stuff here manually as it gets overwritten. View the following link for further details:
  5. Run the collectstatic command to set up the static items for the admin interface:
    [server]$ cd $HOME/
    [server]$ python collectstatic
  6. Set up your database as required within the file. The section of the file originally looks like this:
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  7. Edit to make the following additions with your actual database credentials:
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mydatabase',
            'USER': 'mydatabaseuser',
            'PASSWORD': 'mypassword',
            'HOST': '',
            'PORT': '3306',
  8. The default is to use sqlite3, which may be suitable for the smallest of sites, but it's likely you'll want to set up a mysql database.
  9. Also in your file, update the ALLOWED_HOSTS field with your domain name. At first, it appears like this:
    Update it to include your domain name.
    ALLOWED_HOSTS = ['' , '']
  10. Make sure you're in your project directory:
    [server]$ cd ~/<projectname>
  11. Once configured, run migrate in your project directory:
    [server]$ python migrate
    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
    Applying contenttypes.0001_initial... OK
    Applying auth.0001_initial... OK
    Applying admin.0001_initial... OK
    Applying admin.0002_logentry_remove_auto_add... OK
    Applying contenttypes.0002_remove_content_type_name... OK
    Applying auth.0002_alter_permission_name_max_length... OK
    Applying auth.0003_alter_user_email_max_length... OK
    Applying auth.0004_alter_user_username_opts... OK
    Applying auth.0005_alter_user_last_login_null... OK
    Applying auth.0006_require_contenttypes_0002... OK
    Applying auth.0007_alter_validators_add_error_messages... OK
    Applying auth.0008_alter_user_username_max_length... OK
    Applying sessions.0001_initial... OK
  12. Create a superuser:
    [server]$ python createsuperuser
    Username (leave blank to use 'username'): my_django_user
    Email address:
    Password (again):
    Superuser created successfully.
  13. Navigate to your /home/username/ directory.
    [server]$ cd /home/username/
  14. Add a /tmp/restart.txt file:
    [server]$ mkdir tmp
    [server]$ cd tmp
    [server]$ touch restart.txt
  15. Whenever you make a change to your configuration, make sure to run the following in your site's directory to notify Passenger of the change:
    [server]$ touch tmp/restart.txt

Confirming the installation was successful

Visit your website.


You should now see the standard Django holding page and be able to access the admin console at

See also

Did this article answer your questions?

Article last updated .