Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install PHP Opcache on MacOS High Sierra?

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.

like image 893
DisgruntledGoat Avatar asked Dec 13 '17 00:12

DisgruntledGoat


People also ask

How do I enable OPcache?

OPcache can only be compiled as a shared extension. If you have disabled the building of default extensions with --disable-all, you must compile PHP with the --enable-opcache option for OPcache to be available. Once compiled, you can use the zend_extension configuration directive to load the OPcache extension into PHP.

How do I use OPcache in PHP?

OPCache can only be compiled as a shared extension under this version. Firstly, you need to enable the building of default extension with –enable-opcache option to make it available. Afterwards, you can use the zend_extension configuration directive to lead the OP Cache extension into PHP.


1 Answers

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
like image 64
kite.js.org Avatar answered Oct 06 '22 00:10

kite.js.org