Faster PHP with Fastdomain Shared Hosting Account with PHP APC extension

Now I decided to share my personal know how on how to activate PHP APC extension under FastDomain shared Hosting account. You won’t find this information anywhere because I found it myself by trial and error.

FastDomain’s shared hosting account is OK and this is my second year with FastDomain. About a year ago, I had a little project to work with Magento. I had to deploy an ecommerce site and I reviewed various free shopping cart solutions. And my final decision was Magento. It’s an open source software and backed by large and active community. It is pretty flexible, powerful and free.

However, the catch was that the Magento requires at least 32 M for the memory_limit settings of php.ini. (Now the minimum memory requirement is unbelievable 256M) But 48M or 64M was recommended. Also it needs more CPU power because it is based on Zend Framwork. Zend Framework is very popular framework from the Zend, maker of PHP. But it requires much more memory and CPU power than conventional PHP scripts.

But the FastDomain’s default PHP memory_limit was 32M and the Magento’s initial speed under FastDomains’s shared account was somewhat slow and disappointing. However, for me, there was no additional budget for a dedicated server or even a Virtual Private Server which costs at least $35 per month.

So I decided to look for the ways to optimize the Magento. Many discussion forum articles were discussing about APC (Alternative PHP Cache) PHP extension. This extension is basically to boost the performance of software applications written using the PHP. Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request. And it can improve the speed by 1~3 times faster. I was really happy becauseI thought that I finally found the right solution.

But then I realized that FastDomains’s PHP configuration does not support APC extension by default. At this point, I was really tired of Magento but decided to give one more chance and searched Google and tried everything that I could with CPanel.

And I finally found the way to increase the memory limit over default 32M and how to deploy APC extension under FastDomain. So here they are.

Requirements:

  • You need to have SSH terminal access to your account. By default SSH access is not enabled, but you can easily get the access by applying for it. (Try ‘SSL/Shell access’ icon in CPanel)
  • If this is your first time with SSH, you should download PuTTY SSH terminal program.

  • Some knowledge of Linux console command is recommended. (For example, how to navigate between folders , list directories, move files, etc) But you can use FTP program to do the same tasks.

Step 1

From the CPanel, click on the ‘PHP Config’ icon.

image

Step 2

From the page, you can have customized php.ini in your ~/public_html folder. First , you will need to decide how to load customized php.ini file. If you want to create one php.ini file and use it globally through out the subdirectories and add domains folders, select “PHP5(Sing php.ini)” option. If you want to have different settings in php.ini for different addon domains, select “PHP5”. If you select ‘PHP5’, you will need to save php.ini in each subdirectories that you want to apply customized php.ini settings.

image

Second, click on the ‘INSTALL PHP.INI MASTER FILE’ button to save default php.ini.default file into your ~/public_html folder. You can edit this file according to your needs and renamed it to php.ini later.

Also , at this point, I recommend you to create test.php and add below snippet to check the APC extension is installed and enabled successfully at a later time. This file can be saved anywhere but let’s save it right in the ~/public_html, so you can access it by typing ‘http://yourdomain.com/test.php

<?php
phpinfo();
?>;

Why don’t you test it now? You will see a webpage similar to below image. If you try to find keyword “APC” in the web browser, you won’t be able to find it since we haven’t enable it yet.image

Also note that the default memory size is 32M. image

Step 3

Now rename the ~/public_html/php.ini.default file to ~/public_html/php.ini. Then you can use ‘vi’ editor or download to your PC using FTP to edit it.

First, we would like to increase the memory_limit. Search the ‘memory_limit’ keyword and edit it to your preferred amount of memory. I used 128M just for this demo. For your information, I never used large number for the memory. I used less than 64M just in case.

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60     ; Maximum amount of time each script may spend parsing request data
memory_limit = 128M      ; Maximum amount of memory a script may consume (32MB)

After editing the file, you can try to reload the test.php page to see if the update is reflected properly.

image

Ok, this was easy.

Step 4

Now, we are ready to deploy APC extension. Fortunately, FastDomain shared hosting account provides C compiler. You will need to download APC source code and compile apc.so and define it in the custom php.ini file. The compilation process is not that difficult.

Download http://pecl.php.net/get/APC-3.1.2.tgz and upload into your account path ~/public_html

Now login to your FastDomain SSH account and run below commands.

#cd public_html
#gunzip APC-3.1.2.tgz ; tar xf APC-3.1.2.tar
#cd APC-3.1.2
#/usr/bin/phpize
#./configure --enable-apc --enable-apc-mmap  --with-php-config=/usr/bin/php-config
#make
#cp modules/apc.so ~/public_html

After this, you will have apc.so file in the ~/public_html folder. Now you are almost ready.

More references:

Step 5

Open php.ini to edit file again. At the end of the file add below lines. Note that “yourid” in the extension path should be your login id.

[apc]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.max_file_size = 10M
apc.stat=1
 
extension=../../../../../../home/yourid/public_html/apc.so

Once you save the file, try the test.php file to see if APC is loaded successfully. If you search for the keyword “APC”, you will be able to find one. Wonderful!

image

If you can’t find “APC”, check out the ~/public_html/error_log. You will be able to find some clues why it didn’t work.

Remember, APC extension helps not only Magento but all PHP applications. Once the setup is completed, it affect all the addon domains too (under single php.ini option). Enjoy the benefits of APC today! I mean if you are using FastDomain shared hosting service.

One thing to note is that you will need to compile apc.so everytime the PHP is upgraded in your FastDomain server. When the PHP version is upgraded, your old apc.so won’t work unless you compile it again.

Thank you for reading and please leave any comment.

Leave a Reply