文檔的時效性 平臺環境 軟件環境 目標的目錄配置 主機域名等的設置 ... 這些的不同,造成軟件的部署過程總是磕磕絆絆。為了降低挫折感,本文通過分步驟試驗逐漸實現最終目標。
軟件環境:
ubuntu 12.04 64位 python 2.7.3 django 1.5.1 apache 2.2 mod_wsgi 3.3
目標:
在 ~/firstdj 中建立一個django項目 使用 apache wsgi 方式提供web服務 通過 http://firstdj/ 訪問hello頁面
準備:安裝需要的軟件
這個比較簡單,沒什么特別注意的。 # 安裝 apache2 $ sudo apt-get -y install apache2-mpm-worker apache2-dev # 安裝 python $ sudo apt-get -y install python python-dev python-setuptools # 安裝 python 的輔助軟件 $ sudo easy_install virtualenv virtualenvwrapper pip
實驗一、使用apache2提供靜態頁面
目的: 驗證 apache2的安裝 配置虛擬主機 apache 預備知識 可執行程序 /usr/sbin/apache2 配置文件目錄 /etc/apache2/ 網站(web)文件目錄 /var/www 這個可以配置,修改 /etc/apache2/sites-available/default 這個文件的下面的字段 DocumentRoot /var/www ,比如你改到/var/temp 那么就把這行改成: DocumentRoot /var/temp 配置文件并不是在httpd.conf里面,而是apache2.conf,而這里面并沒有配置所有的東西,如端口是在ports.conf這個文件里面,而網站的根目錄是在 /etc/apache2/sites-available/default 這個文件中。 雖然也有httpd.conf這個文件,但是httpd.conf里面是空的,其實你可以在這里面加一些配置,因為apache2.conf里面會把httpd.conf加到它的文件里面。 操作思路 修改主機名字為 firstdj ,作為域名 禁用系統默認的 default 虛擬站點 建立一個最簡化的虛擬主機,使用 http://firstdj/ 訪問。 具體步驟 修改主機名 通過修改/etc/hostname把主機名改為 firstdj ,為了清晰,進入root賬戶)。 $ sudo su # echo "firstdj" > /etc/hostname # echo -e "n127.0.0.1 firstdj.local firstdjn" >> /etc/hosts # hostname -F /etc/hostname 默認這個時候已經能夠在 http://firstdj/ 訪問了。如果你能夠看到 It works! 頁面,說明 apache2 安裝正常。否則檢查 apache2 是否在運行: $ sudo su # service apache2 status # 查看狀態 # service apache2 start # 啟動 # service apache2 stop # 停止 # service apache2 reload # 重新應用配置文件 # service apache2 restart # 重新啟動進程 配置虛擬主機 雖然這時候能夠訪問 http://firstdj/ ,但實際上是ubuntu系統本身安裝后給的默認配置 $ cd /etc/apache2 # 進入 apache2 的配置目錄 $ ls ./sites-enabled # 查看當前生效的站點 返回 000-default , 這是ubuntu默認啟動的站點 $ sudo su # a2dissite default # 取消默認站點 default 這時候sites-enabled目錄下沒有文件 # service apache2 reload # 使配置生效 現在刷新一下 http://firstdj/ ,應該已經不能訪問了?,F在 /etc/apache2/sites-available/ 目錄下,建立一個名為 firstdj 的文件,為了清晰,我盡量進行了刪減,具體內容如下: <VirtualHost *:80> ServerName firstdj DocumentRoot /var/www </VirtualHost> 配置文件建立完畢,我們讓它生效。 $ sudo su # a2ensite firstdj # 激活 firstdj 站點 # ls /etc/apache2/sites-enabled/ # 查看當前生效的站點 返回 firstdj ,表示只有firstdj站點有效 # apachectl configtest # 檢查一下 apache2 配置文件語法 返回結果: apache2: Could not reliably determine the server\'s fully qualified domain name, using 127.0.0.1 for ServerName Syntax OK # echo -e "nServerName firstdjn" >> /etc/apache2/apache2.conf 在 apache2.conf 中增加主機名后解決報錯問題 # apachectl configtest # 這次結果應該只有 Syntax OK # service apache2 reload 現在又能夠正常訪問 http://firstdj/ 。
實驗二、安裝配置 wsgi 模塊
操作思路 安裝 wsgi 模塊 配置一個簡單的虛擬主機 具體步驟 安裝 mod_wsgi 我為了省事,采用源安裝,如果需要3.4版本,可以采用源碼安裝,參考這里。 wsgi主站 編譯安裝wsgi $ sudo apt-get install libapache2-mod-wsgi #安裝 mod_wsgi $ sudo dpkg -l libapache2-mod-wsgi #查看wsgi的版本 結果: libapache2-mod 3.3-4build1 $ a2enmod wsgi #驗證模塊安裝正常 Module wsgi already enabled 驗證 wsgi 為了驗證wsgi的正常使用,準備手工建一個最簡單的wsgi應用,實際就是一個py腳本。 在 /var/www/目錄下,建立一個名為 main.wsgi 文件,內容如下: def application(environ, start_response): status = \'200 OK\' output = \'Hello World!n試試中文\' response_headers = [(\'Content-type\', \'text/plain\'), (\'Content-Length\', str(len(output)))] start_response(status, response_headers) return [output] 在 /etc/apache2/sites-available/firstdj 中增加一行,同時可以取消 DocumentRoot 配置,修改后內容如下: <VirtualHost *:80> ServerName firstdj WSGIScriptAlias / /var/www/main.wsgi </VirtualHost> 應用配置 $ sudo service apache2 reload 現在刷新 http://firstdj 能夠返回 Hello World! 說明 wsgi 解析正常
實驗三、直接安裝django
在實際生產環境,通常會使用 virtualenv 來支持多版本的python應用,但是同樣也增了 wsgi 的配置復雜性,所以先進行最簡單的試驗。 目的: 在本機安裝 django 配置 apache + wsgi 操作思路 在系統范圍安裝 django (不使用 VirtualEnv) 使用 wsgi 解析 django 跑通 django book 的 helloworld 例子. 具體步驟 安裝 django $ sudo pip install django 系統默認會把 django 安裝到 /usr/local/lib/python2.7/dist-packages 目錄中 在 ~/目錄建立一個 django 項目 $ cd ~ # 進入home目錄 $ django-admin.py startproject firstdj # 建立一個 firstdj 項目 $ cd ~/firstdj $ python manage.py runserver # 啟動django測試服務器 訪問 http://127.0.0.1:8000/ ,能夠看到 django 的 It Worked! 頁面,說明django安裝正常。 配置 wsgi 解析 django 1. 修改 django 項目的 ~/firstdj/firstdj/wsgi.py 文件 去掉注釋后,默認的 wsgi.py 文件內容為: import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstdj.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 增加 firstdj 項目的路徑到系統路徑,修改后完整的 wsgi.py 文件內容如下: import os import sys root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), \'..\')) sys.path.insert(0, os.path.abspath(os.path.join(root_path, \'firstdj\'))) sys.path.insert(0, root_path) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstdj.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 2. 修改 apache2 的配置文件 /etc/apache2/sites-available/firstdj 更換文件中的 main.wsgi 為剛才修改的 wsgi.py , 修改后的內容是: <VirtualHost *:80> ServerName firstdj WSGIScriptAlias / /home/bl/firstdj/firstdj/wsgi.py </VirtualHost> 3. 重新應用 apache2 配置文件 $ sudo service apache2 reload 訪問 http://firstdj/ ,能夠看到 django 的 It Worked! 頁面,說明django安裝正常。 建立一個 django 的 hello world 1. 新建 ~/firstdj/firstdj/views.py 文件,內容如下: # -*- coding: UTF-8 -*- from django.http import HttpResponse def hello(request): return HttpResponse("This is django Hello World") 2. 修改 ~/firstdj/firstdj/urls.py 文件,增加 hello的映射,去掉注釋后內容如下: from django.conf.urls import patterns, include, url urlpatterns = patterns(\'\', url(r\'^$\', \'firstdj.views.hello\'), ) 3. 驗證 hello world 正常 $ cd ~/firstdj/ $ python manage.py runserver 訪問 http://127.0.0.1:8000/ ,能夠看到 This is django Hello World 頁面,說明 hello 配置正常。 4. 重新應用 apache2 配置文件 $ sudo service apache2 reload 訪問 http://firstdj/ ,能夠看到 django 的 This is django Hello World 頁面,說明django 被 wsgi 正常解析了。
實驗四、通過 virtualenv 使用 django
在剛才成功的基礎上,使用 virtualenv 的方式使用 django,核心的問題是修改 ~/firstdj/firstdj/wsgi.py 文件,指定正確的。 目的: 配置 virtualenv 環境下的 django + apache + wsgi virtualenvwrapper 方式下的配置 操作思路 刪除系統級的 django 在 ~/firstdj/ 目錄下,配置 virtualenv 使 http://firstdj/ 生效 使用 virtualenvwrapper 方式 具體步驟 刪除系統 django $ sudo pip uninstall django 在 ~/firstdj/ 目錄下建立 venv 環境 $ cd ~/firstdj/ $ virtualenv venv 現在 ~/firstdj/ 目錄下的結構是: /home/bl/firstdj |---venv | |---local | |---include | |---lib | | |---python2.7 | | | |---site-packages | | | | |---pip-1.3.1-py2.7.egg | | | | | |---EGG-INFO | | | | | |---pip | | | | | | |---commands | | | | | | |---backwardcompat | | | | | | |---vcs | | | |---distutils | |---bin |---firstdj 在新建的 venv 環境下安裝 django $ cd ~/firstdj/ $ ~/firstdj/venv/bin/pip install django 把新建的 venv 環境下的python 包路徑(~/firstdj/venv/lib/python2.7/site-packages/) 加入系統路徑中。 1. 在 ~/firstdj/firstdj/wsgi.py 文件中增加一行,修改后內容如下: import os import sys root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), \'..\')) sys.path.insert(0, os.path.abspath(os.path.join(root_path, \'firstdj\'))) sys.path.insert(0, root_path) sys.path.insert(0, os.path.abspath(os.path.join(root_path, \'venv/lib/python2.7/site-packages/\'))) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstdj.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 2. 重新應用 apache2 配置 $ sudo service apache2 reload 現在訪問 http://firstdj/ ,又能夠看到 django 的 This is django Hello World 頁面。 最后一步就是在 virtualenvwrapper 環境下配置 wsgi , 和普通 virtualenv 的環境的唯一不同是virtualenvwrapper 的python 安裝包路徑默認在 ~/.virtualenvs 目錄下,比如以環境名 ELC 為例,它的安裝包路徑是: /.virtualenvs/ELC/lib/python2.7/site-packages 相應的修改 ~/firstdj/firstdj/wsgi.py 文件,修改后內容如下: import os import sys root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), \'..\')) sys.path.insert(0, os.path.abspath(os.path.join(root_path, \'firstdj\'))) sys.path.insert(0, root_path) sys.path.insert(0, \'/home/bl/.virtualenvs/ELC/lib/python2.7/site-packages/\') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstdj.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() 再次重新應用 apache2 配置后,訪問 http://firstdj/ ,又能夠看到 django 的 This is django Hello World 頁面。 增加django的靜態鏈接 為了能夠訪問 django 的靜態文件,比如各種模版,還需要在 /etc/apache2/sites-available/firstdj 中設置一些別名,最終完整的 apache2 虛擬站點通常是這個樣子: <VirtualHost *:80> ServerAdmin root@firstdj ServerName firstdj Alias /site_media/ /home/bl/firstdj/site_media/ Alias /robots.txt /home/bl/firstdj/site_media/robots.txt Alias /favicon.ico /home/bl/firstdj/site_media/favicon.ico Alias /static/ /home/bl/.virtualenvs/ELC/lib/python2.7/site-packages/django/contrib/admin/static/ CustomLog "|/usr/sbin/rotatelogs /home/bl/firstdj/logs/access.log.%Y%m%d-%H%M%S 5M" combined ErrorLog "|/usr/sbin/rotatelogs /home/bl/firstdj/logs/error.log.%Y%m%d-%H%M%S 5M" LogLevel warn WSGIDaemonProcess firstdj user=bl group=bl processes=1 threads=15 maximum-requests=10000 python-path=/home/bl/.virtualenvs/ELC/lib/python2.7/site-packages/ WSGIProcessGroup firstdj WSGIScriptAlias / /home/bl/firstdj/firstdj/wsgi.py <Directory /home/bl/firstdj/firstdj/site_media> Order deny,allow Allow from all Options -Indexes FollowSymLinks </Directory> </VirtualHost>
網站欄目:django部署
地址分享:http://m.kartarina.com/article20/cpcgco.html
成都網站建設公司_創新互聯,為您提供定制開發、網站設計公司、商城網站、面包屑導航、企業建站、網站排名
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯