Advertisement
Guest User

Untitled

a guest
May 5th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. ======================================================
  2. Setting up Django using Apache/mod_wsgi on Ubuntu 8.10
  3. ======================================================
  4.  
  5. This article will cover setting up Django using Apache/mod_wsgi on Ubuntu
  6. 8.10. The article is targeted at a production environment, but keep in mind
  7. this is a more generalized environment. You may have different requirements,
  8. but this article should at least provide the stepping stones.
  9.  
  10. The article will use distribution packages where nesscary. As of 8.10 the
  11. Python packages provided by Ubuntu have reached a stable point, in my opinion
  12. of course.
  13.  
  14. This article will be broken down into small managable chunks. Here is a quick
  15. overview of what will be covered:
  16.  
  17. * setting up global Python tools
  18. * setting up the database (PostgreSQL or MySQL)
  19. * creating the application environment
  20. * hooking your application into the webserver
  21.  
  22. Let's get started.
  23.  
  24. Setting up global Python tools
  25. ==============================
  26.  
  27. Python 2.5 comes pre-installed on Ubuntu 8.10. We will not have to worry about
  28. futzing around getting it installed, even if it was a single command. However,
  29. we want to work with an isoslated environment for our application. This is
  30. encouraged because it will not give you a headache in the future.
  31.  
  32. Let's get the right tools for this::
  33.  
  34. sudo aptitude install python-setuptools
  35. sudo easy_install pip
  36. sudo pip install virtualenv
  37.  
  38. Yes, I do realize that we just used three installers to install three
  39. packages. One day this is will be easier, but I'd say its easy now. We now
  40. have virtualenv (tool for creating virtual Python environments) and pip (tool
  41. for installing Python packages sanely) installed.
  42.  
  43. The last dependancy we should care about is PIL. If you are going to be using
  44. a Django project that relies on ImageField, this dependancy is required::
  45.  
  46. sudo aptitude install python-imaging
  47.  
  48. Setting up the database
  49. =======================
  50.  
  51. This section will only cover PostgreSQL and MySQL. Django has support for
  52. SQLite and Orcale. SQLite will work out of the box (it comes with Python 2.5),
  53. but is not recommended in a production environment. I mentioned Orcale because
  54. we have support (enough said).
  55.  
  56. PostgreSQL
  57. ----------
  58.  
  59. Let's get the PostgreSQL packages::
  60.  
  61. sudo aptitude install postgresql-8.3
  62. sudo aptitude install python-psycopg2
  63.  
  64. Setup the user and database we will work with::
  65.  
  66. su postgres
  67. createuser botland
  68. createdb -E utf8 --owner=botland botland_botland
  69. echo "ALTER USER botland WITH PASSWORD 'password'" | psql template1
  70.  
  71. MySQL
  72. -----
  73.  
  74. Let's get the MySQL packages::
  75.  
  76. sudo aptitude install mysql-server-5.0
  77. sudo aptitude install python-mysqldb
  78.  
  79. Setup the user and database we will work with::
  80.  
  81. mysql --user=root -p
  82.  
  83. Once in the console type::
  84.  
  85. CREATE DATABASE botland_botland;
  86. CREATE USER 'botland'@'localhost' IDENTIFIED BY 'password';
  87. GRANT ALL PRIVILEGES ON botland_botland.* TO 'botland'@'localhost';
  88.  
  89. Creating the application environment
  90. ====================================
  91.  
  92. The first step is create a user where we want to run the application::
  93.  
  94. adduser botland
  95.  
  96. Once your application user is created become that user. Next, we need to setup
  97. our virtual Python environment::
  98.  
  99. mkdir ~/virtualenvs
  100. virtualenv ~/virtualenvs/botland
  101. source ~/virtualenvs/botland/bin/activate
  102. easy_install pip
  103.  
  104. The last command is important because pip will not work inside the virtual
  105. environment unless installed "locally".
  106.  
  107. You can now install any dependancies that are application specific. These will
  108. be installed in the isolated environment named botland. For purporses of this
  109. article the only dependancy is Django::
  110.  
  111. pip install Django
  112.  
  113. The next steps are going to be very application specific. For this article,
  114. we are going to create a new application. However, in your case, you may
  115. already have one. The important bits is that we are going to store the Django
  116. project in the ``~/webapps`` directory. Keep that in mind as you read the rest
  117. of the article such that you can modify paths according to your application.
  118.  
  119. Let's create the application we will use for the rest of the article::
  120.  
  121. mkdir ~/webapps
  122. cd ~/webapps
  123. django-admin.py startproject botland_project
  124.  
  125. Create a ``local_settings.py`` file in the Django project directory with
  126. settings specific to the server. In this case this will consist of database
  127. settings::
  128.  
  129. DATABASE_ENGINE = "postgresql_psycopg2" # change this to "mysql" for MySQL
  130. DATABASE_NAME = "botland_botland"
  131. DATABASE_USER = "botland"
  132. DATABASE_PASSWORD = "password"
  133. DATABASE_HOST = "127.0.0.1"
  134.  
  135. Hook in the ``local_settings.py`` file in to your ``settings.py`` by adding
  136. the following lines to the bottom::
  137.  
  138. try:
  139. from local_settings import *
  140. except ImportError:
  141. pass
  142.  
  143. Let's sync the database::
  144.  
  145. python manage.py syncdb
  146.  
  147. The last thing we need to do for our application is to create a WSGI file that
  148. Apache's mod_wsgi will need to hook into Django. Create ``botland.wsgi`` in a
  149. directory named ``deploy`` in your project::
  150.  
  151. import os
  152. import sys
  153.  
  154. # put the Django project on sys.path
  155. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
  156.  
  157. os.environ["DJANGO_SETTINGS_MODULE"] = "botland_project.settings"
  158.  
  159. from django.core.handlers.wsgi import WSGIHandler
  160. application = WSGIHandler()
  161.  
  162. Hooking your application into the webserver
  163. ===========================================
  164.  
  165. Let's finish off the article by hooking your application into Apache. Get the
  166. required distribution packages installed::
  167.  
  168. sudo aptitude install apache2
  169. sudo aptitude install libapache2-mod-wsgi
  170.  
  171. We will create a new virtual host for our application. The virtual host will
  172. be ``example.com`` for this article, but change this to a name that you will
  173. want to use. You could just override the default virtual host if you'd like.
  174.  
  175. Create a new file, ``/etc/apache2/sites-available/example.com``::
  176.  
  177. <VirtualHost *:80>
  178. ServerName example.com
  179.  
  180. WSGIDaemonProcess botland-production user=botland group=botland threads=10 python-path=/home/botland/virtualenvs/botland/lib/python2.5/site-packages
  181. WSGIProcessGroup botland-production
  182.  
  183. WSGIScriptAlias / /home/botland/webapps/botland_project/deploy/botland.wsgi
  184. <Directory /home/botland/webapps/botland/deploy>
  185. Order deny,allow
  186. Allow from all
  187. </Directory>
  188.  
  189. ErrorLog /var/log/apache2/error.log
  190. LogLevel warn
  191.  
  192. CustomLog /var/log/apache2/access.log combined
  193. </VirtualHost>
  194.  
  195. Enable the virtual host file we created::
  196.  
  197. cd /etc/apache2/sites-enabled
  198. ln -s ../sites-available/example.com
  199.  
  200. Restart Apache::
  201.  
  202. /etc/init.d/apache2 restart
  203.  
  204. You should now be able to access the application by pointing your web browser
  205. to the virtual host (don't actually try example.com it won't work).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement