34,79 €
Improve the performance of your PHP application to ensure the application users aren't left waiting
This book is for those who have basic experience in PHP programming. If you are developing performance-critical applications, then this book is for you.
PHP is a great language for building web applications. It is essentially a server-side scripting language that is also used for general-purpose programming. PHP 7 is the latest version, providing major backward-compatibility breaks and focusing on high performance and speed.
This fast-paced introduction to PHP 7 will improve your productivity and coding skills. The concepts covered will allow you, as a PHP programmer, to improve the performance standards of your applications. We will introduce you to the new features in PHP 7 and then will run through the concepts of object-oriented programming (OOP) in PHP 7. Next, we will shed some light on how to improve your PHP 7 applications' performance and database performance. Through this book, you will be able to improve the performance of your programs using the various benchmarking tools discussed.
At the end, the book discusses some best practices in PHP programming to help you improve the quality of your code.
The book is a step-by-step guide to improve the quality of your code in PHP through real-time examples. The book takes a practical approach to improving the quality of your code.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 219
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2016
Production reference: 1190416
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-226-5
www.packtpub.com
Author
Altaf Hussain
Reviewer
Raul Mesa Ros
Commissioning Editor
Kunal Parikh
Acquisition Editor
Vinay Argekar
Content Development Editor
Priyanka Mehta
Technical Editor
Ravikiran Pise
Copy Editor
Shruti Iyer
Project Coordinator
Izzat Contractor
Proofreader
Safis Editing
Indexer
Rekha Nair
Graphics
Abhinash Sahu
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
Altaf Hussain is a fullstack web and mobile app developer with over 6 years of experience in PHP development. He received his degree in electrical engineering and specialized in computer and communications from Pakistan. Altaf is an electrical engineer on paper and a software engineer by heart.
He worked as a system programmer in his team, developing control software for small test robots using assembly languages and C. After this, Altaf got interested in web technologies and never looked back. He has worked with numerous PHP frameworks, including Zend, Laravel, and Yii, and open source systems such as Drupal, WordPress, PrestaShop, and Magento. Altaf designed and built two proprietary CMS systems with full support for multiple languages and models, permissions, and translations, as well as different kinds of multilingual content management. Now, he works in the fashion industry as the head of IT at shy7lo.com, where his role is to manage the development team on the premises and abroad, in order to manage Magento and Laravel applications development and the deployment life cycle. Besides web apps, Altaf has worked on iOS and Android applications, including building APIs in Lumen. He is a big fan of service-oriented architecture (SOA) and successfully uses it in different applications.
Altaf actively researches on website performance and has deployed the latest technologies, such as PHP 7, NGINX, Redis, Varnish, and others, in production environments for high-speed and scalable applications. He is a Debian lover and uses it for all of his web application deployments.
When not working, Altaf writes articles for programmingtunes.com and techyocean.com. He has reviewed several books for Packt Publishing, including Learning Phalcon PHP, Mastering jQuery Mobile, and PrestaShop Module Development.
I would like to thank my parents, wife, and son, Haashir Khan, who were all very helpful during the development of this book and my overall career. Without their help and support, this book would not have been possible to complete. I would also like to thank the PHP community for creating such awesome and beautiful tools and making the life of a web developer easy.
Raul Mesa has been writing software for the Web since 2006. He started with Java, later moved on to PHP, and obtained several certifications such as Zend Engineer and Zend Framework. Having worked on several high-traffic web projects, Raul is nowadays very interested in the DevOps philosophy.
He currently works as a senior web developer at EuroMillions.com using DevOps and PHP technologies. Raul also leads various small-to-medium sized projects.
His twitter handle is @rmrbest.
I would like to thank my wife, Noemi, and my daughter, Valeria, for their support and love, and also my father who bought me my first computer back in 1992.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
The PHP community faced a huge problem over decades: performance. No matter how powerful hardware they got, in the end, PHP was a bottleneck in itself. With PHP 5.4.x, 5.5.x, and 5.6.x, PHP's performance started to improve, but still it was a huge problem in high-load applications. The community developed caching tools such as Alternative PHP Cache (APC) and Zend OpCache, which cached the opcode for high performance, and these tools had a good effect on the performance.
To get rid of the performance issues of PHP, Facebook built their own open source tool called HHVM (HipHop Virtual Machine). According to their official website, HHVM uses the Just In Time (JIT) compilation to achieve superior performance while maintaining the development flexibility that PHP provides. HHVM had great performance compared to PHP, and it is widely used in production for heavy applications such as Magento.
PHP went to war with HHVM using PHP Next Generation (PHPNG). The whole purpose of PHPNG is to increase performance and focus on the rewriting and optimization of the Zend engine memory allocation and PHP data types. People around the world started benchmarking PHPNG and HHVM, and according to them, PHPNG was outperforming HHVM.
At last, PHPNG was merged with the master branch of PHP, and after a tremendous amount of optimization and complete rewriting, PHP 7 was released with huge performance improvements. PHP 7 is still not JIT, but its performance is great and similar to HHVM. This is a huge performance increase from the older versions of PHP.
Chapter 1, Setting Up the Environment, covers how to set up different development environments, including the installation of NGINX, PHP 7, and Percona Server on Windows, different Linux distros, and setting up the Vagrant virtual machine for development purposes.
Chapter 2, New Features in PHP 7, covers the major new features introduced in PHP 7, including Type Hints, Group use Declarations, Anonymous classes, and new operators, such as Spaceship operator, Null Coalesce operators, and the Uniform variable syntax.
Chapter 3, Improving PHP 7 Application Performance, covers different techniques to increase and scale a PHP 7 application's performance. In this chapter, we cover optimization of NGINX and Apache, CDN, and CSS/JavaScript, such as merging and minifying them, full-page caching, and installing and configuring Varnish. At last, we discuss an ideal infrastructure setup for application development.
Chapter 4, Improving Database Performance, covers techniques to optimize MySQL and Percona Server configuration for high performance. Also, it covers different tools to monitor the performance of a database. It also covers Memcached and Redis for caching objects.
Chapter 5, Debugging and Profiling, covers debugging and profiling techniques including the use of Xdebug for debugging and profiling, debugging with Sublime Text 3 and Eclipse, and the PHP DebugBar.
Chapter 6, Stress/Load Testing PHP Applications, covers different tools to stress and load test the application. It covers Apache JMeter, ApacheBench, and Siege for load testing. It also covers how to load test different open source systems such as Magento, Drupal, and WordPress on PHP 7 and PHP 5.6, and compares their performance on PHP 7 and PHP 5.6.
Chapter 7, Best Practices in PHP Programming, covers a few best practices for producing quality standard code. It covers coding styles, design patterns, service-oriented architecture, test-driven development, Git, and deployments.
Appendix A, Tools to Make Life Easy, discusses three of these tools in much more detail. The tools we will discuss are Composer, Git, and Grunt watch.
Appendix B, MVC and Frameworks, covers MVC design patterns and the most popular frameworks that are used in PHP development, which include Laravel, Lumen, and Apigility.
Any hardware specification that is compliant to run the latest versions of the following software should be enough to get through this book:
This book is for those who have basic experience in PHP programming. If you are developing performance-critical applications, then this book is for you.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."
A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/LearningPHP7HighPerformance_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
PHP 7 has finally been released. For a long time, the PHP community was talking about it and has still not stopped. The main improvement in PHP 7 is its performance. For a long time, the PHP community faced performance issues in large-scale applications. Even some small applications with high traffic faced performance issues. Server resources were increased, but it did not help much because in the end the bottleneck was PHP itself. Different caching techniques were used, such as APC, and this helped a little. However, the community still needed a version of PHP that could boost the application's performance at its peak. And this is where PHPNG comes in.
PHPNG stands for PHP next generation. It is a completely separate branch and is mainly targeted for performance. Some people thought that PHPNG is JIT (Just In Time) compilation, but in reality, PHPNG is based on a refactored Zend Engine, which was highly optimized for performance. PHPNG is used as a base for PHP 7 development, and according to the official PHP wiki page, the PHPNG branch is now merged into the master branch.
Before starting to build an application, the development environment should be finalized and configured. In this chapter, we will discuss setting up the development environment on different systems, such as Windows and different flavors of Linux.
We will cover the following topics:
All other environments can be skipped, and we can set up the environment that we will use.
There are many tools available that have Apache, PHP, and MySQL bundled for Windows, provide easy installation, and are very easy to use. Most of these tools already provide support for PHP 7 with Apache, such as through XAMPP, WAMPP, and EasyPHP. EasyPHP is the only one that also provides support for NGINX and provides easy steps to changes webserver from NGINX to Apache or Apache to Nginx.
XAMPP is also available for Linux and Mac OS X. However, WAMP and EasyPHP are only available for Windows. Any of these three can be used for this book, but we recommend EasyPHP as it supports NGINX, and for this book, we mostly use NGINX.
Any of the three tools can be used, but we require more control over every element of our web server tools, so we will also install NGINX, PHP 7, and MySQL individually and then connect them together.
NGINX Windows binaries can be downloaded from http://nginx.org/en/download.html. We recommend using a stable version, though there is no problem with using a mainline version. PHP Windows binaries can be downloaded from http://windows.php.net/download/. Download either 32-bit or 64-bit binaries of the non-thread safe version according to your system.
Perform the following steps:
The –b option starts PHP and binds to the path for external FastCGI servers. The preceding command binds PHP to loop back the 127.0.0.1 IP on port 9000. Now, PHP is accessible on this path.
To configure NGINX, open the nginx_folder/conf/nginx.conf file. The first thing to do is to add root and index to the server block, as follows:Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
Note the fastcgi_param option. The highlighted complete_path_webroot_folder path should be the absolute path to the HTML directory inside the nginx folder. Let's say that your NGINX is placed at the D:\nginx path; then, the absolute path to the HTML folder will be D:\nginx\html. However, for the preceding fastcgi_param option, \ should be replaced by /.
Now, restart NGINX by issuing the following command in the root of the NGINX folder:On Windows and Mac OS X, we recommend that you use a virtual machine installed with all the tools on a Linux flavor to get the best performance out of the server. It is easy to manage everything in Linux. There are vagrant boxes available that have everything ready to use. Also, a custom virtual machine configuration with all the tools, including NGINX, Apache, PHP 7, Ubuntu, Debian, or CentOS, and other great ones, can be made at https://puphpet.com, which is an easy-to-use GUI. Another nice tool is Laravel Homestead, which is a Vagrant box with great tools.
Ubuntu is derived from Debian, so the process is the same for both Ubuntu and Debian. We will use Debian 8 Jessie and Ubuntu 14.04 Server LTS. The same process can be applied to desktop versions for both.
First, add the repositories for both Debian and Ubuntu.
As of the time we're writing this book, Debian does not provide an official repository for PHP 7. So, for Debian, we will use dotdeb repositories to install NGINX and PHP 7. Perform the following steps:
The first two commands will add dotdeb repo to Debian and the last command will refresh the cache for sources.
As of the time of writing this book, Ubuntu also does not provide PHP 7 in their official repos, so we will use a third-party repo for the PHP 7 installation. Perform the following steps:
The rest of the process is mostly the same for both Debian and Ubuntu, so we wont list them separately, as we did for the adding repositories section.
The following is a list of three useful NGINX commands:
This will install PHP 7 along with the other modules mentioned. Also, we installed
