NeelZone

What's in the Zone?

Limping From Scratch

Posted by indroneel on February 18, 2009

Setting up the technology stack is usually the first stumbling block encountered while developing with PHP and MySQL. The common practice is to use Apache web server as the HTTP gateway. Setting up these three components separately is not a trivial task because of the variety of configuration parameters, file system paths and launch options involved.

To mitigate this, many a software provider have come up with pre-integrated Apache, PHP and MySQL distributions that require much less effort in configuration and setup. Usually called a LAMP (acronym for Linux, Apache, MySQL and PHP) or WAMP in case of windows, these bundles provide an accelerated approach for newbies who have embarked on the PHP track.

Simplicity notwithstanding, most LAMP/WAMP implementations still include certain limitations when it comes to extreme portability, configurability and custom integration. A separate discussion of these limitations is beyond the scope of this article. Instead, we shall guide you through a step-by-step approach to creating your own integrated, flexible and redistributable AMP environment from scratch.

A Different Web Server

Our integrated platform shall incorporate LightTPD as the web server, even though Apache is a more popular choice. The reason for this deviation stems from the following:

  1. LightTPD, as the name suggests, is an extremely lightweight server. A typical Apache installation usually consumes about 20 MB of RAM at runtime. In comparison, LightTPD usually requires around 7 – 10 MB of RAM, making it a much faster and resource-friendly alternative.
  2. The Apache configuration always requires absolute paths for file system locations (e.g. server root path, web folders, etc.) LightTPD, on the other hand, can work pretty well with relative file paths, resulting in better portability.

Since we shall make use of LightTPD, our integrated bundle should be categorized as a LiMP. Furthermore, this article will describe the creation of a LiMP for the windows operating environment only.

Benefits

The primary target herein is to demonstrate how easy it is to create a redistributable integrated platform for PHP and MySQL development, in the face of existing popular solutions. Additionally, our LiMP implementation will provide the following subtle benefits:

Full Portability

Extract and run the package on any windows machine, under any folder with no setup. This means that it should be equally possible to keep a copy on an attached media (e.g. a pen drive) and carry the same across machines with absolutely no restrictions or additional installation steps.

Most LAMP/WAMP requires an installation process for each machine, or extra reinstallation steps when migrating to a different file system location.

Independent Upgrades

Upgrade specific components of the integrated platform independent of others. For example, it should be possible to move to a different version of PHP or MySQL without breaking the whole platform.

Most LAMP/WAMP comes up with periodic and complete platform updates and does not guarantee integrity in operation after ad-hoc replacement of specific components.

PHP as Fast CGI

When run in fast CGI mode, PHP provides significant improvements in terms of performance and more optimal resource consumption. Our platform will run PHP exclusively in fast CGI mode.

Practically no LAMP/WAMP provides out-of-the-box, non-trivial and supported options for PHP in fast CGI mode.

Selective Integration

Have a trimmed down version of the platform by removing components that are not required by your application. For example, if you are planning to deploy a DokuWiki instance, it is a good idea to altogether remove the heavy MySQL server component. Alternatively, it should be possible to add extra components (e.g. a Java servlet environment or Perl or a custom MySQL installation) with equal ease.

Most LAMP/WAMP will allow you to shutdown the specific component (if running as a service) but may not allow you to remove the component altogether.

Flexibility in Folder Structure

End-users have the option to install their applications under a different folder that is both external and relative to the platform’s folder hierarchy. Moving to a different file system location or a different machine will not break this arrangement as long as the folders involved are duly moved along with the core LiMP folders.

Most LAMP/WAMP mandates that applications be installed under specific paths within their corresponding folder hierarchy or use absolute path names which limit portability.

Things You Will Need

  • Download a copy of the MySQL server from http://www.mysql.com. The latest stable version of MySQL at the time of writing this article is 5.1. You should get the no-install package for windows that can be extracted and run without the need for elaborate setup.
  • Download a copy of PHP from http://www.php.net. The latest stable version of PHP at the time of writing this article is 5.2.8. You should get the no-install package for windows that can be extracted and run without the need for special setup.
  • Download a copy of LightTPD from http://code.google.com/p/wlmp-project. The latest stable version of LightTPD at the time of writing this article is 1.4.20. You should get the no-install package that can be extracted and run without the need for extra setup.
  • RunHiddenConsole.EXE is a small utility that enables processes to be launched in the background without an attached console window.
  • Process.EXE is a small utility that helps to enumerate and kill active processes running in the background.

For the benefit of readers, the last two utility applications have been provided as part of a resource bundle attached with this article.

Package Directory Structure

The snapshot below illustrates the package directory structure that we shall follow for our LiMP distribution. It should be possible to adopt a different structure with alternate folder names with little difficulty.

LiMP Package Directory Structure

LiMP Package Directory Structure

Note the sites folder that resides outside of the LiMP package directory structure. All PHP applications will reside in separate subfolders under sites.

Getting Started

Extract the contents of PHP package into the folder called php. Also extract the contents of LightTPD package into the folder called lighttpd. These are must have components.

If your application needs a MySQL server, extract the contents of the corresponding package into the folder called mysql.

Create a file called lighttpd.conf.template and place the same under the folder lighttpd/conf. The contents of this file should be a concatenation of lighttpd-srv.conf, lighttpd-tag.conf and lighttpd-inc.conf in that order. These three files comes pre-bundled with LightTPD distribution and is available under the lighttpd/conf folder after extraction.

Startup and Shutdown Scripts

Startup and shutdown scripts are created as batch files using the Windows command language. The purpose of these scripts is three-fold:

  1. Create a custom LightTPD configuration file on the fly at the time of platform startup. This file is created using lighttpd/conf/lighttpd.conf.template as the template and other system parameters available at runtime.
  2. Launch the MySQL, PHP fast CGI and LightTPD as background processes (in that order) during startup. Note that inclusion of MySQL in this list is optional.
  3. During shutdown, kill or shutdown the above mentioned background processes but in reverse order.

The startup script file is named as startserver.cmd and is placed under the root of the package directory structure. The contents of this file are as follows:

@echo off
@setlocal

set LIMP_ROOT=%cd%
cd ..\sites
set SITE_ROOT=%cd%
cd %LIMP_ROOT%

echo dir.limp = "%LIMP_ROOT%\lighttpd/" > lighttpd\conf\lighttpd.conf
echo dir.site = "%SITE_ROOT%/" >> lighttpd\conf\lighttpd.conf
type %cd%\lighttpd\conf\lighttpd.conf.template >> %cd%\lighttpd\conf\lighttpd.conf

RunHiddenConsole mysql\bin\mysqld.exe --standalone
RunHiddenConsole php\php-cgi.exe -b 127.0.0.1:521
RunHiddenConsole lighttpd\lighttpd.exe -f lighttpd\conf\lighttpd.conf -m lighttpd\lib -D

@endlocal

The first two echo statements create a file called lighttpd\conf\lighttpd.conf (relative to the script file location) and add the following lines at the very beginning:

dir.limp = "E:\Limp_From_Scratch\MyLimp/"
dir.site = "E:\Limp_From_Scratch\sites/"

Generated folder names shall vary depending on the actual file system location used. If the file lighttpd\conf\lighttpd.conf pre-exists, it will be erased and overwritten with new contents.

The next type statement appends the contents of lighttpd/conf/lighttpd.conf.template to the file lighttpd/conf/lighttpd.conf after the first two lines generated by the previous echo statements.

The remaining three lines launch the different services in the background. Refer to the product user manuals to understand the meaning of different command line options used while launching each service.

The shutdown script file is named as stopserver.cmd. The contents of this file are as follows:

@echo off
@setlocal

process -k LightTPD.exe
process -k php-cgi.exe
mysql\bin\mysqladmin.exe -b -h 127.0.0.1 -u root shutdown

@endlocal

To understand more about the parameters accepted by Process.EXE on the command line, type

process.exe –h

in a CMD window. Note that the LightTPD and PHP-CGI instances are killed while the MySQL instance is shutdown gracefully to avoid database corruption.

If you are not planning to have a MySQL server as part of your stack, the corresponding lines for database startup and shutdown can be removed from within the scripts.

Customizing the LightTPD configuration

Any changes to the LightTPD configuration must be made in the file conf/lighttpd.conf.template since the actual configuration file used is auto-generated every time the platform is started. The changes described here are one time only. Any subsequent migration of the platform to a different media and/or file-system location will not require any change in this configuration file.

For the benefit of readers, a sample copy of lighttpd.conf.template file has been provided as part of a resource bundle attached with this article. Required changes (from the default set of configuration values) already incorporated in this template, are as described below:

Modify the paths to access logs, error logs and pid files — prepend the same with dir.limp resulting in usage of absolute path names.

# where to send error-messages to
server.errorlog = dir.limp + "logs/lighttpd-srv.error.log"

# accesslog module
accesslog.filename = dir.limp + "logs/lighttpd-srv.access.log"

# to help the rc.scripts
#server.pid-file = dir.limp + "logs/lighttpd-srv.pid"

Enable the module for fast CGI by un-commenting the line containing the entry mod_fastcgi under the list of server modules.

server.modules = (
...
"mod_fastcgi",
...
)

Modify the path to document root folder – prepend the same with dir.site resulting in usage of absolute path names.

server.document-root = dir.site + "root/"

In the example above, the document root points to the folder sites/root. In case you wish to retain the default document root HTDOCS, you should use the variable dir.limp instead

server.document-root = dir.limp + "HTDOCS/"

Establish connection with the PHP Fast CGI server. See example below

fastcgi.server = (
".php" => (
"localhost" => (
"host" => "127.0.0.1",
"port" => 521
)
)
)

Create aliases for additional applications deployed in separate sub-folders under sites. Our example defines three different sites: blog, phpmyadmin and wiki (see screenshot for directory structure above). The corresponding entries in the configuration file should be as follows:

alias.url = (
"/dba" => dir.site + "phpmyadmin",
"/blog" => dir.site + "blog",
"/wiki" => dir.site + "wiki",
)

Let’s Launch!

The LiMP platform is now ready to go live. Create a file called index.php under the document root folder with the following contents:

<?php
phpinfo();
?>

Point your web browser to http://localhost. If everything works fine, you should see the PHP runtime information page displayed.

Things to Note

When run in fast CGI mode, PHP requires that any file passed from the web server should be referenced using absolute names. Hence having all aliases and document location prefixed with either of dir.limp and dir.site is mandatory.

Generated folder names contained in variables dir.limp and dir.site must end with a trailing slash to avoid additional changes to the configuration template. Furthermore, this must be a forward slash since LightTPD has a problem interpreting a back slash at the end of a string.

The MySQL installation has an empty password for the superuser root account. This is a major security risk and a password should be defined for all production deployment. Once a password has been generated using mysqladmin.exe (or a similar) tool, the command to shutdown MySQL in the stopserver.cmd script should be changed to:

mysql\bin\mysqladmin.exe -b -h 127.0.0.1 -u root -p {password} shutdown

Where {password} should be replaced with the actual root password.

Resources and Downloads

[1] limp_from_scratch.zip contains a copy of RunHiddenConsole.EXE, Process.EXE and lighttpd.conf.template alongwith startup and shutdown scripts described in this article. Note: due to WordPress restrictions, the file is uploaded with the .PDF extension. You should rename the file to have a .ZIP extension after download.

[2] Lighty2Go is the only other portable LiMP package for windows. However, it does not provide PHP in fafst CGI mode.

[3] XAMPP is the most popular and stable LAMP/WAMP distribution till date. However, it has some restrictions on portability.

Advertisements

6 Responses to “Limping From Scratch”

  1. […] NeelZone placed an observative post today on Limping From ScratchHere’s a quick excerptSetting up the technology stack is usually the first stumbling block encountered while developing with PHP and MySQL. The common practice is to use Apache web server as the HTTP gateway. Setting up these three components separately is not a trivial task because of the variety of configuration parameters, file system paths and launch options involved.To mitigate this, many a software provider have come up with pre-integrated Apache, PHP and MySQL distributions that require much less effort in con […]

  2. […] Limping From Scratch « NeelZone […]

  3. moko said

    thank for information….

  4. I read your posts for a long time and must tell that your articles always prove to be of a high value and quality for readers.

  5. Very Informative article, i really needed some info on lightweight LAMP.

  6. fantastic post, very informative. I’m wondering why the other experts of this sector do not understand this. You should continue your writing. I’m confident, you have a huge readers’ base already!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: