anaconda + django + apache2

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

    sudo 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     
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. 运行

4.1 单独运行django

  在mysite 目录下运行:

  python manage.py runserver 0.0.0.0:8000

  在浏览器输入 http://localhost:8000/polls/

  如果是在云服务器上运行,则需要:

(1)在项目下的setting.py中,在Allowed_Hosts中添加 ‘*’ 字段

    ALLOWED_HOSTS = ['*']

  如果运行成功,则说明django运行正常。

4.2 通过apache2 运行django

   sudo service apache2 reload

   sudo service a2ensite  mysite.conf

   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

  则表示wsgi 配置成功。  

5.问题

1. 如果 /var/log/apache2/error.log 中出现

Permission denied: mode_wsgi(pid=...): unable to stat Python home ...

则表示权限不够,需要修改整个用户目录的权限

chmod 755  /home/szh

2. 关于static

可以在项目根目录下建立一个文件夹static

然后在 setting.py 中添加如下内容:

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), ]

则static中的内容可以被外部访问。

3. 关于权限

由于apache2默认启用用户: www-data, 所以需要给 www-data 相关的执行权限,它才能读写static文件夹。

4. 500 internal server error

配置完成后,执行 python manage runserver 0.0.0.0:8000可以正常运行

但是执行 sudo /etc/init.d/apache2 start , 则服务器长时间没有反应, 最后给出 500 internal server error的结果。

查看/var/log/apache2/error.log, 发现以下提示

Truncated or oversized response headers received from daemon process 

 参考 https://serverfault.com/questions/844761/wsgi-truncated-or-oversized-response-headers-received-from-daemon-process ,

在/etc/apache2/apache2.conf 最后添加:

 WSGIApplicationGroup %{GLOBAL}

则运行正常。

5. 错误

浏览器出现以下报错:

You don't have permission to access this resource.

服务端 /var/log/apache2/error.log 出现以下报错:

client denied by server configuration: /data/szh/django_project/chess/chess/wsgi.py

检查后发现是 mysite.conf 配置文件中的路径有错误。  

6. 其它参考

https://www.cnblogs.com/xcfzy/p/10095069.html

posted on 2020-02-21 15:59  兔子二  阅读(365)  评论(0编辑  收藏  举报

导航