Using VirtualBox to create a virtual Linux web-server on a Windows computer

Submitted by slava on 19 May 2016

My main computer for web development has Windows operating system. Sometimes I need to check how the site that I'm working on looks like when deployed on a Linux server. This can be done in multiple ways, and one of them is creating a virtual Linux machine and testing the site there.

This article will explain how you can create a virtual Linux web-server on a Windows computer with the help of VirtualBox.

First, you should download and install VirtualBox for Windows hosts.

When installed, start the program and begin creating a new virtual machine by clicking on the "New" button. Since we don't need the GUI, 1024MB of memory should be enough. Enter "ubuntu" as the name of the virtual machine and check what options VirtualBox gives you in the drop-down box that is shown below - choose "Ubuntu (64-bit)" option if it is available, or choose the 32-bit version otherwise.

Creating a new virtual machine with VirtualBox

Now download the disk image of the most recent version of Ubuntu Server (when I wrote this article it was 16.04). Depending on what Ubuntu version you selected when created the virtual machine, choose either the 64-bit image, or the 32-bit one.

In the virtual machine settings (the gear icon), go to the "Storage" tab and select the downloaded Ubuntu image for the virtual CD/DVD drive:

Выбор образа Ubuntu Server в качестве диска в VirtualBox

Start the virtual machine, choose your language and install Ubuntu Server. Leave defaults for most options, choose en_US.UTF-8 as your locale and tick checkboxes next to the "OpenSSH server", "LAMP server" and "standard system utilities" pre-configured software packages:

Selecting Ubuntu Server packages

When asked to enter the MySQL password, do not leave the field empty. If you don't enter your password there you won't be able to access the MySQL server.

Now let's configure the access to the virtual machine from your Windows computer.

Open the "Network" settings tab and make sure that "Nat" is selected in the "Attached to" drop-down box. Then expand the "Advanced" section, click the "Port forwarding" button and configure it as follows:

  1. For HTTP: TCP connections to the 8080 port on the local computer (127.0.0.1, "Host") should be forwarded to the 80 port on the virtual machine ("Guest")
  2. For SSH: TCP connections to the 22 port on the local computer (127.0.0.1) should be forwarded to the 22 port on the virtual machine

Forwarding HTTP and SSH ports from the host OS to the virtual machine

Restart the virtual machine and open http://127.0.0.1:8080/ in the browser on your Windows computer - this should display the standard "welcome" page rendered by the virtual web-server.

Also, try to connect to the virtual machine via SSH. If you, like me, use the git bash console, the command will be "ssh username@127.0.0.1" (replace "username" with the actual username that you configured for the virtual machine). Another popular SSH client is "putty" - you can use it instead of git bash.

Now edit the C:\Windows\System32\drivers\etc\hosts file (with the administator permissions) and add there a new line: "127.0.0.1 matrix" (instead of "matrix" you may use any name you like). After saving the file you will be able to access the virtual web-server from your Windows browser at this address: http://matrix:8080/

In order to not enter the password every time you establish the SSH connection to the virtual machine, you can do the following:

  1. Open the SSH console to the virtual machine and create the "~/.ssh" directory:
    mkdir ~/.ssh;
  2. Open the git bash console on your Windows computer and generate SSH keys (skip this step if you created the keys for other servers earlier):
    ssh-keygen -t rsa -b 2048
  3. In the git bash console, copy the keys to the virtual machine (replace "username" and "matrix" with the actual values):
    cat ~/.ssh/id_rsa.pub | ssh username@matrix "cat >> ~/.ssh/authorized_keys"

Copying SSH keys to the virtual machine

To copy files between the virtual machine and your local Windows computer you can use any FTP client that supports SFTP connections (for instance, FileZilla) - just create an SFTP connection to 127.0.0.1 and use the username and the password that you configured for the user on the virtual machine.

By default all files in the web directory on the virtual machine belong to the "root" user. It makes sense to change the ownership to the user that you created when installing Ubuntu (replace "username" with the actual value):

sudo adduser username www-data
sudo chown -R www-data:www-data /var/www
sudo chmod -R g+rwX /var/www

Also let's install "composer" and PHP extensions that you will likely need:

sudo apt install zip unzip php-zip composer php-gd php-imagick php-xml php-curl

Now enable mod_rewrite: 

  1. Edit the Apache config file (sudo vim /etc/apache2/apache2.conf) and replace "AllowOverride None" with "AllowOverride All" for the /var/www/ directory.
  2. Enable mod_rewrite module (sudo a2enmod rewrite)
  3. Restart Apache (sudo service apache2 restart)

Your virtual web-server is ready!

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
By submitting this form, you accept the Mollom privacy policy.