How to Optimize and speed up PHP with OPCache

PHP 5.5 and 5.6 comes with OPCache. OPCache speeds up PHP execution by storing precompile bytecode of PHP in shared memory. Taking advantage of fast memory operations compared to hard disk operation, OPCache eliminates the process of having to read PHP scripts from the disk each time whenever a script must be executed.

In short, OPCache saves the time needed to serve PHP-powered websites!

How to enable OPCache?
This tutorial is written from Ubuntu 14.04 LTS standpoint – using php5-fpm, but it should also work for other Linux distro.

  1. First edit ‘/etc/php5/fpm/php.ini‘ file.
  2. Find “opcache” section in the PHP ini.
  3. Uncomment and change opcache.enable to opcache.enable=1
  4. Do the same for >opcache.memory_consumption, changed its value from 64 to 128 (or 256)
  5. Change opcache.interned_strings_buffer from 4 to 16
  6. Changed the opcache.max_accelerated_files to 8192

Activate OPCache with php5enmod command.

sudo php5enmod opcache

Save file and restart php5-fpm.

sudo service php5-fpm restart

PHP OPCache should be running on your server now. Here’s a reference of /etc/php/fpm/php.ini file in Ubuntu 14.04 LTS.

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=0

; The OPcache shared memory storage size.
opcache.memory_consumption=128

; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=16

; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 100000 are allowed.
opcache.max_accelerated_files=8192

BONUS: How to verify whether PHP OPCache is running ??
You can verify whether OPCache is running by using opcache-status by Rasmus Lerdorf. Just drop the script in one of your web directory and browse. If you’re not a Git fan, I’ve also have taken the liberty of mirroring the download: opcache-status.zip

opcache-status screenshot
opcache-status-ss

Checking if website is Mobile-Friendly

Google has announced that they will take Mobile-Friendly site into account when indexing sites. Thus, it is prudent to ensure the particular website that you’re maintaining is mobile friendly.

There are two main tools released by Google for testing if the website is Mobile-Friendly:

  1. Google Mobile Friendly Test
  2. PageSpeed Insights

However, personally I like PageSpeed Insights tool better because it gave more in-depth explanation on how to improve my site.

mobile-friendly-test

Have fun trying out.

How to check if your website supports SPDY 3.1

Although Google has announce to drop SPDY support by early February 2016 in favor of HTTP/2, SPDY remains the best HTTP protocol extension to maximize compression and reduce webpage load latency.

The current and final SPDY implementation supported by Mozilla Firefox and Google Chrome is SPDY/3.1.

In order to check if your webserver supports SPDY 3.1, just simply:

  1. Go to your website, eg: https://blog.mypapit.net/
  2. Using Chrome, type – “chrome://net-internals/#spdy” at the addressbar
  3. You should see your website name and spdy/3.1 protocol listed, refer to Screenshot below

spdy

Additionally, you could go to spdycheck.org to check if your web server supports SPDY.

Have fun!

Moved to Singapore!

The server finally has a new home! I finally had the time to look back into this aging blog and decided to moved it to a new datacenter in Singapore!

The new blog also features SSL-only connection which added a layer of privacy between the blog and your browsers.

You can expect more updates from now on!

What to do when your Google disabled your Gmail account?

Recently I managed to get my gmail account disabled by Google. At first, I didn’t think that it would be a big deal since free email account can be registered every now and then, if it managed to get disabled/suspended.

gmail disabled

But…. It turns out I WAS WRONG.

See, when Google disabled a Gmail account, it also disabled all other accounts that associates with that account, including (but not limited to) :

  • Youtube (your videos will be inaccessible)
  • Blogger (all your blogs will be made inaccessible)
  • Google+
  • Google Play / Android Market (partially affects your Android phone and contacts, your paid apps will not be available)
  • Google Drive / Google Docs (you won’t be able to access your files /documents)
  • Google Code (loose access to your code repo)
  • And anything that is related to Google service

How does a Gmail account gets disabled ?

Officially Google states that each accounts holder must comply with Google Terms of Service.

Unofficially, there could be tons of reasons why Google disable a particular Google account, which includes:

  • Suspected partaking in spamming activity
  • Age factor (based on D.O.B data entered, those under 13yrs old aren’t allowed to use Google
  • Suspected using illegal credit card (in case of purchasing through Google Wallet or Google Pay)
  • When Google suspect that your account has been compromised
  • etc…

Anyway, they didn’t tell me exactly what I did and why they disabled my account (they tell me that they won’t disclose details to avoid their automated system from being compromised by hackers).

What they do is ask me when the last time I access my account, using which browser and what are the things that I do with my Google account recently before my account being disabled.

After that, within 24 hours, my account has been restored! So if you find yourselves in similar situation, then you should try get some help from Google Products (Gmail) forums. The people there are helpful, as long as you don’t get too emotional and push them too much.

Note that you should understands the Google Product Terms & Policy when you are using any Google Products. Because from what I’ve seen from the forum, there are also those who aren’t as lucky as I am, getting their accounts permanently disabled. Once it is permanently disabled, there are absolutely *ZERO* chances of recovering your precious data.

Be warned.

Mypapit GNU/Linux blog is now served with CloudFlare!

After long and careful consideration, I decided to enable CloudFlare for my blog.

CloudFlare is a content delivery network which aims to enhance website security and performance. CloudFlare CDN offers protection againts many forms of malicious activity including: spammers, email harvesters, SQL Injection, XSS, denial-of-service attack and suspicious web requests. Therefore saving valueable bandwidth from the web hosting machine.

My Personal Experience with CloudFlare
After a while using CloudFlare, I’ve notice that:

  • My site uses less bandwidth
  • The php-fcgi uses less (valueable RAM)
  • Less comment spam received from blogs
  • Site loads faster, not prone to being bogged down during peak hour

So far, so good, I love using CloudFlare…