FastCGI + Django

Hello world :slight_smile:

I have some Django websites on shared hosting at DreamHost using Passenger. They will shut down Passenger support at the end of March leaving only FastCGI available. I read somewhere that FastCGI was removed long ago in Django.

Is it possible to run Django with FastCGI?

Would it be possible to bring back the FastCGI support in Django?

If not then I will have to change hosting company. Can you recommend other company with Django support shared hosting databases emails etc?

Any hints welcome :slight_smile:
Tomek

Version 1.9 to be accurate.

Not without a bit of work. (There are references to using FastCGI with uWSGI - see Quickstart for Python/WSGI applications — uWSGI 2.0 documentation - but I’m not sure that helps when you’re dealing with a cheap hosting provider.)

See the Django mailing list thread at https://groups.google.com/g/django-developers/c/oGmD8LvLTPg to see the discussions around the deprecation and removal of FastCGI. Messages in that thread contain a lot of details about why the decision was made and what some of the technical issues were with it.

1 Like

Thank you @KenWhitesell :slight_smile:

Instead of relying on Django’s deprecated FastCGI support, you can use external tools like uWSGI or Gunicorn . These WSGI servers can interface with FastCGI, allowing you to run Django applications with your shared hosting provider.

Yes @neilson9 some sort of additional local Python module is required between Djngo and Apache FastCGI but I could not find modern working examples.

I guess lots of people are in similar situation and I really like having a server to work on (Shared Hosting) rather a server to maintain (VPS). I read mailing list archives provided by @KenWhitesell and someone suggested providing a migration path for people forced to use FastCGI by the people who removed FastCGI support but that did not happen.

My question is in advance. I still have a month to find a way to run Django on Apache+FastCGI or switch hosting provider. Time will tell.

Hi @cederom, I’ve found out today I am facing the same thing as yours. Not sure the way to run Django on Apache+FastCGI worked well for you or you need to switch hosting provider?

Appreciate your advice on this. Thanks

Gunicorn seems most sensible solution and well maintained but there is no such support yet to make it work as FastCGI-To-WSGI module… its a standalone WSGI server… however author replied quickly and if there is more interest things will happen… please join the discussion on GitHub :slight_smile:

I have managed to come up with a somewhat working configuration for this. Tested with python 3.12.2, django 5.0.3, uwsgi 2.0.24. Based on this documentation, but tweaked a little because uwsgi doesn’t seem to terminate properly when the fcgi script is terminated.

  1. Download and build the python version of your choice, and/or create/activate a virtualenv that is to be used by your application
  2. CPUCOUNT=1 pip3 install uwsgi (note that you need CPUCOUNT=1 or the build will fail by trying to spawn too many threads)
  3. If it isn’t there already, create wsgi.py as below in the same folder as your settings.py. Make sure to change “myproject” to whatever your project is called.
import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
  1. In your hosting root directory, create a .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^uwsgi.fcgi/ - [L]
RewriteRule ^(.*)$ uwsgi.fcgi/$1 [L]
  1. In your hosting root directory, create a uwsgi.ini file and adjust names/paths as appropriate:
flock = /home/myuser/example.com/uwsgi.ini
account = myuser
domain = example.com

protocol = fastcgi
master = true
processes = 3
logto = /home/%(account)/%(domain)/app.uwsgi.log
virtualenv = /home/%(account)/%(domain)/venv
chdir = /home/%(account)/%(domain)/myproject
idle = 50
die-on-idle = true
module = myproject.wsgi:application
touch-reload = %p
auto-procname = true
procname-prefix-spaced = [%(domain)]
  1. In your hosting root directory, create uwsgi.fcgi:
#!/bin/sh
/home/myuser/example.com/venv/bin/uwsgi /home/myuser/example.com/uwsgi.ini
  1. Give the fcgi script execute permissions: chmod 755 uwsgi.fcgi

Note that dreamhost has the fastcgi idle timeout set to 60 sec, so if you don’t have a request for around 60 seconds then uwsgi will need to start up again on the next request.

1 Like

Thanks @jjh :slight_smile:

Do you use REST API with POST requests? All seems to work fine (on the web frontend) except REST API POST :astonished: I use standard Authorization: token <token> request header… it worked with mod_wsgi but does not with mod_fcgid. Work in progress :slight_smile:

Update: CGIPassAuth on needs to be added to .htcaccess so that Authorization is not stripped from headers (it is stripped by default)!

Here goes the updated .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

AddHandler fcgid-script fcgi
CGIPassAuth on

RewriteRule ^static - [L]
RewriteRule ^uwsgi.fcgi/ - [L]
RewriteRule ^(.*)$ uwsgi.fcgi/$1 [PT]
  • Redirect HTTP to HTTPS.
  • Add fcgi file handling (did not work for me by default).
  • Prevent REST API headers Authorization removal.
  • Skip static files to be processed by fcgi script.
  • The [PT] directive skips the uwsgi.fcgi prefix in the URL.

ALL IS SET! WHOAH! NO NEED TO CHANGE HOSTING COMPANY :sunglasses:

I’m afraid my django site is pretty basic and quite static, so I don’t really use any of the REST POST stuff. Sorry!

Okay I just made REST API working with a “simple solution” that took me around 3h to find but all is working now… my previous reply is updated with the complete .htaccess solution :slight_smile:

Good stuff. Hopefully dreamhost releases an “official” method in their documentation at some point.