django - 使用 apache 和 mod_wsgi 在 DigitalOcean 上部署 django 项目并获取错误,因为目标 WSGI 脚本无法作为 Python 模块加载
问题描述
使用 apache 和 mod_wsgi 在 DigitalOcean 上部署 django 项目
我在用着
Ubuntu 16.04
apache 2.4
python3.5.2
django==1.11
Firebase
sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
sudo apt-get install libapache2-mod-wsgi-py3
sudo apt-get install aptitude
sudo aptitude install apache-dev
pip3 install mod-wsgi
安装了所有 pip3 模块并且 apache 正在运行
授予 www 文件夹权限为
sudo chown -R www-data:www-data www/
将 wsgi.py 文件创建为可执行文件
sudo chmod a+x wsgi.py
我的 wsgi.py 文件为
import os
import sys
sys.path.append('/var/www/myproject')
sys.path.append('/usr/local/lib/python3.5/dist-packages')
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") #also tried
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
apache 配置文件 000-default.conf
<VirtualHost *:80>
ServerName www.mysite.in
ServerAlias mysite.in
ServerAdmin myemail@abc.com
DocumentRoot /var/www/
ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess mysite.in python-path=/var/www/myproject python-home=/usr/local/lib/python3.5/dist-packages
WSGIProcessGroup mysite.in
WSGIScriptAlias / /var/www/myproject/myproject/wsgi.py
</VirtualHost>
修改 000-default.conf 文件后运行命令
sudo service apache2 reload
sudo a2ensite 000-default.conf
sudo service apache2 reload
sudo systemctl restart apache2.service
我的项目文件夹结构是
.
├── __pycache__
│ └── config.cpython-35.pyc
├── myproject
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ └── settings.cpython-35.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── myprojectapp
│ ├── admin.py
│ ├── __init__.py
│ ├── mail_html.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── __pycache__
│ │ ├── __init__.cpython-35.pyc
│ │ └── models.cpython-35.pyc
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── admin.json
├── config.py
├── db.sqlite3
├── firebasesdk-file.json
├── manage.py
├── requirements.txt
└── test.py
在 mysite_error.log 文件中,我收到错误消息
[Thu Oct 24 04:43:11.940530 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] mod_wsgi (pid=6581): Target WSGI script '/var/www/myproject/myproject/wsgi.py' cannot be loaded as Python module.
[Thu Oct 24 04:43:11.940823 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] mod_wsgi (pid=6581): Exception occurred processing WSGI script '/var/www/myproject/myproject/wsgi.py'.
[Thu Oct 24 04:43:11.943517 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] Traceback (most recent call last):
[Thu Oct 24 04:43:11.943754 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/var/www/myproject/myproject/wsgi.py", line 16, in <module>
[Thu Oct 24 04:43:11.943897 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] application = get_wsgi_application()
[Thu Oct 24 04:43:11.944040 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Thu Oct 24 04:43:11.944133 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] django.setup(set_prefix=False)
[Thu Oct 24 04:43:11.944260 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 27, in setup
[Thu Oct 24 04:43:11.944349 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] apps.populate(settings.INSTALLED_APPS)
[Thu Oct 24 04:43:11.944464 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 108, in populate
[Thu Oct 24 04:43:11.944561 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] app_config.import_models()
[Thu Oct 24 04:43:11.944686 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/usr/local/lib/python3.5/dist-packages/django/apps/config.py", line 202, in import_models
[Thu Oct 24 04:43:11.944791 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] self.models_module = import_module(models_module_name)
[Thu Oct 24 04:43:11.944880 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Thu Oct 24 04:43:11.944945 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] return _bootstrap._gcd_import(name[level:], package, level)
[Thu Oct 24 04:43:11.945013 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Thu Oct 24 04:43:11.945087 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Thu Oct 24 04:43:11.945161 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
[Thu Oct 24 04:43:11.945245 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
[Thu Oct 24 04:43:11.945367 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "<frozen importlib._bootstrap_external>", line 665, in exec_module
[Thu Oct 24 04:43:11.945461 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Thu Oct 24 04:43:11.945608 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/var/www/myproject/myprojectapp/models.py", line 31, in <module>
[Thu Oct 24 04:43:11.945712 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] cred = credentials.Certificate('../myproject/firebasesdk-file.json')
[Thu Oct 24 04:43:11.945799 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] File "/usr/local/lib/python3.5/dist-packages/firebase_admin/credentials.py", line 83, in __init__
[Thu Oct 24 04:43:11.945863 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] with open(cert) as json_file:
[Thu Oct 24 04:43:11.945979 2019] [wsgi:error] [pid 6581:tid 139660427339520] [remote 127.0.0.1:0] FileNotFoundError: [Errno 2] No such file or directory: '../myproject/firebasesdk-file.json'
我的项目中也有 firebasesdk-file.json
还尝试了虚拟环境
我在哪里创建了 venv 并在 venv 内安装了 python3.5.2 的所有 pip3 模块
更改了关于 venv 的 wsgi.py 和 000-default.conf 文件,对 vitualenvironment 没有任何帮助
如何修复此错误?
谢谢。
解决方案
您python-home
应该指向 python 二进制文件,在命令行类型which python3
上,输出应该类似于/usr/bin/python3
但在您的安装中可能会有所不同尝试将其更改python-home
为。
推荐阅读
- php - 如何划分数组的每个元素
- node.js - Dialogflow - 在给定延迟后添加后续事件
- javascript - 需要获取所有子组件实例的所有值并在parents方法中使用:ReactJS
- selenium - 我怎么能等待(保持)我的执行,直到一个元素出现,一旦它消失,我应该在 selenium 中继续前进
- node.js - 将 new.html 设置为基础文件
- java - 此版本的 ChromeDriver 尚未使用 Chrome 版本 75 和 java.lang.IllegalStateException 在新浏览器窗口打开后进行测试
- c++ - 使用 Howard Hinnant 的日期库将 double 转换为 zoned_time
- c++ - 在 std::remove_if 执行期间遍历容器是否安全?
- linux - 如何为此代码编写 if 记录的缩写版本?
- sql - SQL Server 无法将 varchar 值转换为数据类型 int