1. 程式人生 > >Apache + Django環境搭建

Apache + Django環境搭建

    環境:Macbook,conda,python2.7

1、安裝mod_wsgi模組

    通過pip安裝:pip install mod_wsgi。網路上很多教程說通過homebrew安裝或者通過原始碼安裝,實測失敗。

    安裝後的路徑為:/Users/XXX/anaconda3/envs/djg/lib/python2.7/site-packages/mod_wsgi

2、Apache配置

    Mac系統自帶Apache,執行如下命令啟動Apache:

sudo apachectl start

    在瀏覽器訪問localhost,提示“It works!”說明啟動成功。

    (1)配置wsgi支援

    開啟配置檔案:/private/etc/apache2/httpd.conf,在LoadModule部分新增下面的配置:其中,若是不知道mod_wsgi-py27.so的具體路徑,可以在conda的envs中進行搜尋

LoadModule wsgi_module /Users/XXX/anaconda3/envs/djg/lib/python2.7/site-packages/mod_wsgi/server/mod_wsgi-py27.so

    (2)配置虛擬主機

    主要目的是方便多個網站的管理

    在httpd.conf中,取消如下配置的註釋 

Include /private/etc/apache2/extra/httpd-vhosts.conf

    開啟/etc/apache2/extra/httpd-vhosts.conf檔案,修改為:

include /private/etc/apache2/extra/vhosts/localhost.conf
include /private/etc/apache2/extra/vhosts/dev.mysite.com.conf

    這裡一行就是一個站點的配置檔案,下面為這些站點編輯conf檔案:

    在extra下新建目錄vhosts

sudo mkdir -p /etc/apache2/extra/vhosts/

    在 vhost目錄下新建localhost.conf和dev.mysite.com.conf檔案

    localhost.conf:

<VirtualHost *:80>
    DocumentRoot "/Users/xx/Sites/localhost"
    ServerName localhost
    ErrorLog "/Users/xx/Sites/logs/localhost-error_log"
    CustomLog "/Users/xx/Sites/logs/localhost-access_log" common
    <Directory "/Users/xx/Sites/localhost">
            Require all granted
    </Directory>
</VirtualHost>

    dev.mysite.com.conf:(這是真正要建立的站點的配置檔案,Apache就是根據這個檔案來部署我們的站點)

<VirtualHost *:80>

#    LogLevel info

    ServerName dev.mysite.com
    ServerAdmin [email protected]

    # Static files
    DocumentRoot "/Users/XX/Sites/mysite.com"
    Alias /static/ /Users/XX/Sites/mysite.com/static/

    <Directory "/Users/XX/Sites/mysite.com/mysite">
        Require all granted
    </Directory>

    <Directory "/Users/XX/Sites/mysite.com/static">
        Order deny,allow
        Allow from all
    </Directory>

    # WGSI configuration
    WSGIDaemonProcess mysite.com processes=2 threads=15 display-name=%{GROUP} python-path=/Users/XX/Sites/mysite.com/mysite/:/Users/XX/anaconda3/envs/djg/lib/python2.7/site-packages

    WSGIProcessGroup mysite.com

    WSGIScriptAlias / /Users/XX/Sites/mysite.com/mysite/mysite/wsgi.py

    <Directory "/Users/XX/Sites/mysite.com/mysite/mysite">
        <Files wsgi.py>
            Order allow,deny
            Allow from all
        </Files>
    </Directory>

    #log dir
    ErrorLog "/Users/XX/Sites/logs/localhost-error_log"
	CustomLog "/Users/XX/Sites/logs/localhost-access_log" common

</VirtualHost>

    其中,ServerName是站點的域名,想要通過域名訪問,還需要對本機的/etc/hosts進行修改,加入一行:

127.0.0.1       dev.mysite.com

    static部分是靜態檔案的存放目錄(css、fonts、img、js等),下面還需修改setting的配置,使django執行collectstatic時指向該目錄

    <Directory>部分是設定訪問許可權的

    WGSI configuration部分設定wsgi的路徑,其中WSGIScriptAlias指向django專案的wsgi.py檔案

    log部分設定log的目錄,用以檢視錯誤日誌

    (3)測試配置檔案,看是否有語法錯誤、路徑錯誤等

apachectl configtest

    提示Syntax OK,說明沒有錯誤 

3、新建django專案

    在~目錄下新建一個Sites目錄,用以存放所有的站點檔案,在Sites目錄下新建logs目錄,存放日誌資訊,再在其中建一個mysite.com目錄作為我們的測試站點,在其中新建一個static檔案用以存放靜態檔案。

    在mysite.com目錄中,執行如下命令,新建一個django專案,專案名稱為mysite

django-admin.py startproject mysite

    新建成功後,mysite.com下多了一個資料夾mysite,進入該目錄,執行如下命令,新建一個app,名稱為index

python manage.py startapp index

    進入index資料夾,新建一個templates目錄,在templates目錄下新建index.html檔案,內容不重要,只要是正確的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>Test</title>
    </head>
    
    <body>
    	Hello apache and django
    </body>
</html>

    修改mysite/index/views.py,新增如下函式:

def index(request):
	return render(request, 'index.html')

    開啟mysite/mysite/settings.py,修改如下部分:

#新增允許訪問的主機,'*'表示所有主機。如果沒新增部署後將會拒絕訪問
ALLOWED_HOSTS = ['*']
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #新增一行
    'index',
]
STATIC_URL = '/static/'
#新增一行,指示static檔案的存放位置
STATIC_ROOT = '/Users/XX/Sites/mysite.com/static'

    修改mysite/mysite/urls.py,新增url如下:

from django.conf.urls import url
from django.contrib import admin
from index import views as index_view

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/', index_view.index),
]

    在mysite目錄下執行如下程式碼,收集靜態檔案    

python manage.py collectstatic

4、測試

    重啟apahce

sudo apachectl graceful

    在瀏覽器位址列輸入地址http://dev.mysite.com/index,訪問結果如下,說明環境搭建成功

    

    如果出錯,開啟Sites/logs目錄下的相關log檔案檢視錯誤日誌

5、擴充套件

    在以後新建專案時,需要做的工作如下:

    (1)在/etc/apache2/extra/vhosts/下新建一個conf檔案,並進行相關配置

    (2)在/etc/apache2/extra/httpd-vhosts.conf檔案中,include(1)中的conf檔案

    (3)新建django專案,進行站點開發

    (4)重新載入Apache配置:sudo apachectl graceful