我一直在探索,应该说这个错误的根源在于我的代码中
我有两个新手错误:
-
print
生产中的句子
在我上面的问题中显示的回溯中,我有一个print
我里面的一句话get_context_data()
这种方式的作用:
File "/home/ubuntu/workspace/neurorehabilitation-system/userprofile/views.py" in get_context_data
50. print (user.is_physiotherapist)
有可能每次执行此打印语句时,该进程都会尝试写入我的亚马逊 ec2 机器实例中的 stdout 文件。
我删除了该行中的这个打印句子,并通过 git 将更改检索到我的生产服务器中,然后重新启动 Gunicorn 服务器,一切工作都很完美。
-
我有
DEBUG=True
生产中
我有以下设置文件:
settings/
base.py # --- without DEBUG
development.py # --- DEBUG=True
testing.py # --- DEBUG=True
production.py # --- DEBUG=False
staging.py # --- DEBUG=False
全部文件 (development.py, testing.py, production.py, staging.py
)继承自base.py
但我不知道如何在我的 ec2 实例中执行 production.py,它继承了所有的 base.py 并将 DEBUG 覆盖为 False。
我一直在探索,一种可能性是根据运行我的应用程序的主机的名称更改它们的值(True 或 False),如这篇文章所示 https://nicksergeant.com/automatically-setting-debug-in-your-django-app-based-on-server-hostname/
就我而言,这是我的主机名的值
(nrb_dev)ubuntu@ip-172-31-27-249:~$ python
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> a=socket.gethostname()
>>> a
'ip-172-31-27-249'
>>>
>>> if a != 'ip-172-31-27-249':
... DEBUG = print ('Caleno juiciocito')
...
>>> DEBUG
True
>>>
这意味着,将以下内容放入我的 base.py 中:
import socket
if socket.gethostname() == 'ip-172-31-27-249':
DEBUG = False
else:
DEBUG = True
尽管我在代码中硬编码了生产服务器的主机名。
这意味着当我们想要使用其他主机名在其他计算机上部署我的项目时,我将在手动修改后添加一个点
尽管它有效,但这是否是最佳实践?
我认为最合适的另一个选择是修复我的价值DJANGO_SETTINGS_MODULE
环境变量
在我的特殊情况下我正在使用virtualenvwrapper
我有两个虚拟环境,所以:
nrb_dev
对于我的开发环境
nrb_test
对于我的测试环境。
我有一些钩子在激活虚拟环境时被激活
In nrb_dev
in $VIRTUAL_ENV/bin/postactivate
我有这个:
export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.development"
以同样的方式,在nrb_test
in $VIRTUAL_ENV/bin/postactivate
我有这个:
export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.testing"
这意味着在我的亚马逊 EC2 生产机器中我应该更改钩子$VIRTUAL_ENV/bin/postactivate
来选择settings/production.py
这样的方式:
export DJANGO_SETTINGS_MODULE="neurorehabilitation.settings.production"
只是为了测试效果和时间方式,我打印了DEBUG
我的价值settings/production.py
from .base import *
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
print (DEBUG) # just for now.
当我启动 Gunicorn 守护进程服务器时,我可以看到 DEBUG 值设置为False
(nrb_dev)ubuntu@ip-172-31-27-249:~/workspace/neurorehabilitation-system$ gunicorn -c neurorehabilitation/gunicorn_config.py neurorehabilitation.wsgi
[2016-01-08 00:26:15 +0000] [6691] [INFO] Starting gunicorn 19.4.5
[2016-01-08 00:26:15 +0000] [6691] [INFO] Listening at: http://127.0.0.1:8000 (6691)
[2016-01-08 00:26:15 +0000] [6691] [INFO] Using worker: sync
[2016-01-08 00:26:15 +0000] [6694] [INFO] Booting worker with pid: 6694
False
^C[2016-01-08 00:26:19 +0000] [6691] [INFO] Handling signal: int
补充笔记
我可以探索 Django记录功能 https://docs.djangoproject.com/en/1.9/topics/logging/用于注册事件和我的应用程序的其他内容。
我应该探索督导服务 http://docs.gunicorn.org/en/latest/deploy.html#supervisor以更好的方式管理生产中的gunicorn进程。
主管的其他资源:
如何在Ubuntu中安装和管理supervisor https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps
使用 Nginx、Gunicorn、virtualenv、supervisor 和 PostgreSQL 设置 Django http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/