Historically, PHP scripts run on every HTTP request. Starting, the script performs some kind of initialization (for example, it establishes a connection to the DBMS), after which it analyzes the request and generates a response. However, everyone knows that in the world of Python and Ruby a different approach has been adopted: web applications in these languages ​​are loaded into memory at the same time as the web server (or application server). The application server interacts with the script using standard
WSGI and
Rack interfaces. This approach is certainly not without flaws, the main one of which is probably related to a sharp increase in overhead costs when hosting a large number of sites on one server, however, it has an important advantage: initialization is performed only once, then the script only responds to incoming HTTP- requests.
Attempts to transfer the request processing cycle to the body of the PHP script have already been
made , while it was possible to achieve a significant increase in productivity. However, for this, you had to write a lot of code.
However, just the other day, the creators of uWSGI
implemented experimental interface
support , similar to WSGI / Rack, for PHP. Let me remind you that uWSGI is an extremely flexible and functional application server that supports almost all existing languages ​​and technologies that can be easily used as an alternative to PHP-FPM. Thus, it became possible to create web applications that are constantly loaded into memory, with much less blood.
The interface is called "phpsgi". The plugin that implements support for this interface is still quite raw, however, the developer has already expressed a desire to show it to the general public.
')
But I never mind you.
Installing a plug-in with an already configured uWSGI is extremely simple:
uwsgi --build-plugin https://github.com/unbit/uwsgi-phpsgi
After executing this command, the file “phpsgi_plugin.so” will appear in the current directory, which is enough to be placed in the directory with the other server plugins (usually / usr / lib / uwsgi). After that, you can configure the vassal (virtual node), or start the uWSGI instance manually. For simplicity, consider the second option.
uwsgi --plugin phpsgi --http-socket :9090
If you run such a command, uWSGI will refer to a file called “app.php”, which should contain the “application” function. At the moment, since the plugin is experimental, these names are strictly defined in the code. Consider the simplest example of PHP code that can be “fed” to this plugin.
<?php echo 'Loading'; function application($env) { return ['200 OK', ['Content-Type' => 'text/plain'], 'Hello, world!']; }
This example will display the string “Hello, world!” In the browser, while the string “Loading” will be displayed once in the uWSGI vassal log.