django-virtualenv-apache-mod_wsgi
| rev 19 | rev 20 | ||||
|---|---|---|---|---|---|
| f | 1 | Django virtualenv Apache2 mod_wsgi | f | 1 | Django virtualenv Apache2 mod_wsgi |
| 2 | =============================================== | 2 | =============================================== | ||
| n | n | 3 | |||
| 4 | By the end of this article you will know how to deploy a Django web application | ||||
| > | using a python virtualenv, using an Apache2 webserver with mod_wsgi. | ||||
| 3 | 5 | ||||
| n | 4 | Steps taken to deploy a Django web application (osqa.net) in a virtualenv, usin | n | 6 | You should bookmark this page. |
| > | g apache2, and mod_wsgi. | ||||
| 5 | 7 | ||||
| 6 | **Why use wsgi and virtualenv?** | 8 | **Why use wsgi and virtualenv?** | ||
| 7 | 9 | ||||
| 8 | * I also host pylons applications using mod_wsgi and apache2 and mod_python is | 10 | * I also host pylons applications using mod_wsgi and apache2 and mod_python is | ||
| > | incompatible. | > | incompatible. | ||
| t | 9 | * mod_wsgi is faster. | t | 11 | * mod_wsgi is fast. |
| 10 | * I don't want my public site-packages library conflicting with other applicati | 12 | * I don't want my public site-packages library conflicting with other applicati | ||
| > | ons. | > | ons. | ||
| 11 | 13 | ||||
| rev 18 | rev 19 | ||||
|---|---|---|---|---|---|
| 159 | CustomLog /var/log/apache2/access-lostquery.log combined | 159 | CustomLog /var/log/apache2/access-lostquery.log combined | ||
| 160 | 160 | ||||
| t | 161 | # prevent django from seting static files | t | 161 | # Prevent django from serving static files |
| 162 | DocumentRoot /www/lostquery.com/trunk/forum | 162 | DocumentRoot /www/lostquery.com/trunk/forum | ||
| 163 | Alias /m/ /www/lostquery.com/trunk/forum/skins/ | 163 | Alias /m/ /www/lostquery.com/trunk/forum/skins/ | ||
| rev 17 | rev 18 | ||||
|---|---|---|---|---|---|
| 222 | 222 | ||||
| 223 | 223 | ||||
| n | 224 | Restart Apache2 | n | 224 | Enable the Virtual Host and reload Apache2 |
| 225 | -------------------- | 225 | ------------------------------------------------ | ||
| 226 | 226 | ||||
| 227 | #. Reset apache2 so that it reads in the new config files: | 227 | #. Reset apache2 so that it reads in the new config files: | ||
| 229 | .. code-block :: bash | 229 | .. code-block :: bash | ||
| 230 | 230 | ||||
| t | t | 231 | $ a2ensite lostquery | ||
| 232 | |||||
| 231 | $ sudo service apache2 reload | 233 | $ sudo service apache2 reload | ||
| 232 | 234 | ||||
| rev 16 | rev 17 | ||||
|---|---|---|---|---|---|
| 131 | $ a2enmod rewrite | 131 | $ a2enmod rewrite | ||
| 132 | 132 | ||||
| n | 133 | #. Create a sites-enabled vhost apache2 file: | n | 133 | #. Create a sites-available vhost apache2 file: |
| 134 | 134 | ||||
| 135 | .. code-block :: bash | 135 | .. code-block :: bash | ||
| 136 | 136 | ||||
| t | 137 | $ sudo vi /etc/apache2/sites-enabled/007-lostquery | t | 137 | $ sudo vi /etc/apache2/sites-available/007-lostquery |
| 138 | 138 | ||||
| 139 | #. Setup the host file: | 139 | #. Setup the host file: | ||
| rev 15 | rev 16 | ||||
|---|---|---|---|---|---|
| 229 | .. code-block :: bash | 229 | .. code-block :: bash | ||
| 230 | 230 | ||||
| t | 231 | $ sudo service apache2 restart | t | 231 | $ sudo service apache2 reload |
| 232 | 232 | ||||
| rev 14 | rev 15 | ||||
|---|---|---|---|---|---|
| 142 | :linenos: | 142 | :linenos: | ||
| 143 | 143 | ||||
| n | 144 | #lostquery.com | n | 144 | #lostquery.com |
| 145 | 145 | ||||
| n | 146 | #Djangos embeded mod_wsgi | n | 146 | #Djangos embeded mod_wsgi |
| 147 | <VirtualHost *:80> | 147 | <VirtualHost *:80> | ||
| 148 | ServerName lostquery.com | 148 | ServerName lostquery.com | ||
| 149 | ServerAlias www.lostquery.com | 149 | ServerAlias www.lostquery.com | ||
| 150 | ServerAdmin foxhop1@gmail.com | 150 | ServerAdmin admin@admin.com | ||
| 151 | 151 | ||||
| n | 152 | # ReWrite URL to WWW | n | 152 | # ReWrite URL to WWW |
| 153 | RewriteEngine On | 153 | RewriteEngine On | ||
| 154 | RewriteCond %{HTTP_HOST} ^www.lostquery.com | 154 | RewriteCond %{HTTP_HOST} ^www.lostquery.com | ||
| 155 | RewriteRule (.*) http://lostquery.com$1 [R=301,L] | 155 | RewriteRule (.*) http://lostquery.com$1 [R=301,L] | ||
| 156 | 156 | ||||
| n | 157 | # Log Files | n | 157 | # Log Files |
| 158 | ErrorLog /var/log/apache2/error-lostquery.log | 158 | ErrorLog /var/log/apache2/error-lostquery.log | ||
| 159 | CustomLog /var/log/apache2/access-lostquery.log combined | 159 | CustomLog /var/log/apache2/access-lostquery.log combined | ||
| 160 | 160 | ||||
| n | n | 161 | # prevent django from seting static files | ||
| 162 | DocumentRoot /www/lostquery.com/trunk/forum | ||||
| 163 | Alias /m/ /www/lostquery.com/trunk/forum/skins/ | ||||
| 164 | Alias /upfiles/ /www/lostquery.com/trunk/forum/upfiles/ | ||||
| 165 | <Directory /www/lostquery.com/trunk/forum/skins> | ||||
| 166 | Order allow,deny | ||||
| 167 | Allow from all | ||||
| 168 | </Directory> | ||||
| 169 | |||||
| 161 | # Setup mod_wsgi | 170 | # Setup mod_wsgi | ||
| 171 | WSGIDaemonProcess lostquery display-name=lostquery user=www-data proce | ||||
| > | sses=2 threads=15 | ||||
| 162 | WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi | 172 | WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||
| 163 | 173 | ||||
| n | 164 | <Directory /www/lostquery.com/mod_wsgi> | n | ||
| 165 | Order deny,allow | ||||
| 166 | Allow from all | ||||
| 167 | </Directory> | ||||
| 168 | </VirtualHost> | 174 | </VirtualHost> | ||
| t | t | 175 | |||
| 176 | |||||
| 169 | 177 | ||||
| 170 | Configure mod_wsgi | 178 | Configure mod_wsgi | ||
| rev 13 | rev 14 | ||||
|---|---|---|---|---|---|
| 20 | .. code-block :: bash | 20 | .. code-block :: bash | ||
| 21 | 21 | ||||
| n | 22 | $ sudo apt-get install python-setuptools | n | 22 | $ sudo apt-get install python-setuptools memcached |
| 23 | 23 | ||||
| 24 | #. Install virtualenv: | 24 | #. Install virtualenv: | ||
| 65 | .. code-block :: bash | 65 | .. code-block :: bash | ||
| 66 | 66 | ||||
| n | 67 | $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid So | n | 67 | $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid So |
| > | uth | > | uth python-memcached | ||
| 68 | 68 | ||||
| 69 | 69 | ||||
| 87 | $ vi settings_local.py | 87 | $ vi settings_local.py | ||
| 88 | 88 | ||||
| n | 89 | #. You must tell the Django's application what database to use: | n | 89 | #. You must tell the Django's application what database to use. Also configure |
| > | memcached: | ||||
| 90 | 90 | ||||
| 91 | .. code-block :: python | 91 | .. code-block :: python | ||
| 92 | 92 | ||||
| n | n | 93 | # database settings | ||
| 93 | DATABASE_NAME = 'databasename' # Or path to database file if using sqli | 94 | DATABASE_NAME = 'databasename' # Or path to database file if using sqli | ||
| > | te3. | > | te3. | ||
| 94 | DATABASE_USER = 'username' # Not used with sqlite3. | 95 | DATABASE_USER = 'username' # Not used with sqlite3. | ||
| 98 | DATABASE_PORT = '' | 99 | DATABASE_PORT = '' | ||
| 99 | 100 | ||||
| t | t | 101 | # memcached settings (osqa is slow as dirt without this) | ||
| 102 | CACHE_BACKEND = 'memcached://127.0.0.1:11211/' | ||||
| 103 | |||||
| 100 | 104 | ||||
| 101 | #. Give apache access to a few directories, /trunk/log and /trunk/forum/upfiles: | 105 | #. Give apache access to a few directories, /trunk/log and /trunk/forum/upfiles: | ||
| rev 12 | rev 13 | ||||
|---|---|---|---|---|---|
| t | 1 | Django virtualenv apache2 mod_wsgi | t | 1 | Django virtualenv Apache2 mod_wsgi |
| 2 | =============================================== | 2 | =============================================== | ||
| 3 | 3 | ||||
| rev 11 | rev 12 | ||||
|---|---|---|---|---|---|
| 210 | 210 | ||||
| 211 | 211 | ||||
| t | 212 | Reset apache2 | t | 212 | Restart Apache2 |
| 213 | -------------------- | 213 | -------------------- | ||
| 214 | 214 | ||||
| rev 10 | rev 11 | ||||
|---|---|---|---|---|---|
| 13 | 13 | ||||
| 14 | 14 | ||||
| n | 15 | Setup virtualenv | n | 15 | Install virtualenv |
| 16 | ------------------ | 16 | ------------------ | ||
| 17 | 17 | ||||
| 29 | 29 | ||||
| 30 | 30 | ||||
| n | 31 | Create virtualenv | n | 31 | Create a virtualenv |
| 32 | --------------------- | 32 | --------------------- | ||
| 33 | #. Move to the directory where you want to create the virtualenv: | 33 | #. Move to the directory where you want to create the virtualenv: | ||
| 68 | 68 | ||||
| 69 | 69 | ||||
| n | 70 | Setup Django web application | n | 70 | Install and configure the Django web application |
| 71 | ------------------------------- | 71 | ---------------------------------------------------- | ||
| 72 | 72 | ||||
| 73 | #. Get the sourcecode: | 73 | #. Get the sourcecode: | ||
| 118 | 118 | ||||
| 119 | 119 | ||||
| n | 120 | Setup Apache2 | n | 120 | Configure Apache2 VirtualHost |
| 121 | -------------------------------- | 121 | -------------------------------- | ||
| 122 | 122 | ||||
| 164 | </VirtualHost> | 164 | </VirtualHost> | ||
| 165 | 165 | ||||
| t | 166 | Setup mod_wsgi | t | 166 | Configure mod_wsgi |
| 167 | -------------------------------- | 167 | -------------------------------- | ||
| 168 | 168 | ||||
| rev 8 | rev 9 | ||||
|---|---|---|---|---|---|
| 118 | 118 | ||||
| 119 | 119 | ||||
| n | 120 | Setup Apache2 and mod_wsgi | n | 120 | Setup Apache2 |
| 121 | -------------------------------- | 121 | -------------------------------- | ||
| 122 | 122 | ||||
| 136 | 136 | ||||
| 137 | .. code-block :: apache | 137 | .. code-block :: apache | ||
| n | n | 138 | :linenos: | ||
| 138 | 139 | ||||
| 139 | #lostquery.com | 140 | #lostquery.com | ||
| 162 | </Directory> | 163 | </Directory> | ||
| 163 | </VirtualHost> | 164 | </VirtualHost> | ||
| t | t | 165 | |||
| 166 | Setup mod_wsgi | ||||
| 167 | -------------------------------- | ||||
| 164 | 168 | ||||
| 165 | #. Create the egg-cache directory: | 169 | #. Create the egg-cache directory: | ||
| rev 7 | rev 8 | ||||
|---|---|---|---|---|---|
| 207 | 207 | ||||
| 208 | Reset apache2 | 208 | Reset apache2 | ||
| t | 209 | ================= | t | 209 | -------------------- |
| 210 | 210 | ||||
| 211 | #. Reset apache2 so that it reads in the new config files: | 211 | #. Reset apache2 so that it reads in the new config files: | ||
| rev 6 | rev 7 | ||||
|---|---|---|---|---|---|
| 108 | 108 | ||||
| 109 | 109 | ||||
| t | 110 | #. Create the database `(example syntax to create a mySQL database) <http://www. | t | 110 | #. Create the database `(example syntax to create a mySQL database) <http://www. |
| > | foxhop.net/mySQL#create-database>`_: | > | foxhop.net/mySQL#create-database>`_. | ||
| 111 | |||||
| 112 | Let the django application build the tables in the new database: | ||||
| 111 | 113 | ||||
| 112 | .. code-block :: bash | 114 | .. code-block :: bash | ||
| rev 5 | rev 6 | ||||
|---|---|---|---|---|---|
| t | 1 | cyYtPp <a href="http://otzgpccbdlci.com/">otzgpccbdlci</a>, [url=http://ibjrwib | t | 1 | Django virtualenv apache2 mod_wsgi |
| > | easzi.com/]ibjrwibeaszi[/url], [link=http://fyflcwvhthyq.com/]fyflcwvhthyq[/link | ||||
| > | ], http://batxicnswtea.com/ | ||||
| 2 | =============================================== | ||||
| 3 | |||||
| 4 | Steps taken to deploy a Django web application (osqa.net) in a virtualenv, usin | ||||
| > | g apache2, and mod_wsgi. | ||||
| 5 | |||||
| 6 | **Why use wsgi and virtualenv?** | ||||
| 7 | |||||
| 8 | * I also host pylons applications using mod_wsgi and apache2 and mod_python is | ||||
| > | incompatible. | ||||
| 9 | * mod_wsgi is faster. | ||||
| 10 | * I don't want my public site-packages library conflicting with other applicati | ||||
| > | ons. | ||||
| 11 | |||||
| 12 | .. contents:: | ||||
| 13 | |||||
| 14 | |||||
| 15 | Setup virtualenv | ||||
| 16 | ------------------ | ||||
| 17 | |||||
| 18 | #. Install Setup tools: | ||||
| 19 | |||||
| 20 | .. code-block :: bash | ||||
| 21 | |||||
| 22 | $ sudo apt-get install python-setuptools | ||||
| 23 | |||||
| 24 | #. Install virtualenv: | ||||
| 25 | |||||
| 26 | .. code-block :: bash | ||||
| 27 | |||||
| 28 | $ sudo easy_install virtualenv | ||||
| 29 | |||||
| 30 | |||||
| 31 | Create virtualenv | ||||
| 32 | --------------------- | ||||
| 33 | #. Move to the directory where you want to create the virtualenv: | ||||
| 34 | |||||
| 35 | .. code-block :: bash | ||||
| 36 | |||||
| 37 | $ cd /www/lostquery.com | ||||
| 38 | |||||
| 39 | |||||
| 40 | #. Create python environment: | ||||
| 41 | |||||
| 42 | .. code-block :: bash | ||||
| 43 | |||||
| 44 | $ virtualenv --no-site-packages virtpy | ||||
| 45 | |||||
| 46 | |||||
| 47 | |||||
| 48 | Install Django | ||||
| 49 | ---------------- | ||||
| 50 | |||||
| 51 | #. Activate the virtualenv: | ||||
| 52 | |||||
| 53 | .. code-block :: bash | ||||
| 54 | |||||
| 55 | $ source virtpy/bin/activate | ||||
| 56 | |||||
| 57 | #. The shell should have changed to *(virtpy) $* . Run this command to install | ||||
| > | django to the virtualenv: | ||||
| 58 | |||||
| 59 | .. code-block :: bash | ||||
| 60 | |||||
| 61 | $ easy_install django | ||||
| 62 | |||||
| 63 | #. Install any dependencies you will need to the virtualenv: | ||||
| 64 | |||||
| 65 | .. code-block :: bash | ||||
| 66 | |||||
| 67 | $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid So | ||||
| > | uth | ||||
| 68 | |||||
| 69 | |||||
| 70 | Setup Django web application | ||||
| 71 | ------------------------------- | ||||
| 72 | |||||
| 73 | #. Get the sourcecode: | ||||
| 74 | |||||
| 75 | .. code-block :: bash | ||||
| 76 | |||||
| 77 | $ svn co http://svn.osqa.net/svnroot/osqa/trunk | ||||
| 78 | |||||
| 79 | #. Configure the application: | ||||
| 80 | |||||
| 81 | .. code-block :: bash | ||||
| 82 | |||||
| 83 | $ cd trunk | ||||
| 84 | |||||
| 85 | $ cp settings_local.py.dist settings_local.py | ||||
| 86 | |||||
| 87 | $ vi settings_local.py | ||||
| 88 | |||||
| 89 | #. You must tell the Django's application what database to use: | ||||
| 90 | |||||
| 91 | .. code-block :: python | ||||
| 92 | |||||
| 93 | DATABASE_NAME = 'databasename' # Or path to database file if using sqli | ||||
| > | te3. | ||||
| 94 | DATABASE_USER = 'username' # Not used with sqlite3. | ||||
| 95 | DATABASE_PASSWORD = 'p@ssw0rd' # Not used with sqlite3. | ||||
| 96 | DATABASE_ENGINE = 'mysql' #mysql, etc | ||||
| 97 | DATABASE_HOST = 'localhost' | ||||
| 98 | DATABASE_PORT = '' | ||||
| 99 | |||||
| 100 | |||||
| 101 | #. Give apache access to a few directories, /trunk/log and /trunk/forum/upfiles: | ||||
| 102 | |||||
| 103 | |||||
| 104 | .. code-block :: bash | ||||
| 105 | |||||
| 106 | $ chmod 777 log | ||||
| 107 | $ chmod 777 forum/upfiles | ||||
| 108 | |||||
| 109 | |||||
| 110 | #. Create the database `(example syntax to create a mySQL database) <http://www. | ||||
| > | foxhop.net/mySQL#create-database>`_: | ||||
| 111 | |||||
| 112 | .. code-block :: bash | ||||
| 113 | |||||
| 114 | $ python manage.py syncdb --all | ||||
| 115 | $ python manage.py migrate forum --fake | ||||
| 116 | |||||
| 117 | |||||
| 118 | Setup Apache2 and mod_wsgi | ||||
| 119 | -------------------------------- | ||||
| 120 | |||||
| 121 | #. Turn on mod_rewrite: | ||||
| 122 | |||||
| 123 | .. code-block :: bash | ||||
| 124 | |||||
| 125 | $ a2enmod rewrite | ||||
| 126 | |||||
| 127 | #. Create a sites-enabled vhost apache2 file: | ||||
| 128 | |||||
| 129 | .. code-block :: bash | ||||
| 130 | |||||
| 131 | $ sudo vi /etc/apache2/sites-enabled/007-lostquery | ||||
| 132 | |||||
| 133 | #. Setup the host file: | ||||
| 134 | |||||
| 135 | .. code-block :: apache | ||||
| 136 | |||||
| 137 | #lostquery.com | ||||
| 138 | |||||
| 139 | #Djangos embeded mod_wsgi | ||||
| 140 | <VirtualHost *:80> | ||||
| 141 | ServerName lostquery.com | ||||
| 142 | ServerAlias www.lostquery.com | ||||
| 143 | ServerAdmin foxhop1@gmail.com | ||||
| 144 | |||||
| 145 | # ReWrite URL to WWW | ||||
| 146 | RewriteEngine On | ||||
| 147 | RewriteCond %{HTTP_HOST} ^www.lostquery.com | ||||
| 148 | RewriteRule (.*) http://lostquery.com$1 [R=301,L] | ||||
| 149 | |||||
| 150 | # Log Files | ||||
| 151 | ErrorLog /var/log/apache2/error-lostquery.log | ||||
| 152 | CustomLog /var/log/apache2/access-lostquery.log combined | ||||
| 153 | |||||
| 154 | # Setup mod_wsgi | ||||
| 155 | WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||||
| 156 | |||||
| 157 | <Directory /www/lostquery.com/mod_wsgi> | ||||
| 158 | Order deny,allow | ||||
| 159 | Allow from all | ||||
| 160 | </Directory> | ||||
| 161 | </VirtualHost> | ||||
| 162 | |||||
| 163 | #. Create the egg-cache directory: | ||||
| 164 | |||||
| 165 | .. code-block :: bash | ||||
| 166 | |||||
| 167 | $ mkdir /www/lostquery.com/mod_wsgi | ||||
| 168 | |||||
| 169 | $ mkdir /www/lostquery.com/mod_wsgi/egg-cache | ||||
| 170 | |||||
| 171 | #. Create the wsgi dispatch file: | ||||
| 172 | |||||
| 173 | .. code-block :: bash | ||||
| 174 | |||||
| 175 | $ vi /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||||
| 176 | |||||
| 177 | #. Setup the wsgi dispatch file: | ||||
| 178 | |||||
| 179 | .. code-block :: python | ||||
| 180 | :linenos: | ||||
| 181 | |||||
| 182 | import os | ||||
| 183 | import sys | ||||
| 184 | sys.stdout = sys.stderr | ||||
| 185 | # Add the virtual Python environment site-packages directory to the path | ||||
| 186 | import site | ||||
| 187 | site.addsitedir('/www/lostquery.com/virtpy/lib/python2.6/site-packages') | ||||
| 188 | |||||
| 189 | |||||
| 190 | # Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages | ||||
| 191 | import os | ||||
| 192 | os.environ['PYTHON_EGG_CACHE'] = '/www/lostquery.com/mod_wsgi/egg-cache' | ||||
| 193 | |||||
| 194 | #If your project is not on your PYTHONPATH by default you can add the follow | ||||
| > | ing | ||||
| 195 | sys.path.append('/www/lostquery.com/trunk') | ||||
| 196 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' | ||||
| 197 | |||||
| 198 | |||||
| 199 | import django.core.handlers.wsgi | ||||
| 200 | application = django.core.handlers.wsgi.WSGIHandler() | ||||
| 201 | |||||
| 202 | |||||
| 203 | |||||
| 204 | |||||
| 205 | |||||
| 206 | Reset apache2 | ||||
| 207 | ================= | ||||
| 208 | |||||
| 209 | #. Reset apache2 so that it reads in the new config files: | ||||
| 210 | |||||
| 211 | .. code-block :: bash | ||||
| 212 | |||||
| 213 | $ sudo service apache2 restart | ||||
| 214 | |||||
| rev 3 | rev 4 | ||||
|---|---|---|---|---|---|
| t | 1 | gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvq | t | 1 | cyYtPp <a href="http://otzgpccbdlci.com/">otzgpccbdlci</a>, [url=http://ibjrwib |
| > | dfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/link | > | easzi.com/]ibjrwibeaszi[/url], [link=http://fyflcwvhthyq.com/]fyflcwvhthyq[/link | ||
| > | ], http://nihzxpbxowui.com/gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakh | > | ], http://batxicnswtea.com/ | ||
| > | qx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrm | ||||
| > | aiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.com/gXYsGz <a href="http://hm | ||||
| > | toqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/u | ||||
| > | rl], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.com | ||||
| > | /gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomv | ||||
| > | qdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/lin | ||||
| > | k], http://nihzxpbxowui.com/gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmak | ||||
| > | hqx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxr | ||||
| > | maiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.com/gXYsGz <a href="http://h | ||||
| > | mtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/ | ||||
| > | url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.co | ||||
| > | m/gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkom | ||||
| > | vqdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/li | ||||
| > | nk], http://nihzxpbxowui.com/ | ||||
| rev 1 | rev 2 | ||||
|---|---|---|---|---|---|
| t | 1 | Django virtualenv apache2 mod_wsgi | t | 1 | gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvq |
| > | dfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/link | ||||
| > | ], http://nihzxpbxowui.com/gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakh | ||||
| > | qx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrm | ||||
| > | aiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.com/gXYsGz <a href="http://hm | ||||
| > | toqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/u | ||||
| > | rl], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.com | ||||
| > | /gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomv | ||||
| > | qdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/lin | ||||
| > | k], http://nihzxpbxowui.com/gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmak | ||||
| > | hqx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxr | ||||
| > | maiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.com/gXYsGz <a href="http://h | ||||
| > | mtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvqdfnbn.com/]qxkomvqdfnbn[/ | ||||
| > | url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/link], http://nihzxpbxowui.co | ||||
| > | m/gXYsGz <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkom | ||||
| > | vqdfnbn.com/]qxkomvqdfnbn[/url], [link=http://wdkfimxrmaiy.com/]wdkfimxrmaiy[/li | ||||
| > | nk], http://nihzxpbxowui.com/ | ||||
| 2 | =============================================== | ||||
| 3 | |||||
| 4 | Steps taken to deploy a Django web application (osqa.net) in a virtualenv, usin | ||||
| > | g apache2, and mod_wsgi. | ||||
| 5 | |||||
| 6 | **Why use wsgi and virtualenv?** | ||||
| 7 | |||||
| 8 | * I also host pylons applications using mod_wsgi and apache2 and mod_python is | ||||
| > | incompatible. | ||||
| 9 | * mod_wsgi is faster. | ||||
| 10 | * I don't want my public site-packages library conflicting with other applicati | ||||
| > | ons. | ||||
| 11 | |||||
| 12 | .. contents:: | ||||
| 13 | |||||
| 14 | |||||
| 15 | Setup virtualenv | ||||
| 16 | ------------------ | ||||
| 17 | |||||
| 18 | #. Install Setup tools: | ||||
| 19 | |||||
| 20 | .. code-block :: bash | ||||
| 21 | |||||
| 22 | $ sudo apt-get install python-setuptools | ||||
| 23 | |||||
| 24 | #. Install virtualenv: | ||||
| 25 | |||||
| 26 | .. code-block :: bash | ||||
| 27 | |||||
| 28 | $ sudo easy_install virtualenv | ||||
| 29 | |||||
| 30 | |||||
| 31 | Create virtualenv | ||||
| 32 | --------------------- | ||||
| 33 | #. Move to the directory where you want to create the virtualenv: | ||||
| 34 | |||||
| 35 | .. code-block :: bash | ||||
| 36 | |||||
| 37 | $ cd /www/lostquery.com | ||||
| 38 | |||||
| 39 | |||||
| 40 | #. Create python environment: | ||||
| 41 | |||||
| 42 | .. code-block :: bash | ||||
| 43 | |||||
| 44 | $ virtualenv --no-site-packages virtpy | ||||
| 45 | |||||
| 46 | |||||
| 47 | |||||
| 48 | Install Django | ||||
| 49 | ---------------- | ||||
| 50 | |||||
| 51 | #. Activate the virtualenv: | ||||
| 52 | |||||
| 53 | .. code-block :: bash | ||||
| 54 | |||||
| 55 | $ source virtpy/bin/activate | ||||
| 56 | |||||
| 57 | #. The shell should have changed to *(virtpy) $* . Run this command to install | ||||
| > | django to the virtualenv: | ||||
| 58 | |||||
| 59 | .. code-block :: bash | ||||
| 60 | |||||
| 61 | $ easy_install django | ||||
| 62 | |||||
| 63 | #. Install any dependencies you will need to the virtualenv: | ||||
| 64 | |||||
| 65 | .. code-block :: bash | ||||
| 66 | |||||
| 67 | $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid So | ||||
| > | uth | ||||
| 68 | |||||
| 69 | |||||
| 70 | Setup Django web application | ||||
| 71 | ------------------------------- | ||||
| 72 | |||||
| 73 | #. Get the sourcecode: | ||||
| 74 | |||||
| 75 | .. code-block :: bash | ||||
| 76 | |||||
| 77 | $ svn co http://svn.osqa.net/svnroot/osqa/trunk | ||||
| 78 | |||||
| 79 | #. Configure the application: | ||||
| 80 | |||||
| 81 | .. code-block :: bash | ||||
| 82 | |||||
| 83 | $ cd trunk | ||||
| 84 | |||||
| 85 | $ cp settings_local.py.dist settings_local.py | ||||
| 86 | |||||
| 87 | $ vi settings_local.py | ||||
| 88 | |||||
| 89 | #. You must tell the Django's application what database to use: | ||||
| 90 | |||||
| 91 | .. code-block :: python | ||||
| 92 | |||||
| 93 | DATABASE_NAME = 'databasename' # Or path to database file if using sqli | ||||
| > | te3. | ||||
| 94 | DATABASE_USER = 'username' # Not used with sqlite3. | ||||
| 95 | DATABASE_PASSWORD = 'p@ssw0rd' # Not used with sqlite3. | ||||
| 96 | DATABASE_ENGINE = 'mysql' #mysql, etc | ||||
| 97 | DATABASE_HOST = 'localhost' | ||||
| 98 | DATABASE_PORT = '' | ||||
| 99 | |||||
| 100 | |||||
| 101 | #. Give apache access to a few directories, /trunk/log and /trunk/forum/upfiles: | ||||
| 102 | |||||
| 103 | |||||
| 104 | .. code-block :: bash | ||||
| 105 | |||||
| 106 | $ chmod 777 log | ||||
| 107 | $ chmod 777 forum/upfiles | ||||
| 108 | |||||
| 109 | |||||
| 110 | #. Create the database `(example syntax to create a mySQL database) <http://www. | ||||
| > | foxhop.net/mySQL#create-database>`_: | ||||
| 111 | |||||
| 112 | .. code-block :: bash | ||||
| 113 | |||||
| 114 | $ python manage.py syncdb --all | ||||
| 115 | $ python manage.py migrate forum --fake | ||||
| 116 | |||||
| 117 | |||||
| 118 | Setup Apache2 and mod_wsgi | ||||
| 119 | -------------------------------- | ||||
| 120 | |||||
| 121 | #. Turn on mod_rewrite: | ||||
| 122 | |||||
| 123 | .. code-block :: bash | ||||
| 124 | |||||
| 125 | $ a2enmod rewrite | ||||
| 126 | |||||
| 127 | #. Create a sites-enabled vhost apache2 file: | ||||
| 128 | |||||
| 129 | .. code-block :: bash | ||||
| 130 | |||||
| 131 | $ sudo vi /etc/apache2/sites-enabled/007-lostquery | ||||
| 132 | |||||
| 133 | #. Setup the host file: | ||||
| 134 | |||||
| 135 | .. code-block :: apache | ||||
| 136 | |||||
| 137 | #lostquery.com | ||||
| 138 | |||||
| 139 | #Djangos embeded mod_wsgi | ||||
| 140 | <VirtualHost *:80> | ||||
| 141 | ServerName lostquery.com | ||||
| 142 | ServerAlias www.lostquery.com | ||||
| 143 | ServerAdmin foxhop1@gmail.com | ||||
| 144 | |||||
| 145 | # ReWrite URL to WWW | ||||
| 146 | RewriteEngine On | ||||
| 147 | RewriteCond %{HTTP_HOST} ^www.lostquery.com | ||||
| 148 | RewriteRule (.*) http://lostquery.com$1 [R=301,L] | ||||
| 149 | |||||
| 150 | # Log Files | ||||
| 151 | ErrorLog /var/log/apache2/error-lostquery.log | ||||
| 152 | CustomLog /var/log/apache2/access-lostquery.log combined | ||||
| 153 | |||||
| 154 | # Setup mod_wsgi | ||||
| 155 | WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||||
| 156 | |||||
| 157 | <Directory /www/lostquery.com/mod_wsgi> | ||||
| 158 | Order deny,allow | ||||
| 159 | Allow from all | ||||
| 160 | </Directory> | ||||
| 161 | </VirtualHost> | ||||
| 162 | |||||
| 163 | #. Create the egg-cache directory: | ||||
| 164 | |||||
| 165 | .. code-block :: bash | ||||
| 166 | |||||
| 167 | $ mkdir /www/lostquery.com/mod_wsgi | ||||
| 168 | |||||
| 169 | $ mkdir /www/lostquery.com/mod_wsgi/egg-cache | ||||
| 170 | |||||
| 171 | #. Create the wsgi dispatch file: | ||||
| 172 | |||||
| 173 | .. code-block :: bash | ||||
| 174 | |||||
| 175 | $ vi /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||||
| 176 | |||||
| 177 | #. Setup the wsgi dispatch file: | ||||
| 178 | |||||
| 179 | .. code-block :: python | ||||
| 180 | :linenos: | ||||
| 181 | |||||
| 182 | import os | ||||
| 183 | import sys | ||||
| 184 | sys.stdout = sys.stderr | ||||
| 185 | # Add the virtual Python environment site-packages directory to the path | ||||
| 186 | import site | ||||
| 187 | site.addsitedir('/www/lostquery.com/virtpy/lib/python2.6/site-packages') | ||||
| 188 | |||||
| 189 | |||||
| 190 | # Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages | ||||
| 191 | import os | ||||
| 192 | os.environ['PYTHON_EGG_CACHE'] = '/www/lostquery.com/mod_wsgi/egg-cache' | ||||
| 193 | |||||
| 194 | #If your project is not on your PYTHONPATH by default you can add the follow | ||||
| > | ing | ||||
| 195 | sys.path.append('/www/lostquery.com/trunk') | ||||
| 196 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' | ||||
| 197 | |||||
| 198 | |||||
| 199 | import django.core.handlers.wsgi | ||||
| 200 | application = django.core.handlers.wsgi.WSGIHandler() | ||||
| 201 | |||||
| 202 | |||||
| 203 | |||||
| 204 | |||||
| 205 | |||||
| 206 | Reset apache2 | ||||
| 207 | ================= | ||||
| 208 | |||||
| 209 | #. Reset apache2 so that it reads in the new config files: | ||||
| 210 | |||||
| 211 | .. code-block :: bash | ||||
| 212 | |||||
| 213 | $ sudo service apache2 restart | ||||
| 214 | |||||
| empty | rev 1 | ||||
|---|---|---|---|---|---|
| t | t | 1 | Django virtualenv apache2 mod_wsgi | ||
| 2 | =============================================== | ||||
| 3 | |||||
| 4 | Steps taken to deploy a Django web application (osqa.net) in a virtualenv, usin | ||||
| > | g apache2, and mod_wsgi. | ||||
| 5 | |||||
| 6 | **Why use wsgi and virtualenv?** | ||||
| 7 | |||||
| 8 | * I also host pylons applications using mod_wsgi and apache2 and mod_python is | ||||
| > | incompatible. | ||||
| 9 | * mod_wsgi is faster. | ||||
| 10 | * I don't want my public site-packages library conflicting with other applicati | ||||
| > | ons. | ||||
| 11 | |||||
| 12 | .. contents:: | ||||
| 13 | |||||
| 14 | |||||
| 15 | Setup virtualenv | ||||
| 16 | ------------------ | ||||
| 17 | |||||
| 18 | #. Install Setup tools: | ||||
| 19 | |||||
| 20 | .. code-block :: bash | ||||
| 21 | |||||
| 22 | $ sudo apt-get install python-setuptools | ||||
| 23 | |||||
| 24 | #. Install virtualenv: | ||||
| 25 | |||||
| 26 | .. code-block :: bash | ||||
| 27 | |||||
| 28 | $ sudo easy_install virtualenv | ||||
| 29 | |||||
| 30 | |||||
| 31 | Create virtualenv | ||||
| 32 | --------------------- | ||||
| 33 | #. Move to the directory where you want to create the virtualenv: | ||||
| 34 | |||||
| 35 | .. code-block :: bash | ||||
| 36 | |||||
| 37 | $ cd /www/lostquery.com | ||||
| 38 | |||||
| 39 | |||||
| 40 | #. Create python environment: | ||||
| 41 | |||||
| 42 | .. code-block :: bash | ||||
| 43 | |||||
| 44 | $ virtualenv --no-site-packages virtpy | ||||
| 45 | |||||
| 46 | |||||
| 47 | |||||
| 48 | Install Django | ||||
| 49 | ---------------- | ||||
| 50 | |||||
| 51 | #. Activate the virtualenv: | ||||
| 52 | |||||
| 53 | .. code-block :: bash | ||||
| 54 | |||||
| 55 | $ source virtpy/bin/activate | ||||
| 56 | |||||
| 57 | #. The shell should have changed to *(virtpy) $* . Run this command to install | ||||
| > | django to the virtualenv: | ||||
| 58 | |||||
| 59 | .. code-block :: bash | ||||
| 60 | |||||
| 61 | $ easy_install django | ||||
| 62 | |||||
| 63 | #. Install any dependencies you will need to the virtualenv: | ||||
| 64 | |||||
| 65 | .. code-block :: bash | ||||
| 66 | |||||
| 67 | $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid So | ||||
| > | uth | ||||
| 68 | |||||
| 69 | |||||
| 70 | Setup Django web application | ||||
| 71 | ------------------------------- | ||||
| 72 | |||||
| 73 | #. Get the sourcecode: | ||||
| 74 | |||||
| 75 | .. code-block :: bash | ||||
| 76 | |||||
| 77 | $ svn co http://svn.osqa.net/svnroot/osqa/trunk | ||||
| 78 | |||||
| 79 | #. Configure the application: | ||||
| 80 | |||||
| 81 | .. code-block :: bash | ||||
| 82 | |||||
| 83 | $ cd trunk | ||||
| 84 | |||||
| 85 | $ cp settings_local.py.dist settings_local.py | ||||
| 86 | |||||
| 87 | $ vi settings_local.py | ||||
| 88 | |||||
| 89 | #. You must tell the Django's application what database to use: | ||||
| 90 | |||||
| 91 | .. code-block :: python | ||||
| 92 | |||||
| 93 | DATABASE_NAME = 'databasename' # Or path to database file if using sqli | ||||
| > | te3. | ||||
| 94 | DATABASE_USER = 'username' # Not used with sqlite3. | ||||
| 95 | DATABASE_PASSWORD = 'p@ssw0rd' # Not used with sqlite3. | ||||
| 96 | DATABASE_ENGINE = 'mysql' #mysql, etc | ||||
| 97 | DATABASE_HOST = 'localhost' | ||||
| 98 | DATABASE_PORT = '' | ||||
| 99 | |||||
| 100 | |||||
| 101 | #. Give apache access to a few directories, /trunk/log and /trunk/forum/upfiles: | ||||
| 102 | |||||
| 103 | |||||
| 104 | .. code-block :: bash | ||||
| 105 | |||||
| 106 | $ chmod 777 log | ||||
| 107 | $ chmod 777 forum/upfiles | ||||
| 108 | |||||
| 109 | |||||
| 110 | #. Create the database `(example syntax to create a mySQL database) <http://www. | ||||
| > | foxhop.net/mySQL#create-database>`_: | ||||
| 111 | |||||
| 112 | .. code-block :: bash | ||||
| 113 | |||||
| 114 | $ python manage.py syncdb --all | ||||
| 115 | $ python manage.py migrate forum --fake | ||||
| 116 | |||||
| 117 | |||||
| 118 | Setup Apache2 and mod_wsgi | ||||
| 119 | -------------------------------- | ||||
| 120 | |||||
| 121 | #. Turn on mod_rewrite: | ||||
| 122 | |||||
| 123 | .. code-block :: bash | ||||
| 124 | |||||
| 125 | $ a2enmod rewrite | ||||
| 126 | |||||
| 127 | #. Create a sites-enabled vhost apache2 file: | ||||
| 128 | |||||
| 129 | .. code-block :: bash | ||||
| 130 | |||||
| 131 | $ sudo vi /etc/apache2/sites-enabled/007-lostquery | ||||
| 132 | |||||
| 133 | #. Setup the host file: | ||||
| 134 | |||||
| 135 | .. code-block :: apache | ||||
| 136 | |||||
| 137 | #lostquery.com | ||||
| 138 | |||||
| 139 | #Djangos embeded mod_wsgi | ||||
| 140 | <VirtualHost *:80> | ||||
| 141 | ServerName lostquery.com | ||||
| 142 | ServerAlias www.lostquery.com | ||||
| 143 | ServerAdmin foxhop1@gmail.com | ||||
| 144 | |||||
| 145 | # ReWrite URL to WWW | ||||
| 146 | RewriteEngine On | ||||
| 147 | RewriteCond %{HTTP_HOST} ^www.lostquery.com | ||||
| 148 | RewriteRule (.*) http://lostquery.com$1 [R=301,L] | ||||
| 149 | |||||
| 150 | # Log Files | ||||
| 151 | ErrorLog /var/log/apache2/error-lostquery.log | ||||
| 152 | CustomLog /var/log/apache2/access-lostquery.log combined | ||||
| 153 | |||||
| 154 | # Setup mod_wsgi | ||||
| 155 | WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||||
| 156 | |||||
| 157 | <Directory /www/lostquery.com/mod_wsgi> | ||||
| 158 | Order deny,allow | ||||
| 159 | Allow from all | ||||
| 160 | </Directory> | ||||
| 161 | </VirtualHost> | ||||
| 162 | |||||
| 163 | #. Create the egg-cache directory: | ||||
| 164 | |||||
| 165 | .. code-block :: bash | ||||
| 166 | |||||
| 167 | $ mkdir /www/lostquery.com/mod_wsgi | ||||
| 168 | |||||
| 169 | $ mkdir /www/lostquery.com/mod_wsgi/egg-cache | ||||
| 170 | |||||
| 171 | #. Create the wsgi dispatch file: | ||||
| 172 | |||||
| 173 | .. code-block :: bash | ||||
| 174 | |||||
| 175 | $ vi /www/lostquery.com/mod_wsgi/dispatch.wsgi | ||||
| 176 | |||||
| 177 | #. Setup the wsgi dispatch file: | ||||
| 178 | |||||
| 179 | .. code-block :: python | ||||
| 180 | :linenos: | ||||
| 181 | |||||
| 182 | import os | ||||
| 183 | import sys | ||||
| 184 | sys.stdout = sys.stderr | ||||
| 185 | # Add the virtual Python environment site-packages directory to the path | ||||
| 186 | import site | ||||
| 187 | site.addsitedir('/www/lostquery.com/virtpy/lib/python2.6/site-packages') | ||||
| 188 | |||||
| 189 | |||||
| 190 | # Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages | ||||
| 191 | import os | ||||
| 192 | os.environ['PYTHON_EGG_CACHE'] = '/www/lostquery.com/mod_wsgi/egg-cache' | ||||
| 193 | |||||
| 194 | #If your project is not on your PYTHONPATH by default you can add the follow | ||||
| > | ing | ||||
| 195 | sys.path.append('/www/lostquery.com/trunk') | ||||
| 196 | os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' | ||||
| 197 | |||||
| 198 | |||||
| 199 | import django.core.handlers.wsgi | ||||
| 200 | application = django.core.handlers.wsgi.WSGIHandler() | ||||
| 201 | |||||
| 202 | |||||
| 203 | |||||
| 204 | |||||
| 205 | |||||
| 206 | Reset apache2 | ||||
| 207 | ================= | ||||
| 208 | |||||
| 209 | #. Reset apache2 so that it reads in the new config files: | ||||
| 210 | |||||
| 211 | .. code-block :: bash | ||||
| 212 | |||||
| 213 | $ sudo service apache2 restart | ||||
| 214 | |||||
Remarkbox