How to install PHP Opcache on MacOS High Sierra?

DisgruntledGoat picture DisgruntledGoat · Dec 13, 2017 · Viewed 7.3k times · Source

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.

Answer

kite.js.org picture kite.js.org · Dec 15, 2017

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