วันศุกร์ที่ 5 เมษายน พ.ศ. 2556

Prepare Python + Django Development Environment


Here are steps I set up Ubuntu for web development using
- MySQL 5.5 + MySQL Workbench + phpMyAdmin
- Apache2
- Python 2.7.3 + Django 1.4.5
- Eclipse + PyDev + Color Theme
- South 0.7.6 (library to manage model sync with database in Django)
- Django Debug Toolbar

Install MySQL 5.5

detail here >> https://help.ubuntu.com/12.04/serverguide/mysql.html
  to install MySQL (root password = admin....)
    sudo apt-get install mysql-server
  to check if MySQL Server is running
    sudo netstat -tap | grep mysql
  to start/stop/restart MySQL
    sudo service mysql stop
    sudo service mysql restart
  to change config file
    config file at /etc/mysql/my.cnf
    after change, restart MySQL
  to change root password
    sudo dpkg-reconfigure mysql-server-5.5

Install MySQL Tuner

detail here >> https://help.ubuntu.com/12.04/serverguide/mysql.html
  to install
    sudo apt-get install mysqltuner
  to run
    mysqltuner

Install MySQL Workbench

detail here >> http://www.upubuntu.com/2012/12/install-mysql-workbench-5244-from-ppa.html?m=0
  to install
    sudo add-apt-repository ppa:olivier-berten/misc
    sudo apt-get update
    sudo apt-get install mysql-workbench
  to run
    mysql-workbench &

Install Apache2

  to install
    sudo apt-get install apache2
  to test
    http://127.0.0.1

  file location
    document root at /var/www
    config file at /etc/apache2/apache2.conf
    additional config at /etc/apache2 directories, e.g.
       /etc/apache2/mods-enabled (for Apache modules),
       /etc/apache2/sites-enabled (for virtual hosts), and
       /etc/apache2/conf.d.
    log file at /var/log/apache2

Install phpMyAdmin

detail here >> https://www.digitalocean.com/community/articles/how-to-install-and-secure-phpmyadmin-on-ubuntu-12-04
  sudo apt-get install phpmyadmin
    Select Apache2 for the server
    Choose YES when asked about whether to Configure the database
  edit /etc/apache2/apache2.conf to add following line
    Include /etc/phpmyadmin/apache.conf
  sudo service apache2 restart << restart Apache2
  test access to http://localhost/phpmyadmin

Install Python 2.7

No need for this step since Python 2.7.3 is installed by default on Ubuntu 12.04

NOTE: Python files at /usr/local/lib/python2.7/

Install Django 1.4

1. install mod_wsgi (for Apache2, similar to mod_php to link Apache with script)
  sudo apt-get install libapache2-mod-wsgi
2. install Python library for install Python package
  sudo apt-get install python-setuptools python-pip
3. install Django
  sudo pip install django==1.4.5
4. test Django
  python
  >>> import django
  >>> django.VERSION
  (1, 4, 5, 'final', 0)

NOTE: django files at /usr/local/lib/python2.7/dist-packages/django/

Install Eclipse

detail here >> http://www.linoob.com/2011/09/starting-with-python-on-eclipse-in-ubuntu/
detail for color theme here >> http://marketplace.eclipse.org/content/eclipse-color-theme#.UeCmfxcW3Zg
detail for template highlight here >> http://eclipse.kacprzak.org/

1. install Eclipse from Ubuntu Software Center (no need to install java before hand)
2. setup Pydev
   2.1 from Eclipse menu > Help > Install New Software > Add button
   2.2 input Name=PydevEnv(or what ever), URL=http://pydev.org/updates then click OK
   2.3 click Works With field, select Pydev from list below, then click Next, Next, and follow the steps
3. configure Pydev
  from Eclipse menu > Window > Preferences > PyDev > Interpreter Python > click AutoConfig > click OK for default selelction
  (no need to select the first one for Pydev)
4. [optional but nice to have] add color theme for eclipse
   4.1 from Eclipse menu > Help > Install New Software > Add button
   4.2 input Name=(what ever), URL=http://eclipse-color-theme.github.com/update then click OK
   4.3 select Eclipse Color Theme from list below, then click Next, Next, and follow the steps.
   4.4 to change color theme, go to menu Window > Preference > General > Appearance > Color Theme (for dark theme I use Sunburst).
5. [optional but nice to have] add highlight for Django template
   5.1 from Eclipse menu > Help > Install New Software > Add button
   5.2 input Name=(what ever), URL= http://eclipse.kacprzak.org/updates then click OK
   5.3 select Django Template Editor from list below, then click Next, Next, and follow the steps.
   5.4 to change settings, go to menu Window > Preference > Django Editor

Install MySQL for Python (MySQL connector for Python programming)

detail here >> http://blog.mattwoodward.com/2012/08/installing-mysql-python-module-on-ubuntu.html
1. (if not installed yet) Install pip
  sudo easy_install pip
2. (just for sure) upgrade pip
  sudo pip install pip --upgrade
3. build the dependencies for the python-mysqldb libraries
  sudo apt-get build-dep python-mysqldb
4. use pip to install the Python MySQL libraries
  sudo easy_install -U distribute
  sudo pip install MySQL-python


Install South (database migration in Django)

detail here >> http://south.readthedocs.org/en/0.7.6/
1. Install South (database migration in Django)
  sudo easy_install South
2. register to Eclipse
  go to menu Window > Preferences > PyDev > Interpreter-Python
  click button 'New Folder' to add
  /usr/local/lib/python2.7/dist-packages/South-0.7.6-py2.7.egg

Install Django Debug Toolbar (provide additional debug information)

detail here
>> https://github.com/django-debug-toolbar/django-debug-toolbar
     very detail in installation/configuration in this official site
>> http://www.packtpub.com/article/django-debug-toolbar
     explain screen image and usage

1. install debug tool bar
  sudo easy_install django_debug_toolbar
2. add 'debug_toolbar.middleware.DebugToolbarMiddleware', to the end of middleware classes in project settings
3. edit INTERNAL_IPS = ('127.0.0.1', ) in my django.global_settings.py
   by default, debug toolbar is displayed only when DEBUG is True and run from IP listed in INTERNAL_IPS.
4. add 'debug_toolbar', to the INSTALLED_APPS in project settings.py
<< optional >>
5. add a tuple called DEBUG_TOOLBAR_PANELS to settings.py to specify the full Python path to panels that are included in toolbar.
    # comment out unnecessary panels or re-order if needed
    DEBUG_TOOLBAR_PANELS = (
        'debug_toolbar.panels.version.VersionDebugPanel',
        'debug_toolbar.panels.timer.TimerDebugPanel',
        'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
        'debug_toolbar.panels.headers.HeaderDebugPanel',
        'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
        'debug_toolbar.panels.template.TemplateDebugPanel',
        'debug_toolbar.panels.sql.SQLDebugPanel',
        'debug_toolbar.panels.signals.SignalDebugPanel',
        'debug_toolbar.panels.logger.LoggingPanel',
    )
6. add a dictionary called DEBUG_TOOLBAR_CONFIG to settings.py to specify additional configuration
    DEBUG_TOOLBAR_CONFIG = {
        'INTERCEPT_REDIRECTS': False,
        'SHOW_TOOLBAR_CALLBACK': None,
        'EXTRA_SIGNALS': ['myproject.signals.MySignal'],
        'HIDE_DJANGO_SQL': False,
        'SHOW_TEMPLATE_CONTEXT': False,
        'TAG': 'div',
        'ENABLE_STACKTRACES' : True,
    }
7. add the debug_toolbar directory to your Python path (so far not needed)

NOTE: The debug toolbar will only display itself if the mimetype of the response is either text/html or application/xhtml+xml and contains a closing tag.

Create Sample Django Project

1. create project folder
  cd /home/somnuk/workspace
  django-admin.py startproject twc
  << ls to see result >>
  somnuk@somnuk-NB:~/workspace$ ls twc
  manage.py  twc
  somnuk@somnuk-NB:~/workspace$ ls twc/twc
  __init__.py  settings.py  urls.py  wsgi.py

2. create file /home/somnuk/workspace/twc/apache/django.wsgi with following content
  NOTE: make sure no indent in this file (indent in Python has meaning)
    import os
    import sys

    sys.path.append('/home/somnuk/workspace/twc')
    os.environ['DJANGO_SETTINGS_MODULE'] = 'twc.settings'
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()

3. create necessary folders
  mkdir /home/somnuk/workspace/twc/static
  mkdir /home/somnuk/workspace/twc/media
  mkdir /home/somnuk/workspace/twc/django-static
  NOTE:
  - corresponding to file twc.conf created in step 4
  - if these folders not exists, Internal Server Error when test in step 6

4. create file /etc/apache2/sites-available/twc.conf with following content
  Alias /robots.txt /home/somnuk/workspace/twc/static/robots.txt
  Alias /favicon.ico /home/somnuk/workspace/twc/static/favicon.ico

  AliasMatch ^/([^/]*\.css) /home/somnuk/workspace/twc/static/styles/$1

  Alias /media/ /home/somnuk/workspace/twc/media/
  Alias /static/ /home/somnuk/workspace/twc/django-static/

  <Directory /home/somnuk/workspace/twc/django-static>
    Order deny,allow
    Allow from all
  </Directory>

  <Directory /home/somnuk/workspace/twc/media>
    Order deny,allow
    Allow from all
  </Directory>

  WSGIScriptAlias / /home/somnuk/workspace/twc/apache/django.wsgi

  <Directory /home/somnuk/workspace/twc/apache>
    Order allow,deny
    Allow from all
  </Directory>

5. enable site
  sudo a2ensite twc.conf
  sudo /etc/init.d/apache2 restart

6. access http://127.0.0.1
  if error, check apache log at /var/log/apache2

ไม่มีความคิดเห็น:

แสดงความคิดเห็น