0. 环境
系统ubuntu 16.04
python 环境:python2.7, python3.5, anaconda
anaconda 安装在 /home/szh/anaconda3/envs/env_py3.6
django 项目所在路径:/home/szh/django_project/mysite
1. 安装apache2
sudo apt-get update
sudo apt-get install apache2
sudo apt-get install apache2-dev
2. 安装mod_wsgi
(参照 https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html)
(1)下载并解压mod_wsgi-X.Y.tar.gz
(2)编译安装
./configure --with-python=/home/szh/anaconda3/envs/env_py3.6/bin/python
make
make install
3. 配置apache2
(1) 配置 apache2.conf
(参考:https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi/ )
在/etc/apache2/apache2.conf 文件最下面添加以下三行(目的是为了让apache2能正确装入mod_wsgi)。
LoadFile /home/szh/anaconda3/envs/env_py3.6/lib/libpython3.6m.so.1.0 LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so WSGIPythonHome /home/szh/anaconda3/envs/env_py3.6
(2) 添加mysite.conf
在/etc/apache2//etc/apache2/sites-available 文件夹下添加 mysite.conf 文件,内容如下:
<VirtualHost *:80> ServerName localhost:80 ServerAlias otherdomain.com ServerAdmin xxx@Email.com Alias /media/ /home/szh/django_project/mysite/media/ Alias /static/ /home/szh/django_project/mysite/static/ <Directory /home/szh/django_project/mysite/media> Options FollowSymLinks Require all granted </Directory> <Directory /home/user/szh/django_project/mysite/static> Options FollowSymLinks Require all granted </Directory> WSGIScriptAlias / /home/szh/django_project/mysite/mysite/wsgi.py <Directory /home/szh/django_project/mysite/mysite> <Files wsgi.py> Require all granted </Files> </Directory> WSGIDaemonProcess mysite python-home=/home/szh/anaconda3/envs/env_py3.6 python-path=/home/szh/django_project/mysite WSGIProcessGroup mysite </VirtualHost>
4. 运行
sudo /etc/init.d/apache2 start
查看/var/log/apache2/error.log,其中有以下行:
Apache/2.4.18 (Ubuntu) mod_wsgi/4.7.1 Python/3.6 configured
在浏览器输入 http://localhost/polls/