What is CGI mode? - cgi

What does it mean when we say an application can run in CGI mode? I was reviewing the features of various CMS systems on cmsmatrix.org and "CGI mode support" was listed as a feature. What are the other "modes" in which a web application can run?

Basically, CGI implies that the webserver will execute an external process, get its result (a generated HTML page, an image, ...) and send it back to the client.
This has major drawbacks because it launchs the external process each time it is needed, so this can be a big overhead.
You have also FastCGI that launch the external process once, and reuses it when needed.
But usually, languages are integrated directly in the webserver.
For example, Apache has a mod_perl module to execute perl scripts, instead of executing perl scripts via CGI

CGI stands for 'Common Gateway Interface', which is an old architecture for web applications. CGI works by placing the variables from the HTTP request and fork/exec()ing the CGI process. It gained popularity in the early days of web development as it worked well on a unix host. Perl/CGI was a popular architecture in this era and it contributed substantially to the popularity of Perl as a language.
The main claim to fame of CGI is that it doesn't require much plumbing, so it will work with most web servers. The main drawback is that the fork-exec process is slow-ish as the CGI script has to be started (which may involve starting a perl or other interpreter). On Windows, spawning a new process is much slower than unix, so CGI is even more inefficient.

CGI is a protocol that is used by web servers to call executable files on the server. Upon receiving a request, it sends the information about the request to the cgi script and returns the result of that script back to the browser.
An alternative to that is fastcgi. This means, that the web server does not contact a script to answer the request, but a process. The communication protocol is still the same, though (hence the name).

Related

What is HTTPD exactly?

I mean is "httpd" only used by Apache for the download of the software or is it used by other websites as well? Also is it necessary to have httpd to run "cgi" or not?
And why does Apache use httpd to download the http server instead of having it in a file on their http website?
Apache HTTPD is an HTTP server daemon produced by the Apache Foundation. It is a piece of software that listens for network requests (which are expressed using the Hypertext Transfer Protocol) and responds to them.
It is open source and many entities use it to host their websites.
Other HTTP servers are available (including Apache Tomcat which is designed for running server side programs written in Java (which don't use CGI)).
CGI is a protocol that allows an HTTP server to use an external piece of software to determine how to respond to a request instead of simply returning the contents of a static file. Many HTTP servers support the CGI protocol.
You can use CGI without an HTTP server, but this typically has few uses beyond allowing a developer to perform command line testing of the CGI program. (You certainly can't interact with it directly from a web browser).
HTTP Daemon is a software program that runs in the background of a web server and waits for the incoming server requests. The daemon answers the request automatically and serves the hypertext and multimedia documents over the Internet using HTTP.
Apache Httpd is basically a web server used for handling requests and delivering static content. While CGI is a protocol which adds a scripts with the request and based on the script the content is delivered instead of simply returning a static content. So it is not necessary to use CGI with apache httpd but for delivering a dynnmic content httpd and cgi are used together.
Also using httpd with cgi is a very heavy process of delivering dynamic content as it creates and destroys process with every request response cycle, there are many other efficient alternatives with latest technology.
HTTPd - HyperText Transfer Protocol Daemon
HTTPd is a software program, that usually runs in the background, as a process.
It plays the role of server in a client-server model using HTTP and/or HTTPS network protocols.
HTTPd waits for the incoming client requests and for each request it answers by replying with requested information.
Following are some commonly used HTTPd
Apache
BusyBox
CERN HTTPd
Lighttpd
Ngnix

Running Twisted on Azure Websites

Can Azure Websites host Twisted applications? e.g. something like:
from twisted.internet import reactor
from twisted.web import server
site = server.Site(myresource)
reactor.listenTCP(80, site)
reactor.run()
From http://azure.microsoft.com/en-us/documentation/articles/web-sites-python-configure/ it sounds like only WSGI apps are supported, but just wanted to confirm from an Azure Websites expert that there's no way to directly run something like the above.
--
This excerpt from discussion with Glyph (Twisted author) in the #twisted.web IRC channel covers the Twisted half of this question:
16:53:28 glyph: twisted has a WSGI _container_
16:53:34 glyph: twisted _is not_ a WSGI application
16:53:36 glyph: in any part
16:53:43 glyph: so you can't make twisted into a WSGI app
16:53:55 glyph: you can maybe invoke some Twisted code _from_ a WSGI app
16:54:05 glyph: but what that example is doing is speaking HTTP, and WSGI applications have to speak WSGI, they are not allowed to speak HTTP directly.
16:56:47 tos9: crochet?
16:56:56 glyph: tos9: crochet can't eat the inbound HTTP socket
16:56:58 glyph: tos9: so it doesn't help
16:57:11 glyph: you could write a thing that did the _outgoing_ traffic with Twisted, but since you can't handle the inbound request, you're bummed
16:57:37 glyph: basically Twisted's job is doing network I/O and if you're inside a WSGI stack, someone else is already doing the job of doing the network I/O
If there is in fact no way to directly run something like this, it seems like choosing a language other than Python buys you more flexibility on Azure Websites. For example, from http://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/ it looks like you can host a Node app on Azure Websites which speaks HTTP directly. Confirmations or corrections gratefully received.
Please check https://social.msdn.microsoft.com/Forums/en-US/ed1c80c4-4621-4d02-8902-6ecc1166ac8c/running-twisted-on-azure-websites?forum=windowsazurewebsitesprevie&prof=required for answer.
As you described in Running Twisted on Azure Websites .
What you said is right. For node.js, you can host a Node app on Azure Websites which speaks HTTP directly. Please refer to http://blogs.msdn.com/b/hanuk/archive/2012/05/05/top-benefits-of-running-node-js-on-windows-azure.aspx
For Python, there's no direct way run twiisted code via http.
Yes, it can. Their main page says
Azure supports any operating system, language, tool, and framework— from Windows to Linux, SQL Server to Oracle, C# to Java.
The page you referenced shows a basic template - it is an example of how to set up a particular WSGI application. You don't have to do it this way.
You can setup a virtual machine and put anything you like on it.
If Twisted doesn't require any specific binaries to be installed (no registry keys, hard coded path requirements, etc) then you can just copy the relevant binaries over with your website and invoke those bits instead.

Communicating between Apache and a different process

I want to communicate between Apache and an external process.
I can modify the source of the process (written in C++) as much as I want, but Apache should (hopefully) remain the same. I was thinking about just using an Intranet socket between PHP and the program, but that just seems inefficient and hard to do if there are multiple page loads at once, and using a file is even worse.
Essentially, Apache (and PHP) would query the external program, and should read or modify a hashtable. How should I go about doing this?
Make your 'external process' expose an HTTP server, then reverse-proxy from apache to that HTTP server. Done.

nginx/apache/php vs nginx/php

I currently have one server with nginx that reverse_proxy to apache (same server) for processing php requests. I'm wondering if I drop apache so I'd run nginx/fastcgi to php if I'd see any sort of performance increases. I'm assuming I would since Apache's pretty bloated up, but at the same time I'm not sure how reliable fastcgi/php is especially in high traffic situations.
My sites gets around 200,000 unique visitors a month, with around 6,000,000 page crawls from the search engines monthly. This number is steadily increasing so I'm looking at perfomrance options.
My site is very optimized code wise and there isn't any caching (don't want that either), each page has a max of 2 sql queries without any joins on other tables, indexes are perfect as well.
In a year or so I'll be rewriting everything to use ClearSilver for the templates, and then probably use python or else c++ for extreme performance.
I suppose I'm more or less looking for any advice from anyone who is familiar with nginx/fastcgi and if willing to provide some benchmarks. My sites are one server with 1 quad core xeon, 8gb ram, 150gb velociraptor drive.
nginx will definitely work faster than Apache. I can't tell about fastcgi since I never used it with nginx but this solution seems to make more sense on several servers (one for static contents and one for fastcgi/PHP).
If you are really targeting performance -and even consider C/C++- then you should give a try to G-WAN, an all-in-one server which provides (very fast) C scripts.
Not only G-WAN has a ridiculously small memory footprint (120 KB) but it scales like nothing else. There's work ahead of you if you migrate from PHP, but you can start with the performance-critical tasks and migrate progressively.
We have made the jump and cannot consider to go back to Apache!
Here is a chart showing the respective performances of nginx, apache and g-wan:
g-wan.com/imgs/gwan-lighttpd-nginx-cherokee.png
apache does not seem to lead the pack (and that's a -Quad XEON # 3GHz).
Here is an independent benchmark for g-wan vs nginx, varnish and others http://nbonvin.wordpress.com/2011/03/14/apache-vs-nginx-vs-varnish-vs-gwan/
g-wan handles much more requests per second with much less CPU time.
NGINX is the best choice as a webserver now a days.
The main difference between Apache and NGINX lies in their design
architecture. Apache uses a process-driven approach and creates a
new thread for each request. Whereas NGINX uses an event-driven
architecture to handle multiple requests within one thread.
As far as Static content is concerned, Nginx overpasses Apache.
Both are great at processing Dynamic content.
Apache runs on all operating systems such as UNIX, Linux or BSD and
has full support for Microsoft Windows & NGINX also runs on several
modern Unix-like systems and has support for Windows, but its
performance on Windows is not as stable as that on UNIX platforms.
Apache allows additional configuration on a per-directory basis via
.htaccess files. Where Nginx doesn’t allow additional configuration.
Request Interpretation-Apache pass file System location. Nginx
Passes URI to interpret requests.
Apache have 60 official dynamically loadable modules that can be
turned On/Off.Nginx have 3rd Party core modules (not dynamically
loadable).NGINX provides all of the core features of a web server,
without sacrificing the lightweight and high-performance qualities
that have made it successful.
Apache Supports customization of web server through dynamic modules.
Nginx is not flexible enough to support dynamic modules and loading.
Apache makes sure that all the website that runs on its server are
safe from any harm and hackers. Apache offers configuration tips for
DDoS attack handling, as well as the mod_evasive module for
responding to HTTP DoS, DDoS, or brute force attacks.
When Choose Apache over NGINX?
When needs .htaccess files, you can override system-wide settings on
a per-directory basis.
In a shared hosting environment, Apache works better because of its
.htaccess configuration.
In case of functionality limitations – use Apache
When Choose NGINX over Apache?
Fast Static Content Processing
Great for High Traffic Websites
When Use Both of them -Together
User can use Nginx in front of Apache as a server proxy.

Changing cgi to Fastcgi

How feasible is to change a C/C++ cgi application to Fastcgi? Does this require only change in code? Or will it require a change in the setup of apache server?
What will be the obvious benefits of the change? Is the change from cgi to Fastcgi worth the benefits?
The major benefit of switching to FastCGI is that your application will run continuously in the background, instead of being started for each request. If your app has a lot of initialization, you could see a big speed boost by cutting out that time.
It will require reconfiguring Apache, but it's not complex. And it should require changing your app, because now there's only one instance per server, not one instance per request. You'll have to decide for yourself if that change is feasible; it really depends on how your code is structured.