Does the Apache convert PHP code to some forms? - php

In some programming languages like C,C++,c#,Java,..etc when the code is compiled then the code is converted into another form in order to execute it. Does the Apache do the same or just it executes it without any conversion?

Apache doesn't do anything except handle the incoming request and serve the resulting output. Everything else is done by the PHP interpreter which pre-compiles the PHP code to a bytecode form, and then executes the bytecode instructions.

Apache is just a webserver that may or may not be running with PHP as a module. It's better to think of the webserver as a mere mediator between the frontend and the php binary.
The latter is compiled, yes, but it runs your code without compiling. It is an interpreted language.
There are ways to accelerate php processing using some opcode cache or just in time compilers, but default PHP doesn't deal with that.

Related

How does the PHP interpreter interact with Apache?

We've been thinking on writing a php interpreter for a project we've been working on. Interpreting aside, I've been trying - with no success - to find how the PHP interpreter interfaces with the http server. So I've come to you, o' dear knowledgeable people of Stack Overflow.
I have read that php listens on localhost:9000. Goes without saying that I tried to connect to that and it was no use. Maybe the webserver runs "php file.php" and gets the output?
Also, could a php interpreter that interprets .php files and our interpreter (lets call it pqp for the sake of argument) that interprets .pqp files coexist in a single webserver?
Thank you very much!
There are certain ways in which PHP interacts with a webserver. One is indeed to pipe PHP files into the PHP cli and serve the output as in this question. As I wrote there: It's really not recommended.
The next best thing is to expand the sapi of PHP, which is providing a set of connectors to a webserver. In this case, PHP is going to act like a webserver module (see apache+mod_php). A downside of this is that PHP is trying to parse everything it gets its hands on.
A webserver-independent method is to connect to PHP via the Common Gateway Interface, whcih is a well-defined interface for various interactive serverside components. It is, however, terribly slow (and a bit insecure).
The final step is to implement FastCGI. This is an improvement over CGI in that it tries to connect to a running process. In the PHP world, this is achieved through php-fpm which (by default) indeed listens on 9000/tcp.

Why does PHP just work with Ajax but Python doesn't?

Building a PHP script that responds to an Ajax request is as easy as:
<?php
$command = $_POST["command"];
if ($command == "say_hello") {
$name = $_POST["name"];
echo json_encode(array("message" => "Hello, " . $name));
}
?>
and, at least if you're using jQuery on the client side, and if you specified a callback function on the original request, the array containing the message will be passed into that callback function.
But with Python it's not that simple. Or at least I haven't figured out how to make it that simple. If I just try to "print" a response (paralleling PHP's "echo" statement above) the client doesn't get anything back.
Whenever I've looked on the internet for how to respond to an Ajax request with Python, the answer always involves using Django or some other web framework.
And I know those things are great, but what is PHP doing that makes using a similar package unnecessary? I would like to be writing my server-side script in Python instead of PHP, but I'd prefer a D.I.Y. approach to using a third-party framework for what should be (right?) a pretty simple task.
Any help? An example of how to do this would be much appreciated.
But with Python it's not that simple. Or at least I haven't figured out how to make it that simple. If I just try to "print" a response (paralleling PHP's "echo" statement above) the client doesn't get anything back.
I might have some of the details wrong, but I'll try to explain why this is the case. Firstly, the PHP you're talking about is baked directly into the apache webserver. When you do an echo, it outputs the result to the response stream (a TCP connection between the server and client). When you do print in python, what you're doing is outputting the result to standard out (the command line).
How most languages work with the web, is that a request comes in to some kind of web server, and that web server executes a program. The program is responsible for returning a stream, which the webserver takes, and streams to the client over the TCP connection.
I'm assuming that the webserver redirects PHPs standard out to the webservers stream, and that is how PHP is able to use echo to return its result. This is the exception, not the rule.
So, to use python to serve requests, you need to have a webserver that can execute python in some way. This is traditionally done using CGI. More recently, something like mod_python was used to host the python within apache. Nowadays, wsgi or mod_wsgi is used, which is the standard defined for webservers talking to python.
You might want to look at something like web.py, which is a minimalist python web framework. This will get you very very close to what you're trying to do.
PHP was built with the web in mind from the very beginning. In contrast Python was designed as a general purpose language.
When you echo from PHP you're actually writing to a stream that is sent to the user as part of an HTTP response. When you do a print in python the output, by default, is written to the stdout stream which means instead of being sent to the user over http the output is written to the console (or whatever is capturing stdout at the moment).
So to PHP, HTTP is a first class citizen. To more general purpose languages like Python, Ruby, Erlang, C, C++, and so many other languages. You have to communicate to HTTP in different ways. PHP already handles that communication through apache's mod_php or through something like PHP-FPM.
Sooo....
As far as creating your own server side script, I'd highly suggest against it as Python's Frameworks take the place of the layer that PHP is built on. So creating a standards compliant http server on your own with Python isn't going to be very simple. The reason that this is so hard is that you'd either have to interface with CGI or WSGI (a Python standard for dealing with the web) or create your own HTTP server. If you think you're up to the task then I highly suggest it! You'd probably learn a whole lot by doing it, but it isn't going to be the easiest thing. The great thing is that so many libraries already take care of this communication for you. For example, if you're looking for something lightweight, I highly suggest a micro-framework like flask which is one I personally use if I need something very simple. It is much easier to get started than django, but it also has less batteries included.
Hope this helps!
There is a python module called json. Perhaps so many of the answers deal with frameworks (like Django) because json is web-centric and Django, tornado, twisted, cherrypy, etc. all are the means by which python interacts with the web.
There are many different ways to do this, and those are addressed in other questions. However, to just answer your question, python has a json library in its standard library.
Most PHP installers configure themselves by default to serve php files in web directories by running the script. Most Python installers do not. Quickest way to serve Python with apache:
In your apache config file, add AddHandler cgi-script .py to whatever block can already serve PHP files
shebang your python scripts by adding #!/usr/local/bin/python or whatever path python is on in your system
remember to print headers first, followed by a newline, then body.
use import cgi if you want to do anything more complicated than sending back a response body.
Note that this is not the recommended way to run a python website, just the fast, php-ish way.
Richardhsu has the gist of it correct.
Essentially python is a general purpose scripting language. As such, it needs a way to connect to the web, such as by opening a port and doing network programming etc. You can also use something CGI to attach to apache or other web server.
This is really no different then how Java, Ruby, Perl or C/C++ get to the web either. In fact, PHP does much the same thing. It's interface to the web server is just tighter then CGI.
If you were to have PHP interpreted from the command line, or compiled, then you would have the exact same issues that you do with Python now. The issue isn't PHP vs Python, it's how you get the scripting language of your choice attached to the internet using the HTTP protocol.

Why PHP (script) serves more requests than CGI (compiled)?

I developed the following CGI script and run on Apache 2 (http://localhost/test.chtml). I did same script in PHP (http://localhost/verifica.php). Later I performed Apache benchmark using Apache Benchmark tool. The results are showed in images.
include
#include <stdlib.h>
int main(void)
{
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<TITLE>Multiplication results</TITLE>\n");
printf("<H3>Multiplication results</H3>\n");
return 0;
}
Someone can explain me why PHP serves more requests than CGI script?
A call to a standalone CGI program results in a fork / exec - the new program has to be loaded completely. This is not very efficient.
Iniitially PHP ran this way, but to speed things up mod_php was developed, where the PHP interpreter ends up built-into and running inside the process space of the Apache server and all it has to do is some simple parsing.
The only thing that you're really measuring here is the overhead caused by an almost no-operation program. The overhead incurred by calling a script or a cgi depends on how your Apache server is configured. Chances are you're using php as a module, meaning that it actually runs inside the apache process. For the CGI you're probably using the regular flavor, ie a process is created for every call.
Depending on what you actually want to test/know/evaluate, you should probably rerun this test a number of times, eg with extensive calculations in php vs a C cgi, using fastcgi, and whatnot. Also, don't forget to check the impact of code caches like APC on the execution of the php code, for certain cases the difference is dramatic.

How to call PHP preprocesser

I have written a web server that servers html,js,css,images etc using C. I want to develop it that it has the capability to server PHP content also.
In that case I think I want to pass the PHP content to PHP preprocesser dynamically. How to call the PHP preprocesser.( I am using Ubuntu)
The easiest way is to invoke the PHP-CGI binary and communicate with that. Here is an example implementation in PHP, but it's quite simple in C as well:
http://nanoweb-instant.googlecode.com/svn/trunk/modules/mod_cgi.php (advisable)
http://nanoweb-instant.googlecode.com/svn/trunk/modules/mod_fcgi.php (avoid)
FastCGI is indeed faster, but is a fugly procotol. So I would recommend to avoid it. (SCGI is nicer, but not available for PHP).
If you really must, then look into http://www.fastcgi.com/drupal/node/5 for a readymade C-library for this purpose.
You could process the PHP file through the command line functions.
http://www.php.net/manual/en/features.commandline.options.php
I believe you would do; php -f <file>
I recommend you to implement the FastCGI protocol. PHP can be run as FastCGI, as well as other scripting languages. This makes it possible to have PHP processes running as daemons, which speeds up page access times compared to traditional CGI and commandline calls.
The best possible way to do this is to implement CGI or FastCGI, which PHP both supports
You could communicate with the PHP interpreter through FastCGI. This should perform better than calling php for each request.

Mixing Python and PHP?

I have some Python scripts that I run on my desktop now for cutting up files. I want to put them on the web and write a simple front-end in PHP where a user uploads a file and it is passed as an argument to a python script on the web server and it is written out in chunks and the user can re-download the chunks.
I know a decent amount of PHP, but I do not see:
How to mix PHP and Python programmatically
Is it possible to have a webpage in python that can just call the python script? Can one have a GUI page that is like zzz.com/text.py as example
For http requests, you need to set your web server to hand over certain request to PHP and others to Python. From within PHP's scripts, if you need to call some Python executable scripts, use one of PHP's shell functions. e.g. exec()
Yes it is possible. The djangobook is a nice tutorial that covers this in one of the earlier chapters. It shows you how to run python as a cgi or with apache.
On a personal note, if you have time to dig deeper into Python, I'd strongly encourage you to do the whole thing in it, rather than mix things with PHP. My experience tells me that there are probably more cases where a PHP app needs some Python support rather than the reverse.
If the supporting language can do everything that the main language does, what's the point of using the main language?

Categories