一,安装 mod_wsgi
sudo apt-get install apache2-threaded-dev
sudo apt-get install apache2 libapache2-mod-wsgi
django 最新版已经不支持mod_python,所以mod_wsgi是最好的选择
增加apache配置端口:
修改/etc/apache/site-available/python
<VirtualHost *:801> ServerAdmin webmaster@localhost DocumentRoot /var/www/python/ <Directory /> Options FollowSymLinks AllowOverride ALl </Directory> <Directory /var/www/python> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory> WSGIScriptAlias / "/var/www/python/server/server/wsgi.py" ErrorLog ${APACHE_LOG_DIR}/py.error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/py.access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost>
增加端口 修改 /etc/apache2/ports.conf
使配置生效:sudo a2ensite python
进入/var/www/python
django-admin.py startproject server
修改var/www/python/server/server/wsgi.py 加入环境变量:
""" WSGI config for server project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/ """ import os import sys path = '/var/www/python/server' if path not in sys.path: sys.path.append(path) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
打开网址查看效果:
localhost:801
查看localhost:801/admin发现格式乱码。原来django的admin模块CSS路径在python引入模块路径下:
import django
print django.__file__
output:
'/usr/local/lib/python2.7/dist-packages/Django-1.8.dev20140828064426-py2.7.egg/django/__init__.pyc'
故css路径如下:
/usr/local/lib/python2.7/dist-packages/Django-1.8.dev20140828064426-py2.7.egg/django/contrib/admin/static
重新配置apache 在其中加上:
Alias /static/ "/usr/local/lib/python2.7/dist-packages/Django-1.8.dev20140828064426-py2.7.egg/django/contrib/admin/static/" <Directory /usr/local/lib/python2.7/dist-packages/Django-1.8.dev20140828064426-py2.7.egg/django/contrib/admin/static/> Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all </Directory>
重启apach2,打开localhost:801/admin 就可以看到久违的css格式。
apache命令分析:
Alias 路径别名。
Options Indexes FollowSymLinks MultiViews