安装
- 安装Apache
sudo apt-get install apache2
-
安装Django
下载Django -
安装mod_wsgi
sudo apt-get install libapache2-mod-wsgi
配置
- 在/var/www/html下创建一个项目:
django-admin startproject testproject
- 在项目目录中创建apache目录,并创建django.wsgi文件
sudo mkdir apache
cd apache
sudo vim django.wsgi
- django.wsgi代码如下:
import os
import sys
path = '/var/www/html/testproject'
if path not in sys.path:
sys.path.append(path)
os.environ['DJANGO_SETTINGS_MODULE'] = 'testproject.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
此处也可不新建这个目录和文件,直接用django自带的wsgi.py文件,不过里面要加上
import sys
path = '/var/www/html/testproject'
if path not in sys.path:
sys.path.append(path)
这三行
- 在/etc/apache2/apache2.conf文件末尾加载wsgi模块
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
- 创建一个新的站点
sudo vim /etc/apache2/sites-available/testproject.conf
testproject.conf:
<VirtualHost *:80>
ServerName testproject.com
DocumentRoot /var/www/html/testproject
<Directory /var/www/html/testproject>
Order allow,deny
Allow from all
# 在新版Apache本中要改成 Require all granted
</Directory>
WSGIScriptAlias / /var/www/html/testproject/apache/django.wsgi
Alias /static/ /var/www/html/testproject/static/
</VirtualHost>
/static/ 为配置的静态文件,不然网站页面混乱
- 在/etc/hosts中添加域名
127.0.0.1 testproject.com
后面的网址也可以是IP ,一个IP多个网站时,每个网站就用不同的域名
- 激活站点,重启apache
a2ensite testproject
sudo service apache2 reload
有时碰到apache2.service is not active, cannot reload.
则参考https://stackoverflow.com/questions/37464915/why-i-cant-reload-apache2-on-ubuntu
apachectl stop /etc/init.d/apache2 start
/etc/init.d/apache2 reload
配置多个网站:
http://blog.csdn.net/naturebe/article/details/54933844