How to use Django with Fastcgi?

How to use Django with Fastcgi

Table of Contents

Web servers like Apache, Nginx, etc. do not know how to run Python applications. That’s why, Django web applications need an interface like WSGI to communicate with web servers.

But, what if the hosting servers do not support these WSGI (Web server gateway interface)?

Luckily, there are options to make Django apps work with FastCGI.

At 1onlyhost, we often get requests from customers to make their Python scripts work in shared hosting as part of our Technical Support Services.

Today, we’ll see how our Support Engineers setup Django to work with FastCGI.

 

 

Why choose FastCGI ?

Although WSGI works well with Django applications, shared hosting servers may not have this interface. That’s why, such customers will have to choose from the available options like FastCGI, SCGI, etc.

FastCGI is an efficient way of letting an external application serve pages to a Web server. It simply works as a link between the web server and the Application you want to run.

FastCGI functions on a client-server model.  Here, the Web server contacts the Django-FastCGI process only while loading a dynamic page. Fortunately, the server response will be really quick, as the FastCGI daemon is already running with the app code already in memory.

However, before proceeding further, we need to confirm that Django version supports FastCGI. It’s support is removed from Django 1.9 on-wards. In such cases, server needs the package django-fastcgi-server.

How to setup Django to make use of FastCGI?

So far, we saw the facts on the working of Django FastCGI model. Now, we’ll see how our Dedicated Engineers help customers to make use of FastCGI in Django framework.

As the first step in using FastCGI with Django, we install a Python FastCGI library called Flup on the server. Further, the configuration steps differ based on the web server.

1. Apache

For example, to use Django with Apache and FastCGI, we install and enable the module mod_fastcgi.

Then, we point Apache at the Django FastCGI instance by editing the httpd.conf file. Here, we use the FastCGIExternalServer directive to specify the location of the FastCGI server. Again, with the help of  mod_rewrite, we tell Apache to use FastCGI for URLs that match a certain pattern.

Unfortunately, many shared-hosting providers don’t allow to run fastcgi server daemons or edit the httpd.conf file. In these cases, our Support Engineers add the necessary entries in the .htaccess file in the website root directory.

2. Lighttpd

Fortunately, Lighttpd is a lightweight Web server that supports FastCGI natively.  Thus, it can easily serve both static and dynamic pages.

Here, to make it work with Django, our Support Engineers ensure that mod_fastcgi is available in server modules list. Additionally, we confirm the availability of  modlues like mod_rewrite, mod_alias and mod_access.

Further, we modify the lighttpd config file and set the fastcgi.server directive to point to the FastCGI process.

3. Nginx

Similarly, It is possible to make Django work with FastCGI in Nginx web server too.

To make it working we add the following directive in the nginx.conf.

fastcgi_param PATH_INFO $fastcgi_script_name;

This would set the path of the FastCGI script that will be used for the processing of the application. We then proceed with the restart of Nginx service.

Common errors with Django FastCGI integration

From our experience in managing servers, we often see customer reporting problems with Django apps. Let’s take a look at them now.

1. Lack of modules

Recently, a customer  was stuck while trying to configure his new Django website. Every time he tried to run the Python script he was getting the following error message:

ImportError:No module named fastcgi

On troubleshooting, we found that the customer was using Django version 1.9 which did not have FastCGI support. Additionally, Flup module was missing on the server too. To fix the Python app, our Support Engineers had to replace the Python version and install the Flup module.

2. Wrong Python version

Likewise, wrong Python version also can create problems with Django FastCGI apps. In servers with multiple Python versions, when you refer the wrong python version, it can show up blank page.

To make the python app working again, we edit the settings and make sure that script calls the correct python version.

[Facing problems with Django FastCGI setup? We can fix it for you.]

Conclusion

In short, when server limits the usage of certain interfaces like WSGI, using Django with FastCGI is a feasible option. Today, we saw how our Support Engineers setup Django apps to use FastCGI and fix common error with the setup.

Leave a Comment

Your email address will not be published. Required fields are marked *

Social Share

Facebook
Twitter
LinkedIn
Telegram

Cheapest Web Hosting

Fasters Web Hosting Promo
Scroll to Top

Launch your Website at RS599

Create a professional website yourself with our domain hosting combo. Get Extra benefits at this Year end Sale with 10% OFF and Enjoy Amazing Deels on Shared Hosting!

Starting at Only RS599/Year

Year End Sale

Hours
Minutes
Seconds

Use Code STARTBIZ at Checkout.

Create an earning opportunity with A Web Hosting Business

Become reseller and start reselling domains, web hosting, ssl certificates and More.

45% OFF

Starting at RS399/Month

Hours
Minutes
Seconds

Use coupon: at the checkout.