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.