Moving from Apache to Nginx with php-fpm

In order to address performance issues we have seen, our company has been switching from apache with mod_php to nginx with php-fpm.

Given that apache httpd webserver is an older code base and does not take advantage of event driven and asyncronous technology this seems to be the future. There are numbers of differences between these two.

Nginx webserver is product that came out of Russia, it had been more obscure, though now it is the #2 webserver. Apache webserver has been an important product in web based technologies for many years, and has been the dominant webserver. Php as a technology is often used in a module in apache (mod_php) or run through a cgi type where php runs in a separate process. To improve performance of cgi type operations the php supports fastcgi. Fastcgi runs a server which keeps a number of php processes live and waiting for traffic and runs them as a server, there are numerous advantages to using this.

At high load apache has been shown to not manage memory as efficiently as possible.

Where nginx really shines is in serving static content, where it’s asynchronous technology allows it to serve more content than other webservers.

Switching to Nginx has required changes, for instance taking out use of apache specific functions like getallheaders() which provides the headers in normal http header form, not in the cgi name scheme that you find in the $_SERVER variable, for instance User-Agent vs HTTP_USER_AGENT, and switching configuration from .htacess files to nginx rewrites.

Configuration of Nginx is different from apache, and ins more scripted than the style of apache.

As far as performance, the verdict is mixed, it can take time to tune nginx and php-fpm. If you are switching thinking it will be a fast win for php applications with peformance problems, think again, you need to look at your code. If you serve a lot of static assets, this might be a boost for you.

Experiences Developing PHP Extensions

In the past I had done a lot of development in C and C++. It used to be the language I worked the most with. C++ can help make developing a complicated application far easier than writing it in pure C. I have moved away into doing more Java work in the last four years. I started with PHP during the latter part of the C/C++ period. I wanted to try to work on developing a PHP extension based on some C and C++ code I had written a number of years ago.

Reading about extension writing appears very intimidating because of the Macros and Zend api aspects. Starting off from scratch this can be very challenging, particularly setting up the structures and the boilerplate as it were for the extension. PHP is written in standard K&R C so that it can be compiled on any platform, which is a great feature, but the code is very intricate, particularly when it comes to object oriented code and reference counting. There have been articles written about wrapping C++ classes into PHP extensions, which seems like a possibility in terms of working with C++ libraries. You physically can build extensions with C++, but for wider distribution on PECL writing your extensions in C is probably the smarter way to go.

I found a Pear project to do the code generation for PHP extensions. This project takes an xml descriptor including code blocks, which generates all the code especially the boilerplate code. It seems to be promissing, It seems it’s best to  continually doing edits in the .xml file and generating new sets of code whenever possible as it makes it easier adding new functions. This approach also adds the necessary support for unit tests. Certain aspects of it seems to not be as well flushed out as could be. In a more complex extension, going beyond what is provided with this project.

PHP uses the the GNU Autotools system, autoconfigure and automake to configure builds. It makes use of M4 macros to manage and extending the basic scripts. It provides a way to interrogate the system and determine the options needed to build a piece of code on a given system. Unfortunately this doesn’t work on Windows. Windows requires a separate build script system in JScript. Aspects of the M4 macros can be challenging to understand which may be a problem with more complex projects.

PHP uses a unit test system using files with snippets of code, expectations, etc, that once one completes make on the extension make test will execute the test and show test results. You write these tests through the .xml described above.

Comparisons of Three PHP MVC frameworks.

PHP is fast becoming one of the dominant languages in the web. It is easy enough for beginners, but has significant power for serious sites. Model View Controller (MVC) frameworks which have been used in many different contexts for desktop applications, mobile phone achitectures and web sites have become a significant force. Java’s Spring Framework  is great in Java and ported to C#. In PHP there are a number that are work considering, some key ones are Zend Framework, by the developers and maintainers of the PHP interpreter, Cake PHP, Code Igniter, and Symphony. I have been working with Zend professionally at work, and have been using Cake and Code Igniter in some personal projects, though have not worked with Symphony. I am going to discuss comparisons of the first three.

Cake PHP is a powerful strongly MVC standards based framework. It has a popular following, and a lot of nice features. If your application can fit into the strict cake pattern, it works well, when you can’t work within the framework’s expectations, it can be challenging and frustrating. This is similar to Spring in Java. Understanding the framework well enough to know how to implement your design is important. One challenge is how tightly coupled the database structure and the model is, including field and table naming. It also has substantial ORM features featureSimple joins where difficult, requiring unnecessary association tables for a simple project. Scripts are used to manage the application dealing with the mappings and creating objects. Cake is in active development and has a strong following, including fanatics.

Code Igniter is a lighter more flexible framework, with base classes that are highly optimzed and kept light by comparison, though you can add complexity as you need it. It has a strong user base and is in active development. For a smaller project, this is a good match. The coupling between model and database are looser. It also has the ORM mapping.  The model can be simple or complicated as necessary. A big project with a lot of complexity, may not be a good fit, but it has good features. Development is pretty fast. Php templating is standard, though a template system is integrated as a feature.

Zend Framework is a framework that is one of the best, but with limitations. I have been using it to build a large REST middle tier that to extend xmlrpc services provided by the Openx Adserver. It has features for many types of technologies, including integrating STOMP JMS features, web services, ajax,  json and front end view functionality. It has some decent database technology which  can work with more databases than just mysql. It uses a modern php style reminiscent of java, including interfaces, abstract base classes, access levels i.e. public, private, protected. It uses a substantial number of design patterns. Class naming is somewhat strict, though by doing so, gives wonderful autoloading features, so no include_once code just construct an object. Zend does not have the strong ORM features, though users have intergrated the Doctrine modeling framework. Others have also included the Ezcomponents framework which can help to do nice features. Php views are standard, though it’s easy to integrate smarty templates. An additional challenge is the structure of the app has changed frequently and technology has been added and improved so some documentation you may find from other sources may be out of date.