Django Apache上运行多个Django项目(转载)
【Django】Apache上运行多个Django项目
运行单个项目的步骤参考:这里
1 安装环境
操作系统:Ubuntu 12.04 LTS 32 位(安装在VMware虚拟机中)
python 版本:
Python 2.7.3
Django版本
>>> django.VERSION (1, 6, 2, 'final', 0)
mod_wsgi版本
mod_wsgi3.x
apache版本及查看命令
x@x:~/program$ /usr/sbin/apachectl -v (版本查看命令) /usr/sbin/apachectl: 87: ulimit: error setting limit (Operation not permitted) Server version: Apache/2.2.22 (Ubuntu) Server built: Mar 19 2014 21:10:40
2 配置
①修改port.conf
x@x:/etc/apache2$ sudo vim ports.conf
添加以下代码 :
NameVirtualHost *:8080
Listen 8080
NameVirtualHost *:80 Listen 80 # add you code here for multiple Django code
NameVirtualHost *:8080 Listen 8080
② 新建两个Django项目
我新建的项目的目录是 x@x:~/program/django$ (这个目录可以自定)
django-admin.py startproject mywork
django-admin.py startproject mywork1
├── mywork │ ├── apache (后加的) │ │ └── django.wsgi (后加的) │ ├── manage.py │ ├── mywork │ │ ├── __init__.py │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ └── static (后加的) ├── mywork1 │ ├── apache (后加的) │ │ └── django.wsgi (后加的) │ ├── manage.py │ ├── mywork1 │ │ ├── __init__.py │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ └── static (后加的) └── UserWeb.tgz
③ 修改httpd.conf 文件
啊
x@x:/etc/apache2$ sudo vim httpd.conf
添加如下代码
# add code for multiple Django projects run in Apache #LoadModule wsgi_module modules/mod_wsgi.so #setting for Django projedt1 **** <VirtualHost *:80> ServerName 127.0.0.1 DocumentRoot /home/x/program/django/mywork Alias /admin_media /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static <Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"> AllowOverride None Options None Order allow,deny Allow from all </Directory> WSGIScriptAlias / /home/x/program/django/mywork/apache/django.wsgi <Directory /home/x/program/django/mywork> Order deny,allow Allow from all </Directory> <Directory /home/x/program/django/mywork/apache> Allow from all </Directory> <Location "/static/"> #配置静态文件,网站中所有静态文件的路径为"/static/xxxx" SetHandler None </Location> </VirtualHost> <VirtualHost *:8080> ServerName 127.0.0.1 DocumentRoot /home/x/program/django/mywork1 Alias /admin_media /usr/local/lib/python2.7/dist-packages/django/contrib/admin/static <Directory "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static"> AllowOverride None Options None Order allow,deny Allow from all </Directory> WSGIScriptAlias / /home/x/program/django/mywork1/apache/django.wsgi <Directory /home/x/program/django/mywork1> Order deny,allow Allow from all </Directory> <Directory /home/x/program/django/mywork1/apache> Allow from all </Directory> <Location "/static/"> #配置静态文件,网站中所有静态文件的路径为"/static/xxxx" SetHandler None </Location> </VirtualHost>
④在Django项目中添加文件夹及文件
在项目文件根目录下添加
目录apache 和static
并在apache中新建文件django.wsgi
文件目录如下:
├── mywork │ ├── apache │ │ └── django.wsgi │ ├── manage.py │ ├── mywork │ │ ├── __init__.py │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ └── static ├── mywork1 │ ├── apache │ │ └── django.wsgi │ ├── manage.py │ ├── mywork1 │ │ ├── __init__.py │ │ ├── settings.py │ │ ├── urls.py │ │ └── wsgi.py │ └── static
mywork/apache/ 中的django.wsgi文件如下
import os, sys sys.path.append('/home/x/program/django/mywork') os.environ['DJANGO_SETTINGS_MODULE'] = 'mywork.settings' os.environ['PYTHON_EGG_CACHE'] = '/tmp' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() #print >> sys.stderr, sys.path
mywork1/apache/ 中的django.wsgi文件如下
import os, sys sys.path.append('/home/x/program/django/mywork1') os.environ['DJANGO_SETTINGS_MODULE'] = 'mywork1.settings' os.environ['PYTHON_EGG_CACHE'] = '/tmp' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() #print >> sys.stderr, sys.path
⑤重启apache服务器,访问Django项目
sudo service apache2 restart
通过ip地址访问即可
127.0.0.1:80 127.0.0.1:8080
如图所示
参考文章