環境:
debian8
apache2.4.10 #請注意自己的apache版本,不同版本配置文件結構差異很大
django1.10
python3.4
默認apache2已經安裝
Django項目路徑 /var/www/mysite
---------------------------------------------------------------------------------------------------
首先安裝MOD_WSGI
apt-get install libapache2-mod-wsgi-py3 #python3 apt-get install libapache2-mod-wsgi #python2
創建django項目
cd /var/www django-admin startproject mysite cd mysite mkdir static cp -R /usr/local/lib/python3.4/dist-packages/django/contrib/admin/static/* ./static/ #復制后台樣式文件到項目目錄
創建虛擬主機配置文件
cd /etc/apache2/sites-enabled/ cp 000-default.conf 001-default.conf nano 001-default.conf
<VirtualHost *:81> ServerAdmin webmaster@localhost DocumentRoot /var/www/mysite ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # 存放用戶上傳圖片等文件的位置 Alias /media/ /var/www/mysite/media/ # 靜態文件(js/css/images)的存放位置 Alias /static/ /var/www/mysite/static/ # 允許通過網絡獲取static的內容 <Directory /var/www/mysite/static/> Require all granted </Directory> # 最重要的!通過wsgi.py讓Apache識別這是一個Django工程,別漏掉前邊的 / WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py # wsgi.py文件的父級目錄 <Directory /var/www/mysite/mysite/> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost> # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
編輯 /etc/apache2/ports.conf 添加一行 Listen 81 #監聽81端口,就是我們新建的虛擬主機
編輯 /etc/apache2/mods-enabled/wsgi.load
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so-3.4 #python3 LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so #python2
編輯 /etc/apache2/mods-enabled/wsgi.conf
添加一行 WSGIPythonPath /var/www/mysite #項目所在路徑
這個時候就差不多了可以 service apache2 start
訪問:127.0.0.1:81
It worked!
Congratulations on your first Django-powered page.
訪問:127.0.0.1:81/admin 會報錯
初始化數據庫文件
manage.py migrate
OK!
如果是在虛擬機或遠程主機部署別忘了在settings.py中修改ALLOWED_HOSTS = [],添加可訪問域名或IP列表
CentOS可以參考http://www.cnblogs.com/starof/p/4685132.html
