PHP 7 in Production
PHP 7 in Production
Since the announcement of the stable release of PHP 7.0.0 on December 3rd, I’ve been excited to see what the performance gains would be in a live production environment. I know that there are a lot of PHP skeptics, but there is a reason that they jumped from PHP 5 to PHP 7.
Join the DZone community and get the full member experience.Join For Free
Since the announcement of the stable release of PHP 7.0.0 on December 3rd, I’ve been excited to see what the performance gains would be in a live production environment. I know that there are a lot of PHP skeptics, but there is a reason that they jumped from PHP 5 to PHP 7. This upgrade represents a big move forward in PHP code efficiency. As I consider myself somewhat of a data scientist, I wanted to write up my experience in such a way that anyone could go out and duplicate the results that I got.
At ZipBooks, we have one server that runs a Laravel-based accounting application and another server that runs a WordPress website. Our PHP upgrade on our Laravel app is yet to be completed but I have finished the upgrade for the server running Wordpress.
We have Wordpress 4.4 running on a single-core Digital Ocean server. There is nothing fancy about it. We use Nginx, PHP, and vanilla MySQL. I used New Relic to generate the graphs and Apache Bench to run some basic stress tests.
I upgrade PHP on the production server to hold as many variables constant as I could. I was most interested in what the change in requests per second would be at 100% CPU load.
Stress Testing PHP 5
I hadn’t yet set up any kind of server monitoring on our Wordpress server so I took this opportunity to set that up so I could make some nice graphs to look at. Then I installed Apache Bench and ran a couple stress tests to see what it would take to push our CPU utilization to 100%. I ended up sending 10,000 requests with 100 concurrent connections. I want to make sure that I had a big enough sample to get a meaningful throughput metric.
Here is the command I used:
sudo ab -n 10000 -c 100 https://zipbooks.com/
Here are the results:
You can see that I did a good job of stressing our poor single core server. The main metric I am going to compare is requests/second.
The little bump before was me just testing out Apache Bench to see what a baseline test would do in terms of resource demand.
PHP 7 Upgrade
I wouldn’t consider myself a server management wizard, but even for me the upgrade was extremely straight-forward. I used the instructions in the comments here.
How to add a Personal Package Archive (PPA)
Add ‘add-apt-repository’ if you don’t already have it:
$ apt-get install python-software-properties
Add PHP 7 package from Ondřej Surý’s website.
$ add-apt-repository ppa:ondrej/php-7.0
Backup any config files that you made custom changes to because you about to over write them.
The next command is going to update your package lists, remove PHP 5 and its dependencies and install PHP 7.
$ apt-get update && apt-get purge php5-fpm && apt-get --purge autoremove && apt-get install php7.0-fpm php7.0-mysql
In my case, I had to go into my Nginx configuration file and change where is was looking for the php socket. I had a little issue there because my php5-fpm socket was in a different folder.
Restart PHP and Nginx.
Stress Testing PHP 7
I liked the approach of stress testing the same server before and after the upgrade because it eliminates a lot of potential unmeasurable variables that could affect a benchmark test. So I have the same server, the same programs, and the same benchmark test. The only thing that has changed in the version of PHP that I am testing.
Here are the results:
Looks like our Wordpress website went from being able to handle 14 requests / second to 46 requests / second. That is a 228% gain in throughput. It cut the CPU load by 50%.
Below is a map of CPU usage over time during the two tests.
I am working on a free accounting software program that is great for tracking expenses and your time as a contractor. You can set people up on recurring billing and process payments via cc. It is built on Laravel and once we upgrade to PHP 7 it will run snappier than ever.
Feel free to check out my Quora profile, Brad Hanks.
ZipBooks is also pretty great at recurring billing. Here's our invoice template.
Published at DZone with permission of Brad Hanks . See the original article here.
Opinions expressed by DZone contributors are their own.