Friday, February 27, 2015

Reduce memory consumption on MySQL 5.6

It's been a while since I come back to check MySQL configuration on my development machine. Just feel a bit surprised that it looks like running really big databases within there. When I restarted it, I noticed that MySQL takes over 500 MB in memory usage. Actually, I didn't even start Apache server which comes with XAMPP package.

Current version of MySQL is 5.6.16. Even I'm quite happy with the database performance, it would be nice to save some resources for the other development tools running in parallel, especially for the big Eclipse IDE.

As long as it's not running as production machine, I'm fine with reducing the memory footprint a bit, actually, quite a bit.

Find my.cnf or my.ini for running MySQL instance and add/change the line as follow:

[mysqld]
performance_schema = off
Have a restart for MySQL service and the memory consumption is dropped back to under 100 MB which is fantastic!

Note: The MySQL Performance Schema is a feature for monitoring MySQL Server execution at a low level. You can turn it back on anytime for performance tuning by setting "performance_schema = on".





Tuesday, February 10, 2015

Installing PHP Accelerator ZendOpcahe on Apache 2.4 for Windows

It's Windows platform again and it's all about how to make it fast on running Apache server. It may be easier to find useful materials for Linux platform.

Supposing you are using XAMPP for Windows 1.8.x, you will feel curious about where ZendOpcache module is sitting. It's not there and you'll need to download it yourself.

Opcode caching is a good way to improve PHP performance as it takes out the compiling time of PHP source code for each consecutive loading after the first time that piece of PHP script is loaded and served.

Here're the steps:

1. Please download the package (version as of today is 7.0.4) via the following link:
http://windows.php.net/downloads/pecl/releases/opcache/7.0.4/php_opcache-7.0.4-5.4-ts-vc9-x86.zip

2. Stop Apache Service.

3. Download and extract the library file “php_opcache.dll” and copy it into the directory “C:\xampp\php\ext\”.

4. Add the following lines (marked in red) to the end of PHP configuration file:

.
.
.
C:\xampp\php\php.ini

 …

 ; ; set in php ini file only

 ; ; make sure it's readable (open_basedir is checked) by coverage viewer script

 ; xcache.coveragedump_directory = ""

 ; ;; END OF XCACHE CONFIG



 [Zend_Opcache]

 zend_extension = "C:\xampp\php\ext\php_opcache.dll"

 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

.
.
.

5. Start Apache service.

6. Examine and check if the libraries are loading properly via http://localhost/xampp/phpinfo.php


Configuration of mod_fcgid in XAMPP Apache for Windows

First thing first, which one is the fastest of serving PHP source?

Here's a nice comparison on various PHP implementations in Apache:

http://2bits.com/articles/apache-fcgid-acceptable-performance-and-better-resource-utilization.html

Interesting points here:


Apache's mod_php
Apache's mod_php is the most widely used mode for PHP with Apache. mod_php itself is the entire PHP interpreter embedded in each Apache process that gets spawned. This provides performance and stability benefits, e.g.
No need to call an external process (e.g. CGI).
No need to communicate with another process via sockets (e.g. Fast CGI).
The APC cache is shared by all Apache processes.
It also has some disadvantages
The memory footprint per Apache process is large, specially when sites indulge in contributed modules.
If Apache is serving static content, e.g. images and CSS files, it still has to spawn large processes because of the embedded PHP interpreter.


CGI
CGI (Common Gateway Interface) is the legacy way of runing applications on the web from the mid 1990s or so. It was too inefficient for anything but small sites. CGI spawns a new process for every incoming request to execute a PHP script, a very resource intensive and inefficient way of doing things. No wonder it faded away over time as web applications became more complex.


FastCGI
FastCGI was introduced to avoid some of the issues with running languages, including PHP, inside the Apache process, as well as avoiding the inefficiency of CGI.
A FastCGI application is executed outside of the web server (Apache or other wise), and waits for requests from the web server using a socket. The web server and the FastCGI application can even be on separate physical machines and communicate over the network.
Because the web server and the application processes are separate better isolation is possible.
In reality, running PHP as mod_fastcgi with Apache has proved to be problematic. Mainly with stability. Even on Drupal.org we tried it for a while, but switched back to mod_php after some time.


mod_fcgid
mod_fcgid was introduced to be binary compatible with FastCGI, but with better control over spawning processes. The benefits of process isolation are still there.


Looks like a brief history on how CGI evolves.

mod_fcgid is a high performance alternative to mod_cgi or mod_cgid, which starts a sufficient number instances of the CGI program to handle concurrent requests, and these programs remain running to handle further incoming requests.

The Apache Software Foundation and the Apache HTTP Server Project are pleased to announce the release of version 2.3.9 of mod_fcgid, a FastCGI implementation for Apache HTTP Server versions 2.0, 2.2, and 2.4. This version of mod_fcgid is a security release.

I have been searching around for the way to implement mod_fcgid in Apache 2.4 for Windows which is embedded in XAMPP 1.8.x package.

Here's the procedure that would work on Windows:

1. Please download the zip package via the following link:
https://www.apachelounge.com/download/win32/modules-2.4/mod_fcgid-2.3.9-win32.zip

2. Stop Apache service.

3. Expand and extract the library file “mod_fcgid.so” and then copy it under the directory C:\xampp\apache\modules\.

4. Create new configuration file with the following content (adjust the actual paths of XAMPP installation directory to suit the needs):

C:\xampp\apache\conf\extra\httpd-fcgid.conf

##################################################################

 # Configuration for mod_fcgid

 # FileName: conf/extras/mod_fcgid_php.conf







 # Fast CGI module Settings (PHP 5.3, 5.4)



  FcgidInitialEnv PHPRC "C:\\xampp\\php"

  FcgidInitialEnv PATH "C:\\xampp\\php;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\System32\\Wbem;"

  FcgidInitialEnv SystemRoot "C:\\Windows"

  FcgidInitialEnv SystemDrive "C:"

  FcgidInitialEnv TEMP "C:\\WINDOWS\\TEMP"

  FcgidInitialEnv TMP "C:\\WINDOWS\\TEMP"

  FcgidInitialEnv windir "C:\\WINDOWS"



 # Fast CGI module Settings (PHP 5.2)



 # FcgidInitialEnv PHPRC "C:/php/"

 # FcgidInitialEnv PATH "C:/php;C:/WINDOWS/system32;C:/WINDOWS;C:/WINDOWS/System32/Wbem;"

 # FcgidInitialEnv SystemRoot "C:/Windows"

 # FcgidInitialEnv SystemDrive "C:"

 # FcgidInitialEnv TEMP "C:/WINDOWS/TEMP"

 # FcgidInitialEnv TMP "C:/WINDOWS/TEMP"

 # FcgidInitialEnv windir "C:/WINDOWS"



 # Global mod_fcgid settings



  FcgidIOTimeout 40

  FcgidConnectTimeout 10

  FcgidMaxProcesses 8

  FcgidOutputBufferSize 64

  ProcessLifeTime 240

  FcgidMaxRequestsPerProcess 500

  FcgidMinProcessesPerClass 0





 # Global Config Example

 # Comment out next 4 lines to use per-Directory or per-VirtualHost configuration

  

     #Options ExecCGI

  Options Indexes FollowSymLinks ExecCGI

     AddHandler fcgid-script .php

     FcgidWrapper "C:/xampp/php/php-cgi.exe" .php

  



 #

 # Examples below show how you can use per-Directory and per- VirtualHost Configs

 # You can use multiple Directory and VirtualHost Configs at the same time

 #



 # Per Directory Config Example

 #

 #

 #  FcgidInitialEnv PHPRC "C:/php"

 #  AddHandler fcgid-script .php

 #  Options Indexes FollowSymLinks ExecCGI

 #  AllowOverride all

 #  FcgidWrapper "C:/php/php-cgi.exe" .php

 #  Require all granted

 #





 # Per VirtualHost Config Example

 #

 #

 # DocumentRoot  /Apache22/htdocs/fcgi

 # ServerName fcgi.local

 # ErrorLog logs/fcgi.error.log

 # CustomLog logs/fcgi.access.log common

 # FcgidInitialEnv PHPRC "C:/php"

 #

 #  AddHandler fcgid-script .php

 #  Options Indexes FollowSymLinks ExecCGI

 #  AllowOverride all

 #  FcgidWrapper "C:/php/php-cgi.exe" .php

 #   Require all granted

 #

 #





 # Do not comment out below line



 ######################################################################





5. Add new lines to the following configuration file:
C:\xampp\apache\conf\httpd.conf
…

#LoadModule watchdog_module modules/mod_watchdog.so

#LoadModule xml2enc_module modules/mod_xml2enc.so

LoadModule fcgid_module modules/mod_fcgid.so





…

#FCGI Module

 Include "conf/extra/httpd-fcgid.conf"

#End of file





6. Comment out several lines to the following configuration file:
C:\xampp\apache\conf\extra\httpd-xampp.conf
…

#
# PHP-Module setup
#
LoadFile "C:/xampp/php/php5ts.dll"
LoadFile "C:/xampp/php/libpq.dll"
LoadModule php5_module "C:/xampp/php/php5apache2_4.dll"

#Comment out to use purely mod_fcgid to serve all .php files, see httpd-fcgid.conf
#
#    SetHandler application/x-httpd-php
#
#
#    SetHandler application/x-httpd-php-source
#
7. Start Apache service and test the web pages.

8. Examine and check if the libraries (including mod_fcgid) are loading properly via http://localhost/xampp/phpinfo.php