By nitipit on Tag:

Last Update

2010-3-21

สิ่งที่อยากจะเขียน

  • วิธีการ deploy django บน apache server หรือ cherokee
  • การวางตำแหน่ง file ของ Gateway Interface เช่น wsgi (เคยอ่านเจอว่าเพื่อความปลอดภัยของเว็บไซต์ครับ)
  • การ config file ต่างๆ เช่น apache.wsgi, settings.py, httpd.conf, hosts
  • การจัดวาง โครงสร้าง folder ต่างๆ ของ Project
  • config wsgi โดยตั้งค่าให้สามารถเรียก module ของ 3-rd application มาใช้ได้ โดยไม่ได้ใช้วิธี python setup install (ลาก folder มาวางเหมือน php) เพื่ออิสระในการเลือก library ของแต่ละโปรเจค โดยไม่ต้อง Setup ที่ Server

Environment ที่ทำอยู่

Linux, Fedora-12

อยากจะแจ้งให้ทราบ

ตัวผมเองยังไม่เคยทำเป็น public website, ปกติทำแค่เป็น web app ที่ใช้ภายในสำนักงาน, ถ้าใครมีความเห็นแล้วให้คำแนะนำผมบ้าง จะขอบคุณมากครับ

เขียนทำไม

เพราะกำลังทำอยู่ แล้วก็อยากจะแชร์, แลกเปลี่ยนความคิดเห็นกับคนอื่น, แล้วก็เก็บไว้เป็นอ่านเองครับ เพราะทำไปนานๆ แล้วชอบลืม >_<

Concept

Alt text

mod_wsgi

จะแบ่งออกเป็น 2 ส่วนหลักๆ ครับ คือ

  1. สำหรับตัว Web Application ของเรา : apache.wsgi, settings.py
  2. สำหรับ Apache Web Server : httpd.conf

Web Application

โครงสร้าง

/home/user/project/my_project/
├── cgi
├── file
├── lib
├── media
└── project
คำอธิบาย (ดูตามรูปภาพใน Concept ด้านบนประกอบ)
./cgi : CGI > สำหรับเก็บ cgi script
./lib : Library,3rd-Application > สำหรับ 3-rd Application ครับ เช่นพวก module ต่างๆ ที่คนอื่นเขียนไว้
./media : StaticMedia > สำหรับเก็บ Static File ต่างๆ ครับ เช่น javascript, css ของ Application
./project : App > django-project ที่เราสร้างขึ้น
./file : Pre-Processed Media > สำหรับเก็บ pre-process file

Configuration

./cgi/apache.wsgi
จะเป็น Interface ระหว่าง Apache กับ App ของเรา โดยไฟล์นี้จะวางไว้ที่ไหนก็ได้ แต่ apache ต้องสามารถ access เข้าไปใช้งานได้ครับ แล้วค่อยไป set ค่าใน WSGIScriptAlias ใน VirtualHost ให้ link มายังไฟล์นี้ ตัวอย่างนี้เลือกที่จะวางไว้ที่ ./cgi/apache.wsgi ใน my_project

#/home/user/project/my_project/cgi/apache.wsgi
import os
import sys

sys.path.append('/home/user/project/my-project')
sys.path.append('/home/user/project/my-project/lib') 
# set environment ตรงนี้ ทำให้สามารถนำ 3-rd module มาวางไว้ใน ./lib
# แล้วสามารถเรียกใช้งานได้เลย (เอา django มาวางในนี้เลยก็ได้ครับ)

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'

import django.core.handlers.wsgi.WSGIHandler()

./project/manage.py
แก้ไข manage.py นิดนึง เพื่อเพิ่ม path ไปที่ ./lib ของ project โดยใช้ sys.path.append()

#!/usr/bin/env python
from django.core.management import execute_manager
import sys # << เพิ่ม
try:
    import settings # Assumed to be in the same directory.
except ImportError:
    import sys

    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory 
containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, 
passing it your settings module.\n(If the file settings.py does indeed exist, 
it's causing an ImportError somehow.)\n" % __file__)
    sys.exit(1)

sys.path.append('/home/nitipit/project/plaza-project/lib') # << เพิ่ม

if __name__ == "__main__":
    execute_manager(settings)

ใน Fedora 12 ผมเจอปัญหาตรงที่ถ้านำไฟล์นี้ไว้ภายใต้ /home/user/ แล้ว apache จะไม่สามารถ access เข้าใช้งานได้ เพราะ /home/user มี permission เป็น 700 อยู่ให้ chmod 711 ก่อน

Apache Server

Configuraion

virtualhost

<VirtualHost *:80>
    ServerAdmin admin@project
    ServerName project
    ErrorLog logs/project-error_log
    CustomLog logs/project-access_log common

    Alias /media/ /home/user/project/my_project/media/
    WSGIScriptAlias / /home/user/project/my_project/cgi/apache.wsgi
</VirtualHost>

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand