If you are developing a website using PHP, you may have noticed that it can become quite slow as your code base grows. This is where PHP caching comes in handy, as it can speed up your website’s load time by caching frequently used data and code. In this article, we will walk you through the process of installing the APC PHP cache utility on your server.
What is APC cache utility?
APC (Alternative PHP Cache) is a popular open-source caching framework for PHP web applications. It is a free, lightweight and highly efficient caching tool that helps speed up PHP performance by caching and optimising PHP bytecode.
APC works by storing compiled PHP code in shared memory, which means that once the code has been cached, it can be accessed more quickly than if it were recompiled every time it is requested. APC can also cache variables, database results and other data that can be used repeatedly throughout a PHP application.
In addition to speeding up PHP performance, APC also reduces server load and improves scalability. By reducing the amount of time it takes for PHP to generate pages, APC allows web servers to handle more requests per second, which means that servers can handle more traffic without slowing down or crashing.
How to install the APC cache utility
- Check if APC is installed on your server
Before installing APC, you should first check whether it is already installed on your server. You can do this by running the following command in the terminal:
php -m | grep apc
If APC is installed, it will be listed in the output.
- Install APC
If APC is not installed on your server, you will need to install it. To do this, you will need to have root access to your server. You can install APC using the following commands in the terminal:
sudo apt-get update sudo apt-get install php-apc
- Configure APC
After installing APC, you will need to configure it. You can do this by editing the php.ini file. The location of this file may vary depending on your server configuration, but it is usually located in /etc/php/apache2/php.ini.
Open the php.ini file in a text editor and add the following lines at the end of the file:
apc.enabled=1 apc.shm_segments=1 apc.shm_size=64M apc.ttl=7200 apc.user_ttl=7200 apc.gc_ttl=3600 apc.stat=1
Save the changes and close the file.
Note: The APC cache utility has several configuration settings that can be customised to optimise its performance for your specific application. Here are some of the most important configuration settings for APC cache:
- apc.enabled – This setting determines whether APC cache is enabled or disabled. By default, it is set to “1” which means APC cache is enabled.
- apc.shm_size – This setting specifies the amount of memory allocated for APC cache. The default value is 32MB, but you can increase this value depending on your application’s needs.
- apc.ttl – This setting determines the maximum time that an item can be stored in the cache before it is considered stale and removed. The default value is 0, which means that items are stored in the cache indefinitely.
- apc.user_ttl – This setting is similar to apc.ttl, but it applies only to items that are stored with the user cache API.
- apc.gc_ttl – This setting specifies how frequently the APC cache garbage collector runs. The default value is 600 seconds (10 minutes).
- apc.mmap_file_mask – This setting specifies the file permissions for the shared memory segment used by APC cache. The default value is “/tmp/apc.XXXXXX”, where “XXXXXX” is a random string of characters.
- apc.include_once_override – This setting determines whether or not APC cache should override PHP’s built-in include_once() function. By default, this is set to “0” which means that the built-in function is used.
- apc.stat – This setting determines whether or not APC cache checks for file modifications every time a script is executed. If this setting is set to “1”, APC cache will check for modifications and re-cache files if necessary.
- apc.num_files_hint – This setting specifies the number of files that APC cache should expect to cache. This is used to optimize memory allocation. The default value is 1000.
- apc.file_update_protection – This setting specifies the minimum time (in seconds) that must pass before APC cache will re-cache a file that has been modified. The default value is 2 seconds.
These are just some of the many configuration settings available for APC cache. It’s important to try different values to find the optimal configuration for your application.
- Restart Apache
After configuring APC, you will need to restart the Apache web server for the changes to take effect. You can do this using the following command in the terminal:
sudo service apache2 restart
- Test APC
To test if APC is working, you can create a PHP file with the following code:
<?php apc_add('test', 'Hello World!', 60); echo apc_fetch('test'); ?>
Save the file as test.php and upload it to your server. When you open the file in your web browser, it should display “Hello World!”.
Can I install a PHP cache utility on shared hosting?
If you have shared hosting and do not have root access to the server, then you will not be able to install a PHP cache utility. However, at eukhost, our shared hosting plans, including cPanel and WordPress hosting, come with PHP caching already enabled. We do this through the use of OPCache. OPCache uses shared memory to store precompiled PHP scripts for faster execution.
In this article, we have shown you how to install APC, a PHP cache utility, on your server. By caching frequently used data and code, APC can speed up your website’s load time, making it more responsive and improving the user experience.
Looking for fast, reliable hosting with 24/7 technical support? Visit our homepage.