Need help?

Our experts have had an average response time of 13.14 minutes in February 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

How to use Django with Fastcgi?

by | Feb 5, 2019

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 Bobcares, 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.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

Categories

Tags

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF