Skip to content

Instantly share code, notes, and snippets.

@tiffany-taylor
Last active February 16, 2022 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tiffany-taylor/f9ad3ffb2951e7a3098c0212f2b22164 to your computer and use it in GitHub Desktop.
Save tiffany-taylor/f9ad3ffb2951e7a3098c0212f2b22164 to your computer and use it in GitHub Desktop.

Setting up a Virtual Machine using Virtualbox and Ubuntu

Originally written to work with Patrick Louys' No Framework Tutorial for PHP.

I use Git Bash, which is installed with Git, as my SSH client, you can use whatever you prefer. Git Bash is a variant of MinGW.

Note: "head mode" means that the VM has a GUI, "headless mode" means that the VM is running, but there is no GUI, and requires a secure connection into the server, in this case by SSH.

Download/Install Virtualbox

download Virtualbox

Download/Install Ubuntu

  1. download Ubuntu
  2. create new VM inside Virtualbox
  3. assign iso to CD-ROM medium
  4. install ubuntu to VM

User Setup

In the installation process, you will be required to create a password for the root account. Make this password as complex as feasibly possible. I usually have my password manager generate a password for me, and then I keep the password stored in my password manager in case I need it in the future. You will also be prompted to create a new user account and a password for this user account. This account will not have root access by default, so you will need to add it to sudo/sudoers.

If sudo is not installed already, it will need to be installed before you can add your user account to sudo/sudoers.

Install sudo and add user account to group

  1. start server in head mode
  2. access terminal, and switch user to root su - root
  3. install sudo
    • apt-get install sudo first, check that sudo/sudoers group exists
    • cat /etc/group if the group does not exist, then create it
  4. in terminal, type usermod -a -G sudo <user account>, where <user account> is replaced with your user account you've created during installation.

Next, install SSH and vim.

Install SSH and vim

  1. install ssh via terminal
    • sudo apt-get install ssh
  2. install vim via terminal
    • sudo apt-get install vim

Port forwarding will need to be set up in Virtualbox in order to SSH into the VM from your SSH client.

Set-up port forwarding

  1. Settings -> Network -> Attached to: NAT -> Advanced -> Port Forwarding
    1. name the rule ssh

    2. host port: 3022

    3. guest port: 22

      • leave guest/host IP blank
      • this allows SSHing into the VM
    4. name the rule web server

    5. host port: 8080

    6. guest port: 80

      • leave guest/host IP blank
      • this allows accessing the guest web server by host web browser

Set-up Shared Folder

  1. create directory on Windows computer somewhere
    • e.g. C:\ubuntu-share
  2. start VM with head
  3. in the VM window, click on Devices menu, Shared Folders
  4. add shared folder from Windows computer, checkmark auto-mount and make permanent
    • if make permanent isn't checkmarked, the shared folder will have to be added again the next time the VM is booted up
  5. also in Devices menu, Insert Guest Additions CD image
    • this will install the required software on the ubuntu VM to make the shared folder work
    • if Linux gives an error: Oops! There was a problem running this software. Unable to locate the program, then add exec to /etc/fstab for /dev/sr0
  6. install software (it will be an autorun prompt)
  7. restart VM
  8. either start the VM in headless mode and SSH in, or open the VM normally and open terminal
    • to ssh into VM: ssh -p 3022 <account name>@127.0.0.1
  9. create a directory in ubuntu to mount the share folder to
    • e.g. mkdir ~/share
  10. sudo mount -t vboxsf ubuntu-share ~/share
    • ubuntu-share is the folder name from shared folders (Windows dir), ~/share is the folder location in ubuntu
  11. create a file in ubuntu-share, e.g. index.php (put <?php phpinfo(); in the file)
  12. check that it's in ~/share by ls in the folder, it should be in there

For additional information on setting up shared folders for a Windows host and a Linux guest

Install PHP7.2

  1. boot up VM in headless mode
  2. open git bash
  3. ssh into VM
    • ssh <username>@127.0.0.1 -p 3022
  4. install php
    1. sudo apt-get update
    2. sudo apt-get install software-properties-common
    3. sudo add-apt-repository ppa:ondrej/php
    4. sudo apt-get update
    5. sudo apt-get install php7.2-fpm

To use PHP's built-in web server, apache must be shutdown

(Failed to listen on 0.0.0.0:80 (reason: Address already in use))

Shutdown Apache

sudo /etc/init.d/apache2 stop

Run built-in PHP web server from shared folder

  1. Navigate to shared folder
    • e.g. cd ~/share
  2. sudo php -S 0.0.0.0:80
    • requires sudo because it'll say permission denied on port 80
  3. check that it's working in host web browser by going to: 127.0.0.1:8080

using index.php

<?php
phpinfo();

should show php's info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment