Server-Side Scripting – Help Understanding PHP and Ruby

cgiPHPrubyscriptingserver-side

As far as I understand, there are basically 3 options for doing server-side scripting these days:

  1. Using scripting languages that can be directly interpreted/executed by the web server (e.g., PHP and ASP), where the scripts are interpreted/executed on the fly (i.e., as HTTP requests come), the output is embed into HTML pages is then sent back to the client.

  2. Using any language (e.g., C, C++, PERL, Python) the operating system of the server is capable of executing (either using an interpreter or using the executable file already compiled) and then using CGI to communicate between the web server and the OS. Output of the scripts comes via CGI to the server in the form of complete HTML pages, and is then sent back to the client.

  3. Using Java on a server that can handle servlets/JSPs, which is pretty much the same idea as option 1 above, except using Java instead of PHP/ASP.

Questions:

  1. Is my understanding so far on track, or did I get something wrong?

  2. Are ASP and PHP the only languages that can be interpreted and executed directly by a web server?

  3. Where does Ruby fall in the classification above? Can it be interpreted/executed by servers like PHP? Or does it communicate via CGI?

  4. Is server-side scripting via CGI becoming obsolete or not at all?

Best Answer

Your understanding is correct, if you're from the past. You're pretty much describe as it looked like in 1990s.

Yes, many languages can be executed directly by a web server plugin. Right on for PHP, mod_php for Apache is still the most popular way to host it. However, high-traffic sites use more modern approach, using web server only as a proxy for FastCGI (in case of PHP it's PHP-FPM)

the output is embed into HTML pages is then sent back to the client.

I think you're referring to early 90's so called spaghetti code, however modern approach is to use one of many MVC frameworks. In case of PHP that would mean for example Zend Framework (there are numerous alternatives).

As for ASP, you're probably referring to so called "classic ASP", which is obsolete. Currently it's ASP.NET, which can use any of .NET languages (C# being the most popular), and of course the .NET framework.

C and C++ are not typically used for web application. If so, such services are implemented either as stand alone servers, as module for web server or as FastCGI.

Perl can be executed directly from web serve module using mod_perl. There is also PSGI, which is basically clone of Python's WSGI.

Python is very popular language for web apps. It can be directly executed from Apache web server via mod_python, however that is obsolete and not recommended. Currently the way to go with Python is either via WSGI server module. WSGI server implemented in Python (eg. CherryPy, WebPy) or using stand alone Python web stack (Tornado and Twisted's Web module are fine examples). And of course again, you'd most probably will be using WSGI compatible MVC framework, Django is the most popular one (again, multiple alternatives available).

Ruby, again very popular language for web apps. Best known for web framework Ruby on Rails, which again is MVC. You can execute Ruby directly from server module via mod_ruby or via FastCGI.

Servlets/JSP are executed in stand-alone J2EE application servers, such as JBoss or Tomcat. It's more commonly used to add web interface to business system rather than to create stand alone web apps.

Classical CGI (ie. spawning process on each request) has become obsolete many years ago. It has been replaced by FastCGI (where process is long-running, rather than spawned on each request), server modules, interfaces such as WSGI and clones and stand-alone solutions.

Also the paradigm of the request processing has evolved, with CGI it was process per request. Then was process pool (or thread pool), each process (thread) handling one request at a time. However now, most modern approach is for web servers and stand-alone frameworks to use event-driven programming.