Guest User

Untitled

a guest
Jul 21st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 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.4
  62. sudo aptitude install postgresql-server-dev-8.4 # So we can build python-psycopg2
  63. sudo aptitude install python-psycopg2
  64.  
  65. Setup the user and database we will work with::
  66.  
  67. Reset the password for the ‘postgres’ admin account for the server:
  68. sudo -u postgres psql postgres
  69. \password postgres
  70. # Enter password (twice) and then Ctrl-D to exit
  71. ^D
  72.  
  73. Change the system password for postgres user:
  74.  
  75. sudo passwd -d postgreslisten_addresses = '*'
  76. sudo su postgres -c passwd
  77. su postgres
  78. createuser botland
  79. Shall the new role be a superuser? (y/n) y
  80. createdb -E utf8 --owner=botland botland_botland
  81. echo "ALTER USER botland WITH PASSWORD 'password'" | psql template1
  82. sudo vim /etc/postgresql/8.4/main/postgresql.conf
  83. listen_addresses = '*'
  84. password_encryption = on
  85.  
  86. Edit /etc/postgresql/8.4/main/pg_hba.conf. Change
  87. local all all ident
  88. to
  89. local all all md5
  90.  
  91. Restart postgresql:
  92. sudo /etc/init.d/postgresql-8.4 restart
  93.  
  94. MySQL
  95. -----
  96.  
  97. Let's get the MySQL packages::
  98.  
  99. sudo aptitude install mysql-server-5.0
  100. sudo aptitude install python-mysqldb
  101.  
  102. Setup the user and database we will work with::
  103.  
  104. mysql --user=root -p
  105.  
  106. Once in the console type::
  107.  
  108. CREATE DATABASE botland_botland;
  109. CREATE USER 'botland'@'localhost' IDENTIFIED BY 'password';
  110. GRANT ALL PRIVILEGES ON botland_botland.* TO 'botland'@'localhost';
  111.  
  112. Creating the application environment
  113. ====================================
  114.  
  115. The first step is create a user where we want to run the application::
  116.  
  117. sudo adduser botland
  118.  
  119. Once your application user is created become that user. Next, we need to setup
  120. our virtual Python environment::
  121.  
  122. mkdir ~/virtualenvs
  123. virtualenv --no-site-packages --distribute ~/virtualenvs/botland
  124. source ~/virtualenvs/botland/bin/activate
  125. easy_install pip
  126.  
  127. The last command is important because pip will not work inside the virtual
  128. environment unless installed "locally".
  129.  
  130. You can now install any dependancies that are application specific. These will
  131. be installed in the isolated environment named botland. For purporses of this
  132. article the only dependancy is Django::
  133.  
  134. pip install psycopg2 # This will build psycopg2
  135. pip install Django
  136.  
  137. Or use a requirements file:
  138.  
  139. pip install -U -r requirements.pip
  140.  
  141. The next steps are going to be very application specific. For this article,
  142. we are going to create a new application. However, in your case, you may
  143. already have one. The important bits is that we are going to store the Django
  144. project in the ``~/webapps`` directory. Keep that in mind as you read the rest
  145. of the article such that you can modify paths according to your application.
  146.  
  147. Let's create the application we will use for the rest of the article::
  148.  
  149. mkdir ~/webapps
  150. cd ~/webapps
  151. django-admin.py startproject botland_project
  152.  
  153. Create a ``local_settings.py`` file in the Django project directory with
  154. settings specific to the server. In this case this will consist of database
  155. settings::
  156.  
  157. DATABASE_ENGINE = "postgresql_psycopg2" # change this to "mysql" for MySQL
  158. DATABASE_NAME = "botland_botland"
  159. DATABASE_USER = "botland"
  160. DATABASE_PASSWORD = "password"
  161. DATABASE_HOST = "127.0.0.1"
  162.  
  163. Hook in the ``local_settings.py`` file in to your ``settings.py`` by adding
  164. the following lines to the bottom::
  165.  
  166. try:
  167. from local_settings import *
  168. except ImportError:
  169. pass
  170.  
  171. Let's sync the database::
  172.  
  173. python manage.py syncdb
  174.  
  175. The last thing we need to do for our application is to create a WSGI file that
  176. Apache's mod_wsgi will need to hook into Django. Create ``botland.wsgi`` in a
  177. directory named ``deploy`` in your project::
  178.  
  179. import os
  180. import sys
  181.  
  182. # put the Django project on sys.path
  183. sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
  184.  
  185. os.environ["DJANGO_SETTINGS_MODULE"] = "botland_project.settings"
  186.  
  187. from django.core.handlers.wsgi import WSGIHandler
  188. application = WSGIHandler()
  189.  
  190. Hooking your application into the webserver
  191. ===========================================
  192.  
  193. Let's finish off the article by hooking your application into Apache. Get the
  194. required distribution packages installed::
  195.  
  196. sudo aptitude install apache2
  197. sudo aptitude install libapache2-mod-wsgi
  198.  
  199. We will create a new virtual host for our application. The virtual host will
  200. be ``example.com`` for this article, but change this to a name that you will
  201. want to use. You could just override the default virtual host if you'd like.
  202.  
  203. Create a new file, ``/etc/apache2/sites-available/example.com``::
  204.  
  205. <VirtualHost *:80>
  206. ServerName example.com
  207.  
  208. WSGIDaemonProcess botland-production user=botland group=botland threads=10 python-path=/home/botland/virtualenvs/botland/lib/python2.5/site-packages
  209. WSGIProcessGroup botland-production
  210.  
  211. WSGIScriptAlias / /home/botland/webapps/botland_project/deploy/botland.wsgi
  212. <Directory /home/botland/webapps/botland/deploy>
  213. Order deny,allow
  214. Allow from all
  215. </Directory>
  216.  
  217. ErrorLog /var/log/apache2/error.log
  218. LogLevel warn
  219.  
  220. CustomLog /var/log/apache2/access.log combined
  221. </VirtualHost>
  222.  
  223. Enable the virtual host file we created::
  224.  
  225. cd /etc/apache2/sites-enabled
  226. ln -s ../sites-available/example.com
  227.  
  228. Restart Apache::
  229.  
  230. /etc/init.d/apache2 restart
  231.  
  232. You should now be able to access the application by pointing your web browser
  233. to the virtual host (don't actually try example.com it won't work).
Add Comment
Please, Sign In to add comment