Installing a virtual Ubuntu LAMP-server on Mac OS X using VirtualBox
Virtualization is a hot topic in ICT. How can you put this to use at home and more specifically on your Mac ? You can use it to run Windows on your Mac, but also to try out different flavors of Linux or to run a LAMP (Linux Apache MySQL PHP) development server. If you have a spare recent pc you can install a baremetal hypervisor like XenServer5, but that’s a different story. If you want to install a hypervisor or virtual machine monitor on your MacBook, Mac mini or iMac, there are a number of solutions available. To name a few: VMWare, Parallels, Q and VirtualBox.
First requirement: it has to be free, free as in made freely available by the software vendor, not as in a pirated or illegal copy. So skip VMWare (unless you stick with the VMWare Player) and Parallels.
Second requirement: it must be a hypervisor project, preferably open source, with active development, user groups, forums and good documentation.
So I chose VirtualBox. For the cross-platformers among you, like myself, VirtualBox also runs on Windows, Linux and OpenSolaris and supports a large number of guest operating systems including Windows (NT 4.0, 2000, XP, Server 2003, Vista), DOS/Windows 3.x, Linux (2.4 and 2.6), Solaris, OpenSolaris, and OpenBSD.
But this post will be about installing a virtual Ubuntu LAMP or web server on Mac OS X using VirtualBox.
Getting started
For starters, you will need a Mac. I have a MacBook 2.16 GHz Intel Core Duo, with 2 GB RAM and 120 GB hard disk and Mac OS X Leopard (10.5.6) installed. My Mac is connected to a wireless router.
Next, you will need to download VirtualBox for Intel Macs (mine is version 2.1.0) and Ubuntu server (I chose for the 8.04 LTS 32 bit version).
You will also need enough diskspace to install one or more virtual machines, but that goes without saying … right ? 8 GB and up is recommended for a virtual machine, but since we will be using the server edition, CLI only (no GNOME, no KDE, no Office suite, no games, …) 4-5 GB should be more than enough for a development webserver.
The installation of VirtualBox is pretty straightforward, so I’ll skip this.
However, you must change some settings of Mac OS X. First you must enable internet sharing. To do this, open ‘System Preferences’ and in ‘Internet & Network’ click ‘Sharing’. Now you get the window you see in the picture ‘Sharing’, select ‘Internet Sharing’, for ‘Share your connection from:’ select ‘Airport’ (when you use wireless) and select in ‘To computers using’ the ‘Ethernet’ option.
Secondly, some sites mention that you should disable IPv6. I didn’t notice any difference between IPv6 status ‘Off’ or ‘Automatically’. The information on those sites wasn’t very recent and, if I recall correctly, referred to VirtualBox version 1.x. My IPv6 is configured to ‘Automatically’, but if you want to turn it off, here’s how to do it. Again open ‘System Preferences’ and in ‘Internet & Network’ click ‘Network, next click the ‘Advanced…’ button and the ‘TCP/IP’ tab. Where it mentions ‘Configure IPv6′ select ‘Off’.
That should take care of the preparations. Let’s get down to business and install a virtual webserver.
Create a virtual machine
Start VirtualBox. Before we go into the step by step creation process, I will explain some of the choices I made. For RAM I chose 256 MB, this is the minimum for an Ubuntu server, but as it is just for a virtual development or test server on my MacBook, this should be enough. Keep in mind that the memory you allocate to your virtual machine is no longer available for your real machine; 2 GB RAM minus 256 MB still leaves plenty of RAM available for my Mac, enough to run a second and even a third virtual machine if I want to, but if you only have 1 GB of RAM to start with, you will probably notice that everything slows down if you allocate 512 MB to your virtual machine. According to VirtualBox the recommended disk space for Ubuntu is 8 GB, but that is for the desktop version with Gnome and all the bells and whistles you don’t need in a server. I chose 5 GB to be on the safe side, but after installation I noticed that Ubuntu server needs less than 1 GB of disk space, so I guess 2 or 3 GB should be more than adequate. It depends of course on what kind of sites you’ll be developing. I chose for ‘Fixed-size storage’ instead of ‘Dynamically expanding storage’, but both options work, so no worries here. In the case of ‘Dynamically expanding storage’ you select the maximum disk space and that space will only be addressed when needed, so you save some space on your hard drive … at least for a while.
Create your virtual server:
- click on the ‘New’ icon
- click ‘Next’ to go to ‘VM Name and OS Type’
- choose a meaningful name
- as ‘Operating System’ select ‘Linux’
- as ‘Version’ select ‘Ubuntu’ or ‘Linux 2.6′, both options work
- click ‘Next’ to go to ‘Memory’
- select amount of RAM, I chose 256 MB
- click ‘Next’ to go to ‘Virtual Hard Disk’
- leave ‘Boot Hard Disk’ checked
- click ‘New…’
- click ‘Next’ to go to ‘Hard Disk Storage Type’
- I chose for ‘Fixed-size storage’
- click ‘Next’ to go to ‘Virtual Disk Location and Size’
- I kept the default name and location
- select amount of disk space, I chose 5 GB
- click ‘Next’ and ‘Finish’
- for ‘Fixed-size storage’ this can take a couple of minutes
- click ‘Next’ and ‘Finish’ (again)
Virtual machine settings
Before actually installing your virtual Ubuntu server, you must change some settings.
In the main VirtualBox window, select the virtual machine you’ve just created and go to ‘Settings’ either by a right mouse click on the virtual machine or by clicking on the ‘General’ link (in blue). Now click on the ‘Advanced’ tab and check ‘Enable PAE/NX’, this is required to install an Ubuntu server, it is however not needed for an Ubuntu desktop or so I’ve read.
On top of this window you see a list of icons: ‘General’, ‘Storage’, ‘Audio’, ‘Network’, ‘Ports’, … Click on ‘Network’. Here several settings need to be changed.
Change the ‘Adapter Type’ to ‘Intel PRO/1000 T Server (…)’. This adapter is less demanding on your CPU.
Change ‘Attached to’ to ‘Host Interface’, ‘NAT’ is fine when you only want an outgoing connection like for Ubuntu Desktop, but you want your Ubuntu Server to accept incoming connections (HTTP, FTP, SSH) too, otherwise your server is useless, so change it to ‘Host Interface’.
Which host interface to select in ‘Host Interfaces’ ? I read another blog that mentions that you must select ‘en0: Ethernet’ even if you only have a wireless connection. I believe this caused all the problems I encountered installing this virtual machine. At first it was a success, the installation went fine, the server got assigned an IP address in the 192.168.2.0 network although I have no idea where this network comes from (Mac or VirtualBox). The updating, upgrading, configuring, … everything went smooth. I could connect from my MacBook to the virtual machine via HTTP and SSH. Next day, no more connections from the virtual server to the internet or from my MacBook to the server (cannot allocate memory errors, see troubleshooting chapter at the bottom). I lost count on how many attempts I made to get it to work. I was convinced that the problem was either caused by the IPv6 setting or because I didn’t install the linux-virtual kernel and the ‘Guest Additions’ right away. Google searches indicated these to be the most likely culprits. Once the problem occurred, the only solution was to delete the virtual machine and start all over again. It was only after I selected ‘en1: Airport’ as ‘Host interfaces’ that it kept working. I could see a lot benefits by using the rogue/alien/virtual/… 192.168.2.0 network: I would be able to connect to my webserver even when my Mac was offline or on a different wireless network, so I didn’t want to give it up so easily. By selecting ‘en1: Airport’ my virtual machines now connect to my wireless router, which has a 192.168.1.0 network. And there are benefits for this solution too: your virtual machines can connect to other machines on your home network (you can use this to make backups) or let those other machines connect to the virtual servers on your Mac (e.g. a Windows pc to test a website with IE). Maybe I did something wrong, maybe the information on the other site was wrong, but 4 times in a row I installed servers with ‘en1: Airport’ as host interface and they keep on working.
Installing Ubuntu server
Don’t close the ‘Settings’ window yet. Click on ‘Storage’ and select the tab ‘CD/DVD-ROM’. If you already closed the window you can click on the link ‘CD/DVD-ROM’. Here you can either mount your Ubuntu CD-ROM in your ‘Host CD/DVD Drive’ or select the ‘ISO Image File’ you downloaded on your computer. When you choose the ‘ISO Image File’ option, click on the folder button on the right and in the new window ‘Virtual Media Manager’, click on the ‘Add’ button and browse to the location of the ISO file. Don’t forget to unmount the CD- or DVD-ROM the first time you shut down after installing.
From an economical and tree hugger point of view it is better to install from an ISO image
.
Now you may hit the ‘Start’ button after you’ve selected the virtual machine.
I am going to walk you through the installation process, but very briefly.
- select language
- select ‘Install Ubuntu server’ or ‘Check CD for defects’ first
- select language, country and keyboard
- let it configure your network with DHCP, you can change it later
- choose a name for your webserver
- as partitioning method I chose ‘Guided – use entire disk’
- for the next step there was only 1 disk to select
- agree to write the changes to disks
- you can take a break now
- set up a user and password
- ‘HTTP proxy information’: leave it blank
- you can take another short break now
- select at least the following software to install: ‘LAMP server’ and ‘OpenSSH server’
- you will be asked to provide a password for the MySQL root user
- after installation, let your virtual machine reboot and don’t worry about the CD-ROM or ISO image, it will be ignored, but unmount it when you shut your new server down
There are several advantages for using ssh from a Mac OS X Terminal instead of the terminal or user interface provided by VirtualBox:
- more control over the layout: font, size, colors, background
- better keyboard support (e.g. @ # ^ | `)
- faster, more responsive … at least that is my impression
Now you have installed a virtual Ubuntu LAMP or web server. It just needs some polishing and then you are ready to go. I believe it is best to continue and especially optimize your kernel for a VM environment before calling it a day.
Finishing off
You do not have to do all of the following steps, for example the steps on how to configure the Apache webserver and vsftpd FTP-server, you can skip these or alter them to your liking, but I just want to leave you with a working webserver. Since it is supposed to be a virtual development server on your notebook or desktop I haven’t really bothered with security, creating virtual host, optimizations, …
I recommend you do not skip the network configuration, the update and upgrade and the installation of the virtual kernel and guest additions.
Network configuration: Currently your network configuration is based on DHCP. This is not very useful for a server, you need a static IP address. In the user interface provided by VirtualBox, log in to your server. First check your current network configuration:
johan@lamp:~$ ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:d4:b0:68
inet addr:192.168.1.4 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fed4:b068/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1167 errors:0 dropped:0 overruns:0 frame:0
TX packets:829 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:523485 (511.2 KB) TX bytes:103126 (100.7 KB)
Base address:0xc010 Memory:f0000000-f0020000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:12 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:600 (600.0 B) TX bytes:600 (600.0 B)
Obviously I am connected to the network provided by my wireless router, now edit your network configuration using the command:
sudo vi /etc/network/interfaces
For safety reasons it is best to make a copy of this file before editing. I will demonstrate the settings for my home network, they might be different for yours, but this example should help you to figure it out:
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 iface eth0 inet static address 192.168.1.80 gateway 192.168.1.1 netmask 255.255.255.0
Notice that I don’t need to specify a wireless-essid, wireless-key or wireless-channel, even though it is, indirectly, using a wireless connection, the settings are for a wired connection.
Next restart the network using the command:
sudo /etc/init.d/networking restart
You can check the new configuration with the command ifconfig.
Update/Upgrade: After an installation it is recommended to update and upgrade the installed software. On an Ubuntu server you do this via the following two commands, the second one can take a while:
sudo apt-get update
sudo apt-get upgrade
Virtual kernel: You have to customize or optimize the kernel for a VM environment:
sudo apt-get install linux-virtual
Installing Guest Additions: To install the Guest Additions, click, with your virtual server running and in the active window, in the ‘Menu Bar’ on ‘Devices’ and ‘Installing Guest Additions…’. The ISO image containing those additions is now mounted to your virtual machine. By default a CD-ROM is not automatically mounted in Ubuntu server, so use the following commands to gain access to the additions:
johan@lamp:/$ sudo mount /media/cdrom mount: block device /dev/scd0 is write-protected, mounting read-only johan@lamp:/$ cd /media/cdrom johan@lamp:/media/cdrom$ ls 32Bit VBoxLinuxAdditions-amd64.run VBoxWindowsAdditions-amd64.exe 64Bit VBoxLinuxAdditions-x86.run VBoxWindowsAdditions.exe AUTORUN.INF VBoxSolarisAdditions.pkg VBoxWindowsAdditions-x86.exe
According to the VirtualBox manual, you must install DKMS to be able to compile the kernel using the command (don’t do it, continue to read):
sudo apt-get install dkms
If you do that, and you execute ‘VBoxLinuxAdditions-x86.run’ you will get the following output:
johan@lamp:/media/cdrom$ sudo sh ./VBoxLinuxAdditions-x86.run [sudo] password for johan: Verifying archive integrity... All good. Uncompressing VirtualBox 2.1.0 Guest Additions for Linux installation............ VirtualBox 2.1.0 Guest Additions installation Please install GNU make. Please install the build and header files for your current Linux kernel. The current kernel version is 2.6.24-23-virtual Please install the GNU compiler. Problems were found which would prevent the Guest Additions from installing. Please correct these problems and try again.
As far as I can figure this out, installing DKMS doesn’t take into account that you have installed the linux kernel for virtualised hardware (linux-virtual). So instead install the headers for the right kernel and the necessary tools to be able to build.
sudo apt-get install linux-headers-`uname -r` build-essential
After that you can install the additions (make sure that you are in the CD-ROM directory):
sudo sh ./VBoxLinuxAdditions-x86.run
The VirtualBox manual recommends installing them, but I haven’t used them yet. This post is all about installing a virtual Ubuntu server on Mac OS X. It’s not an in-depth look at or the nuts and bolds of VirtualBox.
Changing the configuration of the webserver to enable the per-user web directories. This means that every user can have his/her own website by creating a special directory in his/her home directory. First create a ‘public_html’ directory in your home and change the group to ‘www-data’ to give the Apache webserver the necessary permissions to process your webpages.
cd $HOME
mkdir public_html
sudo chgrp www-data public_html/
Enable the userdir module and restart or reload the Apache webserver.
sudo a2enmod userdir
sudo /etc/init.d/apache2 force-reload
Create a PHP test page info.php in your public_home directory with the following contents
<?php phpinfo(); ?>
and on your client computer enter the following URL in your browser (change the IP address and username)
http://192.168.1.80/~johan/info.php
and you should get the version information for your PHP installation.
Installing an FTP-server to upload your websites. I am used to the ‘Very Secure FTPD’ server, vsftpd, although I believe this is not the default one for Ubuntu. Anyway it is easy to install if you use the following commands to install and stop vsftpd and edit the config file.
sudo apt-get install vsftpd
sudo /etc/init.d/vsftpd stop
sudo vi /etc/vsftpd.conf
In the configuration file uncomment the following settings
local_enable=YES
write_enable=YES
local_umask=022
and change the ‘YES’ to ‘NO’ in
anonymous_enable=NO
and save. Restart.
sudo /etc/init.d/vsftpd start
Now you should be able to upload files to your virtual webserver using an FTP-client like FileZilla. Enter the IP-address, your username and password, click ‘connect’. If all goes well, you will see the contents of your home directory on the webserver, including the public_html directory. Any files you upload to the public_html directory will become visible or at least accessible when you enter the following URL in your browser (change IP and username to match your settings):
http://192.168.1.80/~johan/
Some extras: On a webserver I like to have phpMyAdmin and PHP CLI available (when installing phpMyAdmin, select ‘apache2′ when prompted):
sudo apt-get install php5-cli
sudo apt-get install phpmyadmin
The further configuration of your webserver, samba, … is beyond the scope of this post. Good luck.
Troubleshooting
I experienced a lot of problems at first. The installation went fine, the updating, upgrading and configuring too. Next day, the server could no longer connect to the internet and I could no longer access the server via HTTP, SSH or even ping to it. This happened enough times to exclude an accidental configuration error or a corrupt ISO image. The error messages I got were:
ping: sendto: Cannot allocate memory
ssh: connect to host 192.168.2.22 port 22: Cannot allocate memory
Once these occur just delete your virtual server and start all over again. They are either caused by IPv6 being turned on, not installing the for VM’s optimized kernel or using the fictitious, virtual or whatever it is called 192.168.2.0 network. I have narrowed it down to the use of the 192.168.2.0 network. I do not know enough of virtualization to explain this, I only want a virtual webserver, in fact more than 1 server, running on my MacBook.
You can connect to your virtual server from other machines in your home. This works without hassle for Macs and Linux boxes, but for Windows XP you have to ping or connect via ssh to your virtual server first before you can browse to it (WTF).
PS
I have no idea if this post is also useful for installing a virtual Ubuntu server on a Windows or Linux host.
| Windows users swear at their computers. Mac users swear by their computers. |







helpful stuff, thanks
Thanks, works fine even on WinXP and VirtualBox 2.1.2
Nice 1 man ………
Really good guide. I used it for my installation. You wrote \… I would be able to connect to my webserver even when my Mac was offline or on a different wireless network…\ but I don’t get this feature to work. Everthing goes fine but If I have no wireess connection, airport has no IP and so there is no way to ping the server out of my host mac. Did you solved this anyway?
@Holger
Well, the sentence reads ‘I would be able …’; it is wishful thinking on my part and I didn’t manage to get it to work either, but I got a tip from another visitor so I am working on an update of the post.
Great Stuff… I’ve been plugging away at getting up this same setup, albeit on my Dell in Xubuntu. So yes, it’s practically the same for installing in a Linux host. Some questions about your guest’s networking configuration:
1. “gateway 192.168.1.1″ in the networks file
is this the IP of the host? and wont this cause problems if the host gets a different IP? Infact, on my home network, this is the address of the router, and my laptop (host) gets it’s IP via DHCP… Right now my guest talks directly to the router (as you mentioned). I dont understand why you do that guest networking configuration step?
2. Any luck on setting up a stable “rogue” network inside the host? My original idea was to do something like this, but I dont know where to start. This would really help because I have to work offline quite a bit…
3. Thanks and Good Luck.
@Swift Arrow
The gateway is the one from the router. I used it in the static network configuration; a server with a dynamically allocated IP address is annoying.
I am working on the “rogue” setup and hope to post an update soon.
Just another comment:
TO SET UP SHARED FOLDERS: (that’s why we install the guest additions, right?)
1. Shut down guest if it is on (sudo halt)
2. In the VirtualBox Manager, click “Settings” with the server selected.
3. Go to Shared folders, and add the folder on the Host that you want to share. Choose to Make it permanent, if you get the option. Note the folder name. Make sure it does not have spaces.
4. Start the server guest.
5. Run the command (sudo mkdir /media/share)
6. After logging in, run the command (sudo mount -t vboxsf “folder_name” /media/share)
7. now type (ls /media/share) and it should show the contents of the shared folder.
8. To add the share to fstab -automatic mounting on boot-, do:
sudo cp /etc/fstab /etc/fstab.backup
sudo vi /etc/fstab
use arrow keys to navigate to the end of the last line, press a
type in “folder_name /media/share vboxsf rw,uid=1000 0 0″
press escape, type in :wq
sudo reboot
Mine, while booting, says that it failed to mount with no such device error, but it works fine nevertheless.
9. Since we generally want to share a folder in which we put all of our web files, make a symlink as follows: (sudo ln -s /media/share /var/www/share)
10. Now the webserver will serve any file you put in your shared folder on the host!
I still would like to set up networking so I dont need an external connection…
Note that you can add the shared folders even while the guest is running, but it doesn’t know that they exist until it reboots. Hence step 1 above.
And another comment: when editing fstab, if anything goes wrong, you can just copy back the original backup (sudo cp /etc/fstab.backup /etc/fstab)
also, to append to the end of the line in vi, press “a” (the a key) then enter the info on a new line. If your userid is not 1000, then change accordingly.
If you don’t mind, I’ll just dump all the information I can find here…
During the install of Ubuntuserver, you are asked for the MySQL root password.
After installing phpmyadmin,
open host’s browser, go to http://guest.ip/phpmyadmin and use username: root password: whatever you chose.
wondering if its possible to run a remote web server that way? what settings would need to be changed or can you point me to the right direction?
I have accessed the virtual server on my MacBook from another computer in the home network, but not from outside. I don’t think my ISP allows running servers accessible from the internet.
@johan
Hi Johan,
I got it to work. In VirtualBox Menu -> Preferences you should go to the network tab. Create a host-only network adapter with an IP and enable DHCP. Within your VM create a second network adapter and select host-only and your created NIC. No, you can set a static IP within your VM and the same address range and this works even if your macbook is not connected to any network. Now I can test everything without being connected and if I am connected, my linux box have full internet access. If my description was too short, please contact me to get further information. If this works for you, you should update your helpful guide.
@Holger
Thanx. I have recently updated VirtualBox and noticed that they changed the user interface. So maybe I should write a new post or update this one.