Ubuntu14.04 Server amd64 配置 Apache+MySQL+Django

写在前面
由于不一样版本的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

而后html

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文件夹权限python

cd /etc/apache2
sudo nano apache2.conf
找到
<Directory />
        Options FollowSymLinks
        AllowOverride None
        #Require all denied
        Allow from all
</Directory>

井号是我加的,Alow from all也是加的,改为这个样子就是了。mysql

 
创建Django工程目录
不建议建在/var/www,若是系统设置问题致使不识别.py为网页文件时,/var/www做为Apache默认Web文件夹,.py源文件将能够被下载而泄漏。
我把文件放在了/home/djangoapps/
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

 

建站点设置文件安全

去/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>

为了让内容干净,解释就在外面说了:app

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.
查看
相关文章
相关标签/搜索