{"revision": {"id": "f24138a7-2f95-11f1-9e8b-e86a64d24d78", "node_id": "f240510a-2f95-11f1-9afc-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "Django virtualenv apache2 mod_wsgi\r\n===============================================\r\n\r\n Steps taken to deploy a Django web application (osqa.net) in a virtualenv, using apache2, and mod_wsgi.\r\n\r\n**Why use wsgi and virtualenv?**\r\n\r\n * I also host pylons applications using mod_wsgi and apache2 and mod_python is incompatible.\r\n * mod_wsgi is faster.\r\n * I don't want my public site-packages library conflicting with other applications.\r\n\r\n.. contents::\r\n\r\n\r\nInstall virtualenv\r\n------------------\r\n\r\n#. Install Setup tools:\r\n \r\n   .. code-block :: bash \r\n\r\n    $ sudo apt-get install python-setuptools\r\n\r\n#. Install virtualenv:\r\n\r\n   .. code-block :: bash \r\n\r\n    $ sudo easy_install virtualenv\r\n\r\n\r\nCreate a virtualenv\r\n---------------------\r\n#. Move to the directory where you want to create the virtualenv:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ cd /www/lostquery.com\r\n\r\n\r\n#. Create python environment:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ virtualenv --no-site-packages virtpy\r\n\r\n\r\n\r\nInstall Django\r\n----------------\r\n\r\n#. Activate the virtualenv:\r\n     \r\n   .. code-block :: bash\r\n\r\n    $ source virtpy/bin/activate\r\n\r\n#. The shell should have changed to *(virtpy) $* .  Run this command to install django to the virtualenv:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ easy_install django\r\n\r\n#. Install any dependencies you will need to the virtualenv:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid South\r\n\r\n\r\nInstall and configure the Django web application\r\n----------------------------------------------------\r\n\r\n#. Get the sourcecode:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ svn co http://svn.osqa.net/svnroot/osqa/trunk\r\n\r\n#. Configure the application:\r\n    \r\n   .. code-block :: bash\r\n\r\n    $ cd trunk\r\n\r\n    $ cp settings_local.py.dist settings_local.py\r\n\r\n    $ vi settings_local.py\r\n\r\n#. You must tell the Django's application what database to use:\r\n   \r\n   .. code-block :: python\r\n \r\n    DATABASE_NAME     =  'databasename' # Or path to database file if using sqlite3.\r\n    DATABASE_USER     =  'username'     # Not used with sqlite3.\r\n    DATABASE_PASSWORD =  'p@ssw0rd'     # Not used with sqlite3.\r\n    DATABASE_ENGINE   =  'mysql'        #mysql, etc\r\n    DATABASE_HOST     =  'localhost'\r\n    DATABASE_PORT     =  ''\r\n\r\n\r\n#. Give apache access to a few directories, /trunk/log and /trunk/forum/upfiles:\r\n\r\n\r\n   .. code-block :: bash\r\n    \r\n    $ chmod 777 log\r\n    $ chmod 777 forum/upfiles\r\n\r\n\r\n#. Create the database `(example syntax to create a mySQL database) <http://www.foxhop.net/mySQL#create-database>`_.\r\n\r\n   Let the django application build the tables in the new database:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ python manage.py syncdb --all\r\n    $ python manage.py migrate forum --fake\r\n\r\n\r\nConfigure Apache2 VirtualHost\r\n--------------------------------\r\n\r\n#. Turn on mod_rewrite:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ a2enmod rewrite\r\n\r\n#. Create a sites-enabled vhost apache2 file:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ sudo vi /etc/apache2/sites-enabled/007-lostquery\r\n\r\n#. Setup the host file:\r\n\r\n   .. code-block :: apache\r\n    :linenos:\r\n\r\n    #lostquery.com\r\n\r\n    #Djangos embeded mod_wsgi\r\n    <VirtualHost *:80>\r\n        ServerName lostquery.com\r\n        ServerAlias www.lostquery.com\r\n        ServerAdmin foxhop1@gmail.com\r\n\r\n        # ReWrite URL to WWW\r\n        RewriteEngine On\r\n        RewriteCond %{HTTP_HOST} ^www.lostquery.com\r\n        RewriteRule (.*) http://lostquery.com$1 [R=301,L]\r\n\r\n        # Log Files\r\n        ErrorLog /var/log/apache2/error-lostquery.log\r\n        CustomLog /var/log/apache2/access-lostquery.log combined\r\n\r\n        # Setup mod_wsgi\r\n        WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi\r\n\r\n        <Directory /www/lostquery.com/mod_wsgi>\r\n         Order deny,allow\r\n         Allow from all\r\n        </Directory>\r\n     </VirtualHost>\r\n\r\nConfigure mod_wsgi\r\n--------------------------------\r\n\r\n#. Create the egg-cache directory:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ mkdir /www/lostquery.com/mod_wsgi\r\n\r\n    $ mkdir /www/lostquery.com/mod_wsgi/egg-cache\r\n\r\n#. Create the wsgi dispatch file:\r\n\r\n   .. code-block :: bash\r\n  \r\n    $ vi /www/lostquery.com/mod_wsgi/dispatch.wsgi\r\n\r\n#. Setup the wsgi dispatch file:\r\n\r\n   .. code-block :: python\r\n    :linenos:\r\n\r\n    import os\r\n    import sys \r\n    sys.stdout = sys.stderr\r\n    # Add the virtual Python environment site-packages directory to the path\r\n    import site\r\n    site.addsitedir('/www/lostquery.com/virtpy/lib/python2.6/site-packages')\r\n\r\n\r\n    # Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages  \r\n    import os\r\n    os.environ['PYTHON_EGG_CACHE'] = '/www/lostquery.com/mod_wsgi/egg-cache'\r\n\r\n    #If your project is not on your PYTHONPATH by default you can add the following\r\n    sys.path.append('/www/lostquery.com/trunk')\r\n    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'\r\n\r\n\r\n    import django.core.handlers.wsgi\r\n    application = django.core.handlers.wsgi.WSGIHandler()\r\n \r\n\r\n\r\n\r\n\r\nRestart Apache2\r\n--------------------\r\n\r\n#. Reset apache2 so that it reads in the new config files:\r\n\r\n   .. code-block :: bash\r\n\r\n    $ sudo service apache2 restart\r\n\r\n", "source_format": "rst", "revision_number": 12, "created": 1303939107000}}