Improve PHP Performance with APC

PHP isn't know for it's performance out of the box. Sure, it works and it works well but PHP, like other scripting languages, is not very efficient with server resources. Scripting languages have to take the code, compile it, and then execute the compiled code. Every time a page is loaded the code is compiled and this step can take up to 90% of the time and resources on every page load. This is different from a program on your computer where the code is already compiled. If you could eliminate this step a server could handle more page requests. This is where opcode caches, like APC and others, can greatly improve performance by letting us skip this step most of the time. Let's take a look at APC.

Alternative PHP Cache, known as APC, allows your web server to save the compiled PHP so it can skip that compiling step. This can give your server a huge performance boost. In the rest of this post I'm going to talk about how to specifically install APC on Media Temples Dedicated Virtual hosting. If you are using a different host the concept will be the same but the layout and exact structure may be different.

Before you install APC make sure you have the Developer Tools installed and have root access to the server. Both of these you can request through the control panel.

First, download the latest version of APC from http://pecl.php.net/package/apc and put it in the /usr/local/src folder on your server (At this point 3.0.16 is the most recent version).

To decompress the tgz file use the command:

# tar -zxvf APC-3.0.16.tgz

This will decompress the file and create a new folder in the src folder called APC-3.0.16. Change to this new folder. Type:

# whereis phpize

This will will tell you where the program phpize is. It should look like:

phpize: /usr/bin/phpize

Run the same whereis command for php-config and apxs. Next type:

# /usr/bin/phpize
# ./configure --enable-apc --enable-apc-mmap --with-apxs=/usr/sbin/apxs --with-php-config=/usr/bin/php-config
# make
# make install

Make sure you have the right paths for apxs and php-config. Take note where it put the apc.so file. It should look something like /usr/lib/php4/apc.so.

Now locate your php.ini file and open it up to edit it. You can use vi as a command line editor. Vi is not a very easy editor to use but every unix based system has it. The php.ini file should be located at something like /etc/php.ini.

Add this to the file (at the end):

extension = apc.so
apc.enabled = 1
apc.shm_size = 48
apc.include_once_override = 1
apc.mmap_file_mask = /tmp/apc.XXXXXX

This tells it where to find the apc file, to enable it, how large of a cache to have (in megabytes), to handle include_once calls well, and where to have the mmap_file_mask if there are memory issues.

Restart apache and it should be running. The command to do this is something like:

# /etc/init.d/httpd restart

When APC was installed a file called apc.php was created. You can move this file to you web server an use it through your browser to monitor APC. This is good for seeing how much memory it needs and adjusting the settings.

With opcode caches, like APC, web servers can handle much larger loads and we can more effectively use the resources at hand.

Note: Please do this at your own risk. This is for more advanced users. Innovating Tomorrow is not responsible for anything that goes wrong.

Subscribe

Like the post? You can subscribe to the RSS feed or subscribe via email to keep them coming.

Typo

Matt,

"Imporve" (in post title) or "Improve"

Feel free to delete this comment when you revise the post.

Thanks for pointing that out

Thanks for pointing out the typo. Sometimes those happen. :-)

apc.include_once_override

In WordPress you may get an error if you set "apc.include_once_override" to 1.
Try this instead:
apc.include_once_override = 0
or completely remove the line from php.ini.
(Restart apache for php.ini to take effect)

Hope this helps.

Wordpress with include_once_overide

Yeah, just to confirm, you get some wierd behaviour with with include_once_overide on wordpress