Sign in
Log inSign up
VirtualBox Setup for PHP Development

VirtualBox Setup for PHP Development

Emil Moe's photo
Emil Moe
·Jan 25, 2019

Introduction (disclaimer)

This is an article from my archive I never got to post anywhere. Today I'm using Docker and I will later on explain how I made that setup and why I find it more valuable. However some might prefer a VirtualBox solution rather than Docker, so I still wanted to share what I have. One reason is, at least if you are on Windows, already have environments requiring you to use VirtualBox and Linux, Docker simply won't work.

A few sections are reference links as my take would more or less just be a copy paste.

Here is it in it's original form, though I will recommend you to switch to Ubuntu as it gives access to PHP7.x or consider install a project such as Dotdeb to achieve this with Debian.

The setup is based 100% on free accessible software. It was written about 2 years ago.

Requirements

This setup require a bit of software and a couple of hours first time you do it. I hope this article will help you save some time that I had to spend researching the internet for information. The good thing about the setup is that it's 100% free.

What you need can be broken down into 2 categories. The first one is the software for your host system:

For the virtual system I will be using this, but you might have other choices:

  • Apache2
  • PHP5
  • MariaDB
  • NodeJS
  • Composer
  • Sudo
  • SSH
  • Git
Terminal

In order to follow this you must have a SSH Terminal client installed. If you use Linux or Mac OS X, then you can use the built in terminal. On Windows you can either install Git and get Git Bash or download Putty.

Installing VirtualBox

VirtualBox itself should be a painless process, after it is installed create a new virtual machine and call it something like Debian. Give it at least 2GB RAM and 8GB HDD with dynamic space, the rest can be left to default.

VirtualBox Settings

In your VirtualBox Preference pane select the Network tab and Host-only Networks sub tab. Add a new one and double click it. At the DHCP Server tab, unmark Enable Server as we are not going to use this DHCP server.

VM Settings

Select your newly created Virtual Machine (VM) and click Settings tab in the VirtualBox window. At the Network tab leave Adapter 1 to NAT, that will ensure your VM has access to the internet. At Adapter 2 enable it and select Host-only Adapter and name select the one you created just before.

Click at the Shared Folders tab and add your local folder where you store your websites in. Name the share web.

Installing Debian

Now boot up your VM by double clicking it or using the green arrow. VirtualBox will ask you to locate the Debian image file you downloaded according to the requirements.

In the graphical userinterface (GUI) choose Install, we won't take the Graphical Install since it is a server and don't want any graphical interfaces to eat up the memory.

Language

The language you choose is 100% up to you, but I recommend english since it's easier to follow this guide and if you encounter errors later, you will get the error messages in english which are easier to google for help.

Location

Location should almost always be chosen according to where you are physical located, but in some situations you might choose a neighbouring country for different reasons. Except if you have fast internet it doesn't really change a lot, but try to choose something that is in your geographical region.

Locales

Try to choose something that is common to your products locales. If your applications are mainly in danish you should choose Denmark, if they are multilangual United States (en_us.UTF-8) is sufficient. I always take en_us.UTF-8. In any case I recommend you to choose something with UTF-8.

Keymap

The physical design of your keyboard.

Hostname

You can leave this to debian or choose something else if you want a fancy name for you VM.

Domain name

Just leave it blank.

99% don't need this, it's only used for more complex large networks.

Root password

Take whatever you want, I don't think there are restrictions. Passwords here shouldn't be too complicated as you need them during development workflow, but don't leave it blank in rare case of hacking - although all your files will be accessible outside the VM.

User account

Whatever name you like and same thoughts about password as for the root.

Partition

Use the Guided - use entire disk. It won't use your whole host system, only those dynamic 8GB we dedicated later.

Next option should be SCSI3 [..]

For simplicity choose All files in one partition

And finally Finish partitioning and write changes to disk and click Yes

Package manager

The same as location, choose one physical close to you.

Leave the HTTP proxy empty.

Configuring popularity-contest

Up to you, but I prefer to opt-out: No

Software selection

Uncheck everything, I will guide you through installation of everything you need later.

Tip: To uncheck use arrow up and arrow down and space to toggle, click tab to Continue and enter when you are done

Install the GRUB boot lader

Select Yes followed by the non-manually option, something like /dev/sdva..

Finish the installation

Click Continue and you are done. The system will now reboot, but power it off in VirtualBox as we are goin to configure the new VM first.

Installing VM software

Configure the VM network

I have chosen to add this section in this chapter if you happened to skip the installation chapter.

Before we start the installation you need to make some configurations in order for you later to connect to the VM through secure shell (SSH).

Issue the following command on your VM

ifconfig eth1 192.168.56.101 netmask 255.255.255.0 up

This will bind your second adapter to the IP 192.168.56.101 which will be the IP you connect to with SSH. You can optionally add a naming prefix in your hosts file so you don't have to remember the IP. See more on Wikipedia)

To make this binding happen everytime you reboot your VM, edit the interface configurations.

nano /etc/network/interfaces

Add this to the bottom of the configuration.

# The host-only network interface
auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255

Thanks to Christopher Maier

Install SSH

First install the secure shell, so that you can access the VM from Git Bash or Putty on Windows or your Terminal on Linux or macOS. From there you can copy paste commands, so you don't have to re-type every line.

apt-get install ssh

Sudo is used to perform root commands from a regular users account who has been granted permissions. So when you want to install new things, you will use the sudo command prefixed, also when you install node modules (NPM) you will have to use sudo. You can think of it as UAC in Windows or on your Mac when it asks for your password whenever you want to install new software.

To install sudo run

apt-get install sudo

Now let's add your user as authorized to perform all commands. First open the config and then make the changes, replace USERNAME with the username you chose during the installation (with the same capital letters you used). I recommend you to add it on the line after root ALL=(ALL:ALL) ALL to keep order in the config file.

nano /etc/sudoers
USERNAME    ALL=(ALL:ALL) USERNAME

When you issue a sudo command you might be prompted for a password, that is the password for your user account. You will barely have to use the root password anymore.

Now log out of your VM and switch to your prefered terminal (Putty, Git Bash or Terminal) and log in with this command. Replace USERNAME with the one you chose.

ssh USERNAME@192.168.56.101

You might optionally use the hostname you may have set in your hosts. If you for example chose debian as hostname and foo as username, then you can log in with ssh foo@debian.

Install packages for development

My choice of database is MariaDB which is almost identical to MySQL but the license. The difference is:

MariaDB is a community-developed branch of the MySQL database, the impetus being the community maintenance of its free status under GPL, as opposed to any uncertainty of MySQL license status under its current ownership by Oracle

Source: Wikipedia

After installation you won't experience any differences as drivers for MySQL works for MariaDB as well.

First add the repository for MariaDB with these 5 commands

sudo apt-get install software-properties-common
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
sudo add-apt-repository 'deb [arch=amd64,i386] kartolo.sby.datautama.net.id/mariadb/repo/… jessie main'
sudo apt-get update

Next install the software

sudo apt-get install mariadb-server apache2 php5 curl php5-cli php5-mysql php5-curl git
NodeJS and PHP Compose

Most web components these days are distributed with NodeJS' package manager these days (NPM). Especially if you are going to work with Laravel you will need this. You will also need the PHP package manager called Composer in order to work with Laravel.

To do so, first install NodeJS including NPM with these 3 commands.

curl -sL deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs

And then install Composer. But since the actual command for installing Composer varies you will have to go to getcomposer.org/download yourself and copy paste the code from the box on the site to your VM.

You can now move the Composer file to your binary folder, in order to have Composer as a command.

sudo mv composer.phar /usr/local/bin/composer

If you run composer you should now see a list of commands, which tells you Composer is working as a command utility.

Generate Key Pair

Key Pairs consists of a public (~/.ssh/id_rsa.pub) and a private key (~/.ssh/id_rsa) file. The public key file is the one you will share with anyone, or any services, that requires a key authentication. The private key file must always be kept private. If you by accident share the private file, it's highly recommended to generate a new key pair.

In your terminal on your VM enter ssh-keygen te generate a unique key. You will be prompted for a password, you can either just hit enter or enter a password. If you don't enter a password, everyone will have access to where you are granted access if they get your private key file, so it's recommended to use a password.

To get the key you you forward use the command cat ~/.ssh/id_rsa.pub copy the output and paste it to your service.

Time and date

Nothing worse than struggling with incorrect times and dates. Your whole application stack from logs to websites will rely on your systems time and date, it is therefor not only important to set it but also to keep it automatically syncronised. In the following I will walk you through, how you set your timezone, how you find your nearest NTP (Network Time Protocol) server and make it syncronise.

Find and set your timezone

First you need to find your timezone. The timezones are defined as Region/City you can either list all available options, but there will be a long list

timedatectl list-timezones

or better to add some keyword to filter the output. You can either swap keyword with a close metropol or your region.

timedatectl list-timezones | grep -i keyword

Once you have found your desired location make your system use that

sudo timedatectl set-timezone 'Asia/Bangkok'
Automatic syncronised

To syncronise your date and time, you need to install the NTP client

sudo apt-get install ntp

Then find a nearby NTP pool server at The NTP Pool Project. Once you have found it open the configurations file and replace the 4 lines with servers (Server 0 to 3).

sudo nano /etc/ntp.conf

If you are located in Asia you could replace with this

server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 3.asia.pool.ntp.org

Restart the NTP service and check that all configurations went correct

sudo timedatectl set-ntp on
sudo systemctl enable ntp
timedatectl

You should get an output that looks similar to this

     Local time: Fri 2016-06-17 12:37:47 ICT
  Universal time: Fri 2016-06-17 05:37:47 UTC
        RTC time: Tue 2016-06-07 22:50:28
       Time zone: Asia/Bangkok (ICT, +0700)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

What important here is that your Local time and Time zone are correct and NTP enabled and NTP synchronized are yes.

Mount share

turnkeylinux.org/docs/virtualbox-guest-addons

javarevisited.blogspot.com/2015/12/sbinmou…

Permissions

Permissions for host and VM

ubuntuforums.org/showthread.php?t=1398340

sudo mount -t vboxsf -o uid=1000,gid=1000 web /var/www

VBoxManage setextradata Debian VBoxInternal2/SharedFoldersEnableSymlinksCreate/web 1

ahtik.com/blog/fixing-your-virtualbox-shar…

Configure Apache

Dynamic vhost?

pixafy.com/blog/2012/12/dynamic-virtual-ho…

Run at boot or from shortcut

If you want to start your VM whenever you boot your system, you can create a script that will execute it headless, meaning that no visible windows will be shown. You can always open it from VirtualBox or just use SSH to access your VM.

The command to start your VM headless is

vboxmanage startvm {"VMNAME" } --type headless

Where you replace VMNAME with the name you gave your VM, for example Debian. It is the name that is in the VM list in VirtualBox.

Save this to a .sh file on Mac OS X and Linux or .bat file if you are on Windows. You can create the file with the terminal while on host OS.

To create it on Windows

echo vboxmanage startvm {"VMNAME" } --type headless > ~\startvm.bat

To create it on Mac OS X and Linux

echo vboxmanage startvm {"VMNAME" } --type headless > ~/startvm.sh

Depending on your system you can set that file to execute on boot.

Thanks to VirtualBox.org