Skip to content

feat: additional opcache setting in check php.ini - #9032

Merged
michalsn merged 14 commits into
codeigniter4:4.6from
ddevsr:check-php-ini-opcache
Jan 11, 2025
Merged

feat: additional opcache setting in check php.ini#9032
michalsn merged 14 commits into
codeigniter4:4.6from
ddevsr:check-php-ini-opcache

Conversation

@ddevsr

Copy link
Copy Markdown
Collaborator

Description
I think this additional needed to recommendation.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@ddevsrddevsr added enhancement PRs that improve existing functionalities 4.6 labels Jul 8, 2024
Comment threadsystem/Security/CheckPhpIni.php Outdated
@kenjis

Copy link
Copy Markdown
Member

I have concerns.

  1. Too many items in the output can be difficult to read. I want to display only the really important items.
  2. The current output is too wide to see.
  3. It is difficult to determine recommended values for OPCache settings because they are highly dependent on the environment.
+---------------------------------+---------+---------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
| Directive | Global | Current | Recommended | Remark |
+---------------------------------+---------+---------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+
| error_reporting | 32767 | 32767 | 5111 | |
| display_errors | 1 | 1 | 0 | |
| display_startup_errors | 1 | 1 | 0 | |
| log_errors | 1 | 1 | | |
| error_log | | | | |
| default_charset | UTF-8 | UTF-8 | UTF-8 | |
| memory_limit | 1G | 1G | | > post_max_size |
| post_max_size | 8M | 8M | | > upload_max_filesize |
| upload_max_filesize | 2M | 2M | | < post_max_size |
| request_order | GP | GP | GP | |
| variables_order | GPCS | GPCS | GPCS | |
| date.timezone | UTC | UTC | UTC | |
| mbstring.language | neutral | neutral | neutral | |
| opcache.enable | 1 | 1 | 1 | |
| opcache.enable_cli | 0 | 0 | | |
| opcache.jit | tracing | tracing | | |
| opcache.jit_buffer_size | 0 | 0 | | |
| opcache.memory_consumption | 128 | 128 | 728 | Increasing the configured memory size (MB) will improve performance by caching those files (consideration based on free space of memory) |
| opcache.interned_strings_buffer | 8 | 8 | 64 | |
| opcache.max_accelerated_files | 10000 | 10000 | 56000 | Increasing the configured accelerated will many cache files into OPCache hash table |
| opcache.max_wasted_percentage | 5 | 5 | 15 | |
| opcache.validate_timestamps | 1 | 1 | 0 | |
| opcache.revalidate_freq | 2 | 2 | 0 | |
| opcache.save_comments | 1 | 1 | 1 | |
+---------------------------------+---------+---------+-------------+------------------------------------------------------------------------------------------------------------------------------------------+

@neznaika0

Copy link
Copy Markdown
Contributor

Probably a simple mention of opcache On/Off is enough.
Many people don't use the console when developing. Especially the newbies.
I think special attention to php.ini is not required. More questions about apache/nginx

@kenjis

Copy link
Copy Markdown
Member

This command is mainly used to check the PHP settings of production environments.

The Recommended column shows the recommended values for production environment. They may differ in development environments.
https://codeigniter4.github.io/CodeIgniter4/installation/running.html#checking-php-ini-settings

@kenjis

Copy link
Copy Markdown
Member

@ddevsr If you want to show opcache detailed settings, how about adding an argument?
E.g. php spark phpini:check opcache shows only opcache settings.

@github-actions

Copy link
Copy Markdown

👋 Hi, @ddevsr!

We detected conflicts in your PR against the base branch 🙊
You may want to sync 🔄 your branch with upstream!

Ref: Syncing Your Branch

@github-actionsgithub-actionsBot added the stale Pull requests with conflicts label Aug 22, 2024
@ddevsr
ddevsrforce-pushed the check-php-ini-opcache branch from 0079494 to 5418869CompareAugust 22, 2024 08:54
@ddevsrddevsr removed the stale Pull requests with conflicts label Aug 22, 2024
@ddevsr
ddevsr requested a review from kenjisAugust 22, 2024 09:19
@ddevsr
ddevsrforce-pushed the check-php-ini-opcache branch from 21e7965 to 36698fcCompareAugust 22, 2024 09:26
@ddevsr
ddevsr requested a review from michalsnAugust 22, 2024 09:43
@ddevsr

Copy link
Copy Markdown
CollaboratorAuthor

@kenjis@michalsn

$ php spark phpini:check
CodeIgniter v4.5.4 Command Line Tool - Server Time: 2024-08-22 09:50:55 UTC+00:00
+-------------------------+---------------------------------------+---------------------------------------+-------------+---------------------------------------+ | Directive | Global | Current | Recommended | Remark | +-------------------------+---------------------------------------+---------------------------------------+-------------+---------------------------------------+ | error_reporting | 32767 | 32767 | 5111 ||| display_errors | 1 | 1 | 0 ||| display_startup_errors | 1 | 1 | 0 ||| log_errors | 1 | 1 |||| error_log | D:/Project/laragon/tmp/php_errors.log | D:/Project/laragon/tmp/php_errors.log |||| default_charset | UTF-8 | UTF-8 | UTF-8 ||| max_execution_time | 0 | 0 || The default is 30. || memory_limit | 1024M | 1024M ||> post_max_size || post_max_size | 2G | 2G ||> upload_max_filesize || upload_max_filesize | 2G | 2G ||< post_max_size || max_input_vars | 1000 | 1000 || The default is 1000. || request_order | GP | GP | GP ||| variables_order | GPCS | GPCS | GPCS ||| date.timezone | Asia/Jakarta | Asia/Jakarta | UTC ||| mbstring.language | neutral | neutral | neutral ||| opcache.enable | 0 | 0 | 1 ||| opcache.enable_cli | 1 | 1 | 1 ||| opcache.jit | tracing | tracing | tracing ||| opcache.jit_buffer_size | 128M | 128M | 256 | Adjust with your free space of memory || zend.assertions | 1 | 1 | -1 || +-------------------------+---------------------------------------+---------------------------------------+-------------+---------------------------------------+
$ php spark phpini:check opcache
CodeIgniter v4.5.4 Command Line Tool - Server Time: 2024-08-22 09:52:13 UTC+00:00
+---------------------------------+---------+---------+-------------+----------------------------------------------------------------------------------+
| Directive | Global | Current | Recommended | Remark |
+---------------------------------+---------+---------+-------------+----------------------------------------------------------------------------------+
| opcache.enable | 0 | 0 | 1 ||| opcache.enable_cli | 1 | 1 | 1 ||| opcache.jit | tracing | tracing | tracing ||| opcache.jit_buffer_size | 128M | 128M | 256 | Adjust with your free space of memory || opcache.memory_consumption | 728 | 728 | 512 | Adjust with your free space of memory || opcache.interned_strings_buffer | 64 | 64 | 64 ||| opcache.max_accelerated_files | 58000 | 58000 | 40000 | Find many files in your project (example: find your_project/ -iname *.php|wc -l) || opcache.max_wasted_percentage | 15 | 15 | 15 ||| opcache.validate_timestamps | 0 | 0 | 0 ||| opcache.revalidate_freq | 0 | 0 | 0 ||| opcache.save_comments | 0 | 0 | 0 ||
+---------------------------------+---------+---------+-------------+----------------------------------------------------------------------------------+

@michalsn

Copy link
Copy Markdown
Member

This looks good, however, I wonder if we should keep all 4 opcache keys in the default table. IMO only the first 2 will be relevant now.

@kenjiskenjis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for updating!

But I don't agree with the recommendation values.
I will see the values.

Comment threadsystem/Security/CheckPhpIni.php Outdated
if ($argument === 'opcache') {
$items = [
'opcache.enable' => ['recommended' => '1'],
'opcache.enable_cli' => ['recommended' => '1'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why opcache.enable_cli is 1?

In my understanding, it does not improve performance unless you set opcache.file_cache correctly.
https://stackoverflow.com/questions/77778860/proper-opcache-configuration-for-cli

@kenjis

Copy link
Copy Markdown
Member

Recommendations by ChatGPT CodeIgniter 4 Assistant


When deploying CodeIgniter 4 (CI4) in a production environment, configuring PHP’s OPcache properly is essential to ensure optimal performance. Below are the recommended values for the specified OPcache settings that should be good for most CI4 users. However, certain values might need adjustment based on the specific needs of your application and server resources.

Recommended PHP ini settings for OPcache in a Production Environment

  • opcache.enable: 1 (or On)

    • Recommendation: Always enable OPcache in production to improve PHP performance by caching compiled script bytecode.
  • opcache.enable_cli: 0 (or Off)

    • Recommendation: Generally, you don't need OPcache for CLI scripts. However, if your application runs CLI scripts frequently, you might consider enabling it (1 or On).
  • opcache.jit: off

    • Recommendation: Just-In-Time (JIT) compilation can provide performance improvements for certain workloads, but it's generally safe to keep it off unless you've profiled your application and observed tangible benefits from JIT.
  • opcache.jit_buffer_size: 0

    • Recommendation: Set to 0 to disable JIT unless you've determined that enabling JIT is beneficial. If enabling JIT, you might set this value between 32M to 128M depending on available memory and workload characteristics.
  • opcache.memory_consumption: 128M to 256M

    • Recommendation: This defines how much memory OPcache can use. For most applications, 128M should be sufficient, but for larger applications with many scripts, 256M might be better.
  • opcache.interned_strings_buffer: 8M to 16M

    • Recommendation: This is used for storing interned strings. 8M is a good starting point, and 16M can be used for larger applications.
  • opcache.max_accelerated_files: 10000 to 20000

    • Recommendation: This defines the maximum number of files that OPcache can cache. A setting of 10000 should work for most applications, but for large codebases, 20000 might be necessary.
  • opcache.max_wasted_percentage: 5%

    • Recommendation: When the percentage of wasted memory (due to cache fragmentation) exceeds this value, OPcache will restart. A 5% threshold is generally recommended.
  • opcache.validate_timestamps: 0 (or Off)

    • Recommendation: Disable in production. This improves performance by not checking the script modification time on every request. If your deployment process doesn’t guarantee that updated files are reloaded (e.g., through a cache invalidation strategy), you might need to set this to 1 and use opcache.revalidate_freq.
  • opcache.revalidate_freq: 0 to 60

    • Recommendation: If opcache.validate_timestamps is 1, this setting controls how often (in seconds) OPcache will check for script changes. Setting this to 0 means it checks on every request, which is not recommended for production. Instead, use a value like 60 to reduce the performance hit.
  • opcache.save_comments: 1 (or On)

    • Recommendation: Keep comments enabled as they are often required by various frameworks and tools. Disabling this can cause issues, especially with Doctrine annotations and other comment-based parsing tools.

Additional Considerations:

  • Memory Resources: Ensure your server has enough memory to accommodate these settings. Adjust values like opcache.memory_consumption and opcache.jit_buffer_size based on your server’s available RAM.
  • Application Size: Larger applications might require higher values for settings like opcache.max_accelerated_files and opcache.memory_consumption.
  • Testing: Always profile your application with these settings to ensure they are optimal for your specific environment. What works well for one CI4 application might need tweaks for another.

@kenjis

Copy link
Copy Markdown
Member

Recommendations by Claude 3.5 Sonnet


As a PHP expert, I can provide recommended values for the PHP ini settings you've listed, specifically tailored for a CodeIgniter 4 production environment. These recommendations should be suitable for most CI4 users, but keep in mind that optimal values can vary depending on your specific application and server resources.

opcache.enable = 1
opcache.enable_cli = 0
opcache.jit = 1255
opcache.jit_buffer_size = 100M
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
opcache.max_wasted_percentage = 10
opcache.validate_timestamps = 0
opcache.revalidate_freq = 0
opcache.save_comments = 1

Let me explain each setting:

  1. opcache.enable = 1: This enables OPcache for web requests, which is crucial for performance in production.

  2. opcache.enable_cli = 0: OPcache is typically not needed for CLI operations, so it's usually disabled.

  3. opcache.jit = 1255: This enables JIT compilation with a balanced setting. The value 1255 represents tracing JIT for all JIT-able functions.

  4. opcache.jit_buffer_size = 100M: This allocates 100MB for JIT-compiled code. Adjust based on available memory, but 100MB is a good starting point.

  5. opcache.memory_consumption = 256: Allocates 256MB for OPcache. Adjust based on your application size and available memory.

  6. opcache.interned_strings_buffer = 16: Allocates 16MB for interned strings. This is usually sufficient for most applications.

  7. opcache.max_accelerated_files = 10000: Allows caching of up to 10,000 files. Adjust based on the number of PHP files in your application.

  8. opcache.max_wasted_percentage = 10: Restarts the OPcache when 10% of memory is wasted. This helps manage memory efficiently.

  9. opcache.validate_timestamps = 0: Disables file timestamp checking. This improves performance but requires a server restart or OPcache reset when files are updated.

  10. opcache.revalidate_freq = 0: Related to validate_timestamps. When set to 0 and validate_timestamps is off, it never checks file timestamps.

  11. opcache.save_comments = 1: Preserves comments in the cached files. This is important for CodeIgniter 4, as it uses doc comments for annotations.

These values provide a good starting point for a CodeIgniter 4 production environment. However, you may need to adjust them based on your specific application requirements, server resources, and performance needs. Regular monitoring and tuning can help you find the optimal settings for your particular use case.

Comment threadsystem/Security/CheckPhpIni.php Outdated
'opcache.max_wasted_percentage' => ['recommended' => '15'],
'opcache.validate_timestamps' => ['recommended' => '0'],
'opcache.revalidate_freq' => ['recommended' => '0'],
'opcache.save_comments' => ['recommended' => '0'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting opcache.save_comments to 0 is dangerous. Some packages may use doc comments.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already test smooth for my project. For recomendation production perspective, i think can disabled in production with remark notes

"require": {
"php": "^8.1",
"chillerlan/php-qrcode": "^5.0",
"codeigniter4/framework": "^4.5.3",
"firebase/php-jwt": "^6.10",
"mpdf/mpdf": "^8.2"
},

Comment threadsystem/Security/CheckPhpIni.php Outdated
Comment threadsystem/Security/CheckPhpIni.php Outdated
Comment threadsystem/Security/CheckPhpIni.php Outdated
Comment threadsystem/Security/CheckPhpIni.php Outdated
Comment threadsystem/Security/CheckPhpIni.php Outdated
Comment threadsystem/Security/CheckPhpIni.php Outdated
Comment threadsystem/Security/CheckPhpIni.php Outdated
$items = [
'opcache.enable' => ['recommended' => '1'],
'opcache.enable_cli' => ['recommended' => '1'],
'opcache.jit' => ['recommended' => 'tracing'],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it safe to recommend to enable jit?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not safe if using third-party ext like xdebug.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on other and my benchmark, JIT have benefit significant. Moreover, in PHP8.4 a new, better JIT has been implemented

Ref :

Comment threadsystem/Commands/Utilities/PhpIniCheck.php Outdated
@michalsn

Copy link
Copy Markdown
Member

I guess there is no consensus on recommended values?

@samsonasiksamsonasik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can accept it, and improve recommended values as we go later ;)

@michalsnmichalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable, although some sort of changelog entry would be nice.

@paulbalandanpaulbalandan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, pending changelog entry.

@michalsnmichalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a changelog entry after merging this in a separate PR.

@michalsn
michalsn merged commit 468e84b into codeigniter4:4.6Jan 11, 2025
@ddevsr
ddevsr deleted the check-php-ini-opcache branch January 13, 2025 01:26
@paulbalandanpaulbalandan mentioned this pull request Jan 19, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementPRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@ddevsr@kenjis@neznaika0@michalsn@samsonasik@paulbalandan