Introducing MemChat: Open source group chat framework in PHP supporting Memcached, APC, SQLite, Flat Files and MySQL

MemChat is an open source group chat framework for personal and enterprise level websites. Written in PHP, MemChat can be configured to run with Memcached, APC, SQLite, Flat files and MySQL as it’s storage engine. With memcached, APC and Flat files serving as temporary storages and MySQL, SQLites being permanent storage engines.

MemChat uses MemBurger API for storing all the incoming messages in the storage engine. MemBurger is an open source PHP wrapper for all the storage engines mentioned above providing Collapsed Forwarding and Stale-While-Revalidate functionality.

MemChat can also be configured to notify the site owners at various event handlers provided. e.g. In case of a wordpress blog, site owner might want to get notified when someone post a new message on one of the blog post. Infact developers can write plugins for custom event handlers. MemChat provides two kind of notification methods. By default, MemChat uses XMPP protocol to notify blog owners. For these kind of notifications, JAXL an open source Jabber XMPP Client Library is used to send instant messages as notifications. MemChat can also be configured to send notifications using SMTP protocol a.k.a EMail.

MemChat also comes with a profanity word filter using PHProfane library i.e. one can configure MemChat to block all spam messages posted in the chat rooms. Developers can write a plugin to add custom spam words in the profanity filter.

MemChat Flow Diagram:
Below is a flow diagram showing how all the above components are clubbed in as MemChat:
MemChat Workflow Diagram

MemChat use cases:
MemChat framework requires a unique alphanumeric id for each group you want to create. For example, WordPress blog makes a good environment to setup MemChat since every blog post have a unique id. Hence to setup MemChat on a wordpress blog all we need to do is, call

$memchat_ui_html = memchat_init($memchat_group_id);

method, where $memchat_group_id = Blog post id.

Similarly MemChat can be setup on a number of places as listed below:

  1. Forums: Since each forum have a unique id per discussion thread, MemChat suits well here. Forum owners can setup MemChat for allowing current viewing users of the thread to chat in real time. If using temporary storage engines (since they scale up well), forum owners can easily write event handlers to save chat messages asynchronously so that interesting discussions can be made a part of actual discussion thread.
  2. Social Networking Websites: A social network will generally have group pages, fan pages, event pages and application pages. Since all pages will have a unique id associated with them, MemChat is a good fit here. Simply pass $blog_id = “group-1234”, for setting up a chat room on group page having id 1234. Similarly for fan, event and application pages.
  3. Blogs: A blog as discussed above is a perfect place for setting up MemChat. WordPress blog owners will be able to setup MemChat on their blog using WP-Chat plugin.

Setting up MemChat in 3 steps:
MemChat can be setup in 3 simple steps:

  1. Download: MemChat is hosted on Google Code.
  2. Update Configuration: Update MemChat config file to setup various default behavior and actions.
  3. Initialize: Include MemChat class file in your PHP template.
    include_once("/path/to/memchat.class.php")

    Next simply initialize MemChat

    $memchat_ui_html = memchat_init($memchat_group_id);

    which will return the user interface HTML code. Finally append it to your template and MemChat group is ready to serve.

The ease of setup can be imagine from the fact that, WP-Chat wordpress plugin development required only the above 3 steps. I was able to develop the plugin within 5 minutes.

MemChat Performance Benchmark
Initial benchmarking results show, On a single apache and single storage instance MemChat is capable of handling over 250 (MySQL) incoming chat messages per second. Capacity reaches in thousands when configured with Memcached or APC cache. Again Memcached configuration and APC no stat feature makes a difference in performance results.

Why still in alpha?
Yes, MemChat is still in alpha release. I want to test all MemChat features on a live traffic and hence I chose my blog for its testing. I have installed MemChat on my blog which is currently using APC cache as storage medium. Over two weeks or so I will configure MemChat with various other storage engines to have a final round of test. Also the event handler and PHProfane plugin feature is still under development. MemBurger API also requires a more modular design pattern so that extending it for any other storage types become trivial.

Try out MemChat on my blog, can be seen as a facebook type chat bar below. This will be soon available as WP-Chat plugin for wordpress blogs. WP-Chat user interface is customizable just like wordpress themes.

Let me know your views and suggestions for improvement.

MySQL Query Cache, WP-Cache, APC, Memcache – What to choose

Hello Cache Freaks,

Ever since I changed my job (from Business Intelligence to Web development) and started working with my present employer, I have had a chance to work on a lot of scalable projects. From making my project to scale from 20 Million PV’s to 100 Million PV’s to development of an internal tool, the answer to all scalable applications have been caching.

There are a lot of caching techniques which are being employed by sites worldwide.

  1. WP-Cache used in wordpress – a file system based caching mechanism
  2. APC Cache – an opcode based caching system
  3. Memcache – an in memory caching system
  4. Query Cache – caching mechanism employed in MySQL

Here in this post I would like to pen down my experiences while working with all the caching mechanism. Their pros and cons. What things you need to take care while working with them and every little tit bit which comes to my mind while writing this post.

Query Cache – inbuilt cache mechanism in MySQL
Query cache is an inbuilt cache mechanism for MySQL. Basically when you fire a query against a MySQL database, it goes through a lot of core modules. e.g. Connection Manager, Thread Manager, Connection Thread, User Authentication Module, Parse Module, Command Dispatcher, Optimizer Module, Table Manager, Query Cache Module and blah blah. Discussing these modules is out of scope of this blog post. The only module we are interested here is Query Cache Module.

Suppose I fire a query:
$query = “Select * from users”;

MySQL fetches it for the first time from the database and caches the result for further similar query. Next time when you fire a similar query, it picks the result from the cache and deliver it back to you.

However, there are a few drawbacks with Query Cache:

  • If there is a new entry in the users table, the cache is cleared.
  • Secondly, even if the result of your query is cached, MySQL has to go through a number of core modules before it give back the cached result to you.
  • Thirdly, even if your results are caches, you need to connect to your MySQL database, which generally have a bottleneck with number of connections allowed.

One thing which you should take care while replying on Query Cache is that, your query must not have any parameter which is random or changes quite often. For e.g. If you wish to fetch url’s of 100 photo from the database, and then you want to present them in a random fashion every time, you might be tempted to use rand() somewhere in your MySQL queries. However by using rand() you ensure that the query is never returned from cache, since rand() parameter always makes the query look different.

Similarly, If you have a case where you need to show data not older than 15 days, and by mistake you also include the seconds parameter in your SQL query, then the query will never return from cache.

Hence for a scalable site, with 100 Million PV’s you can’t really survive with a simple query cache provided by MySQL database.

WP-Cache – Caching mechanism for WordPress blogs
WP-Cache is a file system based caching mechanism i.e. it caches your blog posts in form of simple text files which are saved on your file system. You can have a look at these cached files by visiting wp-content/cache folder inside your blog directory. Generally you will find two set of files for a single blog post. One .html and another .meta file.

.html file generally contains the static html content for your blog post. Once published, the blog post content is static, hence instead of fetching it’s data from the database, WP-Cache serves it from the cache directory.

.meta file contains serialized information such as Last Modified, Content-Type, URI which WP-Cache uses to maintain cache expiry on your blog.

WP-Cache works really well, however if the traffic starts increasing on your blog, then the bottleneck will be maximum number of child processes which apache can create (For starters you can think, each user connecting to your blog as one apache process, hence there is a restriction on number of users who can connect to your blog at a particular time). In such case the solution can be either to have multiple front end servers with a load balancer to distribute the traffic among front end servers, or to have a better cache solution such as a memory based caching mechanism (e.g. Memcache). Also since memory read is always faster than file read, you must go for a memory based cache system.

APC Cache – An opcode based cache for PHP
APC stands for Alternative PHP Cache. In one of my previous post How does PHP echo’s a “Hello World”? – Behind the scene, I talk about how PHP churns out “Hello World” for you.

PHP takes your code through various steps:

  1. Scanning – The human readable source code is turned into tokens.
  2. Parsing – Groups of tokens are collected into simple, meaningful expressions.
  3. Compilation – Expressions are translated into instruction (opcodes)
  4. Execution – Opcode stacks are processed (one opcode at a time) to perform the scripted tasks.

Opcode caches let the ZEND engine perform the first three of these steps, then store that compiled form (opcodes) so that the next time a given script is used, it can use the stored version without having to redo those steps only to come to the same result.

However the problem with APC cache is that it is not a distributed cache system. By distributed cache I mean, if you have 3 frontend server then you need to have a copy of this opcode on all the three fronend server. Also like WP-Cache APC is again a file system driven cache system, which is not the optimal solution. Also with APC cache, PHP still has to go through the last step as described above.

Memcache – In memory based cache mechanism
Memcache is the solution when you talk about million PV’s on your site. It is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

For starters, Memcache is not PHP, nor Python or any other thing as you may think. It’s a deamon which runs in the background on your server. Your code connects to it and cache query results, JS, CSS and other cachable data in the server’s memory (RAM). Since its an in-memory caching system, it is faster than any of the discussed caching system above. However it is unreliable and unsecure but then there are ways to tackle this unreliable and unsecure nature of memcache.

Memcache is unreliable because it resides in your system memory. So an event like system reboot or power failure will result in loss of all your cache. Also memcache provides no mechanism to take backup of your caches. Hence once lost, you need to programmatically warmup your caches.

Memcache is unsecure because it doesn’t require any authentication mechanism. There is no username or password with which your code connects to it. (Hence it is super fast, unlike Query cache which has to go through auth module even if the query result is cached). It usually runs at port 11211 on your server and if not taken care, anyone can telnet to port 11211 on your server and steal your caches.

Below are the steps which are being followed on a memcache enabled website:

  1. User enter your site’s url in his browser, say http://localhost
  2. There are about 6 queries which drives your opening page
  3. Lets assume one of the query for this page is $query = “SELECT photo_url from photos LIMIT 0,10”
  4. When the user visit http://localhost, your code will first connect to memcache deamon on port 11211
  5. If memcache deamon is running, it checks if the result of this query are already cached. Generally data is cached in memcache as (key,value) pair
  6. Since this is the first visit on your site, ofcourse there is no query being cached till now. Hence your code now connect to your MySQL database and fetched the resultset. Something like this probably. $resultset = mysql_query($query);
  7. After successfully fetching the resultset, your code will cache this resultset in memcache. The code connects to memcache deamon and saves this (key,value) pair in memory, where $key = md5($query) and $value = serialize($resultset)
  8. After caching the (key,value) pair, your code returns back the fetched resultset to the frontpage where it is being displayed to the user
  9. Similarly all the 6 queries which drives your front page are being cached one by one
  10. Now the next time when another user visit this page i.e. http://localhost, your code will first see if $key = md5($query) is already present in cache. It will find the $key being cached, fetches the serialized resultset from memory, unserialize it and throws back to the front page where it is displayed as intended
  11. While caching (key,value) pair you also have a provision to specify a TTL (time to live) after which memcache will automatically expire your cached result
  12. Suppose you specifies a TTL = 15 Minutes for all the above queries and now a visitor visit http://localhost after 30 minutes
  13. Your code will again first connect to memcache deamon and check if $key = md5($query) is present in cache. Memcache deamon will see that this $key is present but it has expired. It will return a result saying $key is not cached and internally flushes out $key. Your code then again connect to MySQL database, fetches the resultset and cache back the results in memcache for further use

I will leave you with a presentation on memcache which I gave sometime back at office. I hope it will help you gain more understanding of memcache.

Memcache

View SlideShare presentation or Upload your own. (tags: caching memcache)

In my next posts, I will be covering a few code samples and use cases of memcache which you wouldn’t even heard of. If you liked the post don’t forget to promote it on social networking sites and do subscribe to my blog. 🙂

How to get started with web development?

Many of my friends have asked me on how to start with web development. I am not expert but here in this post I would like to describe a few steps that might help one climb the ladder of web development fast. Kindly feel free to add to this blog post if you think some other methodology might be better off.

  1. Where do I start web development? Well the first and best place to start web development is your own system. Yes your very own personal computer. You don’t need a server on internet to start with. Infact I worked locally on my computer for about a month before I went for free hostings, shared hosting and finally VPS. Once you feel that you are comfortable enough with the tid-bits of web development, then you can move to one of them.
  2. How to configure my local computer for that? You have a number of options to start with. I personally started on Tomcat Server after my initial training at Oracle. Tomcat is suited if you are starting with JSP (Java Server/Servlet Pages) . I started by making a few basic pages like creating a login/registration system. I made a few and then hosted on my office computer. Distributed the links to a few around to get the much needed boost. (Yes if you are into web development you need a lot of encouragement and appreciation from others, else your enthusiasm can soon die out). However I soon realized that in todays world of web 2.0 Tomcat is not the thing. So I shifted to Apache.

    Next option you have is to go for Apache (which I use currently for all my developments). I have written 2 posts on how to configure your local system for the same. Read it here: Web Development Part 1 Apache MySQL PHP . If you are interested in starting on a unix machine (which is always the best thing to do) here is the post where I described: How to configure Ubuntu and LAMP on Windows.

  3. Things to concentrate upon initially? Before you go for any big big applications its better to get your basics right. I sucked for long while working with out dated styles and methodology. Here is a bunch of things which you should initially look to:
    • Choose the <div> style to create your HTML pages. I didn’t knew about this methodology and even went ahead to release Altertunes with <table> structure HTML pages.
    • While looking around on internet you will find loads and lot of framework offerings. They really make your work sooo simple. Takes all the burden away from you. Solves all cross browser compatibility for your application and what not. Find a list of frameworks I have tried before here: Essential Frameworks for web development
    • However one thing which you lose by using such frameworks is the insight. You never get to know what cross browser compatibility issues comes and how are they solved. They provide you easy ajax requests but you never get to know the methodology behind it. The biggest drawback which I find is that even for a simplest of application you always need to include whole bunch of files from framework hence making your application heavy for no reason.
    • Hence I finally went ahead coding everything from scratch and never ever used any framework for any of my works.
    • You need to learn how to prevent XSS (Cross browser scripting) and SQL Injection in your applications. Something very important for all your applications.

  4. Getting to a free hosting server: After initial play at your PC, its time to get to a free hosting server which brings in more knowledge and experience. I am lucky in the fact that I moved to X10 Hosting for the very first time. In my opinion they are one of the best in providing free hosts. The amount of features and largely the amount of support at their forums make them the best free hosting services around. I started with http://imoracle.x10hosting.com as my first ever site. Few applications which I build on that were:
    • User login and registration system, where user was authenticated by an link sent to him through email.
    • Inside I had a few basic applications including contact form, multiple picture uploads etc etc.
    • One of the finest feature I had in that was Gmail Contact Import script, which I made using PHP Curl application.
    • Last but not the least I built a chat application similar to browser based chat applications in google and yahoo mails. (Though not that efficient). That was first and the last time I used a framework for any of my application. It was built using Prototype Windows Class framework.

    I told you about these features as these were the applications which got me started initially. Helped me learn the tid bits of web development. May be you too can give a try to a few of them as your first few applications.

  5. Altertunes: Must be wondering why is this name up here again. Well I agree Altertunes fails as a full time product on web but then actually this self initiated project helped me learn all and everything about web development. It was hugely an ajax dominated website with ajax requests flowing in and out of everything you do inside. Basically the social networking aspect inside altertunes helped me learn most of the things. Key things inside altertunes which helped me develop my web dev skills are:
    • Social networking aspects like adding friends, sending personal messages, dedicating a song to your friends etc etc.
    • Importing Gmail, Yahoo and other email contacts. I initially used self made gmail address book import script. But then probably Gmail changed the ways on their mail and my script started failing. However I was lucky to find various services which helps you import mail contacts.
    • Creating of search bots for mp3. This was one of the things done in PERL instead of PHP. Read my blog on how to create an html parser using PERL here. This search bot was similar to any other search bot but it only used to index mp3 files in the database.
    • Use of various web services API’s like Artist Recommendation API by last.fm, Video API by YouTube, Pictures API by Google.
    • Features like creating your custom playlists, bookmarking artists, creation of analytic backend engine to provide users with the best recommended songs based on users listen history.
    • Finally, I learnt to make custom web players. Alterplayer which I made using OpenLaszlo, was something which I did out of PHP, MySQL, Apache.
    • Being at Oracle at that point in time, helped me in maintaining such a relational database with ease.
    • Auto suggest feature which is similar to google auto suggest. Altertunes used to offer you artist names as you typed in the name of the artist.
    • News from Google and Yahoo news. As there were no API’s available for this, I scrapped the google and yahoo news using PERL scripts and showed them to the users. (Illegal it was though) 😉

    However I must say that your website is not complete till you have all the look and feel and User Interface to lure the users. Initially I thought that the UI is coming up good but then as the features which I wanted to offer increased and with my sucking <table> structured HTML pages everything got screwed. However Altertunes helped me develop my web dev skills a hell lot and helped me understanding the right way of doing things. Probably picking up a small project for yourself is always the best idea, as then only you will always try to make your application look more professional.

  6. Time for a shared host : As the traffic increased on Altertunes I had to quickly shift to a shared hosting. I again went for a shared hosting at X10, where I bought a hosting server for around 80$ a year with 99% uptime till date. All my websites including Altertunes, My personal website and a few other under ground works of mine are hosted on the same shared host. Basically shared host assures you of more % uptime, more bandwidth and more forum support from X10. Those guys just go out of their ways at times to help you out.

    However you always have few drawbacks of a shared host:

    • First of all its a shared host i.e. a single CPU with many document roots (one root for each client) in it. At times hosting service providers over shoot the maximum allowed roots on a single CPU, which makes your CPU share low. And hence sucking performances.
    • Your site can go down even without a single error from your side. Yes this is the case which happened with me, when one of the other user on the same CPU set up a cron job (an automated job which runs every X hours) to take a backup of his server every 1 hour. Well in that case every single hour he used to consume maximum CPU cycles and hence all other users their started experiencing major downtimes. However X10 forum moderators are kind enough. They take immediate actions in such cases and ensure everything is fine.
    • You can’t have all PHP PEAR and Perl modules installed. I wanted to have a PHP PERL module installed for me by which I can write perl codes inside my PHP script. But as its a shared host and a package installed effects everyone on the CPU, I was denied by the X10 hosting admins.
    • Similarly for creating a upload meter application you need to have APC (Alternate PHP Cache) module enabled on your server. And I was again denied this package by the X10 hosting as they will cache all other users file which they might not want to.
    • Finally the biggest drawback is that you don’t have access to the httpd.conf i.e you apache server configuration file and your php.ini your php configuration file. Which at times you may want to change to meet your applications demand.

  7. Finally time for a VPS (Virtual Private Server) : When you start feeling you want a total control on your server and applications, its time to switch to a VPS. However they will cost you more than expected. I started with VPS at VPSFarm which I must say totally sucks. Doesn’t go well for me atleast with Zero support (‘Zero’ means zero). I took the Xen 1024 plan which costs around 40$ per month. However by the time my 1st month got over X10 started offering VPS too and with my experience and relations with them I immediately shifted to a VPS at X10. It too costs me same, around 35$ per month with more RAM and more Disk Space.

    However do not move to a VPS because you want to. It takes a lot of hard work to set it up and maintain it further. You basically have no support what so ever. Your mails not going, file not loading, server down, database not working. No one is accountable for that except you. But then you get all the controls on various configuration files which means you can fine tune the system to meet your needs.

    However one thing which I miss is the comfort of CPanel which used to come in by default with free host and shared hostings. I got so used to it in last 1 year.  Plus being from ECE background doesn’t helps me a great deal either. Anyways I suppose I am here to stay on VPS for long. I am slowly migrating all my websites to VPS. 

I guess this is all I had to share here in this post. If I have missed out on anything which many of you have asked me on chat and mails kindly feel free to ask it here. If I feel I need to add anything, will do so.

For experienced developers who accidentally read till here, feel free to add anything you want to. If you feel anything was wrong in my methodology and approach do let us all know. I am still learning.

All the best for all those starting out there.

Patience is the Key I will say finally.

——————————————————————————-
Other Posts you may want to read:
——————————————————————————-

How to configure Ubuntu and LAMP on Windows

Hello all Linux freaks,

Having already looked upon how to configure Apache-PHP-MySQL on Windows, now here I will try to explain in short how to do the same on Linux OS. I personally don’t have a seperate machine for linux. I run Ubuntu on my Windows machine using VM Ware. So before we go on to see how to configure LAMP on Ubuntu, lets see how can we have Ubuntu running on Windows.

  • For this tutorial I have used VMware-player-2.0.2-59824.exe for VM Ware installation and Ubuntu-7.04-desktop-i386.zip for Ubuntu. You will need to download the same from http://www.vmware.com/download/player/ and http://www.ubuntu.com/GetUbuntu/download
  • Install VM Ware, which is the most simple installer as you can get. At the end it will ask you to reboot the machine and kindly do not skip this step.
  • Now, open VM Ware which you have just installed and you should see something like this:

  • Now click the open button and browse to the folder where you have unzipped the Ubuntu zip file.

  • Click open Ubuntu-7.04-desktop-i386.vmx and thats it. You have just installed and configured Ubuntu on Windows. Simple, Isn’t it ? You should be seeing something like this by now:

For my system, Ubuntu automatically picked up various internet settings. However when I tried running the same from my office, I had to make appropriate changes for proxy setting. Kindly do the same for running internet on your Ubuntu.

Now an important thing before we proceed:

  1. The default administrator password for Ubuntu is ubuntu
  2. By default you are not the admin or root user. Hence you will need to prefix sudo or su to run a command as administrator in the Ubuntu terminal.

Also, before we proceed further kindly check if your Ubuntu is configured correctly for internet connection. Just check by opening this blog through mozilla in ubuntu. If it works, you are all set to configure LAMP on ubuntu.

Follow the following steps to configure LAMP on ubuntu (you need to run a few commands on your terminal window)

  • Open file at /etc/apt/source.list and uncheck the box for install from CD. This will let ubuntu install all modules directly from the repository.
  • $ sudo apt-get upgrade
  • $ sudo apt-get update
  • $ sudo apt-get install mysql-server mysql-admin apache2 php5 libapache2-mod-php5 libapache2-mod-auth-mysql php5-mysql phpmyadmin
  • $ sudo mysqladmin -u root password [YOUR_NEW_PASSWORD]
  • $ sudo /etc/init.d/mysql restart
  • $ sudo /etc/init.d/apache2 restart

Thats pretty much we need. Now let us test our configuration and LAMP setup.

  1. $ sudo vim /var/www/phpinfo.php
  2. Type in the following few lines of code in the file:
    <?php
      phpinfo();
    ?>
  3. Open up your browser and type in http://localhost/phpinfo.php
  4. If you are able to see the php config file information on your browser. Thats it.
  5. Next type http://localhost/phpmyadmin
  6. Login as root i.e. Username : root and Password : [YOUR_NEW_PASSOWRD]
  7. If you are lucky enough you will see the phpadmin console.

Congratulations ! Thats pretty much what exactly we need. Now go on to do all your web development on Linux. Hail Windows 😉

I configure all this stuff long back, so if I have missed out on some issues kindly lemme know and comment the same.

Web Development – Part 1: Apache, MySQL, PHP

Hello Friends,

Ever since I have started web development and launched Altertunes, many of my friends have asked me, “How and Where to start web development ?” . Though there are infinite tutorials over the internet on web development, I will try to jot down my experiences and learnings here.

To start with, I will just try to explain a few terms, before we go on and learn how to handle and integrate them together:

  1. Apache: is the web server, which is used world wide for web development. Since April 1996 Apache has been the most popular HTTP server on the World Wide Web. However, since November 2005 it has experienced a steady decline of its market share, lost mostly to Microsoft Internet Information Services. As of April 2008 Apache served 50.42% of all websites.
  2. MySQL: is a multi threaded and multi-user SQL database management system, which has more than 11 million installations. MySQL is popular for web applications and acts as the database component of the LAMP, BAMP, MAMP, and WAMP platforms (Apache-MySQL-PHP/Perl/Python on Linux/BSD/Mac/Windows), and for open-source bug tracking tools like Bugzilla. It’s popularity for use with web applications is closely tied to the popularity of PHP and Ruby on Rails, which are often combined with MySQL. PHP and MySQL are essential components for running popular content management systems such as Joomla, WordPress, Drupal, and some BitTorrent trackers. Wikipedia runs on MediaWiki software, which is written in PHP and uses a MySQL database.
  3. PHP: is a widely-used, general-purpose scripting language that is especially suited for web development and can be embedded into HTML. It generally runs on a web server, taking PHP code as its input and creating web pages as output. It can be deployed on most web servers and on almost every operating system and platform, free of charge. PHP is installed on more than 20 million websites and 1 million servers, although the number of websites with PHP installed has declined since August 2005. It is also the most popular Apache module among computers using Apache as a web server. The most recent major release of PHP was version 5.2.6 on May 1, 2008.

In short, Apache is the web server which serves the web pages for the website. MySQL is the database which is used to store necessary information ranging from user’s data to user’s pictures. PHP is the server side scripting language used to make web pages. Apache server takes PHP code as input, and outputs an HTML page on the browser.

Lets start some installations and configurations: If you don’t want to handle the installations and configurations of WAMP (Apache-MySQL-PHP on Windows), you may choose to download and install the pre-packaged installer from here. However its always better to configure things yourself to know the inner details. If you are brave enough to install and configure these modules, here we go:

  • Apache Installation: Go to http://httpd.apache.org/download.cgi and download the latest Win32 Binary including OpenSSL (MSI Installer) version of Apache Server Installer Available. Double click the installer and you should see something like this:

    Click next and after a few clicks you will be asked for the following parameters:

    Fill in the details, as shown in the figure above , choose installation type as ‘Typical’ and proceed to install the Apache server.

    Open up your favorite browser and type in http://localhost. If you see a page that says “It works!” then the Apache server has been installed successfully.

  • PHP Installation: Go to http://www.php.net/downloads.php and download the latest PHP Zip Package. Unzip the file into your C: drive, and rename the folder to C:PHP. Next copy the file C:PHPphp.ini-dist file to C:Windows and rename the file it C:Windowsphp.ini. This is the PHP Configuation file where we will be setting some of the important parameters later on.Now its time to integrate PHP with Apache we installed earlier. Open the Apache configuation file (Important: you should always have a copy of this file stored somewhere else, because an error or misconfiguration can cause your Apache server to crash!), which is located at C:Program FilesApache Software FoundationApache2.2confhttpd.conf into notepad and copy the following lines at the end of ‘LoadModule’ section in the configuration file:

    #AddType application/x-httpd-php .php
    LoadModule php5_module “C:/PHP/php5apache2.dll”
    AddType application/x-httpd-php .php .phtml .inc .php3
    AddType application/x-httpd-php-source .phps
    # configure the path to php.ini
    PHPIniDir “C:/Windows”

    Now lets configure the root directory of our Apache server. Root directory is the base directory from where the website is served. When you type in http://localhost in your browser, the server looks for the necessary files in the root directory and serves it to the browser. By default, the root directory of the Apache server is set to : C:Apache2htdocs. You may keep the root as C:Apache2htdocs or you may change it to a directory of your choice as follows:

    Look for the ‘DocumentRoot’ Section in the Apache configuration file located here: C:Program FilesApache Software FoundationApache2.2confhttpd.conf. You should see something like this:

    # DocumentRoot: The directory out of which you will serve your
    # documents. By default, all requests are taken from this directory, but
    # symbolic links and aliases may be used to point to other locations.
    DocumentRoot “C:Apache2htdocs”

    Change C:Apache2htdocs to a directory of your choice, anywhere on your computer. For eg. C:Workspace.

    Further scroll down in the httpd.conf file and search for a section which looks like this:

    # This should be changed to whatever you set DocumentRoot to.
    <Directory “C:Apache2htdocs”>

    Change the directory location from C:Apache2htdocs to whatever you have set in the step before.

    Now to test whether everything is configured correctly, create a file in the root directory of Apache server, named phpinfo.php and paste the following code into it:

    <?php phpinfo(); ?>

    Restart your Apache server from here Start > All Programs > Apache HTTP Server > Control Apache Server > Restart, and open the following URL in your browser: http://localhost/phpinfo.php . If everything is well setup and you are lucky enough, you will see something like this:

    Finally, we just need to make a few configurations in the PHP configuration file which we copied to C:Windowsphp.ini. Open the file in a text editor and find the section which looks like this:

    ; Directory in which the loadable extensions (modules) reside.
    extension_dir = “./”

    Change it to look like this:

    ; Directory in which the loadable extensions (modules) reside.
    extension_dir = “C:PHPext”

    Also, find the section in your PHP configuration file which looks like this:

    session.save_path = “/tmp”

    and change it to:

    session.save_path = “C:WINDOWStemp”

    And finally we come to the end of PHP installation section. Congratulations, Good going !

  • MySQL Installation: Go to http://dev.mysql.com/downloads/mysql and download the latest MySQL database. Once ZIP file has finished downloading, extract it using WinZIP or a similiar program on your computer. Double click the setup.exe file to start the installation. You should see something like this:Keep clicking next and keep all the default options (use typical installation and not the detailed one). After installation finishes, go to start -> run -> services.msc and start the MySQL service. (If MySQL service doesn’t exist, the installation was not successful) . Now we need to tell PHP that MySQL exists on your system, and we need to integrate them. Go to php.ini i.e. the PHP configuration file, and make the following changes:

    Search for something like this:
    ;extension=php_mysql.dll

    and make it like this:
    extension=php_mysql.dll (i.e. you just need to remove the semicolon)

    Additionally you need to add this:
    extension=php_mysqli.dll

    at the end of the extensions. Further copy:
    libmysql.dll to C:WindowsSystem32 and restart Apache.

  • PHPMyAdmin and MySQL Query Browser Installation: Though you can always use MySQL database through command line but having PHPMyAdmin or MySQL query browser will make your database management a lot easier and simple. To install download PHPMyAdmin from here. Download the ZIP file and extract it under the root directory of Apache Server, i.e. if root directory is set as C:Workspace , the extracted PHPMyAdmin folder’s location should have a path C:Workspacephpmyadmin.Open the config.ini.php file in the PHPMyAdmin folder and make the following changes:$cfg[‘Servers’][$i][‘user’] = ‘root’;
    $cfg[‘Servers’][$i][‘password’] = ‘YOUR_PASSWORD’; // Your MySQL Password
    /* Authentication type */
    $cfg[‘Servers’][$i][‘auth_type’] = ‘config’;
    /* Server parameters */
    $cfg[‘Servers’][$i][‘host’] = ‘localhost’;
    $cfg[‘Servers’][$i][‘connect_type’] = ‘tcp’;
    $cfg[‘Servers’][$i][‘compress’] = false;
    /* Select mysqli if your server has it */
    $cfg[‘Servers’][$i][‘extension’] = ‘mysql’;
    /* User for advanced features */
    $cfg[‘Servers’][$i][‘controluser’] = ‘root’;
    $cfg[‘Servers’][$i][‘controlpass’] = ‘YOUR_PASSWORD’; // Your MySQL Password
    /* Advanced phpMyAdmin features */
    $cfg[‘Servers’][$i][‘pmadb’] = ‘phpmyadmin’; //Name of PHPMyAdmin folder
    $cfg[‘Servers’][$i][‘bookmarktable’] = ‘pma_bookmark’;
    $cfg[‘Servers’][$i][‘relation’] = ‘pma_relation’;
    $cfg[‘Servers’][$i][‘table_info’] = ‘pma_table_info’;
    $cfg[‘Servers’][$i][‘table_coords’] = ‘pma_table_coords’;
    $cfg[‘Servers’][$i][‘pdf_pages’] = ‘pma_pdf_pages’;
    $cfg[‘Servers’][$i][‘column_info’] = ‘pma_column_info’;
    $cfg[‘Servers’][$i][‘history’] = ‘pma_history’;

    Now open the url, http://locahost/phpmyadmin and if everything is configured correctly you must see something like this:

And thats it, if you are able to see all the above images on your computer, it means you have just configured everything correctly and you are all setup for web development.

I have installed and configured all the components way back (6 months ago), hence there is a possibility that I might have missed out on some trivial points. If you catch hold of any such issue do comment the same.

Enjoy making cool webpages, and All the best for your website 🙂