Skip to content

Instantly share code, notes, and snippets.

@vishnun
Created November 26, 2016 05:48
Show Gist options
  • Save vishnun/e5cc230d1a3aae30ba57ff7bf83aa04b to your computer and use it in GitHub Desktop.
Save vishnun/e5cc230d1a3aae30ba57ff7bf83aa04b to your computer and use it in GitHub Desktop.

Revisions

  1. @epicserve epicserve revised this gist May 27, 2013. 1 changed file with 1 addition and 282 deletions.
    283 changes: 1 addition & 282 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -2,285 +2,4 @@ Ubuntu Server Setup Guide for Django Websites
    ===============================================


    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_. This stack was chosen solely from the reading I've done and talking to other Django developers in order to get their recommendations. This stack seems to be one of the latest "standard" stacks for Django deployment. This guide also assumes that you're familiar with Ubuntu server administration and Django. I needed an example site for this guide so I chose to use my `Django Base Site <https://github.com/epicserve/django-base-site>`_ which is available on Github.

    I would also like to thank `Ben Claar <https://twitter.com/#!/benclaar>`_, `Adam Fast <https://twitter.com/#!/adamcanfly>`_, `Jeff Triplett <https://twitter.com/#!/webology>`_ and `Frank Wiles <https://twitter.com/#!/fwiles>`_ for their suggestions and input on this guide.

    .. _Ubuntu: http://www.ubuntu.com/business/server/overview
    .. _Nginx: http://nginx.org/en/
    .. _Gunicorn: http://gunicorn.org/
    .. _Postgres: http://www.postgresql.org/


    Step 1: Install Ubuntu Server
    -----------------------------

    The version of Ubuntu I'm using for this guide is `Ubuntu 11.10 64 bit Server <http://www.ubuntu.com/start-download?distro=server&bits=64&release=latest>`_. I've installed Ubuntu Server in a VirtualBox VM on my MacBook Pro which is currently running Mac OS X 10.7.2. During the installation of Ubuntu Server I answered the prompts with the following::

    Language: English
    Install Menu: Install Ubuntu Server
    Select a language: English
    Select your location: United States
    Configure the Keyboard: No
    Configure the keyboard: English (US)
    Configure the keyboard: English (US)
    Hostname: ubuntu-vm
    Configure the clock: Yes
    Partition disks: Guided - use entire disk and set up LVM
    Partition disks: SCSI3 (0,0,0) (sda) - 21.5 GB ATA VBOX HARDDISK
    Partition disks: Yes
    Partition disks: Continue
    Partition disks: Yes
    Set up users and passwords: Brent O'Connor
    Set up users and passwords: (Enter a username)
    Set up users and passwords: ********
    Set up users and passwords: ********
    Set up users and passwords: No
    Configure the package manager: <blank>
    Configure taskse1: No automatic updates
    Software selection: <Continue>
    Install the GRUB boot loader on a hard disk: Yes
    Installation complete: <Continue>


    Step 2: Setup Port Forwarding
    -----------------------------

    Under the settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus and add the following settings to setup port forwarding for web and ssh.

    +------------+------------+-----------+-----------+----------+------------+
    | Name | Protocol | Host IP | Host Port | Guest IP | Guest Port |
    +============+============+===========+===========+==========+============+
    | SSH | TCP | | 2222 | | 22 |
    +------------+------------+-----------+-----------+----------+------------+
    | Web | TCP | | 8080 | | 80 |
    +------------+------------+-----------+-----------+----------+------------+

    Step 3: Install Software
    ------------------------

    Before you begin it might be a good idea to update your system clock::

    $ sudo ntpdate time.nist.gov

    Download lists of new/upgradable packages::

    $ sudo aptitude update

    OpenSSH
    ~~~~~~~

    Since I like to connect to my servers using SSH the first thing I install is openssh-server::

    $ sudo aptitude install openssh-server

    Since you setup port forwarding in step 2, you should now be able to open up your Terminal and connect to your Ubuntu Server using the following::

    $ ssh localhost -p 2222

    Python Header Files
    ~~~~~~~~~~~~~~~~~~~

    The Python header files are needed in order to compile binding libraries like ``psycopg2``. ::

    $ sudo aptitude install python2.7-dev

    PostgreSQL
    ~~~~~~~~~~

    ::

    $ sudo aptitude install postgresql postgresql-server-dev-9.1

    Make your Ubuntu user a PostgreSQL superuser::

    $ sudo su - postgres
    $ createuser --superuser <your username>
    $ exit

    Restart PostgreSQL::

    $ sudo /etc/init.d/postgresql restart

    Nginx
    ~~~~~

    ::

    $ sudo aptitude install nginx

    Git
    ~~~

    ::

    $ sudo aptitude install git


    Step 4: Setup a Generic Deploy User
    -----------------------------------

    The reason we are setting up a generic deploy user is so that if you have multiple developers who are allowed to do deployments you can easily add the developer's SSH public key to the deploy user's ``/home/deploy/.ssh/authorized_keys`` file in order to allow them to do deployments.

    ::

    $ sudo useradd -d /home/deploy -m -s /bin/bash deploy


    Step 5: Install an Example Site
    -------------------------------

    Setup a virtualenv::

    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip virtualenv
    $ cd /usr/local/
    $ sudo mkdir virtualenvs
    $ sudo chown deploy:deploy virtualenvs
    $ sudo su deploy
    $ cd virtualenvs
    $ virtualenv --no-site-packages example-site
    $ exit

    .. note::

    I personally use and setup virtualenvwrapper on all my servers and local development machines so that I can use ``workon <virtualenv>`` to easily activate a virtualenv. This is why I put all my virtualenvs in ``/usr/local/virtualenvs``.


    Make a location for the example site::

    $ cd /srv/
    $ sudo mkdir sites
    $ sudo chown deploy:deploy sites
    $ sudo su deploy
    $ cd sites
    $ git clone git://github.com/epicserve/django-base-site.git example-site
    $ cd example-site/
    $ git checkout -b example_site 5b05e2dbe5
    $ echo `pwd` > /usr/local/virtualenvs/example-site/lib/python2.7/site-packages/django_project_root.pth
    $ mkdir -p static/cache
    $ exit
    $ sudo chown www-data:www-data /srv/sites/example-site/static/cache
    $ sudo su deploy

    Create the file ``/srv/sites/example-site/config/settings/local.py`` and add the following. Make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user::

    from base import *

    LOCAL_SETTINGS_LOADED = True

    DEBUG = True

    INTERNAL_IPS = ('127.0.0.1', )

    ADMINS = (
    ('Your Name', '[email protected]'),
    )

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'example_site',
    'USER': 'example_site',
    'PASSWORD': '<enter a new secure password>',
    'HOST': 'localhost',
    }
    }

    Install the sites required python packages::

    $ source /usr/local/virtualenvs/example-site/bin/activate
    $ cd /srv/sites/example-site/
    $ pip install -r config/requirements/production.txt

    Create a PostgreSQL user and database for your example-site::

    # exit out of the deploy user account
    $ exit
    $ createuser example_site -P
    $ Enter password for new role: [enter the same password you used in the local.py file from above]
    $ Enter it again: [enter the password again]
    $ Shall the new role be a superuser? (y/n) n
    $ Shall the new role be allowed to create databases? (y/n) y
    $ Shall the new role be allowed to create more new roles? (y/n) n
    $ createdb example_site -O example_site

    Step 6: Daemonize Gunicorn using Ubuntu's Upstart
    -------------------------------------------------

    Create your Upstart configuration file::

    $ sudo vi /etc/init/gunicorn_example-site.conf

    Add the following and save the file::

    description "upstart configuration for gunicorn example-site"

    start on net-device-up
    stop on shutdown

    respawn

    exec /usr/local/virtualenvs/example-site/bin/gunicorn_django -u www-data -c /srv/sites/example-site/config/gunicorn/example-site.py /srv/sites/example-site/config/settings/__init__.py

    Start the gunicorn site::

    $ sudo start gunicorn_example-site


    Step 7: Setup Nginx to proxy to your new example site
    -----------------------------------------------------

    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::

    server {

    listen 80;
    server_name localhost;
    access_log /var/log/nginx/example-site.access.log;
    error_log /var/log/nginx/example-site.error.log;

    location = /biconcave {
    return 404;
    }

    location /static/ {
    root /srv/sites/example-site/;
    }

    location /media/ {
    root /srv/sites/example-site/;
    }


    location / {
    proxy_pass http://127.0.0.1:8000/;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
    }

    }

    Enable the new site::

    $ cd /etc/nginx/sites-enabled
    $ sudo rm default
    $ sudo ln -s ../sites-available/example-site.conf

    Start nginx::

    $ sudo /etc/init.d/nginx start


    Step 8: Test the new example site
    ---------------------------------

    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site::

    wget -qO- 127.0.0.1:80

    Since you setup port forwarding in step 2 for web, you should also be able to open up your browser on your local host machine and pull up the website using the URL, http://127.0.0.1:8080.

    This guide is has moved to https://epicserve-docs.readthedocs.org/en/latest/django/ubuntu-server-django-guide.html.
  2. @epicserve epicserve revised this gist Apr 18, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,10 @@ Under the settings for your VM in VirtualBox click on the "Network" tab and then
    Step 3: Install Software
    ------------------------

    Before you begin it might be a good idea to update your system clock::

    $ sudo ntpdate time.nist.gov

    Download lists of new/upgradable packages::

    $ sudo aptitude update
  3. @epicserve epicserve revised this gist Apr 18, 2013. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,10 @@ Under the settings for your VM in VirtualBox click on the "Network" tab and then
    Step 3: Install Software
    ------------------------

    Download lists of new/upgradable packages::

    $ sudo aptitude update

    OpenSSH
    ~~~~~~~

  4. @epicserve epicserve revised this gist Apr 18, 2013. 1 changed file with 4 additions and 12 deletions.
    16 changes: 4 additions & 12 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -125,8 +125,7 @@ Step 5: Install an Example Site
    Setup a virtualenv::

    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip
    $ sudo pip install virtualenv
    $ sudo easy_install pip virtualenv
    $ cd /usr/local/
    $ sudo mkdir virtualenvs
    $ sudo chown deploy:deploy virtualenvs
    @@ -149,6 +148,7 @@ Make a location for the example site::
    $ cd sites
    $ git clone git://github.com/epicserve/django-base-site.git example-site
    $ cd example-site/
    $ git checkout -b example_site 5b05e2dbe5
    $ echo `pwd` > /usr/local/virtualenvs/example-site/lib/python2.7/site-packages/django_project_root.pth
    $ mkdir -p static/cache
    $ exit
    @@ -183,15 +183,7 @@ Install the sites required python packages::

    $ source /usr/local/virtualenvs/example-site/bin/activate
    $ cd /srv/sites/example-site/
    $ pip install -r config/requirements.txt

    Install Gunicorn::

    $ pip install gunicorn

    Install psycopg2::

    $ pip install psycopg2
    $ pip install -r config/requirements/production.txt

    Create a PostgreSQL user and database for your example-site::

    @@ -254,7 +246,7 @@ Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and a


    location / {
    proxy_pass http://127.0.0.1:8001/;
    proxy_pass http://127.0.0.1:8000/;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  5. @epicserve epicserve revised this gist Nov 3, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ The version of Ubuntu I'm using for this guide is `Ubuntu 11.10 64 bit Server <h
    Step 2: Setup Port Forwarding
    -----------------------------

    Under the settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus add the following settings to setup port forwarding for web and ssh.
    Under the settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus and add the following settings to setup port forwarding for web and ssh.

    +------------+------------+-----------+-----------+----------+------------+
    | Name | Protocol | Host IP | Host Port | Guest IP | Guest Port |
    @@ -112,7 +112,7 @@ Git
    Step 4: Setup a Generic Deploy User
    -----------------------------------

    The reason we are setting up a generic deploy user is so that if you have multiple developers who are allowed to do deployments you can easily add the developers SSH public key to the deploy users ``/home/deploy/.ssh/authorized_keys`` file in order to allow them to do deployments.
    The reason we are setting up a generic deploy user is so that if you have multiple developers who are allowed to do deployments you can easily add the developer's SSH public key to the deploy user's ``/home/deploy/.ssh/authorized_keys`` file in order to allow them to do deployments.

    ::

    @@ -155,7 +155,7 @@ Make a location for the example site::
    $ sudo chown www-data:www-data /srv/sites/example-site/static/cache
    $ sudo su deploy

    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user::
    Create the file ``/srv/sites/example-site/config/settings/local.py`` and add the following. Make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user::

    from base import *

  6. @epicserve epicserve revised this gist Nov 3, 2011. 1 changed file with 24 additions and 62 deletions.
    86 changes: 24 additions & 62 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -62,59 +62,49 @@ Step 3: Install Software
    OpenSSH
    ~~~~~~~

    Since I like to connect to my servers using SSH the first thing I install is openssh-server:

    .. code-block:: bash
    Since I like to connect to my servers using SSH the first thing I install is openssh-server::

    $ sudo aptitude install openssh-server

    Since you setup port forwarding in step 2, you should now be able to open up your Terminal and connect to your Ubuntu Server using the following:

    .. code-block:: bash
    Since you setup port forwarding in step 2, you should now be able to open up your Terminal and connect to your Ubuntu Server using the following::

    $ ssh localhost -p 2222

    Python Header Files
    ~~~~~~~~~~~~~~~~~~~

    The Python header files are needed in order to compile binding libraries like ``psycopg2``. :

    .. code-block:: bash
    The Python header files are needed in order to compile binding libraries like ``psycopg2``. ::

    $ sudo aptitude install python2.7-dev

    PostgreSQL
    ~~~~~~~~~~

    .. code-block:: bash
    ::

    $ sudo aptitude install postgresql postgresql-server-dev-9.1

    Make your Ubuntu user a PostgreSQL superuser:

    .. code-block:: bash
    Make your Ubuntu user a PostgreSQL superuser::

    $ sudo su - postgres
    $ createuser --superuser <your username>
    $ exit

    Restart PostgreSQL:

    .. code-block:: bash
    Restart PostgreSQL::

    $ sudo /etc/init.d/postgresql restart

    Nginx
    ~~~~~

    .. code-block:: bash
    ::

    $ sudo aptitude install nginx

    Git
    ~~~

    .. code-block:: bash
    ::

    $ sudo aptitude install git

    @@ -124,17 +114,15 @@ Step 4: Setup a Generic Deploy User

    The reason we are setting up a generic deploy user is so that if you have multiple developers who are allowed to do deployments you can easily add the developers SSH public key to the deploy users ``/home/deploy/.ssh/authorized_keys`` file in order to allow them to do deployments.

    .. code-block:: bash
    ::

    $ sudo useradd -d /home/deploy -m -s /bin/bash deploy


    Step 5: Install an Example Site
    -------------------------------

    Setup a virtualenv:

    .. code-block:: bash
    Setup a virtualenv::

    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip
    @@ -152,9 +140,7 @@ Setup a virtualenv:
    I personally use and setup virtualenvwrapper on all my servers and local development machines so that I can use ``workon <virtualenv>`` to easily activate a virtualenv. This is why I put all my virtualenvs in ``/usr/local/virtualenvs``.


    Make a location for the example site:

    .. code-block:: bash
    Make a location for the example site::

    $ cd /srv/
    $ sudo mkdir sites
    @@ -169,9 +155,7 @@ Make a location for the example site:
    $ sudo chown www-data:www-data /srv/sites/example-site/static/cache
    $ sudo su deploy

    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user:

    .. code-block:: python
    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user::

    from base import *

    @@ -195,29 +179,21 @@ Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add
    }
    }

    Install the sites required python packages:

    .. code-block:: bash
    Install the sites required python packages::

    $ source /usr/local/virtualenvs/example-site/bin/activate
    $ cd /srv/sites/example-site/
    $ pip install -r config/requirements.txt

    Install Gunicorn:

    .. code-block:: bash
    Install Gunicorn::

    $ pip install gunicorn

    Install psycopg2:

    .. code-block:: bash
    Install psycopg2::

    $ pip install psycopg2

    Create a PostgreSQL user and database for your example-site:

    .. code-block:: bash
    Create a PostgreSQL user and database for your example-site::

    # exit out of the deploy user account
    $ exit
    @@ -232,15 +208,11 @@ Create a PostgreSQL user and database for your example-site:
    Step 6: Daemonize Gunicorn using Ubuntu's Upstart
    -------------------------------------------------

    Create your Upstart configuration file:
    .. code-block:: bash
    Create your Upstart configuration file::

    $ sudo vi /etc/init/gunicorn_example-site.conf

    Add the following and save the file:
    .. code-block:: bash
    Add the following and save the file::

    description "upstart configuration for gunicorn example-site"

    @@ -251,19 +223,15 @@ Add the following and save the file:

    exec /usr/local/virtualenvs/example-site/bin/gunicorn_django -u www-data -c /srv/sites/example-site/config/gunicorn/example-site.py /srv/sites/example-site/config/settings/__init__.py

    Start the gunicorn site:
    .. code-block:: bash
    Start the gunicorn site::

    $ sudo start gunicorn_example-site


    Step 7: Setup Nginx to proxy to your new example site
    -----------------------------------------------------

    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file:
    .. code-block:: nginx
    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::

    server {

    @@ -296,29 +264,23 @@ Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and a

    }

    Enable the new site:
    .. code-block:: bash
    Enable the new site::

    $ cd /etc/nginx/sites-enabled
    $ sudo rm default
    $ sudo ln -s ../sites-available/example-site.conf

    Start nginx:
    .. code-block:: bash
    Start nginx::

    $ sudo /etc/init.d/nginx start


    Step 8: Test the new example site
    ---------------------------------

    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site:
    .. code-block:: bash
    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site::

    $ wget -qO- 127.0.0.1:80
    wget -qO- 127.0.0.1:80

    Since you setup port forwarding in step 2 for web, you should also be able to open up your browser on your local host machine and pull up the website using the URL, http://127.0.0.1:8080.

  7. @epicserve epicserve revised this gist Nov 3, 2011. 1 changed file with 62 additions and 24 deletions.
    86 changes: 62 additions & 24 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -62,49 +62,59 @@ Step 3: Install Software
    OpenSSH
    ~~~~~~~

    Since I like to connect to my servers using SSH the first thing I install is openssh-server::
    Since I like to connect to my servers using SSH the first thing I install is openssh-server:

    .. code-block:: bash
    $ sudo aptitude install openssh-server
    Since you setup port forwarding in step 2, you should now be able to open up your Terminal and connect to your Ubuntu Server using the following::
    Since you setup port forwarding in step 2, you should now be able to open up your Terminal and connect to your Ubuntu Server using the following:

    .. code-block:: bash
    $ ssh localhost -p 2222
    Python Header Files
    ~~~~~~~~~~~~~~~~~~~

    The Python header files are needed in order to compile binding libraries like ``psycopg2``. ::
    The Python header files are needed in order to compile binding libraries like ``psycopg2``. :

    .. code-block:: bash
    $ sudo aptitude install python2.7-dev
    PostgreSQL
    ~~~~~~~~~~

    ::
    .. code-block:: bash
    $ sudo aptitude install postgresql postgresql-server-dev-9.1
    Make your Ubuntu user a PostgreSQL superuser::
    Make your Ubuntu user a PostgreSQL superuser:

    .. code-block:: bash
    $ sudo su - postgres
    $ createuser --superuser <your username>
    $ exit
    Restart PostgreSQL::
    Restart PostgreSQL:

    .. code-block:: bash
    $ sudo /etc/init.d/postgresql restart
    Nginx
    ~~~~~

    ::
    .. code-block:: bash
    $ sudo aptitude install nginx
    Git
    ~~~

    ::
    .. code-block:: bash
    $ sudo aptitude install git
    @@ -114,15 +124,17 @@ Step 4: Setup a Generic Deploy User

    The reason we are setting up a generic deploy user is so that if you have multiple developers who are allowed to do deployments you can easily add the developers SSH public key to the deploy users ``/home/deploy/.ssh/authorized_keys`` file in order to allow them to do deployments.

    ::
    .. code-block:: bash
    $ sudo useradd -d /home/deploy -m -s /bin/bash deploy
    Step 5: Install an Example Site
    -------------------------------

    Setup a virtualenv::
    Setup a virtualenv:

    .. code-block:: bash
    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip
    @@ -140,7 +152,9 @@ Setup a virtualenv::
    I personally use and setup virtualenvwrapper on all my servers and local development machines so that I can use ``workon <virtualenv>`` to easily activate a virtualenv. This is why I put all my virtualenvs in ``/usr/local/virtualenvs``.


    Make a location for the example site::
    Make a location for the example site:

    .. code-block:: bash
    $ cd /srv/
    $ sudo mkdir sites
    @@ -155,7 +169,9 @@ Make a location for the example site::
    $ sudo chown www-data:www-data /srv/sites/example-site/static/cache
    $ sudo su deploy
    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user::
    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user:

    .. code-block:: python
    from base import *
    @@ -179,21 +195,29 @@ Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add
    }
    }
    Install the sites required python packages::
    Install the sites required python packages:

    .. code-block:: bash
    $ source /usr/local/virtualenvs/example-site/bin/activate
    $ cd /srv/sites/example-site/
    $ pip install -r config/requirements.txt
    Install Gunicorn::
    Install Gunicorn:

    .. code-block:: bash
    $ pip install gunicorn
    Install psycopg2::
    Install psycopg2:

    .. code-block:: bash
    $ pip install psycopg2
    Create a PostgreSQL user and database for your example-site::
    Create a PostgreSQL user and database for your example-site:

    .. code-block:: bash
    # exit out of the deploy user account
    $ exit
    @@ -208,11 +232,15 @@ Create a PostgreSQL user and database for your example-site::
    Step 6: Daemonize Gunicorn using Ubuntu's Upstart
    -------------------------------------------------
    Create your Upstart configuration file::
    Create your Upstart configuration file:
    .. code-block:: bash
    $ sudo vi /etc/init/gunicorn_example-site.conf
    Add the following and save the file::
    Add the following and save the file:
    .. code-block:: bash
    description "upstart configuration for gunicorn example-site"
    @@ -223,15 +251,19 @@ Add the following and save the file::
    exec /usr/local/virtualenvs/example-site/bin/gunicorn_django -u www-data -c /srv/sites/example-site/config/gunicorn/example-site.py /srv/sites/example-site/config/settings/__init__.py
    Start the gunicorn site::
    Start the gunicorn site:
    .. code-block:: bash
    $ sudo start gunicorn_example-site
    Step 7: Setup Nginx to proxy to your new example site
    -----------------------------------------------------
    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::
    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file:
    .. code-block:: nginx
    server {
    @@ -264,23 +296,29 @@ Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and a
    }
    Enable the new site::
    Enable the new site:
    .. code-block:: bash
    $ cd /etc/nginx/sites-enabled
    $ sudo rm default
    $ sudo ln -s ../sites-available/example-site.conf
    Start nginx::
    Start nginx:
    .. code-block:: bash
    $ sudo /etc/init.d/nginx start
    Step 8: Test the new example site
    ---------------------------------
    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site::
    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site:
    .. code-block:: bash
    wget -qO- 127.0.0.1:80
    $ wget -qO- 127.0.0.1:80
    Since you setup port forwarding in step 2 for web, you should also be able to open up your browser on your local host machine and pull up the website using the URL, http://127.0.0.1:8080.
  8. @epicserve epicserve revised this gist Nov 2, 2011. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@ Ubuntu Server Setup Guide for Django Websites
    ===============================================


    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_. This stack was chosen solely because from the reading I've done these seems to be one of the latest "standard" stacks for Django deployment. This guide also assumes that you're familiar with Ubuntu server administration and Django. Also of note, for the example site in this guide I chose to use my `Django Base Site <https://github.com/epicserve/django-base-site>`_ which is on Github.
    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_. This stack was chosen solely from the reading I've done and talking to other Django developers in order to get their recommendations. This stack seems to be one of the latest "standard" stacks for Django deployment. This guide also assumes that you're familiar with Ubuntu server administration and Django. I needed an example site for this guide so I chose to use my `Django Base Site <https://github.com/epicserve/django-base-site>`_ which is available on Github.

    I would also like to thank `Ben Claar <https://twitter.com/#!/benclaar>`_, `Adam Fast <https://twitter.com/#!/adamcanfly>`_, `Jeff Triplett <https://twitter.com/#!/webology>`_ and `Frank Wiles <https://twitter.com/#!/fwiles>`_ for their suggestions and input on this guide.

    .. _Ubuntu: http://www.ubuntu.com/business/server/overview
    .. _Nginx: http://nginx.org/en/
    @@ -133,6 +135,11 @@ Setup a virtualenv::
    $ virtualenv --no-site-packages example-site
    $ exit

    .. note::

    I personally use and setup virtualenvwrapper on all my servers and local development machines so that I can use ``workon <virtualenv>`` to easily activate a virtualenv. This is why I put all my virtualenvs in ``/usr/local/virtualenvs``.


    Make a location for the example site::

    $ cd /srv/
  9. @epicserve epicserve revised this gist Nov 2, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -164,9 +164,9 @@ Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'example_site', # Or path to database file if using sqlite3.
    'USER': 'example_site', # Not used with sqlite3.
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'example_site',
    'USER': 'example_site',
    'PASSWORD': '<enter a new secure password>',
    'HOST': 'localhost',
    }
  10. @epicserve epicserve revised this gist Nov 2, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -148,7 +148,7 @@ Make a location for the example site::
    $ sudo chown www-data:www-data /srv/sites/example-site/static/cache
    $ sudo su deploy

    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save::
    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save the file. I usually use a `random string generator <http://clsc.net/tools/random-string-generator.php>`_ to generate a new password for each new Postgresql database and user::

    from base import *

    @@ -275,5 +275,5 @@ While still connected to your Ubuntu server via SSH run the following, which sho

    wget -qO- 127.0.0.1:80

    Since you setup port forwarding in step 2 for web, you should also be able to open up your browser on your local host machine and pull up the website using the URL, http://127.0.0.1:8080
    Since you setup port forwarding in step 2 for web, you should also be able to open up your browser on your local host machine and pull up the website using the URL, http://127.0.0.1:8080.

  11. @epicserve epicserve revised this gist Nov 2, 2011. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -117,7 +117,7 @@ The reason we are setting up a generic deploy user is so that if you have multip
    $ sudo useradd -d /home/deploy -m -s /bin/bash deploy


    Step 4: Install an Example Site
    Step 5: Install an Example Site
    -------------------------------

    Setup a virtualenv::
    @@ -198,7 +198,7 @@ Create a PostgreSQL user and database for your example-site::
    $ Shall the new role be allowed to create more new roles? (y/n) n
    $ createdb example_site -O example_site

    Step 5: Daemonize Gunicorn using Ubuntu's Upstart
    Step 6: Daemonize Gunicorn using Ubuntu's Upstart
    -------------------------------------------------

    Create your Upstart configuration file::
    @@ -221,7 +221,7 @@ Start the gunicorn site::
    $ sudo start gunicorn_example-site


    Step 6: Setup Nginx to proxy to your new example site
    Step 7: Setup Nginx to proxy to your new example site
    -----------------------------------------------------

    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::
    @@ -268,7 +268,7 @@ Start nginx::
    $ sudo /etc/init.d/nginx start


    Step 7: Test the new example site
    Step 8: Test the new example site
    ---------------------------------

    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site::
  12. @epicserve epicserve revised this gist Nov 2, 2011. 1 changed file with 32 additions and 18 deletions.
    50 changes: 32 additions & 18 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -82,13 +82,6 @@ PostgreSQL

    $ sudo aptitude install postgresql postgresql-server-dev-9.1

    Configure postgres so Django can connect without a password. Edit the file ``/etc/postgresql/9.1/main/pg_hba.conf`` so the following lines in the file are identical to the following. You should be able to do this by changing all the uncommented rows in the fith column to "trust". ::

    local all postgres trust
    local all all trust
    host all all 127.0.0.1/32 trust
    host all all ::1/128 trust

    Make your Ubuntu user a PostgreSQL superuser::

    $ sudo su - postgres
    @@ -114,34 +107,48 @@ Git
    $ sudo aptitude install git


    Step 4: Setup a Generic Deploy User
    -----------------------------------

    The reason we are setting up a generic deploy user is so that if you have multiple developers who are allowed to do deployments you can easily add the developers SSH public key to the deploy users ``/home/deploy/.ssh/authorized_keys`` file in order to allow them to do deployments.

    ::

    $ sudo useradd -d /home/deploy -m -s /bin/bash deploy


    Step 4: Install an Example Site
    -------------------------------

    Setup a virtualenv::

    $ cd /usr/local/
    $ sudo mkdir virtualenvs
    $ sudo chown <your username> virtualenvs
    $ cd virtualenvs
    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip
    $ sudo pip install virtualenv
    $ cd /usr/local/
    $ sudo mkdir virtualenvs
    $ sudo chown deploy:deploy virtualenvs
    $ sudo su deploy
    $ cd virtualenvs
    $ virtualenv --no-site-packages example-site
    $ source example-site/bin/activate
    $ exit

    Make a location for the example site::

    $ cd /srv/
    $ sudo mkdir sites
    $ sudo chown <your username> sites
    $ sudo chown deploy:deploy sites
    $ sudo su deploy
    $ cd sites
    $ git clone git://github.com/epicserve/django-base-site.git example-site
    $ cd example-site/
    $ mkdir -p static/cache
    $ sudo chown www-data:www-data static/cache
    $ echo `pwd` > /usr/local/virtualenvs/example-site/lib/python2.7/site-packages/django_project_root.pth
    $ mkdir -p static/cache
    $ exit
    $ sudo chown www-data:www-data /srv/sites/example-site/static/cache
    $ sudo su deploy

    Create the file ``/srv/sites/example-site/config/settings/local.py`` and add the following and save::
    Create the file ``vi /srv/sites/example-site/config/settings/local.py`` and add the following, make sure to change the password and then save::

    from base import *

    @@ -160,12 +167,15 @@ Create the file ``/srv/sites/example-site/config/settings/local.py`` and add the
    'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'example_site', # Or path to database file if using sqlite3.
    'USER': 'example_site', # Not used with sqlite3.
    'PASSWORD': '<enter a new secure password>',
    'HOST': 'localhost',
    }
    }

    Install the sites required python packages::

    $ source /usr/local/virtualenvs/example-site/bin/activate
    $ cd /srv/sites/example-site/
    $ pip install -r config/requirements.txt

    Install Gunicorn::
    @@ -178,7 +188,11 @@ Install psycopg2::

    Create a PostgreSQL user and database for your example-site::

    $ createuser example_site
    # exit out of the deploy user account
    $ exit
    $ createuser example_site -P
    $ Enter password for new role: [enter the same password you used in the local.py file from above]
    $ Enter it again: [enter the password again]
    $ Shall the new role be a superuser? (y/n) n
    $ Shall the new role be allowed to create databases? (y/n) y
    $ Shall the new role be allowed to create more new roles? (y/n) n
    @@ -210,7 +224,7 @@ Start the gunicorn site::
    Step 6: Setup Nginx to proxy to your new example site
    -----------------------------------------------------

    Create a new file ``/etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::
    Create a new file ``sudo vi /etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::

    server {

  13. @epicserve epicserve revised this gist Nov 2, 2011. 1 changed file with 28 additions and 12 deletions.
    40 changes: 28 additions & 12 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ Ubuntu Server Setup Guide for Django Websites
    ===============================================


    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_.
    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_. This stack was chosen solely because from the reading I've done these seems to be one of the latest "standard" stacks for Django deployment. This guide also assumes that you're familiar with Ubuntu server administration and Django. Also of note, for the example site in this guide I chose to use my `Django Base Site <https://github.com/epicserve/django-base-site>`_ which is on Github.

    .. _Ubuntu: http://www.ubuntu.com/business/server/overview
    .. _Nginx: http://nginx.org/en/
    @@ -30,7 +30,7 @@ The version of Ubuntu I'm using for this guide is `Ubuntu 11.10 64 bit Server <h
    Partition disks: Continue
    Partition disks: Yes
    Set up users and passwords: Brent O'Connor
    Set up users and passwords: oconnor
    Set up users and passwords: (Enter a username)
    Set up users and passwords: ********
    Set up users and passwords: ********
    Set up users and passwords: No
    @@ -41,7 +41,20 @@ The version of Ubuntu I'm using for this guide is `Ubuntu 11.10 64 bit Server <h
    Installation complete: <Continue>


    Step 2: Install software
    Step 2: Setup Port Forwarding
    -----------------------------

    Under the settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus add the following settings to setup port forwarding for web and ssh.

    +------------+------------+-----------+-----------+----------+------------+
    | Name | Protocol | Host IP | Host Port | Guest IP | Guest Port |
    +============+============+===========+===========+==========+============+
    | SSH | TCP | | 2222 | | 22 |
    +------------+------------+-----------+-----------+----------+------------+
    | Web | TCP | | 8080 | | 80 |
    +------------+------------+-----------+-----------+----------+------------+

    Step 3: Install Software
    ------------------------

    OpenSSH
    @@ -51,14 +64,14 @@ Since I like to connect to my servers using SSH the first thing I install is ope

    $ sudo aptitude install openssh-server

    Once that's installed if your using VirtualBox you can setup port forwarding so you can connect to your server from your local Terminal. Under settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus and change the port for host to 2222 and the port for guest to 22. You should now be able to open up your Terminal and connect to your Ubuntu Server using the following... ::
    Since you setup port forwarding in step 2, you should now be able to open up your Terminal and connect to your Ubuntu Server using the following::

    $ ssh localhost -p 2222

    Python Header Files
    ~~~~~~~~~~~~~~~~~~~

    This is needed in order to install python postgres binding libraries like ``psycopg2``. ::
    The Python header files are needed in order to compile binding libraries like ``psycopg2``. ::

    $ sudo aptitude install python2.7-dev

    @@ -79,7 +92,7 @@ Configure postgres so Django can connect without a password. Edit the file ``/et
    Make your Ubuntu user a PostgreSQL superuser::

    $ sudo su - postgres
    $ createuser --superuser oconnor
    $ createuser --superuser <your username>
    $ exit

    Restart PostgreSQL::
    @@ -101,14 +114,14 @@ Git
    $ sudo aptitude install git


    Step 3: Install an example site
    Step 4: Install an Example Site
    -------------------------------

    Setup a virtualenv::

    $ cd /usr/local/
    $ sudo mkdir virtualenvs
    $ sudo chown oconnor virtualenvs
    $ sudo chown <your username> virtualenvs
    $ cd virtualenvs
    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip
    @@ -120,7 +133,7 @@ Make a location for the example site::

    $ cd /srv/
    $ sudo mkdir sites
    $ sudo chown oconnor sites
    $ sudo chown <your username> sites
    $ cd sites
    $ git clone git://github.com/epicserve/django-base-site.git example-site
    $ cd example-site/
    @@ -171,7 +184,7 @@ Create a PostgreSQL user and database for your example-site::
    $ Shall the new role be allowed to create more new roles? (y/n) n
    $ createdb example_site -O example_site

    Step 3: Daemonize Gunicorn using Ubuntu's Upstart
    Step 5: Daemonize Gunicorn using Ubuntu's Upstart
    -------------------------------------------------

    Create your Upstart configuration file::
    @@ -194,7 +207,7 @@ Start the gunicorn site::
    $ sudo start gunicorn_example-site


    Step 4: Setup Nginx to proxy to your new example site
    Step 6: Setup Nginx to proxy to your new example site
    -----------------------------------------------------

    Create a new file ``/etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::
    @@ -241,9 +254,12 @@ Start nginx::
    $ sudo /etc/init.d/nginx start


    Step 5: Test the new example site
    Step 7: Test the new example site
    ---------------------------------

    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site::

    wget -qO- 127.0.0.1:80

    Since you setup port forwarding in step 2 for web, you should also be able to open up your browser on your local host machine and pull up the website using the URL, http://127.0.0.1:8080

  14. @epicserve epicserve revised this gist Nov 1, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    Ubuntu Server Setup Guide for Django Websites
    =============================================
    ===============================================


    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_.
    @@ -69,7 +69,7 @@ PostgreSQL

    $ sudo aptitude install postgresql postgresql-server-dev-9.1

    Configure postgres so Django can connect with out a password. Edit the file ``/etc/postgresql/9.1/main/pg_hba.conf`` so the following lines in the file are identical to the following. You should be able to do this by changing all the uncommented rows in the fith column to "trust". ::
    Configure postgres so Django can connect without a password. Edit the file ``/etc/postgresql/9.1/main/pg_hba.conf`` so the following lines in the file are identical to the following. You should be able to do this by changing all the uncommented rows in the fith column to "trust". ::

    local all postgres trust
    local all all trust
  15. @epicserve epicserve revised this gist Nov 1, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    Ubuntu Server Setup Guide for a Django Websites
    ===============================================
    Ubuntu Server Setup Guide for Django Websites
    =============================================


    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_.
  16. @epicserve epicserve revised this gist Nov 1, 2011. 1 changed file with 249 additions and 1 deletion.
    250 changes: 249 additions & 1 deletion ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -1 +1,249 @@
    Ubuntu Server Setup Guide for a Django Websites
    Ubuntu Server Setup Guide for a Django Websites
    ===============================================


    This guide is a walk-through on how to setup Ubuntu Server for hosting Django websites. The Django stack that will be used in this guide is Ubuntu_, Nginx_, Gunicorn_ and Postgres_.

    .. _Ubuntu: http://www.ubuntu.com/business/server/overview
    .. _Nginx: http://nginx.org/en/
    .. _Gunicorn: http://gunicorn.org/
    .. _Postgres: http://www.postgresql.org/


    Step 1: Install Ubuntu Server
    -----------------------------

    The version of Ubuntu I'm using for this guide is `Ubuntu 11.10 64 bit Server <http://www.ubuntu.com/start-download?distro=server&bits=64&release=latest>`_. I've installed Ubuntu Server in a VirtualBox VM on my MacBook Pro which is currently running Mac OS X 10.7.2. During the installation of Ubuntu Server I answered the prompts with the following::

    Language: English
    Install Menu: Install Ubuntu Server
    Select a language: English
    Select your location: United States
    Configure the Keyboard: No
    Configure the keyboard: English (US)
    Configure the keyboard: English (US)
    Hostname: ubuntu-vm
    Configure the clock: Yes
    Partition disks: Guided - use entire disk and set up LVM
    Partition disks: SCSI3 (0,0,0) (sda) - 21.5 GB ATA VBOX HARDDISK
    Partition disks: Yes
    Partition disks: Continue
    Partition disks: Yes
    Set up users and passwords: Brent O'Connor
    Set up users and passwords: oconnor
    Set up users and passwords: ********
    Set up users and passwords: ********
    Set up users and passwords: No
    Configure the package manager: <blank>
    Configure taskse1: No automatic updates
    Software selection: <Continue>
    Install the GRUB boot loader on a hard disk: Yes
    Installation complete: <Continue>


    Step 2: Install software
    ------------------------

    OpenSSH
    ~~~~~~~

    Since I like to connect to my servers using SSH the first thing I install is openssh-server::

    $ sudo aptitude install openssh-server

    Once that's installed if your using VirtualBox you can setup port forwarding so you can connect to your server from your local Terminal. Under settings for your VM in VirtualBox click on the "Network" tab and then click on the "Port Forwarding" button. Now click on the plus and change the port for host to 2222 and the port for guest to 22. You should now be able to open up your Terminal and connect to your Ubuntu Server using the following... ::

    $ ssh localhost -p 2222

    Python Header Files
    ~~~~~~~~~~~~~~~~~~~

    This is needed in order to install python postgres binding libraries like ``psycopg2``. ::

    $ sudo aptitude install python2.7-dev

    PostgreSQL
    ~~~~~~~~~~

    ::

    $ sudo aptitude install postgresql postgresql-server-dev-9.1

    Configure postgres so Django can connect with out a password. Edit the file ``/etc/postgresql/9.1/main/pg_hba.conf`` so the following lines in the file are identical to the following. You should be able to do this by changing all the uncommented rows in the fith column to "trust". ::

    local all postgres trust
    local all all trust
    host all all 127.0.0.1/32 trust
    host all all ::1/128 trust

    Make your Ubuntu user a PostgreSQL superuser::

    $ sudo su - postgres
    $ createuser --superuser oconnor
    $ exit

    Restart PostgreSQL::

    $ sudo /etc/init.d/postgresql restart

    Nginx
    ~~~~~

    ::

    $ sudo aptitude install nginx

    Git
    ~~~

    ::

    $ sudo aptitude install git


    Step 3: Install an example site
    -------------------------------

    Setup a virtualenv::

    $ cd /usr/local/
    $ sudo mkdir virtualenvs
    $ sudo chown oconnor virtualenvs
    $ cd virtualenvs
    $ sudo apt-get install python-setuptools
    $ sudo easy_install pip
    $ sudo pip install virtualenv
    $ virtualenv --no-site-packages example-site
    $ source example-site/bin/activate

    Make a location for the example site::

    $ cd /srv/
    $ sudo mkdir sites
    $ sudo chown oconnor sites
    $ cd sites
    $ git clone git://github.com/epicserve/django-base-site.git example-site
    $ cd example-site/
    $ mkdir -p static/cache
    $ sudo chown www-data:www-data static/cache
    $ echo `pwd` > /usr/local/virtualenvs/example-site/lib/python2.7/site-packages/django_project_root.pth

    Create the file ``/srv/sites/example-site/config/settings/local.py`` and add the following and save::

    from base import *

    LOCAL_SETTINGS_LOADED = True

    DEBUG = True

    INTERNAL_IPS = ('127.0.0.1', )

    ADMINS = (
    ('Your Name', '[email protected]'),
    )

    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'example_site', # Or path to database file if using sqlite3.
    'USER': 'example_site', # Not used with sqlite3.
    'HOST': 'localhost',
    }
    }

    Install the sites required python packages::

    $ pip install -r config/requirements.txt

    Install Gunicorn::

    $ pip install gunicorn

    Install psycopg2::

    $ pip install psycopg2

    Create a PostgreSQL user and database for your example-site::

    $ createuser example_site
    $ Shall the new role be a superuser? (y/n) n
    $ Shall the new role be allowed to create databases? (y/n) y
    $ Shall the new role be allowed to create more new roles? (y/n) n
    $ createdb example_site -O example_site

    Step 3: Daemonize Gunicorn using Ubuntu's Upstart
    -------------------------------------------------

    Create your Upstart configuration file::

    $ sudo vi /etc/init/gunicorn_example-site.conf

    Add the following and save the file::

    description "upstart configuration for gunicorn example-site"

    start on net-device-up
    stop on shutdown

    respawn

    exec /usr/local/virtualenvs/example-site/bin/gunicorn_django -u www-data -c /srv/sites/example-site/config/gunicorn/example-site.py /srv/sites/example-site/config/settings/__init__.py

    Start the gunicorn site::

    $ sudo start gunicorn_example-site


    Step 4: Setup Nginx to proxy to your new example site
    -----------------------------------------------------

    Create a new file ``/etc/nginx/sites-available/example-site.conf`` and add the following to the contents of the file::

    server {

    listen 80;
    server_name localhost;
    access_log /var/log/nginx/example-site.access.log;
    error_log /var/log/nginx/example-site.error.log;

    location = /biconcave {
    return 404;
    }

    location /static/ {
    root /srv/sites/example-site/;
    }

    location /media/ {
    root /srv/sites/example-site/;
    }


    location / {
    proxy_pass http://127.0.0.1:8001/;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size 10m;
    }

    }

    Enable the new site::

    $ cd /etc/nginx/sites-enabled
    $ sudo rm default
    $ sudo ln -s ../sites-available/example-site.conf

    Start nginx::

    $ sudo /etc/init.d/nginx start


    Step 5: Test the new example site
    ---------------------------------

    While still connected to your Ubuntu server via SSH run the following, which should spit out the HTML for your site::

    wget -qO- 127.0.0.1:80
  17. @epicserve epicserve created this gist Nov 1, 2011.
    1 change: 1 addition & 0 deletions ubuntu-server-django-guide.rst
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Ubuntu Server Setup Guide for a Django Websites