Installing VirtualBox, CentOS 5.4, Apache, PHP and MySQL
I’ve recently done a project that uses load balancing. mod_proxy is a blog for another day but it was based on a LAMP stack. I install this all the time for lots of different projects. Some are development projects and some are just system administration projects.
My desktop of choice is Windows. I hate it slightly less than I hate MacOS, so I’m going to be installing my LAMP stack inside a virtual machine.
First up let’s get VirtualBox up running:
- Download VirtualBox for your operating system from https://www.virtualbox.org/wiki/Downloads
- Install it, selecting all the defaults.
- Download the latest copy of CentOS from their list of mirrors. At the time of writing this article was
CentOS-5.4-i386-bin-DVD
. - Run VirtualBox.
- Click the New button to create a new virtual machine.
- Give the virtual machine a name. You can give it the name of the operating system or the project you will be working on.
- Select Linux as the operating system.
- CentOS wasn’t in the list so I just selected Other Linux, I don’t think this does anything.
- Click Next, to configure the Memory.
- There 6GB in my PC so I’ll give 1024MB to the virtual machine. You can change this later if you like.
- Click Next, to configure the Virtual Hard Disk.
- Accept the defaults to create new hard disk.
- Click Next, to start the virtual disk creation wizard.
- Select the VDI(Virtual Disk Image) and click Next.
- Select Dynamically allocated because it creates a smaller file which helps with housekeeping and backups at the cost of a small amount of speed each time the hard disk needs to grow. You’ll only really notice this at the beginning when your installing everything but once you’re done you’ll find that it hardly grows after that.
- You can leave the hard disk name the same as the operating system.
- CentOS should install in a couple of gigabytes so accept the default of 8GB. The file won’t be 8GB straight away.
- Select Next, then Create and then Create again.
- The virtual machine’s creation should be instant. We’ll tweak a few settings and install our guest operating system.
- Click the Settings button.
- Select the System tab.
- No one has a floppy disk drive anymore so switching it off will speed up your boot slightly. Uncheck Floppy and move it to the bottom of the list.
- We’ll connect our ISO file to the virtual machine’s CD-ROM drive. So select the Storage tab and highlight the Empty CD-ROM drive.
- Select the Network tab and change the Attached to Bridged Adaptor. This will allow you to pick up a real network address from the DHCP server which makes testing easier.
- The right hand panel will change so click the CD-ROM icon on the far right to open the file selector.
- Select Choose a virtual CD/DVD disk file and navigate to your ISO image and open it up.
- Click Ok to close the Settings panel.
- Press Return to install in graphical mode.
- Don’t bother checking the CD media because it’s really just a file and so won’t suffer from any corruption, hopefully! So select Skip.
- The virtual machine will think about it for a second and then launch the installation application in X-windows.
- Select your language, keyboard country. If you are English then select United Kingdom.
- Your virtual disk has not been partitioned or formatting and the installer notices. So click Yes and accept the defaults.
- Select the defaults for Network Devices.
- Select your location for the timezone.
- Enter your Root password. If the virtual machine is going to be on the internet then choose your password carefully. If it’s just a test system then just choose something simple and small!
- The next screen allows you to select an installation profile. I’m going to be just doing PHP development so don’t need to bother with all sorts of desktop applications like Open Office etc. I do want web servers and networking tools. Select Server - GUI.
- Select Customise now and then Next. We’re going speed up the installation by not installing a load of stuff.
- Under Applications unselect most of them except the editors.
- Under Development make sure you select the Development Libraries and Development Tools.
- Under Servers make sure you explicity check MySQL Database.
- Open the Web Server and explicitly set php-mysql.
- Under Base System -> System Tools, explicitly select tn (telnet is a handy connection tester) and Wireshark (for packet sniffing).
- Under Languages select your country support.
- Click Next and Next again to start the install.
- 4 minutes later you can click the Reboot button.
- When the the virtual PC reboots it puts you in the platform configuration screens. Click Forward.
- If your not on the internet make life simple and switch of the Firewall and the SELinux. You don’t need Kdump either.
- Create yourself a User.
- It’s nice to have a bit of sound so test that.
- Click Finish and reboot.
- Click Settings -> System
- Move Hard Disk to above CD/DVD ROM.
- Then click OK.
- Next click the Start button, to boot the virtual machine.
- You will be booted to the X-Windows login prompt.
- Login as the
root
user. - Open a terminal window; it’s under Application -> Terminal.
- We want Apache and MySQL to start when the virtual machine boots into multi-user mode (runlevel 3).
cd /etc/rc3.d ln -s ../init.d/httpd S99http ln -s ../init.d/mysql S99mysql
- Now we’ll start the services
/etc/init.d/httpd start /etc/init.d/mysqld start
- Change to the web server document root:
cd /var/www/html
- Create a file called
pdo.php
and fill it with:<?php $pdo = new PDO("mysql:host=localhost;dbname=mysql", "root", ""); $sql = "SELECT NOW()"; $stmt = $pdo->query( $sql ); $obj = $stmt->fetch(PDO::FETCH_OBJ); var_dump($obj); ?>
- Get the virtual machines IP address with
/sbin/ifconfig
. - Open a browser and navigate to http://<ipaddress>/pdo.php
- You should see the following output in the web browser:
object(stdClass)#3 (1) { ["NOW()"]=> string(19) “2012-06-11 00:37:14″ }
shutdown -h nowIn VirtualBox:
- Right-click on the virtual machine and select Clone.
- Give it a name ending in “clean” so you know this one shouldn’t be touched.
- The first question VirtualBox asks is whether you want to reinitialise the MAC address on the network cards. The MAC address is the physical network address so if you want to run multiple clones at the same time then you’ll have to do this reinitialise step. Otherwise you could be in a world of IP address conflicts as both clones will appear to be the same machine. So do a Full Clone to keep it safe.
2 comments
Comment from: sarabjeet [Visitor]
Great post indeed and thanks for all the information.
Comment from: SARABJEET [Visitor]
Really like that you are providing such information on PHP MYSQl with JAVASCRIPT, i really thank you for providing such information it was helpful.
Form is loading...