I'm trying to run a Python 3 Django application with uWSGI, and having trouble.
我正在尝试使用uWSGI运行Python 3 Django应用程序,但遇到了麻烦。
I run uWSGI as a daemon:
我将uWSGI作为守护进程运行:
$ sudo service uwsgi start
I'm using a file like the following: [uwsgi]
我正在使用如下文件:[uwsgi]
chdir = /home/ubuntu/my_app
module = my_app.wsgi
# path below is to virtual environment
home = /home/ubuntu/my_app/env
http = :8000
check-static = /var/www/my_app
daemonize = /var/log/uwsgi/my_app
# process-related settings
master = true
processes = 10
vacuum = true
And I get the following puzzling error:
我得到以下令人费解的错误:
mapped 800360 bytes (781 KB) for 10 cores
*** Operational MODE: preforking ***
Traceback (most recent call last):
File "./titlematch_api/wsgi.py", line 14, in <module>
application = get_wsgi_application()
File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
self._wrapped = Settings(settings_module)
File "/home/ubuntu/titlematch_api/env/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "./titlematch_api/settings.py", line 20, in <module>
SECRET_KEY = os.environ['SECRET_KEY']
File "/usr/lib/python3.4/os.py", line 631, in __getitem__
raise KeyError(key) from None
KeyError: 'SECRET_KEY'
I have set the SECRET_KEY
environment variable, both for my current user, and as root, and I can successfully execute os.environ['SECRET_KEY']
in the python shell.
我为我当前的用户和root用户设置了SECRET_KEY环境变量,我可以在python shell中成功执行os.environ ['SECRET_KEY']。
1 个解决方案
#1
1
you cannot really do that as your environment is setup in ~/.bashrc locally which uwsgi does not have access to.
你无法真正做到这一点,因为你的环境是在本地的〜/ .bashrc中设置的,uwsgi无权访问。
you might be able to get away with putting the variable in /etc/rc.local
but Im not even sure that will work
您可以将变量放在/etc/rc.local中,但我甚至不确定它是否可行
you can add them to your file
您可以将它们添加到您的文件中
[uwsgi]
chdir = /home/ubuntu/my_app
module = my_app.wsgi
# path below is to virtual environment
home = /home/ubuntu/my_app/env
http = :8000
check-static = /var/www/my_app
daemonize = /var/log/uwsgi/my_app
# process-related settings
master = true
processes = 10
vacuum = true
#environment
env=SECRET_KEY="My Super Secret Key"
or you can configure it directly on the app object in your python file
或者您可以直接在python文件中的app对象上配置它
app = Flask(__name__)
app.secret_key="My Super Secret Key"
which to be frank is probably the way I would do it
坦率地说,这可能就是我这样做的方式
#1
1
you cannot really do that as your environment is setup in ~/.bashrc locally which uwsgi does not have access to.
你无法真正做到这一点,因为你的环境是在本地的〜/ .bashrc中设置的,uwsgi无权访问。
you might be able to get away with putting the variable in /etc/rc.local
but Im not even sure that will work
您可以将变量放在/etc/rc.local中,但我甚至不确定它是否可行
you can add them to your file
您可以将它们添加到您的文件中
[uwsgi]
chdir = /home/ubuntu/my_app
module = my_app.wsgi
# path below is to virtual environment
home = /home/ubuntu/my_app/env
http = :8000
check-static = /var/www/my_app
daemonize = /var/log/uwsgi/my_app
# process-related settings
master = true
processes = 10
vacuum = true
#environment
env=SECRET_KEY="My Super Secret Key"
or you can configure it directly on the app object in your python file
或者您可以直接在python文件中的app对象上配置它
app = Flask(__name__)
app.secret_key="My Super Secret Key"
which to be frank is probably the way I would do it
坦率地说,这可能就是我这样做的方式