sudo apt-get update sudo apt-get upgrade
而後html
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文件夾權限python
cd /etc/apache2 sudo nano apache2.conf
<Directory /> Options FollowSymLinks AllowOverride None #Require all denied Allow from all </Directory>
井號是我加的,Alow from all也是加的,改爲這個樣子就是了。mysql
sudo mkdir /home/djangoapps sudo mkdir /home/djangoapps/work
建立Django工程(網頁文件夾)web
cd /home/djangoapps/work
django-admin startproject mysite
建wsgisql
在隨便哪裏建wsgi,好比sudo nano /home/djangoapps/work/mysite/apache/django.wsgi
填入以下內容apache
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"的地方都是對應那個工程的名字。django
建站點設置文件安全
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>
爲了讓內容乾淨,解釋就在外面說了:app
sudo a2ensite mysite sudo service apache2 reload
Django站點已經配置好了,可是這時訪問127.0.0.1看到的是apache頁面。
sudo a2dissite 000-default sudo service apache2 reload
這時就能正常訪問剛建的django站點了。
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站點了。
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.