Apache上部署Django
目前,Apache和mod_python是在生产服务器上部署Django的最健壮搭配。mod_python 是一个在Apache中嵌入Python的Apache插件,它在服务器启动时将Python代码加载到内存中。
Django 需要Apaceh 2.x 和mod_python 3.x支持。
Apache的配置参见:http://www.djangoproject.com/r/apache/docs/
使用mod_python部署
1.为了配置基于 mod_python 的 Django,首先要安装有可用的 mod_python 模块的 Apache。
2.然后应该有一个 LoadModule 指令在 Apache 配置文件中。 它看起来就像是这样:
|
1 |
LoadModule python_module /usr/lib/apache2/modules/mod_python.so |
3.配置Apache,用来定位请求URL到Django应用:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 |
<VirtualHost *:80>
ServerName www.example.com
<Location "/mysite1">
SetHandler python‐program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite1.settings
PythonAutoReload Off
PythonDebug Off
PythonPath "['/var/www/html/mysite1'] + sys.path"
PythonInterpreter mysite1
</Location>
<Location "/mysite2">
SetHandler python‐program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE mysite2.settings
PythonAutoReload Off
PythonDebug Off
PythonPath "['/var/www/html/mysite2'] + sys.path"
PythonInterpreter mysite2
</Location>
[......]
</VirtualHost> |
它告诉 Apache,任何在 / mysite这个路径之后的 URL 都使用 Django 的 mod_python 来处理。 它 将DJANGO_SETTINGS_MODULE 的值传递过去,使得 mod_python 知道这时应该使用哪个配置。
查看 mod_python 文档获得详细的指令列表。
4.重启Apache,查看Http://www.example.com/mysite:
|
1 |
/etc/init.d/apache2 restart |
使用mod_wsgi部署
1.下载安装 mod_wsgi 模块,生成mod_wsgi.so和wsgi.conf
2.在配置中加载模块:
|
1 |
LoadModule python_module /usr/lib/apache2/modules/mod_wsgi.so |
3.修改Apache配置文件httpd.conf
|
1
2
3
4
5
6
7
8
9
10
11
12 |
<VirtualHost *:80>
ServerName www.example
DocumentRoot /var/www/html/mysite
WSGIScriptAlias / /var/www/html/mysite/apache/django.wsgi
<Directory />
Order deny,allow
Allow from all
</Directory>
<Directory /apache>
Allow from all
</Directory>
</VirtualHost> |
4.创建并配置wsgi的配置文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
# filename:mysite.apache.django.wsgi
import os, sys
#Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
print >> sys.stderr, sys.path
shell>chmod a+x django.wsgi |
5.修改Django项目配置文件settings.py:
|
1
2
3
4
5
6
7
8
9
10
11
12
13 |
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mysite',
'USER': 'admin',
'PASSWORD': 'admin123',
'HOST': '127.0.0.1',
'PORT': '3306', }
}
TEMPLATE_DIRS = (
'/var/www/html/mysite/templates',
) |
6.重启Apache,访问http://www.example.com/mysite
|
1 |
/etc/init.d/apache2 restart |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://www.cnblogs.com/oubo/archive/2012/04/06/2434961.html
相关文章
- 刀片服务器是什么 刀片服务器的主要特点 2025-05-27
- 利用FTP和计划任务自动备份网站数据和数据库 2025-05-27
- 服务器技术之硬件冗余技术 2025-05-27
- 服务器是租用还是服务器托管好? 2025-05-27
- 什么是DNS以及它如何影响服务器? 2025-05-27
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 108
-
2025-05-26 73
-
2025-05-27 65
-
2025-05-27 35
-
2025-05-26 53

