The Alternative PHP Cache (APC) is a robust, open-source framework that optimizes and caches PHP intermediate code. It’s a PECL extension, sharing the packaging and distribution system with its sister, PEAR.
This tutorial will guide you through the process of enabling APC (Alternative PHP Cache) to enhance the speed of PHP on your server.
Whether you’re using a dedicated server, a VPS server, or even cloud hosting or shared hosting, APC can be a valuable addition to your PHP environment.
Step 1: Install APC using the yum command
To install APC, you’ll need to use the yum command. Here’s the command you need to enter:
[root@centos63 ~]# yum install php-pecl-apc -y
After running this command, the system will resolve dependencies and install the necessary packages. Once the installation is complete, you’ll see a message indicating that php-pecl-apc and php-pear have been installed.
Example:
[root@centos63 ~]# yum install php-pecl-apc -y Loaded plugins: fastestmirror, presto Loading mirror speeds from cached hostfile * base: mirrors.hostemo.com * extras: mirrors.hostemo.com * updates: mirrors.hostemo.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package php-pecl-apc.i686 0:3.1.9-2.el6 will be installed --> Processing Dependency: /usr/bin/pecl for package: php-pecl-apc-3.1.9-2.el6.i686 --> Processing Dependency: /usr/bin/pecl for package: php-pecl-apc-3.1.9-2.el6.i686 --> Running transaction check ---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ==================================================================================================== Package Arch Version Repository Size ==================================================================================================== Installing: php-pecl-apc i686 3.1.9-2.el6 CentOS6.3-Repository 96 k Installing for dependencies: php-pear noarch 1:1.9.4-4.el6 CentOS6.3-Repository 393 k Transaction Summary ==================================================================================================== Install 2 Package(s) Total download size: 489 k Installed size: 2.5 M Downloading Packages: Setting up and reading Presto delta metadata Processing delta metadata Package(s) data still to download: 489 k ---------------------------------------------------------------------------------------------------- Total 3.3 MB/s | 489 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : 1:php-pear-1.9.4-4.el6.noarch 1/2 Installing : php-pecl-apc-3.1.9-2.el6.i686 2/2 Verifying : 1:php-pear-1.9.4-4.el6.noarch 1/2 Verifying : php-pecl-apc-3.1.9-2.el6.i686 2/2 Installed: php-pecl-apc.i686 0:3.1.9-2.el6 Dependency Installed: php-pear.noarch 1:1.9.4-4.el6 Complete!
Step 2: Specify Shared Memory
Next, you’ll need to specify the size of the shared memory. To do this, open the apc.ini file:
[root@centos63 ~]# vi /etc/php.d/apc.ini
On line 12, specify the shared memory size:
; The size of each shared memory segment, with M/G suffix apc.shm_size=512M ; A "hint" about the number of distinct source files that will be included or ; requested on your web server. Set to zero or omit if you are not sure;
Step 3: Restart Apache
After specifying the shared memory size, you’ll need to restart Apache for the changes to take effect. Use the following command to restart Apache:
[root@centos63 ~]# /etc/rc.d/init.d/httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ]
Step 4: Verify APC Installation
Finally, to ensure that APC has been enabled, create a “phpinfo” file:
[root@centos63 ~]# vi /var/www/html/phpinfo.php
In this file, add the following lines:
<?php phpinfo(); ?>
After saving and closing the file, open it in a web browser. You should see APC listed among the enabled modules.
Commands Mentioned
- yum install php-pecl-apc -y – This command installs the APC extension on your server.
- vi /etc/php.d/apc.ini – This command opens the APC configuration file where you can specify the shared memory size.
- /etc/rc.d/init.d/httpd restart – This command restarts the Apache server, allowing the changes to take effect.
- vi /var/www/html/phpinfo.php – This command creates a new PHP file where you can check if APC has been enabled.
Conclusion
Enabling APC with PHP on CentOS 6.3 can significantly enhance the performance of your PHP applications. This tutorial has guided you through the process of installing APC, specifying shared memory, restarting Apache, and verifying the APC installation.
By following these steps, you can optimize your PHP environment and make your server more efficient.
By implementing APC, you’re taking a significant step towards optimizing your server and enhancing the performance of your PHP applications. If you have any further questions, refer to the FAQ section or feel free to ask.