13

I'm using the standard Apache and PHP 7.1 (not Homebrew) that comes with Mac High Sierra. However, it appears that this version doesn't have Opcache enabled even though it should come as standard with PHP 7. It's even listed in phpinfo() under "Module Authors", but no section showing it's actually installed. Calling opcache_get_status gives a fatal error.

I've installed the extension via Homebrew, and linked the opcache.so file. It appears to be working on the CLI but not in Apache. For some reason the CLI and web are using different ini files:

  • /usr/local/etc/php/7.1/php.ini for CLI
  • /etc/php.ini for web

The CLI is parsing the addition files including /usr/local/etc/php/7.1/conf.d/ext-opcache.ini, and php -i shows Opcache. But phpinfo() in the browser does not - no additional ini files are parsed.

I currently have this in /etc/php.ini:

[opcache]
zend_extension="/usr/local/opt/php71-opcache/opcache.so"
opcache.enable=1

But still nothing. I followed the exact same process for xdebug and it worked fine. What am I missing?


I wonder if it would be easier to use the Homebrew version of PHP. But I don't appear to have the required .so file. Various tutorials say to put this in Apache's httpd.conf:

LoadModule php7_module /usr/local/opt/php71/libexec/apache2/libphp7.so

But the libexec directory does not exist. There is lib but neither this nor any other directory has any .so file.

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290

2 Answers2

20

For me it worked by these steps:

  1. Search extension_dir in "phpinfo()" page, I got a path /usr/lib/php/extensions/no-debug-non-zts-20160303
  2. By execute ls -lh /usr/lib/php/extensions/no-debug-non-zts-20160303, I found "opcache.so" , I guess it's installed when upgraded to "High Sierra"
  3. Create "/etc/php.ini" (by copy "/etc/php.ini.default"), and modify:
[opcache]
zend_extension = opcache.so
opcache.enable = 1
  1. Restart apache, module "opcache" is enabled

EDIT / CONCLUSION

Since "opcache extension" is installed on Mac OS High Sierra by default, the solution of enabling opcahe on Mac OS High Sierra is:

  • Create "/etc/php.ini" if you don't have one, by simply copy the default configuration: sudo cp /etc/php.ini.default /etc/php.ini
  • Add zend_extension = opcache.so to /etc/php.ini and set "opcache" enable:

php.ini opcache section looks like:

[opcache]
zend_extension = opcache.so
opcache.enable = 1
kite.js.org
  • 1,599
  • 10
  • 11
  • Why is `zend_extension = opcache.so` needed? – Yes Barry Mar 15 '18 at 19:01
  • @YesBarry, See [opcache installation](http://php.net/manual/en/opcache.installation.php), _OPcache can only be compiled as a shared extension. Once compiled, you can use the zend_extension configuration directive to load the OPcache extension into PHP. This can be done with zend_extension=/full/path/to/opcache.so_ – kite.js.org Mar 16 '18 at 00:53
0

PHP Opcache Error on MacOS

This happened to me, so i hope it helps others.

When running php cli commands, warnings about opcache loading were displayed:

$ php -v
Failed loading /usr/local/opt/php/lib/php/20220829/opcache.so:  dlopen(/usr/local/opt/php/lib/php/20220829/opcache.so, 0x0009): tried: '/usr/local/opt/php/lib/php/20220829/opcache.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/opt/php/lib/php/20220829/opcache.so' (no such file), '/usr/local/opt/php/lib/php/20220829/opcache.so' (no such file)
PHP 8.2.4 (cli) (built: Mar 16 2023 16:25:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

Version Info:

  • MacOS Ventura 13.3.1 (but i'm pretty sure this stands for any MacOS version).
  • Homebrew 4.0.11

Background

I had installed php 5.6 for a legacy project. I then switched to php 8.2. I had installed 5.6 with a download from php.net. I installed php 8.2 using brew. (Spoiler alert, the 5.6 install seems to have left behind an extra ini file).

Locate opcache.so/php.ini

  1. Find your php.ini by looking at phpinfo().
  2. Find all versions of opcache. I used locate which may or may not be enabled on your mac. Another option would be to use find / -name opcache.so but that could take a while and would need to be run via sudo maybe.

phpinfo() output

My phpinfo() showed an additional ini file parsed:

enter image description here

[opcache]
zend_extension=/usr/local/opt/php/lib/php/20220829/opcache.so

More on this below, for now, let's find out what opcache.so exist....

locate opcache.so

Here are all the opcache.so that were found on my system:

$ locate opcache.so
/usr/local/Cellar/php/8.2.4/lib/php/20220829/opcache.so
/usr/local/Cellar/php@5.6/5.6.40_6/lib/php/20131226/opcache.so
/usr/local/php5-7.3.8-20190811-205217/lib/php/extensions/no-debug-non-zts-20180731/opcache.so

There are three. The 8.2 opcache.so (the one i want to use with php 8.2), the 5.6 opcache.so (a second one included by brew, since i also installed 5.6 using brew), and then the php.net installed 5.7 opcache.so.

Now that we know (a) why php can't find it (errant ini file content) and (b) the path to the ini file we do want to use), we simply update the ini file to point to the right opcache.so file.

Shutup jj, fix the issue

The extra ini file was pointing to the wrong opcache.so file.

Edit the opcache ini file and fix php opcache:

[opcache]
;zend_extension=/usr/local/opt/php/lib/php/20220829/opcache.so
zend_extension=/usr/local/Cellar/php/8.2.4/lib/php/20220829/opcache.so

(The semi-colon is a comment; i left the old line there for future reference).

Result:

$ php -v
PHP 8.2.4 (cli) (built: Mar 16 2023 16:25:32) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.4, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.4, Copyright (c), by Zend Technologies

(The error is no longer evident now that the extra parsed ini file points to the correct opcache.so file.)

WEBjuju
  • 5,797
  • 4
  • 27
  • 36