Search:
All Any

Part of mariaus.info network Subscribe via RSS
Follow on twitter Request a tutorial
Create an automated reusable software box model
Wallpaper: wall of pictures
Create a supernova in Photoshop
Visitor request: Advanced reflections in Photoshop
Advanced moving through space animation
Photography and art inspiration # 4
Photography and art inspiration # 2
Photography and art inspiration # 1
Photography and art inspiration # 3
Go to first pageAdd to favorites (this)Contact page ownerRequest a tutorialSubscribe to the Tip Kit news
Request a tutorial
Install a Linux based web server with PHP support on Windows

Learn how to install a linux based web server with apache, php and mysql on your Windows computer.
Every PHP developer knows that creating websites directly on an original webserver is a painful and time consuming process. You need to upload every change you make to the website and it all depends on your internet connection. So most of us have a corner where we can work in, called a local web server. A local web server is a pack of server software for an operating system to simulate a real server environment. That means we can store files in a special directory in our computer and we can navigate a special address in the webbrowser to open these files and parse them with PHP. After developing the website locally we can upload it to a real server. That saves us time, because we don't need to constantly upload our scripts to a server and we have all our websites in a single place. On a Linux computer there's some amazing software to enable web server support. There's a solution for Windows as well, but i don't like it, because in the end the real server you're going to end up with is most likely going to be Linux. And since Linux and Windows servers have some differences, some changes might need to be done before your site can be uploaded to a Linux server from your Windows machine. Not to mention that you can now notice some unexpected errors and malfunctions, related to permissions or other problems. But if you develop a website on a Linux server in the first place - you will most likely face these problems in the early stages of the development and will solve them. Only minor changes will be required to move a finished website from your local server to a real internet server. But installing Linux just for PHP development is not necessary: we can install it on our Windows machine and run both of these operating systems together!

I'm going to share a configuration that i use for my web projects and i hope you can also find it useful. I have set up a special environment that i call the testground. It consists of a virtual Linux machine, running on Windows and a few Windows nameserver tweaks. When i want to develop a site eg. www.test.com, i launch the Linux virtual machine and press a button that tweaks nameserver records. Now when i point my browser to www.test.com - it connects to my virtual server instead of a real www.test.com website. When the work is finished, i turn off the virtual Linux computer and press another button to restore original DNS settings. In this tutorial i will teach you how to make such a configuration for yourself.

Part 1 - Preparation

First, create a directory somewhere in your file system and name it Server or Virtual Server or whatever you like. This directory will contain all the things related to your virtual machine. Next, you need to download virtualization software VirtualBox and a Debian Linux installation disc. The thing you most definately need is an internet connection, because we will be installing Linux from the internet directly. So go to VirtualBox homepage and download it. Install it on your computer, restart if asked. Now download a Debian net install cd from this website. You should know your system architecture to select the correct installation disc. Mine is i386 so i can download the cd image from this url, but your system might differ. Save the ISO file to your Server directory.

NOTE: Some versions of windows don't work well with linux shares. You should use Windows XP or Windows 7 with this tutorial. If you use Windows Vista, you might be unable to access your virtual machine's files.

Part 2 - Creating a virtual Linux computer

To create a Linux virtual machine, launch VirtualBox and press the big button that says New. Some dialog boxes will appear, asking for your input. Follow the screenshots below to know what to do:

img/p49_1.jpg

img/p49_2.jpg

img/p49_3.jpg

img/p49_4.jpg

img/p49_5.jpg

img/p49_6.jpg

img/p49_7.jpg

img/p49_8.jpg


Part 3 - Preconfiguring the virtual machine

Now that you have the machine created, it will appear in the main VirtualBox window and will be named the way you named it in the first screenshot:

img/p49_9.jpg

Press the big Settings button and navigate to CD/DVD-ROM section:

img/p49_10.jpg

img/p49_11.jpg

img/p49_12.jpg


Press OK to save the settings and you're done configuring. Time to install!

Part 4 - Linux installation

Let's run the server and install Linux now. Press the big Start button and wait until this screen appears:

img/p49_13.jpg

When you press the ENTER button, an installation process will begin. When you click your mouse on a virtual machine window, your mouse pointer will be "swallowed" by the virtual machine and you won't be able to escape out of its bounds. At first, you will be notified with a small dialog window that the pointer is about to be captured. Check the do not show this message again checkbox and press Capture. When you want to move your mouse out of the virtual machine window, press the RIGHT ALT key. If the uncapture key differs on your machine, it can be easily found on the statusbar of your virtual machine:

img/p49_14.jpg

As you can see, there's a Right Alt text on the rightmost side of the statusbar. That is your uncapture key. Press it to release your mouse cursor from the virtual machine's capture. OK so now when you've pressed ENTER on the graphical install option, follow the settings below to install the OS:

img/p49_15.jpg

img/p49_16.jpg

img/p49_17.jpg

img/p49_18.jpg

img/p49_19.jpg

img/p49_20.jpg

img/p49_21.jpg

img/p49_22.jpg

img/p49_23.jpg

img/p49_24.jpg

img/p49_25.jpg

img/p49_26.jpg

img/p49_27.jpg

img/p49_28.jpg

img/p49_29.jpg

img/p49_30.jpg

img/p49_31.jpg

img/p49_32.jpg

img/p49_33.jpg

img/p49_34.jpg

img/p49_35.jpg

img/p49_36.jpg

After your virtual machine restarts, press Devices > Unmount CD/DVD-ROM in the main menu to eject the ISO installation image. When Linux boots, you'll see lots of messages pass by. You'll have to wait until everything stops and the words testground login: appear on your screen. It's time to install the software.

Part 5 - Installing server software

We will be installing some programs to run in our server and for this, you will have to enter some commands in the black server window.

Logging in

To login to your server, simply write your username (it should be test) in the black window and hit ENTER. You will then be asked to input your password. Don't worry if you aren't able to see anything while typing the password - it's a normal behaviour. After that you're logged in. Sometimes you might need to gain administrative rights. To do that, type su and press ENTER. You will be asked for root's password (remember, that was the first password you had to define before creating the actual user?).

Installing Apache

Apache is a great webserver for various operating systems and we will be using it to server our web pages. To install it, first type these commands:

cd /home/test/
mkdir www
cd www
mkdir test.com
nano test.com/index.php


The first command switches your current directory to /home/test. This is the home directory of your user (where all your files should be stored). The next command creates a directory, named www in /home/test. The next command navigates to the www directory and the command after that makes another directory named test.com. So now you have the following directory structure: /home/test/www/test.com. This is the directory you will be saving your mail website to. As you can see, in Linux the file paths differ from Windows. In Windows everything starts with a drive letter, followed by :\ symbols like that: C:\Windows\ and in Linux, the main directory is "/". And everything else starts from here. User directories are located in /home/username. The last command you're going to type will open a simple text editor for a file index.php, located in /home/test/www/test.com. Write the following lines in this file:

<?php
echo 'Welcome to test.com local server';
?>


Obviously, you could've written anything here, it's just a test script :) Now press CTRL+X to exit the editor. It will ask you if you want to save the file - press Y and ENTER to save. OK now we've prepared a directory for our main website and we can now install the software itself. First of all, type su and press ENTER. Enter your root's password to continue, because we will be needing administrative rights from now on. To install Apache web server, enter the following command (followed by ENTER, obviously):

apt-get install apache2 php5 libapache2-mod-php5


It will ask you if you're sure, press Y and hit ENTER. After all the running lines stop - the installation is complete. We've just installed a web server with PHP support. But that's still far from the end.

Configuring Apache

To configure the web server, we will need to issue a few commands:

a2dissite default
a2enmod rewrite
cd /etc/apache2/sites-available
nano test.com


The first command disables the default apache's website configuration (we will exchange it with ours). The second enables an additional apache module, called Mod_Rewrite. This is very useful in URL reqriting. The next command navigates to a directory where all the configs for existing websites are stored. The last commands opens a new file, called test.com in the configuration directory. The contents of this file should be as follows:

#
#  test.com (/home/test/www/test.com/)
#
<VirtualHost *:80>
        ServerName  www.test.com
        ServerAlias test.com *.test.com
        DocumentRoot /home/test/www/test.com/
        <Directory /home/test/www/test.com/>
                AllowOverride All
        </Directory>
</VirtualHost>


Press CTRL+X to exit the editor and when asked if you want to save, press Y and ENTER. You've just created a configuration file for a www.test.com domain. So when a browser asks your server for this domain, it will know that the required files will be located in /home/test/www/test.com/ (remember, we've just created an index.php in that directory earlier?). Now that we have a configuration file for this domain - there's still 2 things missing: 1) the file does not work until it's not enabled with a special command; 2) the domain www.test.com does not belong to us and we need to trick our Windows system that it does. The second problem will be solved at the end of this tutorial and to solve the first one, type this:

a2ensite test.com


Installing additions for the web server

Type these commands:

apt-get install php5-gd
apt-get install imagemagick
apt-get install php5-mcrypt


After each of these commands you will probably asked if you're sure and you'll need to press Y and ENTER to confirm. Once again, when the running lines stop - the installation is complete and you can enter the next command. The above commands will install two graphics libraries for php - GD and ImageMagick and allso an MCrypt encryption library.

/etc/init.d/apache2 restart


Type the above command to restart Apache web server.

Installing MySQL database server

Type in this command:

apt-get install mysql-server mysql-client php5-mysql


The installer will ask you to define a root's password. Don't confuse this with the password you already have. You have a Linux root password so far and now you're asked to define a MySQL root password. Root is just another word that means Administrator. MySQL has an administrative user just like the Linux system and the name is the same for both of them: root. So enter a new root's password (you can enter the same password as you use for Linux if you like, but it wouldn't be a good idea in a situation where security matters a lot) and repeat it in the next dialog window.

Installing PhpMyAdmin database configuration

PhpMyAdmin is a database configuration tool and we'll install it to our main website's directory. First of all, let's get rid of the administrative rights, since we will be working with our user now. Type exit to exit the root user and get back to the test. Now type the following commands:

cd /home/test/www/test.com
mkdir pma
cd pma


This will navigate to the main folder of our test.com domain, create a directory pma (stands for PhpMyAdmin) and navigate to it. Now we need to download the newest PhpMyAmin. Go to phpmyadmin.net and find a *.tar.gz version of PhpMyAdmin. You need to find out the link for this file. For example: http://downloads.sourceforge.net/sourceforge/phpmyadmin/phpMyAdmin-3.2.0.1-english.tar.gz?use_mirror=sunet. With the link found, type in the following command:

wget http://downloads.sourceforge.net/sourceforge/phpmyadmin/phpMyAdmin-3.2.0.1-english.tar.gz?use_mirror=sunet


This will download the file in the link to the current folder. If you type ls -la when it's finished, you should see a file phpMyAdmin-3.2.0.1-english.tar.gz. Let's extract it:

tar -xzvf phpMyAdmin-3.2.0.1-english.tar.gz
rm phpMyAdmin-3.2.0.1-english.tar.gz
mv phpMyAdmin-3.2.0.1-english/* .
rmdir phpMyAdmin-3.2.0.1-english


The first command will extract the archive. But when extracting it creates a directory phpMyAdmin-3.2.0.1-english. So the PhpMyAdmin is now located in /home/test/www/test.com/pma/phpMyAdmin-3.2.0.1-english. And since we want it to be located just in /home/test/www/test.com/pma/, we need to move all the files from the directory phpMyAdmin-3.2.0.1-english back to the pma folder. That's what the third command does. The fourth and second commands remove the files we don't need anymore. Next thing you wanna do is type su and get back your administrative rights.

Installing Samba

This is a very important step. If you install Samba, you'll be able to browse your virtual machine as you're able to browse computers in your network. That means you will be able to navigate to the folders, create and modify files with Windows Explorer instead of command line. You will basically no longer need the command line when working with your websites, except when you create new configurations etc. For all other purposes, you'll be able to browse your virtual machine just like any other computer on your network. So type in the following command to install the Samba share server:

apt-get install samba


When installing, you'll be asked to enter a name of a workgroup you're in. You can find out your Windows workgroup by right-clicking on My Computer and selecting Properties. Enter the same workgroup for Linux as well. Nothing will happen if you enter it wrong, you will still be able to access your machine by its IP address. Samba will also ask you some question about modifying the smb.conf file. Answer No to that and your installation should continue. When it's finished, type the following command:

nano /etc/samba/smb.conf


This will open samba's configuration file in a text editor. Scroll down until you find this line:

create mask = 0700


Change this line to the one below:

create mask = 0775


Also find and change these lines:

Find: directory mask = 0700
Change to: directory mask = 0775
Find: read only = yes
Change to: read only = no


The last thing to configure our samba server is setting a password for the test user. Type in:

smbpasswd test


This will ask you to create and repeat a password for the user test. You can use the same password that you already have for the linux test user.

Part 6 - Enabling access to the server

Now we need to enable the access from our windows machine to this computer with a static IP address. To do that, first type the following command:

halt


This will shutdown the Linux server. When the black window dissappears - the server is down. When we do the following modifications, we won't be able to access the internet from the server, but we will be able to access the server from Windows. And that's basically what we need. Since we've installed the common software, we won't be needing the internet connection. So we will now assign a static IP address to the server and we will be able to redirect the websites to this address. If we need to install something later - we can simply undo these settings, install and apply them again. So let's begin. Press the big Settings button in the main VirtualBox window to configure your virtual machine and open the Network page:

img/p49_37.jpg

img/p49_38.jpg

img/p49_39.jpg

Now we need to assign static IP address to our new virtual card. To do that, go to Control Panel and find the Network Connections section. Double-click the virtual card:

img/p49_40.jpg

img/p49_41.jpg

Now if your computer's ip address is from a 192.168.2.X range, you'll need to choose a different range for the virtual card (eg. 192.168.3.2), but you most likely won't have to do that. Press OK to save settings and start up the Linux server again. When Debian loads, log in and type su to gain administrative rights. Type the following command:

ifconfig


This lists all available network interfaces on the virtual machine. The interfaces should be named eth0, eth1, eth2 ... etc. The interface names appear on the left and all the information about them - on the right. You need to find the name of your network card. It should be eth0, but it's possible it might have changed. If it's eth0 - everything is fine. There might be other interfaces like lo - don't mind them, you only need the ethX interface. Type the following command:

nano /etc/network/interfaces


This will open the network card configuration file. Delete everything from that file and write the following lines:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.2.3
netmask 255.255.255.0


Press CTRL+X to save and Y, ENTER to confirm. The next two commands will restart the network card and apply the new settings:

ifdown eth0
ifup eth0


Note that if your network interface is NOT eth0 - you need to replace the eth0 everywhere with the name of the interface from your ifconfig command result. Hopefully you won't have to do that.

Part 7 - Checking

Now let's check if our configuration works. In your Windows system, press Start > Run ... and type cmd. In the command prompt window, type:

ping 192.168.2.3


If there is a ping reply - your virtual server works! And its ip is 192.168.2.3. Let's test the web and samba configurations next. First, open your browser and navigate to this address:

http://192.168.2.3/


This should open the contents of the index.php file that we've created early in this tutorial. For samba, press Start > Run ... and type:

\\192.168.2.3


A prompt should appear asking for your username and password. Enter test for the username and for the password - whatever you've defined with smbpasswd command earlier. You should now be able to see your home directory in Windows Explorer. Navigate to test > www > test.com and this will be the root folder for your main domain. The last thing to do - check out how your PhpMyAdmin works. To do that, navigate to this address in your browser:

http://192.168.2.3/pma/


Part 8 - Domain configuration

So far we've talked about the www.test.com domain, but it doesn't belong to us and i've promised i'd show you how to trick windows into thinking that this domain must point to your virtual server. This is an easy task. Open My Computer and navigate to [i]C:\Windows\System32\drivers\etc[/i]. In this folder, there's a file, named hosts. Open it with notepad and add the following lines:

192.168.2.3     test.com
192.168.2.3     www.test.com


Save the file and restart the browser. Now whenever you navigate to www.test.com, your browser will connect to your virtual server instead of a real www.test.com website. That way you'll have a short address to test your websites with. When you work with another website eg. www.example.com, you will also add two similar lines to the hosts file and trick the browser to temporarily redirect them to your virtual machine. As soon as you delete all these lines from the hosts file, your browser will load the real websites. So when you work with your client's website, you can even have the same domain name and imitate the conditions very close to the real server.

Part 9 - Adding more websites

If you need to work with another website eg. www.example.com, you need to do two things. First, add the following lines to the C:\Windows\System32\drivers\etc\hosts file:

192.168.2.3     example.com
192.168.2.3     www.example.com


Now you are able to trick windows into thinking that this domain is located in your virtual server. But you still need to tell your virtual server that it actually has this domain. To do that, navigate to your Linux machine's www directory using Windows Explorer and create a new folder, named example.com. You really don't have to name it like that, but it's just a system to make things clear and no to get lost later :) Now login to the Linux machine and type su to gain root access. Type the following command:

nano /etc/apache2/sites-available/example.com


A text editor will open and we'll need to write a very similar config to the one we've done earlier:

#
#  example.com (/home/test/www/example.com/)
#
<VirtualHost *:80>
        ServerName  www.example.com
        ServerAlias test.com *.example.com
        DocumentRoot /home/test/www/example.com/
        <Directory /home/test/www/example.com/>
                AllowOverride All
        </Directory>
</VirtualHost>


Now save the file (CTRL+X, y, ENTER) and run the following command to enable this configuration:

a2ensite example.com


Now your virtual server knows that it has an example.com domain parked as well and it knows that the root directory for this domain is www/example.com instead of www/test.com.

Part 10 - DNS Automation

Remember when i said that when i start my work, i press a button that tricks windows into thinking that the domains are located on the virtual machine? Well now you know that it's all thanks to the hosts file. In order to automate this process, you can copy a clean hosts file (without your additions) to your Server folder that you've created in the very beginning. Rename it to hosts_original. Make another copy of it and rename to hosts_testground. Now create a file copy original.bat and add the following line to it:

copy /Y hosts_original "c:\windows\system32\drivers\etc\hosts"


Also create a copy testground.bat file and add this line to it:

copy /Y hosts_testground "c:\windows\system32\drivers\etc\hosts"


Now when you double-click the copy original.bat file - it will replace the hosts file with hosts_original. That is it will restore original DNS settings. But if you click the copy testground.bat - it will replace hosts with hosts_testground file. This is the file you'll be setting your domains in: hosts_testground. That way you'll have a clean copy of hosts file (hosts_original) and a modified one (hosts_testground) and you'll be able to replace the hosts file with any of them. So when you begin your work with the virtual machine, you double-click the copy testground.bat and when the work is done - copy original.bat. Remebmer, if you use this technique - then the domain configuration from the part 8 must be done in the hosts_testground file from your Server directory.

Part 11 - Final words

I know i know ... it's a veeeeery long tutorial, but it's worth your time and effort. If you do everything right - you will have your entire server in a single file and it will be a real Linux based web server. Hope you liked it and learned something new.

Article written by: Marius S.
This article is an intellectual property of its respective author. All images, used here are property of tip-kit.com if not stated otherwise.
Share this article
Digg del.icio.us Facebook Furl Google Reddit Slashdot StumbleUpon Technorati
How easy was it to understand? Was it useful?










Leave a reply
Your name:

Message:

Confirmation code
Please enter the above code:
sunjester says:
so can you tell me the difference between linux's php and windows's php? lol i can guarantee in fact they are the same. im sorry but this is a waste. (oh and btw nobody can take criticizm well so im sure this comment will be trashed...)
2009-07-15 14:35:35 (GMT)
Marius S. says:
Who said anything about differences in Linux and Windows PHPs? Have you understood what the article was about? There are differences in OSes, NOT in PHP and they affect your scripts. File paths and permissions are 2 common differences.
And surprise - your comment is not going to be deleted or trashed or watever. This is a thing that works for me and i'm sharing it with people, because it's a convenient system, you can transfer the server easily to any computer and the most important thing - it might help others learn something. And that's the whole point of this site - learning. Even if you don't like the result or you don't need it but learn something new - that's good enough for me.
2009-07-15 15:46:49 (GMT)
Copyright © 2009 Tip-Kit.Com | Valid XHTML 1.0 | Powered By Isis | RSS