Sometimes you’ll face developments in different languages and environments and handling all of them in the same machine could be messy. So you might want to try installing those environments in virtual machines. In that way you will handle the code in your host and the guests will deal with the environment, and that gives you the advantage of coding without having the machines up.
Preliminary
- Using vim quick command reminder or you could use another editor, so wherever you see I’m using vim, replace with nano.
- i - goes into insert mode
- h, j, k, l - work as arrows
- Esc - goes back into normal mode
- :wq - writes the file and quits vim
- Every time I show a vim command and vertical points, it does mean I’m modifying at the end of the file.
- You will need to generate an ssh key pair. https://help.github.com/articles/generating-ssh-keys has a good step-by-step to get a key pair.
- You will need also to modify your
/etc/hosts
file to add the definition to your new virtual environment, just write something like this at the end$vim /etc/hosts
198.168.56.30 local.example.com
- It’s always good to have an ssh config file so you won’t need to connect using something like this
1$ ssh username@local.example.com
But simply calling this $ssh example
. To do that do as follows. If the file doesn’t exist, just create it.
1$ vim ~/.ssh/config
Host example
User username
HostName local.example.com
Virtualization
Installing the OS
Since this is an example I’ll omit advanced setup and I’ll take the easy path so do as this checklist:
- Start the virtual machine
- Select the language, location and local formats
- Select the keyboard distribution map
- Choose the
eth0
as your primary network interface. That point to the Adapter 1 which is the Bridged adapter - Assign a hostname
- Choose a User, username, and password
- Avoid encrypting your home folder
- Choose your location for clock options
- Use the Guided - use entire disk option to partitioning the disk, select the disk and accept writing the changes to disk (this will depend on your preferences and needs you can partitioning the disk as you want)
- Wait for the installer to install the base system
- Type the proxy info or left in blank if there is no proxy
- Select No automatic updates
- Choose no task to install additional services (optional, up to you)
- Wait for the installer to finish
- Accept the GRUB installation
- And restart the machine
- Login and update - upgrade the system. Then install ssh.
1$ sudo apt-get update && sudo apt-get upgrade && sudo apt-get install ssh
Remove password prompt for sudo
Having these virtual environments demands several sudo uses in order to perform admin operations. So it turns quite annoying typing the password every time you need to do something as an admin. The solution is removing the password prompt for sudo callings.
Keep in mind that this is a suggestion for virtual machines to avoid typing the password several times, IT IS NOT RECOMMENDED FOR PRODUCTION OR YOUR HOST MACHINE. Granting access as root to everything might harm your system, or if there’s someone around your unattended, logged in machine, you could be victim of malicious crackers.
Add the following line at the end of the file (replace username with the username you chose for this example). Doing this probably opened nano instead vim, so to save type ^x (Ctrl + x), and Y to confirm, then return (Enter) to save into the same file.
1$ sudo visudo
username ALL=NOPASSWD: ALL
Modify the interfaces definition
Since we chose the Adapter 1
to be our eth0 interface
and the Adapter 2
to be our eth1 interface
we need to configure the second one to act under the Host-only network
.
1$ sudo vim /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
# Host-Only Network adapter
auto eth1
iface eth1 inet static
address 192.168.56.30
netmask 255.255.255.0
Having this you can reach the server through your console using ssh. This step need a restart or getting the interface up.
At this point you still can’t reach this server using $ssh example
because the guests doesn’t know who you are. In the preliminary section you learned how to create your ssh key pair. Now we need the whole content of the public key (id_rsa.pub
or whatever you called it). That content must be added to an authorized keys file like this
1$ vim ~/.ssh/authorized_keys
# Key for my user from the host machine or any other comment
ssh-rsa SLKJDasodioaidsunoaXZxZxzxzOIUnaosiundOUnodUSAd909204398320948n2039480324nOIUNIodsnudoaiudnoauds09wu4390nu098nn09s8nd09a8dsn09a8sndajsdjkllajsldja0s098098098snd90a8sd09a8sdnalsjljOIJOjsodjas/234sdfdfsczxSDAcaCACCACACCA/asdasdascacacacacaca+sadjoij user@mail.com
Once your authorized keys file has your public key, you can reach the server using $ssh example
and from there is easier to start the virtual machine and minimize it, and just managing it through console using the ssh connection.
Installing VirtualBox Additions
Before installing the VirtualBox additions we need to install some compatibility packages as follows:
1$ sudo apt-get install dkms build-essential linux-headers-$(uname -r)
After that we mount the VBoxGuestAdditions ISO image in the virtual machine as we loaded the Ubuntu image installer. After that we need to mount it in order to run the installer script.
1$ sudo mount /dev/cdrom /mnt
2$ cd /mnt
3$ sudo ./VBoxLinuxAdditions.run
Eventually that will show an error message but the additions will be installed successfully.
Installing the web server environment
For this example I chose PHP with MySQL and Apache2 like this:
1$ sudo apt-get install apache2 php5 libapache2-mod-php5 mysql-server mysql-client php5-mysql
Getting the host folder mapped into the virtual machine
The code hosted in the owner machine must be somehow detected in the virtual machine every time the machine starts up:
1$ sudo vim /etc/fstab
example /home/mesi/example vboxsf gid=33,uid=33,auto,rw 0 0 /home/mesi/example/app /home/mesi/example/app none bind
The meaning of the content is:
- example - Indicates the name of the shared resource
- the following path - indicates where the shared resource will be mapped
- vboxsf - the type of the resource we want to start up
- parameters - the parameters tell the system how the resource must be granted
- The code then will appear in the mapped folder, and now we create a symbolic link into
/var/www
for the mapped folder:
1$ sudo ln -s /home/mesi/example/ /var/www/
Now the final step is to create a virtual host for the example site in apache:
1$ sudo mkdir /etc/apache2/sites-enabled/sites
2$ sudo vim /etc/apache2/sites-enabled/sites/example.conf
<virtualhost *:80>
# Admin email, Server Name (domain name) and any aliases
ServerAdmin amrendonsa@unal.edu.co
ServerName example.com
ServerAlias local.example.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.php
DocumentRoot /var/www/example/
# Custom log file locations
LogLevel warn
ErrorLog /home/mesi/logs/example/error.log
CustomLog /home/mesi/logs/example/access.log combined
</virtualhost>
Then it’s necessary to have the rewrite apache module enabled:
1$ sudo a2enmod rewrite
2$ sudo service apache2 restart
Then it’s a matter to follow the indications for a CakePHP installation.
I just recorded a video showing the whole process…