Home opinion-and-analysis The Linux Distillery Set up your own virtual Linux LAMP server on Windows

Set up your own virtual Linux LAMP server on Windows

Even the most Microsoft-ardent developer will know web hosts favour Linux, Apache, MySQL and PHP over Windows, IIS, ASP.NET and SQL Server. Thanks to virtualisation the days of dual booting are over. Here is how to set up a streamlined LAMP development environment on any Windows PC.

Of course, as a virtual machine is for all intents and purposes a real computer, these instructions apply just as well if you are setting up a real Linux dedicated machine for LAMP development.

There are many reasons to have a LAMP development environment, no matter your preferred choice of computer platform. The truth is even if you are a hard-core Microsoft Windows user you will find web hosts offer cheaper plans for Linux environments than they do for Windows, if they offer Windows at all. Additionally, you can find various free web hosting options that are based on Linux.

Several reasons for this state of affairs are that Linux is inherently secure and lean with a long heritage of robust multiuser time-sharing hosting.

Of course, another very significant reason is that because Linux, Apache, MySQL and PHP are all open source they have no licensing charges whatsoever. This makes them significantly cheaper than Microsoft Windows and particularly when combined with Microsoft SQL Server.

You might be wanting to get into LAMP development - perhaps a totally new software system you are writing from the ground up, or maybe even to create a backup WordPress or Drupal environment so you can experiment before touching your live site hosted elsewhere.

Whatever the reason, the first step for Windows users is to ensure you have a virtual machine environment. You might choose Microsoft's own Virtual PC, or VMWare or VirtualBox or any other virtualisation technology you might prefer.

Next, pick a Linux distribution. While Ubuntu is generally a runaway favourite I've opted for Debian Linux myself for my development platform.


My reasoning is that Debian is powerful and flexible, and importantly, it allows a lightweight system to set up very quickly. My goal in making a virtual machine is to give me the means to write dynamic PHP web apps against a MySQL database without bloat. I don't need or want an X-Windows GUI or Open Office or any other, more user-oriented, programs.

Debian offers a tiny minimal bootable CD-ROM image which is perfect for making a small virtual machine. Download the appropriate ISO image, whether i386 or amd64 depending on whether you want a 32-bit or 64-bit environment.

Next, create a new virtual machine using whichever virtual machine management tool you've chosen. No matter the software the process will be largely identical: name your VM, specify an amount of RAM and create a virtual hard disk drive with a sensible amount of space - which includes not too much as well as not too little. I chose 1Gb of RAM and 20Gb of hard disk space.

Finally, be sure to set your virtual machine's network adapter to a real network interface on your computer and set the virtual machine's CD drive to use the Debian network install ISO image.

Fire the virtual machine off and it will boot from the Debian CD, leading you through its installation process. Be careful to choose the right keyboard layout - Australian users should pick US English and not British English, for instance. If you make the wrong selection (pressing the pipe (|) key displays a tilde (~), for example) you can easily recover by running the command dpkg-reconfigure console-data later on.

You will be prompted to configure the network interface giving it either a static IP address or allowing DHCP to assign one. You will also be prompted for the type of setup to use. You can uncheck everything here, except for web server. Even if you're running your virtual machine on a laptop don't choose the laptop package.

Don't choose the database server option either, unless you wish to use PostgreSQL instead of MySQL (although now Oracle owns MySQL the traditional LAMP metaphor may swing to being LAPP in time).

Finally, you will be presented with a login prompt. Enter the username and password you specified during installation.


We want to make some changes to our base setup. Enter the command su and press enter. Enter the root password which you also gave during installation. If you've not used Linux before, this command elevates your privileges so you are now able to execute commands as the super user. The prompt changes to a hash (#) to indicate this.

First, let's ensure our virtual machine is up to date and has all the software we wish. Enter these commands in turn. You will be prompted to specify a super-user password for MySQL when it is installed:

aptitude update
aptitude upgrade
aptitude install mysql-server mysql-client
aptitude apache2 apache2-doc
aptitude install php5 php5-mysql libapache2-mod-php5

If you are familiar with other languages you might like to also install Perl and/or Python too:

aptitude install perl libapache2-mod-perl2
aptitude install python libapache2-mod-python

Let's also install some useful utilities. PHPMyAdmin provides web-based access to the MySQL database server and its contents:

aptitude install phpmyadmin

Lynx is a text-based web browser which will let you test simple web pages from the command prompt:

aptitude install lynx

For those who are not familiar with either vi or emacs, nano is an easy-to-use editor for creating and modifying text files:

aptitude install nano

Finally, to allow you to login to your virtual machine from other computers on your network install the OpenSSH server:

aptitude install openssh-server

After installing these packages restart Apache:

/etc/init.d/apache2 restart

Let's now make some test pages. The web server root is in the folder /var/www so let's create a file there called test.php. To do this using the nano editor type

nano /var/www/test.php

Enter this line into the file:


Before we can test we need to know the IP address of the virtual machine. If you assigned a static address then you know it already. If you allowed DHCP to assign an address type the command

/sbin/ifconfig

and look for the IP address following the text 'inet addr'. In my case, that's 192.168.1.111.

Take the mouse pointer out of your virtual machine. Leave it running, of course, but open your web browser - whether Internet Explorer, Firefox, Safari, Chrome or something else - and try each of these URLs. Be sure to substitute 192.168.1.111 with the actual IP address of your system.

http://192.168.1.111/
http://192.168.1.111/test.php
http://192.168.1.111/myphpadmin

Each of these URLs should have shown a different result. The first will display a sample Apache 'it works' web page, which is being fed from /var/www/index.html.

The second will load the PHP page you made above, and display the results of the phpinfo() call.

The third will load PHPMyAdmin. You can log in using username root and the super-user password you nominated when installing MySQL. Using this utility you can very easily maintain MySQL databases.

Stop for a moment and take in what you've achieved. You have a fully working Linux server sitting on top of your Windows box, and you can access it through your web browser just like it was a real machine sitting out somewhere on the Internet.


That is pretty much the end of this tutorial, except for one thing. You don't want to get in the habit of becoming super-user so you can create web pages in the /var/www folder. Instead, you can make a folder in your own regular user home folder and have Apache serve files out of there.

Let's create a new Apache configuration file:

nano /etc/apache2/conf.d/mywebapp.conf

Enter this line and save the file, being sure to replace 'david' below with your own username:

Alias /mywebapp /home/david/mywebapp

Restart Apache again with the same command as previously:

/etc/init.d/apache2/restart

There is no need to be the super user any longer; press CTRL + D at the same time to revert back to your ordinary user privileges.

Return to your home folder and make a directory and web page like so:

mkdir mywebapp
cd mywebapp
nano test.html

Enter any text you like into the file test.html and save it.

Go back to your Windows web browser and browse to the URL http://192.168.1.111/mywebapp/test.html, again replacing 192.168.1.111 with the right IP address for your virtual server.

You should find your text appearing. This means you can now freely edit web pages in your virtual machine under the mywebapp folder in your home directory, and view results immediately within your web browser.

LEARN NBN TRICKS AND TRAPS WITH FREE NBN SURVIVAL GUIDE

Did you know: Key business communication services may not work on the NBN?

Would your office survive without a phone, fax or email?

Avoid disruption and despair for your business.

Learn the NBN tricks and traps with your FREE 10-page NBN Business Survival Guide

The NBN Business Survival Guide answers your key questions:

· When can I get NBN?
· Will my business phones work?
· Will fax & EFTPOS be affected?
· How much will NBN cost?
· When should I start preparing?

DOWNLOAD NOW!

David M Williams

joomla site stats

David has been computing since 1984 where he instantly gravitated to the family Commodore 64. He completed a Bachelor of Computer Science degree from 1990 to 1992, commencing full-time employment as a systems analyst at the end of that year. Within two years, he returned to his alma mater, the University of Newcastle, as a UNIX systems manager. This was a crucial time for UNIX at the University with the advent of the World-Wide-Web and the decline of VMS. David moved on to a brief stint in consulting, before returning to the University as IT Manager in 1998. In 2001, he joined an international software company as Asia-Pacific troubleshooter, specialising in AIX, HP/UX, Solaris and database systems. Settling down in Newcastle, David then found niche roles delivering hard-core tech to the recruitment industry and presently is the Chief Information Officer for a national resources company where he particularly specialises in mergers and acquisitions and enterprise applications.