Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15-437 Deploying

ThierrySans
November 05, 2013

15-437 Deploying

ThierrySans

November 05, 2013
Tweet

More Decks by ThierrySans

Other Decks in Education

Transcript

  1. What you need Web Host A home for your website

    Domain Name A url for your website
  2. Development Server vs Production Server ➡ Django provides a development

    server ๏ Does not scale with multiple requests (high threading) ✓ We need to setup a production server
  3. Web Hosting Storage How much space do you need? Bandwidth

    How much traffic do you expect? Money How much do you want to spend daily?
  4. Do you want/need to manage ... physical infrastructure? operating system?

    physical server Virtual Private Server Shared Web Host Yes Yes No No
  5. Dedicated Physical Server ✓ Total Control ๏ Maintenance of the

    physical infrastructure ๏ Administration of the operating system ๏ Flexibility
  6. Virtual Private Server (VPS) ๏ Administration of the operating system

    ✓ No maintenance of the physical infrastructure ✓ Flexibility (pay for what you need)
  7. Shared Web Host ✓ No administration of the operating system

    ๏ Cost ๏ Not adequate for specific needs
  8. Choosing the technology • The operating system • The web

    server • The database http://w3techs.com/technologies
  9. Choosing a technology Choosing a technology depends on Specific needs

    Specific applications that your web applications uses Security What you are comfortable to administer
  10. LAMP vs MEAN • Linux • Apache • MySQL •

    PHP ( / Perl / Python) • MongoDB • Express • Angular • Node.js
  11. The big picture Client Side Web Browser Server Side Web

    Server myServlet.java Database
 Server mod_wsgi
  12. Apache and Python - 3 solutions ➡ How Apache runs

    Python code • mod_wsgi • FastCGI, SCGI, AJP (deprecated) • Mod_python (deprecated)
  13. Apache and mod_wsgi Apache http://www.apache.org/ Python WSGI adapter for Apache

    (mod_wsgi) http://code.google.com/p/modwsgi/ Install mod_wsgi on MAC OS $ brew tap homebrew/apache $ brew install mod_wsgi
  14. File Permissions ➡ Modify the permissions for Apache $ sudo

    chown -R :_www tsans ➡ Modify the permissions for Apache $ chmod g+w tsans/tsans $ chmod g+w tsans/tsans/tsans.db $ chmod g+w tsans/tsans/uploads $ find tsans/tsans/uploads -type d -exec chmod g+w {} \; _www for Mac OS www-data for Linux only for SQLite }
  15. Collect static files tsans/ static/ HelloYou/ static/ tsans/ static/ WebDirectory/

    static/ STATIC_ROOT = os.path.join(PROJECT_PATH, '../static/') tsans/settings.py Configure the static root directory Collect the static file $ python manage.py collectstatic
  16. .... LoadModule wsgi_module libexec/apache2/mod_wsgi.so Alias /static/ /project_path/tsans/static/ <Directory /project_path/tsans/static> Order

    deny,allow Allow from all </Directory> WSGIScriptAlias / /project_path/tsans/tsans/wsgi.py WSGIDaemonProcess example.com python-path=/project_path/tsans:/django_path/site-packages WSGIProcessGroup example.com <Directory /project_path/tsans/tsans> <Files wsgi.py> Order deny,allow Allow from all </Files> </Directory> .... /etc/apache2/httpd.conf Restart Apache $ sudo apachectl restart
  17. Reminder : Find your Django Path $ python >> import

    django >> django.__file__ '/usr/local/lib/python2.7/site-packages/django/__init__.pyc' Find your Django installation path my django_path
  18. Disable Django debug message (once it’s working) DEBUG = False

    TEMPLATE_DEBUG = False tsans/settings.py
  19. Configure Apache # Virtual hosts Include /private/etc/apache2/extra/httpd-vhosts.conf /etc/apache2/http.conf Enable virtual

    hosts <VirtualHost *:80> ServerAdmin [email protected] ServerName site1.example.com ServerAlias www.site1.example.com ErrorLog "/var/log/apache2/site1.example.com-error_log" CustomLog "/var/log/apache2/site1.example.com-access_log" common ****** insert Django apache configuration here ******* </VirtualHost> /etc/apache2/extra/httpd-vhosts.conf Configure a virtual host
  20. Spoof your IP address to the domain (for testing) 127.0.0.1

    site1.example.com 127.0.0.1 www. site1.example.com /etc/hosts
  21. Create a MySQL database for Django DROP DATABASE IF EXISTS

    `mydb`; CREATE DATABASE `mydb` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; USE 'mysql'; GRANT ALL PRIVILEGES ON mydb.* TO 'mydb_user'@'localhost' IDENTIFIED BY 'pass4mydb' WITH GRANT OPTION; FLUSH PRIVILEGES; tsans/mysql/init.sql Initialize the MySQL database $ mysql -u root -p < script.sql
  22. Configure Django to use MySQL .... DATABASES = { 'default':

    { 'ENGINE': 'django.db.backends.mysql', 'OPTIONS': { 'read_default_file': os.path.join(BASE_DIR, ‘tsans/mysql/my.cf’), }, } } .... tsans/settings.py [client] database = mydb user = mydb_user password = pass4mydb default-character-set = utf8 tsans/mysql/my.cf
  23. Python Virtualenv ➡ Running multiple Django applications that have different

    dependencies of different versions
 $ pip install virtualenv
  24. Prerequisite Collect all dependencies $ pip freeze > tsans/REQUIREMENTS.txt
 Cleanup

    the file by removing unnecessary packages Django==1.5.2 PIL==1.1.7 pysqlite==2.6.3 wsgiref==0.1.2 tsans/virtualenv/REQUIREMENTS.txt
  25. Create a virtual environment 1. Create a directory my_app $

    mkdir my_app $ cd my_app $ virtualenv my_app 2. Copy (or create) your django app in my_app 3. Activate the environment $ source bin/activate 4. Install the dependencies $ pip install -r tsans/tsans/virtualenv/REQUIREMENTS.txt 5. Configure Apache
  26. Github (static hosting) $ git checkout gh-pages
 $ git merge

    master
 $ git push origin gh-pages $ git branch gh-pages Create the gh-pages branch (first time only) Synchronize gh-pages with master
  27. How to get a domain name? You need to buy

    one from a Domain Name Registrar