Report this

What is the reason for this report?

How do I install APC on CentOS 7?

Posted on June 1, 2017

Im looking for a tutorial to install and configure APC on CentOS 7. I see there is one for Ubuntu but not for CentOS.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @riahc4 Do you mean APC as in the PHP module? If yes, then it’s been replaced by OPcache, which is many times faster. OPcache is available from 5.6 and activated by default from 7.0

Heya,

Here’s a step-by-step tutorial on how to install and configure APC (Alternative PHP Cache) on CentOS 7. APC is a free and open-source opcode cache for PHP, which can optimize PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

Step 1: Install PHP and PHP-PECL

APC is available through the PECL repository. If you haven’t installed PHP and PECL, you can do so with the following commands:

  1. Install PHP:
sudo yum install php
  1. Install PHP Development Tools:
sudo yum install php-devel
  1. Install PHP-PECL:
sudo yum install php-pear

Step 2: Install APC

Now, install APC using PECL:

sudo pecl install apc

Step 3: Configure PHP to Use APC

  1. Create an APC Configuration File:

    After installing APC, you need to create a configuration file for it so that PHP can use it.

    • Create a new file in the PHP configuration directory (usually /etc/php.d/). The file can be named 40-apc.ini to ensure it loads after the default PHP configurations:
sudo vi /etc/php.d/40-apc.ini
  1. Add Configuration Directives:

    Inside this file, add the following configuration directives:

; Enable APC extension module
extension=apc.so

; The following can be adjusted according to your needs

; The size of each shared memory segment in MB
apc.shm_size=32M

; The number of seconds a cache entry is allowed to idle in a slot before APC dumps the cache
apc.ttl=7200

; The number of seconds a user cache entry is allowed to idle in a slot before APC dumps the cache
apc.user_ttl=7200

; The number of seconds that a cache entry may remain on the garbage-collection list
apc.gc_ttl
  1. Adjust the shm_size, ttl, user_ttl, and gc_ttl values according to your server’s resources and your application’s requirements.

  2. Restart Apache:

    After making changes to PHP configuration, restart your web server to apply them. If you are using Apache, use this command:

sudo systemctl restart httpd

Step 4: Verify APC Installation

To verify that APC is installed and configured correctly, create a PHP file with the following content and place it in your web server’s document root:

<?php
phpinfo();
?>

When you access this file via a web browser, you should see the APC settings and status displayed.

Step 5: Monitor APC Usage (Optional)

To monitor APC usage and status, you can use a script like apc.php which is bundled with APC. It provides a detailed view of the cache usage.

  1. Locate the apc.php File:

    This file is usually located in the APC source folder. You can also find it online or in the PECL/APC package.

  2. Copy apc.php to Your Web Server Document Root:

    Make sure it’s in a protected directory or password-protected to prevent public access, as it can reveal detailed server information.

  3. Access apc.php via Browser:

    Navigate to this file through your web browser to view APC’s cache information.

Additional Notes

  • APC is Deprecated: Note that APC is considered outdated and has been replaced by other caching mechanisms like Zend Opcache and APCu in PHP 7 and later. If you are using PHP 7.x, it’s recommended to use Opcache for opcode caching and APCu for user data caching.
  • Security: Always consider securing your server and PHP installations. Exposing sensitive files like phpinfo.php or apc.php can be a security risk.

By following these steps, you should be able to install and configure APC on CentOS 7 for PHP. Remember, APC is mainly beneficial for older PHP versions (PHP 5.x). If you’re using PHP 7.x or later, consider using the newer alternatives.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.