寫在前面
因為不同版本的apache等軟件文件夾和配置文件的名稱設置都不盡相同,網上累死累活查了好多個博客就沒一個能成功配出來的。
所以本文也不一定能幫到你,請在確定對自己有用之前不要盲目轉載,以免給后來人制造更多的信息篩選負擔。
Made By:
CSGrandeur
寫本文時各軟件的版本
apache2:2.4
Django:1.6.1
MySQL:5.5.37
Python:2.7.6
apache版本不同,配置文件的地方和名稱可能不同。比如看網上的教程,作死也找不到httpd.conf。。。
各種安裝
先考慮要不要
sudo apt-get update sudo apt-get upgrade
然后
python是預裝的,python --version查看
裝Apache、wsgi、Django、MySQL、MySQLdb
sudo apt-get install apache2 sudo apt-get install libapache2-mod-wsgi sudo apt-get install python-django sudo apt-get install mysql-server mysql-client sudo apt-get install python-mysqldb
設置apache文件夾權限
cd /etc/apache2 sudo nano apache2.conf
找到
<Directory /> Options FollowSymLinks AllowOverride None #Require all denied Allow from all </Directory>
井號是我加的,Alow from all也是加的,改成這個樣子就是了。
建立Django工程目錄
不建議建在/var/www,如果系統設置問題導致不識別.py為網頁文件時,/var/www作為Apache默認Web文件夾,.py源文件將可以被下載而泄漏。
我把文件放在了/home/djangoapps/
sudo mkdir /home/djangoapps sudo mkdir /home/djangoapps/work
創建Django工程(網頁文件夾)
cd /home/djangoapps/work
django-admin startproject mysite
建wsgi
在隨便哪里建wsgi,比如sudo nano /home/djangoapps/work/mysite/apache/django.wsgi
填入如下內容
import os import sys path = '/home/djangoapps/work/mysite' if path not in sys.path: sys.path.insert(0, '/home/djangoapps/work/mysite') os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler()
path是剛創建的工程文件夾位置,對應"mysite"的地方都是對應那個工程的名字。
建站點設置文件
去/etc/apache2/sites-available/建站點設置文件
cd /etc/apache2/sites-available sudo nano mysite.conf
填入如下內容
<VirtualHost *:80> #ServerName hello.djangoserver DocumentRoot /home/djangoapps/work/mysite <Directory /home/djangoapps/work/mysite> Order allow,deny Allow from all </Directory> WSGIDaemonProcess mydjangosite processes=2 threads=15 display-name=%{GROUP} WSGIProcessGroup mydjangosite WSGIScriptAlias / /home/djangoapps/work/mysite/apache/django.wsgi </VirtualHost>
為了讓內容干凈,解釋就在外面說了:
ServerName這里注釋掉了,可以設置域名的,設置這里的話其他地方也要配合設置
兩個路徑都是剛剛建的工程的路徑
關於WSGIxxxx三條設置后面再說。
WSGIScriptAlias 空格 / 空格 /wsgi的路徑。前面那個'/'可以是/xxxx什么的,訪問的方式是 localhost/xxxx,不過我沒成功@_@,所以就一個孤零零的 '/'吧。
啟動站點
別離開/etc/apache2/sites-available
sudo a2ensite mysite sudo service apache2 reload
Django站點已經配置好了,但是這時訪問127.0.0.1看到的是apache頁面。
————————————————————————————————————————
關於端口
在/etc/apache2/sites-available可以看到000-default.conf,這個就是apache默認的站點,對應/var/www/html
如果都用80端口的話,訪問到的是apache,而不是剛建的django。
可以關閉這個站點,
sudo a2dissite 000-default sudo service apache2 reload
這時就能正常訪問剛建的django站點了。
也可以換個端口,在mysite.conf文件中,<VirtualHost *:80>改成<VirtualHost *:xxxx>自己要的端口,比如8000。
然后改ports.conf
cd /etc/apache2 sudo nano ports.conf
看到Listen 80了吧,下面加一行 Listen 8000,就能用8000端口了。
sudo service apache2 reload
這樣127.0.0.1訪問的是apache站點,127.0.0.1:8000訪問的就是我們的django站點了。
關於WSGIxxxx

mod_wsgi 有兩種運行模式, 第一種是嵌入模式,類似於mod_python,直接在apache進程中運行,這樣的好處是不需要另外增加進程,但是壞處也很明顯,所有內存都和apache共享,如果和mod_python一樣造成內存漏洞的話,就會危害整個apache。而且如果apache是用worker mpm,mod_wsgi也就強制進入了線程模式,這樣子對於非線程安全的程序來說就沒法用了。 這種模式下只需要在apache下面設置 WSGIScriptAlias /path /path-to-wsgi 即可生效,對於小型腳本的話,直接用這種模式即可。 第二種是后台模式,類似於FastCGI的后台,mod_wsgi會借apache的外殼,另外啟動一個或多個進程,然后通過socket通信和apache的進程聯系。 這種方式只要使用以下配置即可開啟: #啟動WSGI后台,site1是后台名字 WSGIDaemonProcess site1 processes=2 threads=15 display-name=%{GROUP} #分配當前上下文應該使用哪個WSGI后台,可以放在Location里面指定 WSGIProcessGroup site1 #根據當前上下文的ProcessGroup分配到對應的后台 WSGIScriptAlias /path /path-to-wsgi 后台模式由於是與apache進程分離了,內存獨立,而且可以獨立重啟,不會影響apache的進程,如果你有多個項目(django),可以選擇建立多個后台或者共同使用一個后台。 比如在同一個VirtualHost里面,不同的path對應不同的django項目,可以同時使用一個Daemon: WSGIDaemonProcess default processes=1 threads=1 display-name=%{GROUP} WSGIProcessGroup default WSGIScriptAlias /project1 “/home/website/project1.wsgi” WSGIScriptAlias /project2 “/home/website/project2.wsgi” 這樣子兩個django都使用同一個WSGI后台。 也可以把不同的項目分開,分開使用不同的后台,這樣開銷比較大,但就不會耦合在一起了。 display-name是后台進程的名字,這樣方便重啟對應的進程,而不需要全部殺掉。 WSGIDaemonProcess site1 processes=1 threads=1 display-name=%{GROUP} WSGIDaemonProcess site2 processes=1 threads=1 display-name=%{GROUP} <Location “/project1″> WSGIProcessGroup site1 </Location> WSGIScriptAlias /project1 “/home/website/project1.wsgi” <Location “/project1″> WSGIProcessGroup site2 </Location> WSGIScriptAlias /project2 “/home/website/project2.wsgi” 對於django 1.0以下的版本,由於官方認定不是線程安全的,所以建議使用多進程單線程模式 processes=n threads=1 但是我自己在用django 0.9.6,使用多線程模式在很多項目里面基本都沒有問題,包括在worker模式下面使用mod_python,其實是一樣的道理,呵呵。 升級到django 1.0以后,就可以放心的使用多進程多線程模式了: processes=2 threads=64 這樣子性能會更好。 下面是兩種模式的英文原文: When hosting WSGI applications using mod_wsgi, one of two primary modes of operation can be used. In ‘embedded’ mode, mod_wsgi works in a similar way to mod_python in that the Python application code will be executed within the context of the normal Apache child processes. WSGI applications when run in this mode will therefore share the same processes as other Apache hosted applications using Apache modules for PHP and Perl. An alternate mode of operation available with Apache 2.X on UNIX is ‘daemon’ mode. This mode operates in similar ways to FASTCGI/SCGI solutions, whereby distinct processes can be dedicated to run a WSGI application. Unlike FASTCGI/SCGI solutions however, a separate infrastructure is not needed when implementing the WSGI application and everything is handled automatically by mod_wsgi. Because the WSGI applications in daemon mode are being run in their own processes, the impact on the normal Apache child processes used to serve up static files and host applications using Apache modules for PHP, Perl or some other language is much reduced. Daemon processes may if required also be run as a distinct user ensuring that WSGI applications cannot interfere with each other or access information they shouldn’t be able to.