Educating the world

Our blog has over 10,000 readers a month

Installing VirtualBox, CentOS 5.4, Apache, PHP and MySQL

September 11th, 2012

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:

  1. Download VirtualBox for your operating system from https://www.virtualbox.org/wiki/Downloads
  2. Install it, selecting all the defaults.

Next we’ll set up a Virtual machine which will run our guest operating system:

  1. 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.
  2. Run VirtualBox.
  3. Click the New button to create a new virtual machine.
  4. Give the virtual machine a name. You can give it the name of the operating system or the project you will be working on.
  5. Select Linux as the operating system.
  6. CentOS wasn’t in the list so I just selected Other Linux, I don’t think this does anything.
  7. Click Next, to configure the Memory.
  8. There 6GB in my PC so I’ll give 1024MB to the virtual machine. You can change this later if you like.
  9. Click Next, to configure the Virtual Hard Disk.
  10. Accept the defaults to create new hard disk.
  11. Click Next, to start the virtual disk creation wizard.
  12. Select the VDI(Virtual Disk Image) and click Next.
  13. 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.
  14. You can leave the hard disk name the same as the operating system.
  15. CentOS should install in a couple of gigabytes so accept the default of 8GB. The file won’t be 8GB straight away.
  16. Select Next, then Create and then Create again.
  17. The virtual machine’s creation should be instant. We’ll tweak a few settings and install our guest operating system.
  18. Click the Settings button.
  19. Select the System tab.
  20. 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.
  21. 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.
  22. 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.
  23. The right hand panel will change so click the CD-ROM icon on the far right to open the file selector.
  24. Select Choose a virtual CD/DVD disk file and navigate to your ISO image and open it up.
  25. Click Ok to close the Settings panel.

Right, off we go. Click Start and read the popup boxes and click your way through them. VirtualBox will launch the Virtual Machine and will attempt to boot from the ISO image we gave it earlier. You will start to see the CentOS installation happening in a new window that’ll open up

  1. Press Return to install in graphical mode.
  2. 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.
  3. The virtual machine will think about it for a second and then launch the installation application in X-windows.
  4. Select your language, keyboard country. If you are English then select United Kingdom.
  5. Your virtual disk has not been partitioned or formatting and the installer notices. So click Yes and accept the defaults.
  6. Select the defaults for Network Devices.
  7. Select your location for the timezone.
  8. 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!
  9. 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.
  10. Select Customise now and then Next. We’re going speed up the installation by not installing a load of stuff.
  11. Under Applications unselect most of them except the editors.
  12. Under Development make sure you select the Development Libraries and Development Tools.
  13. Under Servers make sure you explicity check MySQL Database.
  14. Open the Web Server and explicitly set php-mysql.
  15. Under Base System -> System Tools, explicitly select tn (telnet is a handy connection tester) and Wireshark (for packet sniffing).
  16. Under Languages select your country support.
  17. Click Next and Next again to start the install.
  18. 4 minutes later you can click the Reboot button.
  19. When the the virtual PC reboots it puts you in the platform configuration screens. Click Forward.
  20. If your not on the internet make life simple and switch of the Firewall and the SELinux.
    You don’t need Kdump either.
  21. Create yourself a User.
  22. It’s nice to have a bit of sound so test that.
  23. Click Finish and reboot.

When the virtual machine reboots stop it so we can change the boot order. We don’t want to boot from CD every time.

  1. Click Settings -> System
  2. Move Hard Disk to above CD/DVD ROM.
  3. Then click OK.
  4. Next click the Start button, to boot the virtual machine.
  5. You will be booted to the X-Windows login prompt.
  6. Login as the root user.
  7. Open a terminal window; it’s under Application -> Terminal.
  8. 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

  9. Now we’ll start the services

    /etc/init.d/httpd start
    /etc/init.d/mysqld start

We should be up and running so we’ll create a .php file to test that PHP and MySQL are working together.

  1. Change to the web server document root:

    cd /var/www/html

  2. Create a file called pdo.php and fill it with:

    PHP

    <?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);
     
    ?>
  3. Get the virtual machines IP address with /sbin/ifconfig.
  4. Open a browser and navigate to http://<ipaddress>/pdo.php
  5. You should see the following output in the web browser:

    object(stdClass)#3 (1) { ["NOW()"]=> string(19) “2012-06-11 00:37:14″ }

Congratulations that was easy.

Now that you have a fresh clean system it’s a good idea to create a clone. We can use this clone to create clean copies for other projects without having to go through all the hassle of installing the operating system. We’ll have to shutdown the virtual machine so from the command line type:

shutdown -h now

In VirtualBox:

  1. Right-click on the virtual machine and select Clone.
  2. Give it a name ending in “clean” so you know this one shouldn’t be touched.
  3. 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.

My installation was about 3GB so it took a few seconds to copy all the data to make the clone.

Firefox doesn't install UK English for British installations by default

September 10th, 2012

If you download Firefox from the main download page, the site doesn’t detect that you are British. You get the standard American download. The result of this is that web sites detect your locale from your browser then layout internationalised content using this detection.

When we started using CIPHR we found that everyone using Firefox was being shown their dates in the wrong (American) order.

Firefox by default installs the languages English (en) and English U.S. (en-us) but not English GB (en-gb).

  1. So from within the menu in Firefox, select Tools -> Options -> Content Tab.
  2. From the Languages section select Choose.
  3. From the Select a language to add drop down select English/United Kingdom [en-gb].
  4. Click Add.
  5. Use the Move Up / Move Down to position en-gb at the top of the preferences.
  6. Click OK to finish.

You may have to refresh the page but you shouldn’t have to restart Firefox.

Alternatively you could download the British version of Firefox. When you navigate to http://www.mozilla.org it forwards you to http://www.mozilla.org/en-US/. Unfortunately there isn’t a http://www.mozilla.org/en-GB/ so you’ll have to click the Systems & Languages link under the download button. Scroll down to the line English (British) and click the Download link from there and follow the same procedure as before.

Cisco ASDM 5.2 for ASA ClassCastException X509TrustManagerImpl to X509ExtendedTrustManager

August 31st, 2012

I hadn’t used my Cisco ASDM 5.2 for ASA for a while and when I needed to add a couple of extra users to the VPN it didn’t work.
Everything seemed to load in ok but when I tried to submit my login credentials it hung. I loaded the Java console and got the following exception when I tried to authenticate.

Using JRE version 1.7.0_05 Java HotSpot(TM) Client VM
User home directory = C:\Users\mrn
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
m:   print memory usage
q:   hide console
s:   dump system properties
----------------------------------------------------
ASDM Application Logging Started at Wed Aug 29 12:46:43 BST 2012
---------------------------------------------
Local DM Launcher Version = 1.5.20
Local DM Launcher Version Display = 1.5(20)
OK button clicked
Cache location = C:/Users/mrn/.asdm/cache
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
  sun.security.ssl.X509TrustManagerImpl cannot be cast to
  com.sun.net.ssl.internal.ssl.X509ExtendedTrustManager
	at com.sun.deploy.security.X509ExtendedDeployTrustManager.(Unknown Source)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at java.lang.Class.newInstance0(Unknown Source)
	at java.lang.Class.newInstance(Unknown Source)
	at com.cisco.launcher.w.a(Unknown Source)
	at com.cisco.launcher.s.actionPerformed(Unknown Source)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.awt.EventQueue$3.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.awt.EventQueue$4.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)

The problem is caused by Cisco ASDM 5.2 requiring Java 6. In spite of having Java 6 and 7 installed, the Cisco software doesn’t know the difference and just accepts the first installation as being the only installation. Version 7 comes first and so the Cisco software tries to use it.

Sun have a couple of internal classes that only sun are supposed to use because they might change without warning. Low and behold they did! X509TrustManagerImpl no longer has the same interface as X509ExtendedTrustManager and so we get a ClassCastException.

In order to work around the problem we must launch the Cisco ASDM 5.2 from an environment where it only knows about Java 6.

Create a file called asdm-launcher.bat and place it into C:\Program Files (x86)\Cisco Systems\ASDM. Use your favourite text editor and add the following to the file:

cd C:\Program Files (x86)\Cisco Systems\ASDM

“C:\Program Files\Java\jre6\bin\javaw.exe” -Xms64m -Xmx512m -Dsun.swing.enableImprovedDragGesture=true -classpath lzma.jar;jploader.jar;asdm-launcher.jar;retroweaver-rt-2.0.jar com.cisco.launcher.Launcher

When the batch file has been saved:

  1. Create a shortcut on the desktop (or wherever) to use it. To make it more similar to the old shortcut you can change the icon used.

  2. Right-click on the shortcut and select Properties then Change Icon.
  3. Click Browse
  4. Enter "C:\Program Files (x86)\Cisco Systems\ASDM\asdm-launcher.exe" and hit return.
  5. Select the icon you would like and press OK.

There is a file called "C:\Program Files (x86)\Cisco Systems\ASDM\asdm-launcher.conf" which contains instructions to launch the Java Virtual Machine (JVM) with certain parameters but I couldn’t get it to pick up a different version of Java. So if anyone knows what goes into that file so that we don’t need a hacked up batch file then let us know in the comments.

I blog things that take ages to find on the interweb but here’s the source of my final solution:
[ https://supportforums.cisco.com/thread/2099390 ]

Special Characters in HTML

August 16th, 2012

I always need to remember the special HTML entity codes for characters. Many blogging applications re-render the characters to make them more readable but when you want to cut and paste the text it’s all wrong. The best example of this is double quotes. If I write

echo "hello world"

into the article body, after rendering it comes out as:

echo “hello world”

This is fine for standard text but if you are writing computer code it’s useless. Try cutting and pasting the above 2 examples into a unix command shell. You will get

hello world

into the article body it actually comes out as

“hello world”

respectively.

There are thousands of web sites where they publish what all these codes are and most of them are a bit rubbish. I came across this on Steve DeGraeve’s site which is one of the better ones. I thought I’d blog it so I don’t lose it in the melee of similar pages returned by Google’s search.

http://www.degraeve.com/reference/specialcharacters.php

Logging into a unix box without a password

August 10th, 2012

From time to time I need to allow one user to log into another machine without being prompted for a password. This is extremely useful for anything that is automated for example doing the nightly backup which copies files across the network or running commands on the other machines. ssh is the program that allows one to login over and encrypted channel but it doesn’t allow you to specify the password on the command line as it would be a huge security risk.

We are going to set up a relationship between 2 computers using a public/private pass key. For the example below we’ll use 2 machines called william and catherine.

  1. Ensure you have OpenSSH installed:

    yum install openssh

  2. Open a command line session on william.
  3. To make life a little easier for ourselves login to catherine using ssh, accept the key fingerprint and then exit straight away.

    [root@william ~]# ssh root@catherine
    The authenticity of host 'catherine (10.0.0.69)' can't be established.
    RSA key fingerprint is 3b:4f:1f:cb:44:56:9b:7f:96:a6:6a:c2:d6:bc:a6:df.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'catherine' (RSA) to the list of known hosts.
    root@catherine 's password:
    Last login: Thu Aug  9 16:18:35 2012 from catherine
    [root@catherine ~]# exit
    logout
    Connection to catherine closed.
    [root@william ~]#

    This will set up the ~/.ssh folder with the correct permissions and also will create a file in there called known_hosts. This file contains the key fingerprints for catherine. If we talk to other hosts later, then they will get added here too.
    Do the same from catherine to make sure all the folders are set up correctly on that side too.

  4. Generate a public and private key pair and save them to william.pub and william respectively.

    ssh-keygen -t dsa -f william -N ""

    giving the result:

    Generating public/private dsa key pair.
    Your identification has been saved in william.
    Your public key has been saved in william.pub.
    The key fingerprint is:
    48:2f:e6:bf:02:7e:2e:d2:b0:9a:1f:c6:7f:99:92:93 root@william
    
    The key's randomart image is:
    +--[ DSA 1024]----+
    |                 |
    |         .       |
    |        o +      |
    |       . =       |
    |        S .      |
    |     o. .  .     |
    |   o  == .  .    |
    |  E  *o.o +  .   |
    |   o*o...o o.    |
    +-----------------+
    

    Note: The -N option allows you to specify a passphrase. If you do then each time you need to use the public key you’ll have to enter the passphrase in order to decrypt it for use. Using an extra passphrase here will mean we won’t be able to make it automatic and our dreams of automating our backups will be gone :(

  5. Now that we have the keys for william we’ll transfer the public key over to catherine.

    scp william.pub root@catherine:~/.ssh/

    when william.pub arrives on catherine it’ll look something like:

    ssh-dss AAAAB3N……f1Jew== root@william

    make sure that the last part (root@william) is accessible i.e. can you ping william from catherine. If you can’t then you can edit the william.pub file and change it to root@10.0.0.1 where 10.0.0.1 is the IP address or name of william.

  6. catherine must have a file containing all the public keys that it is authorised to use. This file is called ~root/.ssh/authorized_keys so add william.pub to the end of it.

    cat william.pub >> authorized_keys

    or if you have lots to do:

    cat *.pub > authorized_keys

  7. Now you can log into catherine from william without entering a password, you just need to specify your local private key identity file:

    ssh -i william root@catherine

    If you don’t want to specify the local private file on the command line all the time you can rename william to the default name of id_dsa:

    mv william id_dsa

    then try to login:

    ssh root@cathrine

Here’s a tip if you are using virtual machines. If you set it all up so that you can log into yourself. Then make a clone. Both machines will have the same keys. They will be able to log into each other because, due to the same public and private keys, they think they are logging into themselves.

This is excellent if you are creating lots of clones for test or development environments because they can all log into each other without passwords without any extra work from you.