OPcache is a powerful PHP extension designed to enhance the performance of PHP scripts by storing precompiled script bytecode in memory. This article covers how to install and configure OPcache on an Ubuntu VPS and provides an example of its usage to improve PHP application performance by reducing script execution time.
12 min
Edited:12-10-2024
OPcache is a caching mechanism built into PHP, designed to accelerate PHP script execution by caching the precompiled bytecode in memory. Instead of compiling the PHP code on every request, OPcache stores the compiled version in memory, which drastically reduces the execution time and improves the overall performance of PHP applications. OPcache is especially useful for high-traffic websites, ensuring that each request does not need to recompile PHP scripts repeatedly. As a result, CPU usage decreases, and the web application becomes faster and more responsive.
Faster Script Execution: OPcache significantly reduces PHP script execution time by avoiding repeated compilation.
Improved Server Performance: It reduces CPU and memory load on the server, allowing more efficient use of server resources.
Supports Large Applications: OPcache is particularly beneficial for large-scale PHP applications with many files and complex logic.
OPcache comes pre-installed with PHP versions 5.5 and higher, so you don’t need to download it separately. However, you need to enable and configure it. Here’s how to install and enable OPcache on an Ubuntu-based VPS.
Step 1: Update Your System ,Before installing or enabling OPcache, make sure your system is up-to-date.
1. sudo apt update
2. sudo apt upgrade
Step 2: Install PHP (if not installed): If PHP isn’t already installed, you can install it along with the necessary PHP extensions:
1. sudo apt install php php-cli php-fpm
If you want to install specific versions of PHP, for example, PHP 8.0:
1. sudo apt install php8.0 php8.0-cli php8.0-fpm
Step 3: Install OPcache (if not already enabled)
For most modern PHP installations, OPcache is included, but you might need to install it manually if it’s not enabled by default.
1. sudo apt install php-opcache
Step 4: Enable and Configure OPcache
Now that OPcache is installed, you need to enable and configure it in your php.ini file.
First, open the configuration file for editing. The location of this file may vary depending on whether you're using PHP CLI, FPM, or Apache, but it is typically found at: For PHP CLI:
1. sudo nano /etc/php/8.0/cli/php.ini
For PHP FPM (if you're using PHP with Nginx):
1. sudo nano /etc/php/8.0/fpm/php.ini
For PHP with Apache:
1. sudo nano /etc/php/8.0/apache2/php.ini
Look for the following lines in the php.ini file and make sure they are properly configured. If they are missing, add them manually:
1. opcache.enable=1
2. opcache.memory_consumption=128
3. opcache.max_accelerated_files=10000
4. opcache.revalidate_freq=60
5. opcache.save_comments=1
Here’s what these settings mean:
opcache.enable=1: This ensures OPcache is enabled.
opcache.memory_consumption=128: Allocates 128MB of memory for the OPcache storage. You can adjust this based on your application needs.
opcache.max_accelerated_files=10000: Defines the maximum number of PHP files that OPcache can store. Increase this if your application has more files.
opcache.revalidate_freq=60: This setting determines how often OPcache checks for script changes. A value of 60 means that OPcache will check for file changes every 60 seconds.
opcache.save_comments=1: Ensures that OPcache saves comments (such as docblocks) in the cached bytecode. If you don’t need comments, you can set this to 0 for a slight performance boost.
Step 5: Restart Your Web Server
After editing and saving the php.ini file, restart the web server to apply the changes. Depending on your server setup, use one of the following commands: For Apache:
1. sudo systemctl restart apache2
For Nginx with PHP-FPM:
1. sudo systemctl restart php8.0-fpm
2. sudo systemctl restart nginx
Step 6: Verify OPcache is Enabled
To verify that OPcache is enabled, you can create a simple PHP file to display the PHP information (phpinfo()).
1. Create a file called info.php in your web server’s root directory (e.g., /var/www/html).
1. sudo nano /var/www/html/info.php
2. Add the following content:
1. <?php
2. phpinfo();
3. ?>
3. Save the file and visit it in your browser (e.g., http://your-server-ip/info.php). Look for an "OPcache" section in the output. This will confirm that OPcache is enabled and show its configuration.
Once OPcache is enabled, it works automatically behind the scenes to cache PHP bytecode. Let’s demonstrate this with a simple PHP application example.
Step 1: Create a Simple PHP Script
Create a PHP script to simulate repeated requests and observe OPcache in action.
1. sudo nano /var/www/html/test_opcache.php
Add the following PHP code:
1. <?php
2. $start_time = microtime(true);
3. // A simple loop to simulate script processing time
4. for ($i = 0; $i < 1000000; $i++) {
5. $dummy = md5($i);
6. } $end_time = microtime(true);
7. $execution_time = ($end_time - $start_time);
8. echo "Script execution time: " . $execution_time . " seconds\n";
9. ?>
Step 2: Access the Script
Access this script in your browser (e.g., http://your-server-ip/test_opcache.php). Initially, you’ll see the script’s execution time printed on the page.
Step 3: Observe OPcache Benefits
Run the script several times, and you should notice a decrease in execution time after the first run. OPcache stores the compiled bytecode of the script, so subsequent executions are faster because PHP doesn’t need to recompile the script.
To monitor OPcache performance and usage, you can use a web-based OPcache GUI like opcache-gui or opcache-status. These tools provide detailed statistics on memory usage, cached scripts, and hit/miss ratios.
Step 1: Download and Set Up OPcache GUI
1. Download opcache-gui (an example GUI tool) from its GitHub repository:
1. cd /var/www/html
2. sudo git clone https://github.com/amnuts/opcache-gui.git opcache
2. Access the GUI by visiting http://your-server-ip/opcache/index.php in your browser. This interface will give you detailed information about OPcache, including memory consumption, hit/miss statistics, and a list of cached scripts.
OPcache is a highly effective way to boost PHP performance by caching precompiled bytecode in memory, reducing the need for repetitive compilation and improving application speed. By following this guide, you’ve installed and configured OPcache on your Ubuntu VPS, and you've seen firsthand how it enhances PHP script execution. Whether you're running a small website or a large-scale web application, enabling OPcache is a simple yet powerful optimization to reduce load and speed up response times.
14-10-2024
This article offers a detailed guide on installing and configuring IPTables on an Ubuntu VPS. IPTables is a powerful firewall tool that helps secure your server by controlling inbound and outbound traffic. Learn how to set up rules for traffic filtering, configure basic security policies, and apply custom rules to protect your VPS.
IPtables
security
12 min
This article offers a comprehensive guide on installing and configuring ModSecurity, a powerful web application firewall (WAF), on an Ubuntu VPS. Learn how to secure your server by filtering and monitoring HTTP requests, set up ModSecurity with Nginx or Apache, and apply rules to protect against common web attacks.
Modsecurity
security
10 min
14-10-2024
This article provides a comprehensive guide on installing and configuring PHP-FPM (FastCGI Process Manager) on an Ubuntu VPS. Learn how to optimize PHP performance for your web applications by configuring PHP-FPM with Nginx or Apache, managing pools, and fine-tuning settings for efficient processing of PHP scripts.
PHP-FPM
speed
optimise
12 min