django-virtualenv-apache-mod_wsgi

JSON

rev 20  |  foxhop  |  1354992446000  |  JSON

rev 19
rev 20
f1Django virtualenv Apache2 mod_wsgif1Django virtualenv Apache2 mod_wsgi
2===============================================2===============================================
nn3 
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.
35
n4 Steps taken to deploy a Django web application (osqa.net) in a virtualenv, usinn6 You should bookmark this page.
>g apache2, and mod_wsgi. 
57
6**Why use wsgi and virtualenv?**8**Why use wsgi and virtualenv?**
79
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.
t9 * mod_wsgi is faster.t11 * mod_wsgi is fast.
10 * I don't want my public site-packages library conflicting with other applicati12 * I don't want my public site-packages library conflicting with other applicati
>ons.>ons.
1113
rev 19  |  foxhop  |  1341522249000  |  JSON

rev 18
rev 19
159          CustomLog /var/log/apache2/access-lostquery.log combined159          CustomLog /var/log/apache2/access-lostquery.log combined
160160
t161          # prevent django from seting static filest161          # Prevent django from serving static files
162          DocumentRoot /www/lostquery.com/trunk/forum162          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 18  |  foxhop  |  1317329911000  |  JSON

rev 17
rev 18
222222
223223
n224Restart Apache2n224Enable the Virtual Host and reload Apache2
225--------------------225------------------------------------------------
226226
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 :: bash229   .. code-block :: bash
230230
tt231    $ a2ensite lostquery
232 
231    $ sudo service apache2 reload233    $ sudo service apache2 reload
232234
rev 17  |  foxhop  |  1317329836000  |  JSON

rev 16
rev 17
131    $ a2enmod rewrite131    $ a2enmod rewrite
132132
n133#. Create a sites-enabled vhost apache2 file:n133#. Create a sites-available vhost apache2 file:
134134
135   .. code-block :: bash135   .. code-block :: bash
136136
t137    $ sudo vi /etc/apache2/sites-enabled/007-lostqueryt137    $ sudo vi /etc/apache2/sites-available/007-lostquery
138138
139#. Setup the host file:139#. Setup the host file:
rev 16  |  fritzvd  |  1317267827000  |  JSON

rev 15
rev 16
229   .. code-block :: bash229   .. code-block :: bash
230230
t231    $ sudo service apache2 restartt231    $ sudo service apache2 reload
232232
rev 15  |  foxhop  |  1309134770000  |  JSON

rev 14
rev 15
142    :linenos:142    :linenos:
143143
n144    #lostquery.comn144     #lostquery.com
145145
n146    #Djangos embeded mod_wsgin146     #Djangos embeded mod_wsgi
147    <VirtualHost *:80>147     <VirtualHost *:80>
148        ServerName lostquery.com148          ServerName lostquery.com
149        ServerAlias www.lostquery.com149          ServerAlias www.lostquery.com
150        ServerAdmin foxhop1@gmail.com150          ServerAdmin admin@admin.com
151151
n152        # ReWrite URL to WWWn152          # ReWrite URL to WWW
153        RewriteEngine On153          RewriteEngine On
154        RewriteCond %{HTTP_HOST} ^www.lostquery.com154          RewriteCond %{HTTP_HOST} ^www.lostquery.com
155        RewriteRule (.*) http://lostquery.com$1 [R=301,L]155          RewriteRule (.*) http://lostquery.com$1 [R=301,L]
156156
n157        # Log Filesn157          # Log Files
158        ErrorLog /var/log/apache2/error-lostquery.log158          ErrorLog /var/log/apache2/error-lostquery.log
159        CustomLog /var/log/apache2/access-lostquery.log combined159          CustomLog /var/log/apache2/access-lostquery.log combined
160160
nn161          # 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_wsgi170          # 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.wsgi172          WSGIScriptAlias / /www/lostquery.com/mod_wsgi/dispatch.wsgi
163173
n164        <Directory /www/lostquery.com/mod_wsgi>n
165         Order deny,allow
166         Allow from all
167        </Directory>
168     </VirtualHost>174     </VirtualHost>
tt175 
176 
169177
170Configure mod_wsgi178Configure mod_wsgi
rev 14  |  foxhop  |  1309133615000  |  JSON

rev 13
rev 14
20   .. code-block :: bash 20   .. code-block :: bash 
2121
n22    $ sudo apt-get install python-setuptoolsn22    $ sudo apt-get install python-setuptools memcached
2323
24#. Install virtualenv:24#. Install virtualenv:
65   .. code-block :: bash65   .. code-block :: bash
6666
n67    $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid Son67    $ easy_install MySQL-python markdown html5lib modwsgideploy python-openid So
>uth>uth python-memcached
6868
6969
87    $ vi settings_local.py87    $ vi settings_local.py
8888
n89#. You must tell the Django's application what database to use:n89#. You must tell the Django's application what database to use.  Also configure 
 >memcached:
90   90   
91   .. code-block :: python91   .. code-block :: python
92 92 
nn93    # database settings
93    DATABASE_NAME     =  'databasename' # Or path to database file if using sqli94    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     =  ''
99100
tt101    # memcached settings (osqa is slow as dirt without this)
102    CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
103 
100104
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 13  |  foxhop  |  1303939136000  |  JSON

rev 12
rev 13
t1Django virtualenv apache2 mod_wsgit1Django virtualenv Apache2 mod_wsgi
2===============================================2===============================================
33
rev 12  |  foxhop  |  1303939107000  |  JSON

rev 11
rev 12
210210
211211
t212Reset apache2t212Restart Apache2
213--------------------213--------------------
214214
rev 11  |  foxhop  |  1303939079000  |  JSON

rev 10
rev 11
1313
1414
n15Setup virtualenvn15Install virtualenv
16------------------16------------------
1717
2929
3030
n31Create virtualenvn31Create 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:
6868
6969
n70Setup Django web applicationn70Install and configure the Django web application
71-------------------------------71----------------------------------------------------
7272
73#. Get the sourcecode:73#. Get the sourcecode:
118118
119119
n120Setup Apache2 n120Configure Apache2 VirtualHost
121--------------------------------121--------------------------------
122122
164     </VirtualHost>164     </VirtualHost>
165165
t166Setup mod_wsgit166Configure mod_wsgi
167--------------------------------167--------------------------------
168168
rev 10  |  foxhop  |  1303938827000  |  JSON

rev 9
rev 10
t No Differences Found t No Differences Found 
rev 9  |  foxhop  |  1303938764000  |  JSON

rev 8
rev 9
118118
119119
n120Setup Apache2 and mod_wsgin120Setup Apache2 
121--------------------------------121--------------------------------
122122
136136
137   .. code-block :: apache137   .. code-block :: apache
nn138    :linenos:
138139
139    #lostquery.com140    #lostquery.com
162        </Directory>163        </Directory>
163     </VirtualHost>164     </VirtualHost>
tt165 
166Setup mod_wsgi
167--------------------------------
164168
165#. Create the egg-cache directory:169#. Create the egg-cache directory:
rev 8  |  foxhop  |  1303938455000  |  JSON

rev 7
rev 8
207207
208Reset apache2208Reset apache2
t209=================t209--------------------
210210
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 7  |  foxhop  |  1303938398000  |  JSON

rev 6
rev 7
108108
109109
t110#. Create the database `(example syntax to create a mySQL database) <http://www.t110#. 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:
111113
112   .. code-block :: bash114   .. code-block :: bash
rev 6  |  foxhop  |  1289340741000  |  JSON

rev 5
rev 6
t1cyYtPp  <a href="http://otzgpccbdlci.com/">otzgpccbdlci</a>, [url=http://ibjrwibt1Django 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 
15Setup 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 
31Create 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 
48Install 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 
70Setup 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 
118Setup 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 
206Reset 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 5  |  unknown  |  1289299539000  |  JSON

rev 4
rev 5
t No Differences Found t No Differences Found 
rev 4  |  unknown  |  1289299537000  |  JSON

rev 3
rev 4
t1gXYsGz  <a href="http://hmtoqnmakhqx.com/">hmtoqnmakhqx</a>, [url=http://qxkomvqt1cyYtPp  <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 3  |  unknown  |  1289287303000  |  JSON

rev 2
rev 3
t No Differences Found t No Differences Found 
rev 2  |  unknown  |  1289287296000  |  JSON

rev 1
rev 2
t1Django virtualenv apache2 mod_wsgit1gXYsGz  <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 
15Setup 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 
31Create 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 
48Install 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 
70Setup 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 
118Setup 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 
206Reset 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 1  |  foxhop  |  1276973965000  |  JSON

empty
rev 1
tt1Django 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 
15Setup 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 
31Create 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 
48Install 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 
70Setup 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 
118Setup 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 
206Reset 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