virtualenv Django在aphae2的配置

网上教程很多 有的是旧版本 有很多误区
导致配了半天 特意记录一下流程

virtualenv下的python3.5 Django在aphache2部署

python3.5 位置 /home/tina/.local/virtualenvs/jobboardscraper

使用 virtualenv 的方式使用 django,核心的问题是修改 项目下的wsgi.py 文件,指定正确

建立Django与Apache的连接

sudo apt-get install libapache2-mod-wsgi #Python2
sudo apt-get install libapache2-mod-wsgi-py3 #Python3

让Apache找到Django

  • 将Django工程放在/var/www/下;
  • sudo vi /etc/apache2/sites-available/yoursite.conf 修改配置文件;
  • sudo a2ensite yoursite.conf 配置文件生效;
  • sudo service apache2 restart 重启Apache。

如你的Apache版本为2.2.x 则将Require all granted改为Order deny,allow Allow from all

执行sudo a2ensite yoursite.conf来使网站生效
也可以执行sudo a2dissite yoursite.conf来使网站失效
最后重启Apache即可 sudo service apache2 restart

配置文件

job.conf

位置 /etc/apache2/sites-available/job.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<VirtualHost *:80>
#serverName 设置本机的ip地址,这样可以通过局域网访问.通过localhost和127.0.0.1,我测试发现访问的结果不一样.
ServerName :10.205.49.198
DocumentRoot /var/www/jobboardscraper


WSGIDaemonProcess python-path=/var/www/jobboardscraper python-home=/home/tina/.local/virtualenvs/jobboardscraper/lib/python3.5/site-packages


<Directory /var/www/jobboardscraper/static/>

Require all granted

</Directory>

<Directory /var/www/jobboardscraper >
Require all granted
</Directory>
WSGIScriptAlias / /var/www/jobboardscraper/jobboardscraper/wsgi.py
#//前面在建立的文件
</VirtualHost>

wsgi.py

位置 var/www/jobboardscraper/jobboardscraper/wsgi.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import sys
import site
from os.path import join, dirname, abspath

site.addsitedir('/home/tina/.local/virtualenvs/jobboardscraper/lib/python3.5/site-packages')

PROJECT_DIR = dirname(dirname(abspath(__file__)))

sys.path.append(r'var/www/jobboardscraper')
sys.path.append(r'var/www/jobboardscraper/jobboardscraper')
sys.path.insert(0, PROJECT_DIR)

# Activate your virtual env
activate_env=os.path.expanduser("/home/tina/.local/virtualenvs/jobboardscraper/bin/activate_this.py") #虚拟环境中的路径
with open(activate_env) as file:
exec(file.read(), dict(__file__=activate_env))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jobboardscraper.settings")
from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

遇到的错误

主要看日志/var/log/apache2/error.log与systemctl -xe

OperationalError: unable to open database file

通过对象调用不到类属性
Django 在读写 sqlite 时会在 project 目录下产生临时文件,所以不仅要对 db.sqlite3 执行 chmod 777,还要对整个 project 目录执行 chmod 777 -R。
参考 http://elvestar.com/notes/django/

import Django 错误

在log里查看当前的python环境,一般原因都算没激活virtualenv(activate_this.py)

name ‘execfile’ is not defined

参考stackoverflow的解释

这是别人python2下配的wsgi.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import os
import sys

# Install venv by `virtualenv --distribute venv`
# Then install depedencies: `source venv/bin/active`
# `pip install -r requirements.txt`
activate_this = '/var/www/apache/csshat.com/csshat.com/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

path = os.path.join(os.path.dirname(__file__), os.pardir)
if path not in sys.path:
sys.path.append(path)

# The application object is used by any WSGI server configured to use this
# file.

# Ensure there is an app.py script in the current folder
from app import app as application

python3 下要修改成

1
2
3
activate_this = '/var/www/apache/csshat.com/csshat.com/venv/bin/activate_this.py'
with open(activate_this) as file:
exec(file.read(), dict(__file__=activate_this))

localhost能访问,局域网访问不了

修改settings.py 改成’*’

1
2
3
4
5
6
7
ALLOWED_HOSTS = [
#'127.0.0.1',
#'localhost',
#'10.205.49.198'
#'.herokuapp.com'
'*'
]

参考链接
https://www.thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
https://blog.csdn.net/caterpillarous/article/details/52917917
https://www.jianshu.com/p/44ab75fbaef2
https://www.jianshu.com/p/b40a4a12fff1
https://blog.csdn.net/qingyuanluofeng/article/details/44184135
https://www.jianshu.com/p/3a45daa9e558
https://jerrynest.io/mod-wsgi-apache-django/