K. Sivilay
K. Sivilay

Reputation: 21

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. However, after all day googling, I still cannot find any instructions on that. Can anyone help me with that, please?

Upvotes: 2

Views: 1537

Answers (1)

Blaise
Blaise

Reputation: 61

I was able to get Django working on Bluehost by doing the following.

  1. I had to build a recent python and set up a virtualenv that gets it nicely configured. Then I used pip to install django and 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.

  2. Use .htaccess to tell apache to run a django.fcgi script. You'll want to make django.fcgi executable.

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.
#!/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.
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. 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. 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).

Note that this approach works for me for Django 3.2.5, well after fcgi support was removed. In this approach, flup is implementing fcgi, and converting the requests to wsgi for django.

Upvotes: 1

Related Questions