Setting up a Linux Web Development Environment in Windows

There are plenty of good reasons why you might want a local Linux based web development environment even if you normally use Windows for your day to day computing. Most web servers on the internet run Linux, and as a web developer trying to deploy a web site or an application you’ll probably find it hard to avoid working with it. So it’s certainly a good idea if you’re familiar with it, and the best way to be familiar with Linux is to use it throughout your development process.

Perhaps you already have a Virtual Private Server online running Linux. If you use it as your production environment it’s often a good idea to have a separate development environment that matches it. Even if you use it as a development environment you might get into situations when a local environment could come in handy.

Luckily, getting a Linux development environment set up on Windows is pretty easy, and we’ll show you how. We’ll install Ubuntu Server in a virtual machine, set up the web server with virtual hosts, and allow you to access them through your browser. We’ll also set up the SSH service so you can use your preferred FTP tools to transfer and edit files on your local server just as if it was a real online server.

Install Ubuntu Server in a Virtual Machine

You will need to download two things: VirtualBox and the Ubuntu Server disk image. You can get VirtualBox from its web site, and Ubuntu Server is available as a torrent among alternative downloads on the Ubuntu web site. Ubuntu Server doesn’t come with all the fancy graphical user interface stuff so it is a bit slimmer. Since we’re basically duplicating a typical server setup locally we wont need its graphical desktop environment.

Once you’ve got both VirtualBox and Ubuntu Server downloaded follow these steps to get your local Ubuntu Server running in a VM.

Creating a new Virtual Machine with a Virtual Disk:

1. Launch VirtualBox and click New in the toolbar.

2. Give it any name you like, select “Linux” as the Type, and leave “Ubuntu (64 bit)” as the Version (assuming you got the 64bit version, otherwise change to fit). Then click next.

3. Choose memory size you wish to dedicate to your new VM. Leaving it at the default recommended value should be just fine. Click next.

4. Leave the “Create a virtual hard drive now” option selected, or select it if it’s not, and then click Create to start the virtual disk creaton wizard.

5. Choose the disk file type you want. Unless you have specific insights or preference for any of the other options you can just leave it at VDI (Virtual Disk Image). Click next.

6. Choose whether you want the virtual disk size fixed or growing as it’s used. Leaving it Dynamically allocated should be good. Otherwise set a reasonable fixed size taking into account that it needs to accommodate Ubuntu Server and all of the software and files you’re gonna put there. Anything beyond 5GB should be ok. Click next.

7. Now you can name your virtual disk and also select where you want it saved on your hard drive if you want it outside the default location used by VirtualBox. To choose a different location just click the yellow folder icon.

8. Set the size of the virtual hard drive, which is basically the maximum size it can reach. You can probably just leave it as is and click Create, which will exit the wizard and create the new Ubuntu Server VM visible on the left pane.

Set up the new VM

Now that we’ve got a VM we need to set it up with our Ubuntu Server disk image in the virtual CD drive, and edit a few of the other useful settings.

1. Make sure your new VM is selected and click Settings, then navigate to Storage, and click the CD icon with a green plus on it to add a new IDE controller for your Ubuntu Server disk image. It will ask whether you want to choose a disk to put in that virtual drive. Click “Choose disk”, navigate to your newly downloaded Ubuntu Server disk image, and click Open.

2. Set up networking for the VM, which is important if you want to be able to access your server from your browser in Windows. In the Settings dialog go to Network, then change the “Attached to” setting to “Bridged Adapter”. In the drop down below it select your network interface. You probably have only one (your internal network card) so it will be already preselected.

3. If you want to have a shared clipboard capacity between your host machine (your Windows environment) and your new VM go to General, then Advanced tab, and set this option to “bidirectional”, which will make copy-pasting work both ways.

4. If you want to increase CPU resources available to your new VM, go to System, then Processor tab, and adjust the slider to fit the number of CPU cores you want to enable for it. You can also adjust the “execution cap” to prevent your VM from being able to use all your CPU time.

5. If you want to have a Shared Folder between your host and VM, where you can put files to make them immediately available within your virtualized system, go to “Shared Folders”, click the blue folder add icon, select the folder you want to share by clicking on the dropdown and choosing Other, and check the “Auto-mount” option, and optionally Read-only to prevent the virtualized host from editing files in that folder. Once done click OK, and then OK again, and you’re done with setting up your VM.

Install Ubuntu Server in the VM

Finally, let’s get Ubuntu Server running in our VM.

1. Make sure your VM is selected in the left pane and click “Start” from the toolbar. It should boot your Ubuntu Server disk image and display a menu where you can choose to Install. The installation process is pretty standard fare for Ubuntu, and pretty simple, but we’ll get through it. Navigate it with arrow keys on your keyboard, and the enter or return key to select.

2. First you’ll choose your preferred language, then your country.

3. It will then ask for a base locale. You should probably leave it as is unless you’re in one of the other named regions. Then a dialog will ask you if you’d like it to detect your keyboard layout or let you choose from a list. If you choose to detect, you’ll have to start typing the keys it instructs you, and answer a question about your keyboard. Then it will tell you what it detected, and if it’s right then you can continue. If you chose to select from a list just navigate to the right one and continue.

4. Now it’s gonna go do its business; detect the (virtual) hardware, and start copying files to your virtual disk. Don’t mind it if it seems like it’s doing nothing for a while, unless it’s too long (like over 5-10 minutes). Give it some time.

5. It will at some point ask you to give your new system a hostname. You can leave it at “Ubuntu” or make it whatever you wish. This name will identify your Ubuntu on your network. Once done, press enter.

6. Now it will ask you for your full name, username, and password. Just type them in and press enter as you do so.

7. It will ask you whether to encrypt your home folder. We don’t need this here so you can just press enter to answer “No”.

8. Next it detects your time zone, probably correctly. Press enter to confirm or no to choose your own.

9. Next it will partition the disk, and offer you a choice to do it manually or a few guided options. Since this is a VM you’ll be safe simply moving on with defaults. When it asks to write the partition table to the disk select “Yes” before pressing enter, and then press enter again when it asks for a volume size. It will ask to write changes to the disk yet again, so just select “Yes” before pressing enter again. Basically this is like installing most Windows applications where you typically just keep pressing next until it’s done.

10. Eventually it will also ask for proxy information. You can probably leave this blank and continue.

11. Next it asks whether you want automatic updates. Either way would be fine so you can just continue with no automatic updates or if you prefer set it to install security updates automatically.

12. Finally it will ask you to select extra software to install to tune the system to your needs. This allows you to immediately select some of the features you might want on your development server. At the least select “OpenSSH server” and “DNS server”. The rest are up to you, but if you’re using the typical LAMP stack when developing (Linux, Apache, MySQL, PHP) you should probably select “LAMP server” as well. To select navigate to the option with arrow keys and select with the space bar. Once done press enter to continue.

13. Next steps will depend on what you selected. As a web developer you’ll probably be familiar with what it will ask you for if you selected the LAMP stack, like the password for the MySQL “root” user (if any).

14. Finally it will ask to install the bootloader. Just press enter to say Yes, and once it does it press enter again to finish the installation. It will reboot into the new system and let you log in with the username and password you specified earlier.

If you’ve opted to install the “LAMP server” among the previous steps you now already have Apache, MySQL, and PHP installed and running. They were started automatically at boot, and the server is already serving a web page on the VM’s IP address.

Remember when we set up networking for the VM by switching it to the Bridged Adapter mode? This allowed the virtual machine to use your network card directly, which gave it its own IP address on your local network.

If you run the ifconfig command on your Ubuntu Server virtual machine now you’ll see its IP address in the eth0 section after “inet addr:”. It will be something like 192.168.5.14.

If you type this IP address in your web browser on Windows you’ll see the default Apache page served up, confirming that your server is working. The default web directory or HTTP directory is at /var/www/html/ where this index.html page resides.

This may in fact be all that you need to get started developing something or testing sans installing any specific software you might need like ruby, python, node.js, or whatever else you typically use in your development. We won’t cover the installation of any extra packages, but a lot of them are available with a simple sudo apt-get install package command (replacing “package” with, say, ruby).

Connecting with SSH

Since we’ve selected to install the OpenSSH server during the installation process you’ve already got it running, and you can already log in to it through the above IP address. While you can certainly simply run all the commands directly in your VirtualBox window using an SSH client like Putty gives you some extra flexibility and convenience like more easily running multiple sessions, setting up colors and fonts, resizing the window as you see fit without scaling, and so on.

So if you’d like to connect to your new server via SSH get a client program for Windows like putty, or the Secure Shell Chrome app, launch it, and add your virtual machine’s IP address as the host. When you connect you can log in with your Ubuntu Server’s username and password.

Setting up Virtual Hosts

We’ll go a bit further though, and just quickly show you how to set up virtual hosts so you can assign different host names, acting similarly to domain names, to different web directories. This can be useful if you’d like to work on multiple projects at the same time, assigning each their own hostname.

There is a lot of tutorials on setting up Apache virtual hosts out there so we wont go into much detail.

First of all we need to create the directories for each virtual host. Let’s call them nuxi1 and nuxi2 to keep things simple. You can pick your own as you please of course. Run the following to create them in /var/www/

sudomkdir-p/var/www/nuxi1/html
sudomkdir-p/var/www/nuxi2/html

Change permissions to the current user so you can edit them with the following:

If all checks out exit and save changes by pressing Ctrl-X and then enter to save.Then do the same for the second host, in this example nuxi2, just replacing the names.

Once that’s done we need to load up the new virtual hosts for which we can use the a2ensite tool provided by the Apache package:

sudo a2ensite nuxi1.conf
sudo a2ensite nuxi2.conf

Then restart Apache with:

service apache2 reload

or

/etc/init.d/apache2 restart

This should activate the new virtual hosts on your server, but right now you’re still not able to access them with these names from Windows. To do that we need to modify the hosts configuration file on Windows. It’s pretty simple:

1. Run notepad (or any other text editor you have) as administrator. Find it in the Start menu or Start screen, right click, and choose “Run as administrator”. When it asks for confirmation click Yes.

2. Go to File > Open (or press Ctrl-o), change the drop down menu next to the “File name” field from “Text Documents (*.txt)” to “All Files”, and navigate to C:\Windows\System32\drivers\etc (you can just copy this path to the file dialog’s address bar and press enter). Then double click on the “hosts” file.

3. Add the following lines to it, replacing the IP address with your own (the one we discussed previously), and nuxi1/nuxi2 with your own names:

192.168.5.14 nuxi1.local
192.168.5.14 nuxi2.local

4. Save the file with Ctrl-S or File > Save.

Now when you go to http://nuxi1.local, for example, you should see a page from your virtual machine’s Apache web server even if you’ve got no web site there. You’ll know it works if you see something like this at the bottom:

“Apache/2.4.7 (Ubuntu) Server at nuxi1.local Port 80”

Note that you might have to manually type in http:// before the host domain to avoid the browser sending the name as a search to Google or your selected search engine, the way it does in Chrome. When you type http:// it knows you want to access a web server, not search the web for nuxi1.local.

If all checks out you’ve got yourself a couple of local domains to use for your development and testing tasks.

Setting up a Permanent Local IP Address

You might notice that devices and computers on your local network don’t always get the same local IP address. For example, if you’ve ever checked, your smartphone connected to your wireless network may sometimes be at 192.168.5.11 and next time at 192.168.5.13. Different IPs will automatically be assigned to anyone else who connects to your WiFi.

Since your new virtual machine acts as if it was a real machine on your local network, thanks to VirtualBox Bridged Adapter networking mode, this behavior will apply to it as well.

Luckily there’s a relatively easy way to solve this. You just need to assign a custom local IP address to your virtual machine and make it the one that is outside the IP range from which your router randomly assigns IP addresses. If it is outside of this range then its IP address will always remain the same.

To set this up you’ll need to log in to your router. You’ve done this before if you’ve ever changed your WiFi password or setup port forwarding. Your router typically resides at addresses like 192.168.1.1 or 192.168.5.1. You just need your router password. If you haven’t set it it’s the default, which you can check from its manufacturer or elsewhere on the web.

Different routers have different web interfaces so we’ll cover a generalized way and hope you can find your way around.

First you need to find out the IP range within which your router assigns the IP addresses to your local devices. You can probably find this in an area called something like “Local Area Network (LAN) Setup”. If you see settings for enabling or disabling the DHCP server (don’t disable!) and settings like “Start IP Address”, and “End IP Address” that’s what you’re looking for!

For example, your start IP address could be 192.168.5.10, and your end IP address could be 192.168.5.99. So if you set your virtual machine’s IP address to 192.168.5.555 it will always have that address.

You can probably assign your custom IP to your virtual machine in the same section of settings where you see this IP range, that is, within your LAN settings. Depending on your router you could do this by either assigning the custom static IP address to a hostname or the MAC address of the virtual machine.

If it’s the hostname it will be the same hostname you selected during Ubuntu Server installation, so if you left it at default it will be simply “ubuntu”.

If it’s the MAC address you can find it under your VirtualBox settings, the Network section. Expand the “Advanced” area and you’ll find the MAC address. It’s displayed in a format that’s just a solid string of numbers and letters, but your router may require a format like 00:00:00:00:00:00. No problem, just add the colon between every two characters.

Note that if you change your virtual machine’s IP address you’ll have to change your Windows hosts file to fit, as shown below. Just change the IP address for each of the virtual host names. You might also have to power off and power on the virtual machine so it reconnects with the new IP.

We hope that as a web developer you might have at least sufficient understanding of your router settings to find this section helpful.

Connecting With an FTP Client

Since you have an SSH server running on your virtual machine you can use SFTP (FTP over SSH) to manage your files on it using most FTP clients. WinSCP and FileZilla are good examples.

Connecting is then pretty standard fare. Use the IP address of your virtual machine as the host, and your Ubuntu username and password. When saving this connection you may also want to set the default path for it to enter to /var/www so you can more quickly navigate to the files you’re working on.

With all of this set up you’re pretty much ready to start using your new local Linux web development server for developing some cool projects.