Install and run the PHP accelerator APC
Now that we have the benchmark results for the default environment, let’s continue with our high speed tuning. First you will install the PHP accelerator APC.
Before installing, let me explain the basics behind PHP execution. PHP execution occurs in two stages. In the first stage, the PHP source code is changed into intermediate code (byte code) by syntax analysis. In the second stage, the intermediate code is rendered by the Zend Engine.
A PHP accelerator takes the intermediate code produced in the first stage and caches it for later use. If there are no changes to the original PHP source code, then the PHP accelerator can speed up the execution time of PHP by skipping the first stage. If the PHP source code is edited, then the cached intermediate code is discarded so even with the presence of a PHP accelerator, the speed will not change.
There are many PHP accelerators but we will use the easy-to-install APC.
First, use yum to install the necessary modules to use APC.
[root@ip www]# yum install php-devel php-pear gcc -y
Next, use the pecl command to install APC. An installation wizard will start, so just keep hitting Enter to continue.
[root@ip-172-30-1-150 www]# pecl install apc
Once compiled, the apc.so file will be created. This is the body of APC. Save the following code as “/etc/php.d/apc.ini”.
extension = apc.so apc.shm_size = 128M apc.enable_cli = 1; apc.filters = filename
[/etc/php.d/apc.ini]
“apc.shm_size” is the shared memory size, and “apc.enble_cli” allows you to use APC with Command Line Interface PHP. “apc.filters” is commented out with ; because this is used to make exceptions to the intermediate code caching. If you have issues with the cache, you can make changes to this filename. Generally, you don’t need to.
Once your changes are saved, restart Apache.
[root@ip www]# systemctl restart httpd
Return to your site and refresh the page. The first time will take longer than usual, so please refresh it a second time. Your page load time should have decreased. In my example the time was 70ms. The default was 176ms.
Use the ab command again and do a benchmark test.
[root@ip www]# ab -n 100 -c 10 http://ec2-xxx.xxx.compute.amazonaws.com/
In my example, the requests per second was 29.20. The summary of my results is as follows:
Tuning contents | Page load time
(compared to default) |
Requests per second |
Default environment | 176ms | 11.24 |
APC | 70ms (250%) | 29.20 |
As you can see, performance increased by 250% with the installation of APC.
In the next article, I will teach you even more high speed techniques that use only the CentOS 7 basic repositories. Until next time!