php 5.6 enable opcache

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

php 5.6 enable opcache

文章 yehlu »

http://www.hostingadvice.com/how-to/ena ... ntu-14-04/

With the release of Ubuntu 14.04 and the proliferation of PHP 5.5, there is going to be a migration away from Alternative Performance Cache (APC) and toward PHP’s new built-in OPcache.

This is a logical move that seems destined for any interpreted language. As websites have become more and more complicated with many processes running, opcode caching has become a necessity – fortunately, it’s simple to implement.

The php.net site has a nice page of all the runtime options available, but we will cover the basics here to get you started quickly.

All you need to do to get OPcache set up is to make changes in the php.ini file on your server.

Open php.ini In Your Favorite Text Editor
To get started open your php.ini file.

Apache web-servers

/etc/php5/apache2/php.ini
Nginx web-servers with PHP-FPM

/etc/php5/fpm/php.ini
Enable the OPcache
To enable the OPcache, change to the following lines — easy enough!

;opcache.enable=0
Change to:

代碼: 選擇全部

opcache.enable=1
Note: you have to uncomment this line as well as change the “0″ to “1″.

Modify the Amount of RAM the OPcache Will Use
With OPcache, there is a trade-off between speed and the amount of RAM used. The more RAM you are willing to dedicate to storing opcode, the more opcode that can be stored. There is a diminishing return at some point, because some code will execute rarely, or your code base might not be that big. It is worth playing with this setting to see where you get the best performance-versus-RAM trade-off. This setting is in megabytes.

;opcache.memory_consumption=64
Change to:

代碼: 選擇全部

opcache.memory_consumption=128
Boost the Number of Scripts that Can Be Cached
OPcache has a strange setting that requires you to not only adjust the amount of RAM, but also define the number of scripts that can be cached. You have the option of tuning this parameter for your own application too, especially if you find that your hit rate is not close to 100 percent.

;opcache.max_accelerated_files=2000
Change to:

代碼: 選擇全部

opcache.max_accelerated_files=4000
Change the Revalidate Frequency
To make sure that the OPcache notices when you change your PHP code, you can set the revalidate frequency. Basically, this will tell the cache how often to check the timestamp on the files. This is measured in seconds.

;opcache_revalidate_freq = 2
Change to:

代碼: 選擇全部

opcache_revalidate_freq = 240
Verify that the PHP OPcache Mod is Enabled
Believe it or not, that converts most of the settings you will need to get started. PHP5 has its own module system (since 5.4), so make sure that OPcache is enabled.

sudo php5enmod opcache
Restart PHP and Your Server
You should now be all set to start using PHP 5.5’s OPcache. You just need to restart your server to get it going.

Apache Web-Servers

sudo service apache2 restart
Nginx Web-Servers

sudo service nginx restart
Conclusion
Well, there you have it. It’s actually amazingly simple to get this up and running. There are a number of options that allow you to monitor your hit rate with OPcache. Here is an open-source solution (OPcache Status) that can be found on GitHub.

Ryan Frankel
Questions or Comments? Ask Ryan!
Ask a question and Ryan will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: php 5.6 enable opcache

文章 yehlu »

https://blog.longwin.com.tw/2016/02/php ... rate-2016/

PHP 於 5.6 後,Debian / Ubuntu Linux 預設都內建 OPcache,有了 OPcache 就不再需要 APC / APCU 囉!

APC / APCU / OPCache 的差異如下:

APC:OP Code Cache + User Data Cache (使用者儲存資料快取)
APCU:User Data Cache
OPCache:OP Code Cache

PHP 使用 Zend OPcache 加速

如何確認系統是否啟用 opcache?可由下述查看:

php -m | grep -i opcache # Zend OPcache
ls /etc/php5/apache2/conf.d/ # 找到 05-opcache.ini
若沒有啟用,OPcache 的啟用方式:

sudo php5enmod opcache
OPcache 參數設定

跟 APC 一樣,OPcache 也可以設定記憶體大小.. 等等的參數,可見此篇:PHP: OPcache Runtime Configuration

下述設定可以參考看看,請再依照自己的環境修改:

vim /etc/php5/apache2/conf.d/05-opcache.ini
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
sudo service apache2 reload
想查看 OPcache 使用的情況,下述工具可以參考使用:

rlerdorf/opcache-status: A one-page opcache status page
PeeHaa/OpCacheGUI - GUI for PHP's OpCache - GUI 查看 OPCache
相關網頁

使用 Zend Opcache 加速 PHP
How to use PHP OPCache? - Stack Overflow
How To Enable PHP 5.5 Opcache on Ubuntu 14.04
回覆文章

回到「PHP」