Django + Apache + mod_wsgi


目录
  • 安装环境
  • Apache 配置
  • mod_wsgi 配置
  • django 网页的配置
  • 参考

安装环境

  1. 安装 apache
sudo apt-get install apache2
sudo apt-get install apache2-dev
  1. 安装 mod_wsgi
    遇到过 mod_wsgi 装不上的问题,其实就是没有安装 apache2-dev,按照上面安装好 apache2 就没问题啦!
sudo pip3 install mod_wsgi
  1. 安装 django
sudo pip3 install django

Apache 配置

  1. Apache相关文件的布局
    文件配置
Debian, Ubuntu (Apache httpd 2.x):
ServerRoot              ::      /etc/apache2
DocumentRoot            ::      /var/www
Apache Config Files     ::      /etc/apache2/apache2.conf
                        ::      /etc/apache2/ports.conf
Default VHost Config    ::      /etc/apache2/sites-available/default, /etc/apache2/sites-enabled/000-default
Module Locations        ::      /etc/apache2/mods-available, /etc/apache2/mods-enabled
ErrorLog                ::      /var/log/apache2/error.log
AccessLog               ::      /var/log/apache2/access.log
cgi-bin                 ::      /usr/lib/cgi-bin
binaries (apachectl)    ::      /usr/sbin
start/stop              ::      /etc/init.d/apache2 (start|stop|restart|reload|force-reload|start-htcacheclean|stop-htcacheclean)

mod_wsgi 配置

需要到 python 环境下的 script 文件夹, 在控制台运行 mod_wsgi-express module-config 获取 WSGI 相关的文件路径。
下面的是 windows 的例子,linux 运行命令后是类似的结果。

D:\Program\Python\Scripts>mod_wsgi-express module-config
LoadFile "d:/program/python/python37.dll"
LoadModule wsgi_module "d:/program/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "d:/program/python"

将上面获取的三行复制并添加到 httpd.conf 文件中,有些系统是 apache2.conf,详细请参考文件配置

django 网页的配置

  1. 编写自己的 django 网页程序
  2. 配置到 apache2
    参考:django
LoadFile "d:/program/python/python37.dll"
LoadModule wsgi_module "d:/program/python/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "d:/program/python"

# wsgi.py path
WSGIScriptAlias / "E:\WorkRecord\WorkRecord\wsgi.py"

# project folder
WSGIPythonPath E:\WorkRecord


Require all granted



# static folder
Alias /static/ E:\WorkRecord\static\


Require all granted

参考

  1. 文件配置
  2. django