📜 ⬆️ ⬇️

Everything you wanted to know about CLI, CGI, MOD, but hesitate to ask

... I know kung fu, karate, taekwondo and many more scary words!
(c) anecdote



Beginning admins recently asked me often “What is the difference between the php-cgi, php-cli and mod_php packages? And why, for example, is there a mod_python for Python, but no python-cgi? ”
At first it was funny, but then it became clear that this question had arisen at least once for everyone involved in administering LAMP (link) and similar servers.
The article was written based on my conversation with felvis , so I apologize in advance for some liberty of presentation.

A few words about the processes


Each process launched in the system has such a garbage as an environment — the environment — which is described by environment variables. In Linux-like systems with the coreutils package installed, you can view your environment with the command env . The environment of an arbitrary process can be viewed through the special /proc file system:
cat /proc/$PID/environ ( $PID - ) .

Another example, instead of the env command, you can run
cat /proc/self/environ

See the same thing, only in a less readable form :)
Everything, now that we need to understand the differences between CGI and CLI, we know. We go further.
')

Letters, letters


For PHP, we have the following packages in the system: mod_php, php-cgi and php_cli.
What is the difference?
mod_php - so-shka, which is additionally tightened at the start of Apache. When accessing the php script from the browser, the scripts are executed with the httpd process environment.
php-cgi - in addition, when Apache starts up, nothing pulls up and every time it is called
  http://www.domain.com/script.php 
properly configured web server pulls / usr / bin / php-cgi /path/to/script.php , forks a new process and the script is already running in the php-cgi environment (which can be very different from the httpd process environment).
Common to mod_php and php-cgi is that they are called by the web server.
Miscellaneous - the fact that the scripts are executed in different environments (for example, you can configure php-cgi so that it does not have access to certain directories in the system, or in general all scripts were started in the form - options for the sea. By the way, in general, php- cgi is a more secure way to handle php scripts).
php-cli - this binary is generally called directly by the user when there is a need to run a php script from the console. Accordingly, the environment in which the script is executed is user (that which is visible through env).
Try running this script from the console and through the browser:
<? php print $ _ENV [ 'PWD' ] ? >

* This source code was highlighted with Source Code Highlighter .

understand what is meant.

By the way, precisely because of the above, the same script will work differently from the console and through the browser. Developer, be careful when debugging!

About packages


Now for Python. Why is there mod_python , but not python-cgi and python-cli ?
Everything is simple, draw an analogy:
  mod_php -> mod_python
 php-cli -> / usr / bin / python
 php-cgi - python with mod_cgi 

It is necessary to clarify separately that CGI is the Common Gateway Interface for web servers (not just Apache). That is, CGI is just a technology with which you can do anything, any binaries that form a web page (although the programs written in C do not care). In Apache, this technology is implemented by mod_cgi - through this module Apache is pulled by a script that contains shebang #! / Usr / bin / python or #! / Usr / bin / php and so on - and the script is executed.
The main drawback of mod_cgi is the speed of execution of scripts, which is why they write separate modules for Apache that interpret PHP, Python, and shell scripts - this is faster.
Of course there is mod_fcgi , which is much faster than mod_cgi - but the description of mod_fcgi is beyond the scope of this article.
_________
The text was prepared in Habra Editor
The text used is a Binary picture from Brian "Visual Paradox" Kissinger .

Source: https://habr.com/ru/post/70716/


All Articles