2011年1月24日 星期一

Django 與 Ubuntu 10.04 server 使用筆記

記得 2009 年年底,我也曾接觸 Django 一陣子,但久而沒碰,等於砍掉重練。最近被派去管一台機器,順手筆記一下到底安裝了啥東西。大學時期,每個人都搶著管機器,想說要有個經驗,碩班後,大概越來不想管機器,而工作之後呢?管機器根本就是個吃力不討好的事,做得好沒人稱讚也不會列入考績,做差會被抱怨啊 XD 所以啊,工作後能不管機器還是不要管,甚至一些案子都忘了規劃管機器的人力費用,也有可能沒規畫架設機器的時間。說完題外話 XD 來回顧一下以前的筆記:



以前我比較熟 FreeBSD,現在?大概沒有一項熟,工作上大概都用 Ubuntu ,目前是用 Ubuntu 10.04 64-bit server 。


光碟安裝完系統後,系統更新:


$ sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
$ sudo vim /etc/apt/source
:%s/us.archive/tw.archive
$ sudo apt-get update


安裝 denyhosts ,可用來阻擋惡意 try 帳密的 IP


$ sudo apt-get install denyhosts
$ sudo touch /etc/hosts.deny
$ sudo touch /etc/hosts.allow
$ sudo /etc/init.d/denyhosts restart


也可以把自己常用的 IP 寫在 /etc/hosts.allow 以防止不小心擋掉自己


sshd: MyIP1, MyIP2, MyIP3


在新增網卡後的設定,此為 VirtualBox 上安裝 eth1 網卡的例子


查看網卡有沒安裝好


$ dmesg | grep eth


若看不到可以移掉下述檔案並重開機偵測


$ sudo mv /etc/udev/rules.d/70-persistent-et.rules /etc/udev/rules.d/70-persistent-et.rules.old 


若 ifconfig 看不到但 dmesg 可以查看到,那就要設定  /etc/network/interfaces ,此例為設定 DHCP 方式


$ sudo vim /etc/network/interfaces
auto eth1
iface eth1 inet dhcp


$ sudo /etc/init.d/networking restart


安裝 MySQL、Apache Web Server、PHP


$ sudo apt-get install mysql-server
$ sudo apt-get install apache2
$ sudo apt-get install php5


設定 Apache 相關


啟用 https 其及 ssl 模組


$ sudo a2enmod ssl
$ sudo a2ensite default-ssl


啟用某些設定檔,如 /etc/apache2/site-available/default_setting


$ sudo a2ensite default_setting


設定完重新啟動


$ sudo /etc/init.d/apache restart


安裝 X-Window,有些環境一開始只可以本機端登入,有些 Web service 測試就需要先安裝一下 X-Window 啦,不然只能用 telnet localhost port 跟 GET / 測試,還滿辛苦的,也可以安裝 lynx 這種工具來使用


$ sudo apt-get ubuntu-desktop


設定開機預設不要進 X-Window (目前還待測試,有點怪,機器跑起來還必須透過 ALT+ F1, ... 切換到其他的來使用)


$ vim /etc/init/gdm.conf 
stop on runlevel [0216]
$ sudo service gdm stop


簡易 MySQL 語法,建立帳號、設定資料庫權限


$ mysql -u root -p
mysql> CREATE DATABASES mydb;
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL ON mydb.* TO 'user'@'localhost';
mysql> FLUSH PRIVILEGES;


$ mysql -u user -p
mysql> USE mydb;
mysql> CREATE TABLE my_table ( int x , int y );
mysql> INSERT INTO my_table (x,y) values( 1, 2 );
mysql> DESC my_table;
mysql> SELECT * FROM my_table;
mysql> DELETE FROM my_table WHERE x=1;
mysql> DROP TABLE my_table;


設定 utf8 相關


$ vim /etc/mysql/my.cnf

[client]
default-character-set = utf8

[mysqld]
default-character-set=utf8
init_connect='SET NAMES utf8'


如果一開始不是 UTF-8 的話,又跑了服務一陣子,那大概就親自去改


$ mysql -u user -p 
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (x86_64) using readline 6.1

Connection id:          252
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         5.1.41-3ubuntu12.9 (Ubuntu)
Protocol version:       10
Connection:             Localhost via UNIX socket
Client characterset:    utf8
Server characterset:    utf8
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 21 hours 38 min 58 sec


Threads: 3  Questions: 5376  Slow queries: 0  Opens: 267  Flush tables: 1  Open tables: 63  Queries per second avg: 0.68
--------------
mysql> ALTER DATABASE mydb CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> USE mydb;
mysql> SHOW CREATE TABLE my_table;
可查看最後的編碼是否為 DEFAULT CHARSET=utf8
mysql> ALTER TABLE my_table CHARACTER SET utf8 COLLATE utf8_general_ci ;


安裝 Django 相關


$ sudo apt-get install libapache2-mod-wsgi python-mysqldb python-setuptool python-django


建立一個 Project


$ cd ~/
$ django-admin startproject myproj
$ cd ~/myproj
$ django-admin startapp myapp



設定 myproj 的 settings.py


DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'mydb' # Or path to database file if using sqlite3.
DATABASE_USER = 'user' # Not used with sqlite3.
DATABASE_PASSWORD = 'password' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''  # Set to empty string for default. Not used with sqlite3.

TIME_ZONE = 'Etc/GMT-8'

INSTALLED_APPS = (
    'myproj.myapp',
)

SYS_TEMP_DIR = '/tmp'


設定 myproj 的 urls.py


from django.conf.urls.defaults import *

urlpatterns = patterns('',
    # Example:
    (r'^myapp', include('myproj.myapp.urls')),
)


設定 myapp 的 model.py


from django.db import models

import datetime

# Create your models here.
class MyTable(models.Model):
        mypath = models.CharField(max_length=255,primary_key=True)
        mydate = models.DateTimeField(default=datetime.date.today,auto_now_add = True)
        myscore = models.IntegerField()


設定 myapp 的 views.py


# -*- coding:utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django import http

from myproj.myapp.models import MyTable

import simplejson
import sys, os, os.path
from time import gmtime, strftime, localtime

from myproj.settings import SYS_TEMP_DIR

def MyTest(request):
        _result = {}

        _name = request.POST.get('name','anonymous') or request.GET.get('name','anonymous')

        #my_time = gmtime()
        my_time = localtime()
        my_date = strftime( "%Y-%m-%d" , my_time )

        save_dir = SYS_TEMP_DIR
        save_file = ''

        if save_dir is None:
                _result['status']='1'
                _result['message']='Internal Error'
                result=simplejson.dumps(_result)
                return http.HttpResponse(str(result))

        if request.FILES and request.FILES['my_file'] :
                upload_file = request.FILES['my_file']
                save_file = str( _name  ) + str( strftime( "_%y_%m_%d_%H_%M_%S" , my_date ) )
                save_path = os.path.join( save_dir , save_file )

                try:   
                        destination = open( save_path , 'wb+' )
                        for chunk in upload_file.chunks():
                                destination.write(chunk)
                        destination.close()
                except:
                      _result['status']='1'
                      _result['message']='Internal Error'
                      result=simplejson.dumps(_result)
                      return http.HttpResponse(str(result))
        else:  
                return render_to_response('my_upload.html' , { 'test':'hello world' } )

        try:   
                db_object = MyTable(mypath=save_file)
                db_object.mydate = my_time
                db_object.save()
        except:
                _result['status']='1'
                _result['message']='Internal Error'
                result=simplejson.dumps(_result)
                return http.HttpResponse(str(result))

        _result['status']='0'
        result=simplejson.dumps(_result)
        return http.HttpResponse(str(result))


設定 myapp 的 urls.py


from django.conf.urls.defaults import *
urlpatterns = patterns('',
        (r'^upload/', 'myproj.myapp.views.MyTest'),
)


建立 templates


$ cd ~/myproj/myapp
$ mkdir templates
$ vim my_upload.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
        <head>
                <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                <title>GPX Upload</title>
        </head>
        <body>
{% if test %}
                <h1>{{ test }}</h1>
{% end if %}
                <form action="." enctype="multipart/form-data" method="post">
                        <input type="file" name="my_file" />
                        <button type="submit">Upload</button>
                </form>
        </body>
</html>



建立 myproj 之 myapp 相關的 db table


$ cd ~/myproj
$ python manage.py syncdb



測試,用瀏覽器連 http://MyIP:8000/myapp/upload/


$ cd ~/myproj
$ python manage.py runserver MyIP:8000



重置 myapp 的 db table


$ cd ~/myproj
$ python manage.py reset myapp


最後,也可以使用 apache 的 mod-wsgi 模組


$ cd ~/myproj
$ mkdir apache
$ vim django.wsgi

import os, sys
sys.path.append('/home/user/myproj')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()


設定 apache 檔


$ vim /etc/apache2/site-available/default

WSGIScriptAlias /
/home/user/myproj/apache/django.wsgi
# WSGIDaemonProcess demo_com user=www-data group=www-data processes=1 threads=10
# WSGIProcessGroup demo_com


$ sudo /etc/init.d/apache restart


沒有留言:

張貼留言