简体   繁体   English

如何使用 WSGI 在 Bluehost 上部署 Django 应用程序?

[英]How to deploy Django app on Bluehost using WSGI?

I am new to Bluehost and I am trying to find instructions on how to deploy Django on Bluehost using WSGI since FastCGI support is deprecated and removed in Django 1.9.我是 Bluehost 的新手,我正在尝试查找有关如何使用 WSGI 在 Bluehost 上部署 Django 的说明,因为 FastCGI 支持已在 Django 1.9 中被弃用和删除。 However, after all day googling, I still cannot find any instructions on that.但是,经过一整天的谷歌搜索,我仍然找不到任何相关说明。 Can anyone help me with that, please?有人可以帮我吗?

I was able to get Django working on Bluehost by doing the following.通过执行以下操作,我能够让 Django 在 Bluehost 上工作。

  1. I had to build a recent python and set up a virtualenv that gets it nicely configured.我不得不构建一个最近的 python 并设置一个 virtualenv 来很好地配置它。 Then I used pip to install django and flup.然后我用pip安装了django和flup。 I also installed psycopg2 since I was using postgresql. This was pretty arduous as there were many missing dependencies such as an up-to-date openssl and postgress, but is pretty off topic for this question.我还安装了 psycopg2,因为我使用的是 postgresql。这非常艰巨,因为缺少许多依赖项,例如最新的 openssl 和 postgress,但这与这个问题无关。

  2. Use .htaccess to tell apache to run a django.fcgi script.使用 .htaccess 告诉 apache 运行 django.fcgi 脚本。 You'll want to make django.fcgi executable.您需要使 django.fcgi 可执行。

AddHandler fcgid-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ django.fcgi/$1 [QSA,L]
  1. Write a django.fcgi bash script that gets the virtualenv set up and runs the python app.编写一个 django.fcgi bash 脚本来设置 virtualenv 并运行 python 应用程序。
#!/bin/bash
HOME=/path/to/my/home/directory

# Useful for debugging. Kill it once things are working.
exec 2>> $HOME/fcgi.log 1>> $HOME/fcgi.log
echo Restarting fcgi
    
# Start up the virtualenvironment.
. $HOME/venv/bin/activate
    
python -m fcgi
  1. Write an fcgi.py script that uses flup to run django.编写一个使用 flup 运行 django 的 fcgi.py 脚本。
from flup.server.fcgi import WSGIServer 
from django.core.wsgi import get_wsgi_application 
import os 
     
if __name__ == '__main__': 
    os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project_name.settings' 
    application = get_wsgi_application()
    # Remember to set debug to False once you have things working.
    WSGIServer(application, debug=True).run()

I found that changing django.fcgi causes the application to get reloaded.我发现更改 django.fcgi 会导致应用程序重新加载。 Touching that file is not sufficient.触摸该文件是不够的。 I actually have to, say, add/remove some whitespace.我实际上不得不,比如说,添加/删除一些空格。

Running this way seems to work, but does sometimes seem to make the bluehost resource limiting unhappy.以这种方式运行似乎可行,但有时似乎会使 bluehost 资源限制不愉快。 I'll sometimes get it spewing我有时会让它喷涌而出

-jailshell: fork: retry: Resource temporarily unavailable

when I am sshed in. I haven't quite figured out the pattern of when it does this yet.当我进入时。我还没有完全弄清楚它何时执行此操作的模式。 But I haven't encountered it when not using fcgi (well, make -j2 will also trigger it).但是我没遇到过不使用fcgi的时候(嗯,make -j2也会触发)。

Note that this approach works for me for Django 3.2.5, well after fcgi support was removed.请注意,在删除 fcgi 支持之后,此方法适用于 Django 3.2.5。 In this approach, flup is implementing fcgi, and converting the requests to wsgi for django.在这种方法中,flup 正在实现 fcgi,并将请求转换为 django 的 wsgi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM