Setting up a LAMP server in VirtualBox

Perhaps you’ve wanted to start creating websites using LAMP (Linux, Apache, MySQL and PHP) technologies. It’s a useful skill and you might make a bit of money out of it. But maybe you’ve not got a Linux webserver handy. If you are on Windows 7 or higher, and your PC is five years old or less, you have at least 8 GB or more of ram and 30GB of spare disk there’s a way to do it that will cost you nothing. Arguably we could install WAMP (Windows, Apache etc.) but my preference is Linux for running Apache/PHP.
There’s an open source virtualisation package called VirtualBox. It was created by Oracle, the same Oracle of Java fame and it needs your PC to support vt-x technology. Thankfully it’s a requirement for PCs that run Windows 10.

When you run VirtualBox, you’ll see a list of virtual machines on the left. As you have none to start with, download a Linux distribution .ISO file. For Ubuntu I generally prefer the LTS versions as they’re supported for five years. For this though I downloaded a fresh Ubuntu 17:04 server and desktop and installed both. Don’t forget to right click view properties on the file and unblock it after downloading.

I created a new VM (Virtual Machine- an instance of Linux etc.) and set the details for the distribution. Allocate 4-16 GB on the slider (more is better but it eats disk space) and have it create a virtual hard disk with 80 GB capacity. Under Settings choose Storage and select the Controller: IDE and on the attributes click the disk and browse to where your ISO is.

Once it’s created select it and alter some more settings. On the advanced tab on General, set it so it can copy to/from the clipboard bi-directionally. I set Display video ram to 64 MB. On the network tab, I enable Adapter 2 and set it to Bridged Adapter. This is important because we want to access our webserver from the Windows PC and you need this to allow that. Adapter 1 is usually enabled and set to NAT. This lets it access the internet.

If you want to change settings later you must stop the VM or power it down. Otherwise the settings are greyed out/read only. You are now ready to start your Linux installation. Here’s detailed installation instructions for VirtualBox.

As you install it, note your user details, specifically the admin password.

If you choose to install the server version then it will prompt you to choose what servers to install. Pick LAMP server and openSSH server. You’ll be prompted for the MySQL Admin password so write that down as well. Running SSH means we can remote in with putty and WinSCP. Putty lets you remotely login to your system and issue commands. WinSCP lets you view the filesystem, copy files. Both are great utilities and vital when working with non-desktop Linux from a Windows PC.
However a desktop Linux that you can access from Windows is a lot easier to get started with.

Desktop Version

If you’re using the desktop version then you’ll need to do a few things extra. One is I suggest you setup a shared folder. In Windows, create an empty folder, right click on it and setup sharing. I find it easiest to add Everyone and setup sharing to read/write. In VirtualBox Settings select Shared Folders and add a path. On my PC the folder is D:\shared. Now tick the Auto mount field. Also on the advanced tab of General set Shared Clipboard to Bidirectional and Drag’n’Drop as well.
When you get the Install Ubuntu or Try Ubuntu choice with the desktop version, pick install.
Finally if you try Copy and Paste you’ll probably find it doesn’t work (yet). On the VirtualBox menu click View and at the bottom you’ll see Insert Guest Additions CD Image. It’s a download from the VirtualBox web site so download it then do the insert. It will show a new icon on the launcher bar (of a CD) If you click Open it will say it’s found software, do you want to run it? Say yes and it will compile and install the additions. You must reboot Ubuntu now. On the cogwheel (top right) click Shut Down. It will pop-up a window letting you Restart or Shutdown. Click Restart.

Installing LAMP on desktop

Linux is fairly easy to use but even on Ubuntu, you will need to use the Terminal. It’s the equivalent of Windows command line but more powerful. If you right click on the screen background you’ll see a popup menu that includes Open Terminal. Click it and when it opens, you’ll see the icon on the launcher bar. Right click to lock it there and you can open it in future with a single click.

Now check if Apache is running already. From the terminal type

ps -A | grep apache2

If you see one more lines like this:

1423 ?   00:00:00 apache2

Then it is running. Otherwise you need to install Apache, MySQL, and PHP.

These are the commands to install them. Apache first.

sudo apt install apache2

If you run Firefox on your desktop Linux, and go to 127.0.0.1 you should see the Apache2 Default page. Next is MySQL.

sudo apt install mysql-server

That will ask you for the new MySQL root user password. After installation use

ps -A | grep mysqld

to confirm it’s running and you should see something like this with a different number at the start.

6294 ? 00:00:00 mysqld

Finally PHP. Install it with this:

sudo apt-get install php7.0 

Now you need to restart the webserver with this command so that it can pickup the PHP installation.

service apache2 restart

Verify that PHP works by creating a file in the web root with these commands from the terminal:

cd /var/www/html
sudo gedit phpinfo.php

An editor window will appear. Type this into it:

 

And click the save button.

Now in the browser window, go to this url: http://127.0.0.1/phpinfo.php

And you should see the PHP info screen.

Finally we want to access this url from the Windows PC.

On Linux desktop you need to install a useful utility called ifconfig. It’s like ipconfig on Windows.

Install ifconfig with this command:

sudo apt install net-tools

After it’s installed, run it with this command

ifconfig

If you remembered to set up the bridged NAT connection in the VirtualBox then ifconfig should show three interfaces. The third is lo. Ignore it.
On my PC the 2nd interface looks like this

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.122  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::1091:bfb6:c240:58d3  prefixlen 64  scopeid 0x20
…

The inet value 192.168.0.122. That is the url to enter into your Windows browser to access the webserver.

192.168.0.122/phpinfo.php

That gives me the same PHP screen as I saw from Linux.

Conclusion

So we’ve setup LAMP on desktop and can access the webserver from Windows.
However we can’t upload files to it. WinSCP lets us browse to the web root (at /var/www/html) but the folder and files are owned by root. In a follow up article I’ll show you how to change permissions so you can edit files or create new files in the web root.

Post a Comment

Your email address will not be published.