My LAMP Server Using VirtualBox
I’m working on a VirtualBox image for a LAMP server. There are a few good reasons to use a virtualised LAMP server:
- Easy to back-up - you essentially only have a few files to back-up, rather than mirroring thousands of them.
- The footprint of the backup can also be kept small as you only need to keep installed what you need.
- You can play around with settings, and if anything badly goes wrong then you can drop back to a previously made version.
- Your host machine's OS (the operating system you're computer uses) can be anything, so your own preferences needn't be restricted to work / web development.
- Testing can be configured to a set-up that's almost exactly like your live server hosting (i.e. you can add the same apache modules, etc)
- When you back-up your VirtualBox image, then you are also backing up any test sites you have installed.
- It's relatively easy to set-up XDebug and SVN (or others) and play around with those settings without it being a burden on your own operating system.
- You can easily try it and see. If it doesn't work for you, try a different image, or try installing a different set-up. It's so easy!
- If you want to, you can reasonably easily (depending on hardware) port your image from the virtual environment to an actual computer (search for VirtualBox image and CloneZilla to get some ideas where to start).
My own set-up is a host OS of Ubuntu 11.04, and a guest OS (the VirtualBox image) of Xubuntu 11.04. There are smaller distros, and it’s probably a good idea for more advanced/confident Linux users to use the server edition, but only if you want to keep the requirements low and focus only on basic LAMP.
I also wanted the installation to be as generic as possible, so I also set my main user to be webdev. It doesn’t really matter, but for me it helps distinguish the roles of the LAMP server and that of the host OS and development.
There are a few optional steps, and a few steps I’ve taken that have incomplete instruction. I’ll make references to other useful links in these cases, and mention if something is required or not. I also should point out that these are really just notes for myself, and things I found useful for the installation along the way. It won’t cover certain basics of LAMP and a basic level of understanding of both LAMP and Linux is required.
Setting up the LAMP serverFirst off you need to download Xubuntu (or other), mount it to the VirtualBox drive and install. You might also want to make sure that everything is up-to-date by loading up the terminal and typing:
sudo apt-get update sudo apt-get upgrade
Then to install the LAMP server, use the following:
sudo apt-get install tasksel sudo tasksel install lamp-server
You will be asked for a new root MySQL password (and username, I think). You can test if it’s worked by browsing to “localhost” in your browser, and you should be getting a page that says something along the lines of “It works!”
Then you will most likely want PHPMyAdmin:
sudo apt-get install phpmyadmin
Here you will be asked if you want to use apache2 (you do) and you will be asked for a username and password for phpmyadmin and the root password that you assigned to mysql during the LAMP installation above.
Once your basic LAMP server is installed you will most likely want mod_rewrite which allows you to test “clean URLS” in your web apps:
sudo a2enmod rewrite sudo /etc/init.d/apache2 restart
There is a list of commonly used modules that I also found that you can install:
sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl php5-json sudo /etc/init.d/apache2 restart
Howerver, you can always add new modules as and when they may be required, at any point. Just make sure that you restart the apache server to enable the changes (i.e. the “/etc/init.d/apache2 restart” command)
Assign a static IPThe most useful things to do once you have installed and updated your host OS (Xubuntu) are to install Guest Additions (which better supports networking and display, among other things). After that you probably would like to set-up a fixed IP for the LAMP server so it can be available to your host operating system, or even across your local network. There are many ways of accomplishing this, although I have tested just two - one method allows you to use a fixed IP that's on the same domain as your local network (often 192.168.0.xxx) and another that adds the LAMP server to a different subdomain IP of your local network (i.e. 192.168.56.xxx). The benefit of the latter is that it won't interfere with your local network, and as this is for testing on only one PC it's not necessary to use an IP from your local network.
Anyway, if you do want your virtual LAMP server to work as an extension of your local network then you simply need to set-up the network card in VirtualBox to “Bridged Network". To use a separate subdomain IP then set Adapter 1 to NAT (using default DHCP settings) and Adapter 2 to Host-only Adapter. In your Xubuntu LAMP server Adapter 2 will probably appear as Auto eth1 and you can change it to a static IP of something like 192.168.56.101 and use 192.168.56.1 as the Gateway and DNS server (you can additionally use Google DNS 22.214.171.124 and 126.96.36.199 or something like OpenDNS).
Once you have a static IP, then it’s easy to add things like FTP, SSH and hosts aliases for your test websites.
SSH (secure shell)It can be a good idea (but not essential), especially if you would like to use the LAMP image more flexibly, to install SSH. Then you can have the option to make certain changes (like to apache.conf or php.ini without actually using the guest operating system, the LAMP server, directly):
sudo apt-get install ssh
You can connect to the LAMP server from your host computer with:
“webdev” is your username and “server.dev” is the name of your lamp server. To determine these (if you’re unsure), in your LAMP server you can use “whoami” and “hostname” at the command prompt.
You might also want to set-up Nautilus (Ubuntu’s file browser) to be able to access the virtual LAMP server’s files using SSH. This allows you to browse the files almost like they’re local files. There is an option “Connect to server…” where you can add those details: the static IP address, username, and folder “/var/www”
Adding a test siteNow you have a choice where your site files can be located. For this example I'm using /home/webdev/Projects, but they could also be in the default location of "/var/www". You only need to be aware of the permissions. I'm not really going to discuss permissions in any detail as it's outside of the scope of this article. Create your Projects folder; from the command prompt type:
cd ~ mkdir Projects cd Projects mkdir -p testside.local/public_html
Then you will need to point apache to that site (also notice that I’m using “public_html” which allows you to have a different public root folder to the root of your site, and you can have an exact duplicate of your live site. Of course, it could be “htdocs” rather than “public_html” depending on your live environment (note, we’re not setting up a live web server, only a testing LAMP server!).
Enter the code, substituting “gedit” with your preferred editor (“kate” on Kubuntu; “mousepad” on Xubuntu, or “scite” as a cool alternative)
sudo gedit /etc/apache2/sites-available/testsite.local
In the text editor, put in something like this (you might also need to create the weblogs folder):
<VirtualHost *:80> ServerName testsite.local DocumentRoot /home/webdev/Projects/testsite.local/public_html ErrorLog /home/webdev/WebLogs/testsite-error.log CustomLog /home/webdev/WebLogs/testsite-access.log common DirectoryIndex index.php index.html </VirtualHost>
Then save the file and enable it with:
sudo a2ensite testsite.local sudo /etc/init.d/apache2 restart
As an additional step you may find it useful to be able to access your test site from the host OS’s browser by using “testsite.local”. In order to do this you need to add a line to the /etc/hosts file:
sudo gedit /etc/hosts
And in this file, add a line like:
Optional thingsSome optional things might be to install FTP access to your LAMP server:
sudo apt-get install pure-ftpd
And also add the group “www-data” to your server’s “webdev” user. If you’re lazy, like me, you can just change the permissions of the files on your website files to 777 (“chmod -R 777 /home/webdev/Projects”) and use your “webdev” user to access the FTP. It doesn’t really matter for these files as it’s only for testing, but I’ll perhaps write another article delving into the correct permissions / settings at a later stage.
XDebugCopy the output of phpinfo() into the site http://xdebug.org/find-binary.php and follow the instructions.
Content versioningI'm currently investigating the best options for myself. Bzr is really easy to work with and install, especially using Eclipse as your IDE, but it's support is less good for NetBeans so I'm looking now at Git which purportedly has better performance than Bzr, and is certainly more widely used (although it's harder to set-up initially).
I may add / amend anything in here as it’s a reference for myself as much as anyone else. I’ve tested the entire process a few times now, so the theory is sound, even if I may have made a few typos or errors writing this :P
Email ServerI haven't done much with this yet, but you can install basic support for email, on the LAMP server, simply by installing sendmail (which php's mail() function uses):
sudo apt-get install sendmail
However, you may or may not need to make changes to the configuration to get it working (correctly). Again, all I’m interested in is getting something basic that I can make tests with.
Update: for sendmail, I also needed to add my hostname (server.dev) and localhost.localdomain. This fixed a problem where it was taking ages to send an email via PHP.
127.0.0.1 localhost localhost.localdomain server.dev