把django開發好的項目部署到apache2服務器。 記錄個人配置過程。php
apache,django,mod_wsgi,python版本以下。不一樣版本大同小異。html
ii apache2 2.2.22-1ubuntu1.6 Apache HTTP Server metapackage ii python-django 1.3.1-4ubuntu1.11 High-level Python web development framework ii libapache2-mod-wsgi 3.3-4ubuntu0.1 Python WSGI adapter module for Apache ii python 2.7.3-0ubuntu2.2 interactive high-level object-oriented language (default version)
ubuntu 用apt-get install apache2安裝apache2後,配置文件都在/et/apache2目錄下。python
基本原理web
apache2在啓動的時候自動讀取/etc/apache2/apache2.conf文件的配置信息,不一樣的配置項按功能分佈在不一樣的文件中,而後被Include包含到apache2.conf這個主配置文件中,方便管理。就是說事實上apache2主配置文件只有一個,即apache2.conf,其餘的都是被include進來的。能夠把全部的配置都放在apache2.conf或者任何一個配置文件中,可是劃分到不一樣文件會讓咱們管理起來方便不少,何樂而不爲?apache
該文件是apache的主配置文件,包括三個級別的配置。django
### Section 1: Global Environment #ServerRoot:apache服務器根目錄。主配置文件,日誌都在該目錄。 #注意路徑結束時不要加斜槓,默認是/etc/apache2 #ServerRoot "/etc/apache2" LockFile ${APACHE_LOCK_DIR}/accept.lock #apache服務啓動時在該文件記錄進程ID號 # This needs to be set in /etc/apache2/envvars PidFile ${APACHE_PID_FILE} #鏈接超時,單位秒 Timeout 300 #是否容許持久鏈接 KeepAlive On #持久鏈接時最大請求數 MaxKeepAliveRequests 100 KeepAliveTimeout 5 # prefork MPM <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule> # worker MPM <IfModule mpm_worker_module> StartServers 10 ServerLimit 100 MinSpareThreads 50 MaxSpareThreads 200 ThreadsPerChild 64 MaxClients 6400 MaxRequestsPerChild 0 </IfModule> # event MPM <IfModule mpm_event_module> StartServers 2 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxClients 150 MaxRequestsPerChild 0 </IfModule> # These need to be set in /etc/apache2/envvars User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} #apache服務啓動後在每一個目錄中查找後綴是.htaccess的文件,這些文件做爲附加的配置 AccessFileName .htaccess #下面幾行防止.htaccess和.htpassword文件被web客戶端訪問 <Files ~ "^\.ht"> Order allow,deny Deny from all Satisfy all </Files> #DefaultType是瀏覽器訪問的MIME類型,設置爲None,讓瀏覽器自行解析 DefaultType None # HostnameLookups: Log the names of clients or just their IP addresses # e.g., www.apache.org (on) or 204.62.129.132 (off). HostnameLookups Off #錯誤日誌文件目錄,默認是配置在此,可在<VirtualHost>中重載 #日誌文件取名時以'/'開頭會形成衝突,不以'/'開頭,會默認加上服務器根目錄前綴 #好比"foo.log"會被加上ServerRoor目錄"/etc/apache2"變成"/etc/apache2/foo.log" ErrorLog ${APACHE_LOG_DIR}/error.log #Log日誌記錄哪些信息,取值能夠是: debug, info, notice, warn, error, crit,alert, emerg. LogLevel warn #導入模塊配置文件 Include mods-enabled/*.load Include mods-enabled/*.conf #導入全部用戶配置文件 Include httpd.conf #導入端口監聽配置文件 Include ports.conf # The following directives define some format nicknames for use with # a CustomLog directive (see below). # If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i # LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %O" common LogFormat "%{Referer}i -> %U" referer LogFormat "%{User-agent}i" agent #攔截訪問 <VirtualHost *:80> <Directory /***> Options None FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost> #導入通用配置目錄 Include conf.d/ #導入虛擬主機的配置 Include sites-enabled/
#2.2.22版本的apache2配置目錄
root@web-01:/etc/apache2# tree -L 1 . ├── apache2.conf 全局配置 ├── apache.pem ├── conf.d 通常性配置文件存放地 ├── envvars 環境變量 ├── httpd.conf ├── magic ├── mods-available 已安裝的模塊 ├── mods-enabled 已啓用的模塊 ├── ports.conf httpd服務端口信息 ├── sites-available 可用站點信息 ├── sites-enabled 已經啓用的站點信息,當中的文件是到/etc/apache2/sites-available/ 文件的軟鏈接。
2.4.7版本的apache2配置文件目錄以下ubuntu
字符集瀏覽器
配置apache網站字符編碼, /etc/apache2/conf.d/charset文件取消註釋#AddDefaultCharset UTF-8服務器
ports.confapp
修改端口的話,/etc/apache2/ports.conf文件中修改NameVirtualHost *:80 改成NameVirtualHost x.x.x.x:80
sites-available和sites-enabled目錄
sites-available目錄是存放可用的內容,但不起做用,只有用ln 連到sites-enabled目錄才能夠起做用。sites-enabled目錄存放真正起做用的配置文件,存放一些指向sites-available目錄的符號連接。因此apache上配置了多個虛擬主機,每一個虛擬主機的配置都放在sites-available下,那麼對於虛擬主機的停用和啓用就很是方便。當sites-enabled下創建一個指向某個虛擬主機配置文件的鏈接時,就啓用了它。若是要關閉某個虛擬主機的話,只須要刪除相應的符號連接便可,不用去改配置文件。
mods-available和mods-enabled目錄
和上面說的sites-available、sites-enabled相似,這兩個目錄 是存放apache功能模塊的配置文件和連接的。好比用apt-get install php5安裝了PHP模塊後,在這兩個目錄裏就有了php5.load、php5.conf和指向這兩個文件的連接。安裝mod-wsgi也同樣。這種目錄結構對於啓用、停用某個 Apache模塊是很是方便的。
配置完apache服務器後,重點就是要指定項目根目錄的位置,Ubuntu默認是/var/www。能夠在/etc/apache2/sites-available目錄的default中看到
root@web-01:/etc/apache2/sites-available# cat default <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost>
這個默認的配置在正式發佈時不須要,因此就不要加到sites-enable目錄。須要訪問本身的項目,就須要配置虛擬主機。
這裏配置的虛擬主機是同一臺服務器同時處理超過一個域名。不一樣域名訪問同一服務器(即同一IP)的相同或不一樣目錄。
有效的站點配置都在/etc/apache2/sites-available目錄。
<VirtualHost>和</VirtualHost>是用來分裝一組僅做用於特定虛擬主機的配置。當服務器接收到一個特定虛擬主機的文檔請求時,它會使用封裝在<VirtualHost>配置段的配置。
語法:
<VirtualHost addr[:端口] [addr[:端口]]...>
</VirtualHost>
addr能夠是:
第一步,在/etc/apache2/sites-available中建立xxx.org虛擬主機
root@web-01:/etc/apache2/sites-available# cat xxx.org <VirtualHost *:80>
# 這裏要注意,目錄是工程目錄,根據實際狀況修改,後面的django.wsgi文件須要手動新建 WSGIScriptAlias / /var/www/stack/wsgi/django.wsgi Alias /site_media /var/www/stack/static_resource ServerName www.xxx.org
#Directory對根目錄限制
<Directory /var/www/stack/static_resource> Options None FollowSymLinks #followsymlinks表示是否容許使用符號連接,默認爲禁用 AllowOverride None #標記禁止用戶對目錄配置文件(.htaccess進行修改)重載,普通站點不建議開啓 Order allow,deny #以allow優先處理,未明確說明容許的都拒絕 Allow from all #明確指出容許全部訪問 </Directory> <Directory /var/www/stack/wsgi> #AuthType Basic #AuthName "xxx" #AuthUserFile /var/www/access #Require valid-user Order allow,deny Allow from all </Directory> ErrorLog /etc/apache2/xxx.org.error.log LogLevel warn </VirtualHost>
第二步,在/var/www/stack/wsgi目錄中新建一個django.wsgi。
root@web-01:/var/www/stack/wsgi# cat django.wsgi import os import sys import django.core.handlers.wsgi from django.conf import settings # Add this file path to sys.path in order to import settings sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..')) os.environ['DJANGO_SETTINGS_MODULE'] = 'stack.settings' sys.stdout = sys.stderr STACK_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) sys.path.append(STACK_PATH) DEBUG = True application = django.core.handlers.wsgi.WSGIHandler()
sys.path.insert()的參數是工程目錄的上級目錄,根據工程目錄狀況修改。
os.environ['DJANGO_SETTINGS_MODULE']='stack.settings',這個stack.settings是工程目錄下的setting文件,根據工程目錄名稱不一樣須要修改。
django.wsgi文件名能夠隨便取,可是必定要與虛擬主機中WSGIScriptAlias配置的名稱保持一致。
第三步,上一步配置好的內容只是「有效」虛擬主機,真正發揮效果的話得放到 /etc/apache2/sites-enabled 文件夾下面。因此使用ln命令來創建一對關聯文件:
ln -s /etc/apache2/sites-available/xxx.org /etc/apache2/sites-enabled/xxx.org
第四步,檢查語法,重啓web服務
謹慎起見,在重啓服務前先檢查語法:
sudo apache2ctl configtest
若是沒有錯誤,再重啓apache2
# /etc/init.d/apache2 restart
或者用service apache2 restart命令。如今就能夠經過www.xxx.org來訪問django項目了。
【這裏wiki使用的是mediawiki,用php寫的,因此不用mod-wsgi】
第一步,在/etc/apache2/sites-available中建立mediawiki的虛擬主機
root@web-01:/etc/apache2/sites-available# cat wiki.xxx.org <VirtualHost *:80> Alias /wiki /var/lib/mediawiki Alias /index.php /var/lib/mediawiki/index.php
#域名配置 ServerName wiki.xxx.org <Directory /var/lib/mediawiki/> Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> #log文件配置 ErrorLog /etc/apache2/wiki.xxx.org.error.log LogLevel warn </VirtualHost>
第二步,上一步配置好的內容只是「有效」虛擬主機,真正發揮效果的話得放到 /etc/apache2/sites-enabled 文件夾下面。因此使用ln命令來創建一對關聯文件:
ln -s /etc/apache2/sites-available/wiki.xxx.org /etc/apache2/sites-enabled/wiki.xxx.org
第三步,檢查語法,重啓web服務
謹慎起見,在重啓服務前先檢查語法:
sudo apache2ctl configtest
若是沒有錯誤,再重啓apache2
# /etc/init.d/apache2 restart
或者用service apache2 restart命令。如今就能夠經過wiki.xxx.org/index.php來訪問wiki了。
Note:
apache2中mediawiki的相關配置也能夠這樣配:
/etc/mediawiki/apache.conf文件默認是mediawiki的apache2相關的配置,因此能夠在/etc/apache2/sites-enabled中建一個軟鏈接到/etc/mediawiki/apache.conf。
# ln -s /etc/mediawiki/apache.conf /etc/apache2/sites-enabled/wiki.xxx.org
具體的配置細節就在/etc/mediawiki/apache.conf中了。
重啓apache2時遇到如下問題
AH00557: apache2: apr_sockaddr_info_get() failed for web01
解決辦法:在/etc/hosts中增長對web01的解析。而後重啓apache再也不報錯AH00557。
# cat /etc/hosts 127.0.0.1 localhost web01
AH00557:apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
解決辦法:在apache配置文件apache2.conf中添加ServerName localhost
root@web01:/etc/apache2# vi apache2.conf
ServerName localhost
配置虛擬主機,從通常到特殊。通常出錯都是和配置項加載的順序有關。
對全部的域名或IP都進行攔截,讓其訪問一個不存在的目錄。
<VirtualHost *:80> <Directory /***> Options None FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> </VirtualHost>
只針對具體的某個域名,在<VirtualHost>中進行配置。
對於IncludeOptional sites-enabled/*.conf,必定要注意只導入以.conf結尾的配置文件,不要覺得放在/sites-enabled/目錄下就會被導入。
爲了不錯誤,能夠寫成IncludeOptional sites-enabled/*