How to Create a Local Dev Server with Vagrant

Now you can start developing like a pro with your own local hosting environment. You may have heard about how you can develop sites locally with your own Apache, MySQL and PHP server. It’s never been easier to install this software and have it running on your computer in a matter of seconds. In this article, we’re going to show you how you can use two applications (Vagrant and VirtualBox) to create unlimited virtual hosting environments in your own computer as a way of testing a website before deploying it to your server.

The local hosting environment is not meant to be accessible through the web—only on your “local” computer.

Before publishing a site to the web, you’ll want to test it out first in a real server environment. Here’s how you can do it:

  • Create a virtual “box”
  • Build your website in the box
  • Test the website in your favorite browser
  • Deploy your site to your hosting account

We’ll show you how to complete each of these steps below.

Why do this?

Local hosting is an important part of the web development process. Before you publish a website for the world, you’ll want to work on it privately until it’s perfect. This is how developers work on sites, and you can do the same thing, whether you’re already a developer or just learning how to code a site for the first time.

First, Install Vagrant and VirtualBox

All you’ll need to get started is your own copy of two free programs: Vagrant and VirtualBox. If you’re using Mac, Windows, or GNU/Linux, these programs are free, and you can install them the way you normally install any other application. Do this first and then you can continue with the rest of this tutorial.

Finding a “Box”

While learning how to use Vagrant, you’ll see the term “Box” used frequently. Box is just another word for virtual computer. We’re basically going to be installing a computer within your computer, complete with its own operating system. Which “Box” you’ll require for your project is up to you. But there exists a wide variety of pre-configured boxes out on the web that you can install instantly.

For our needs of creating a website, we recommend you use Scotch Box, because it comes pre-installed with a fully functioning “LAMP” stack: Linux (Ubuntu OS), Apache, MySQL and PHP (all the software you need for creating a website).

How to “Provision” a New Virtual Box with Vagrant

In order to complete these steps, you’ll need to open a command line session on your computer. Your default terminal app will be sufficient. We’re going to be downloading the Scotch Box from a Github repository and creating a new directory for it. Having a directory name in my mind will help. For our example, we’ll use “new-website”, but you can make it whatever you want. This “folder” will contain your website source files.

  1. Open a new terminal session
  2. Download Scotch Box from its Github repository:
    git clone https://github.com/scotch-io/scotch-box new-website
  3. Change directories into the “new-website” directory:
    cd new-website
  4. Launch (or, “provision”) your new box with Vagrant:
    vagrant up

Give the provisioning process a few moments to complete, and your new virtual server will be ready. Now, if you go the following IP address, you’ll see a Scotch Box page: http://192.168.33.10/.

This page is the default starter page located in the “public” directory (your document root) instead the “new-website” directory. Replace this page with your own content, and you’re ready to go.

How to “Deploy” Your New Site

There are as many deployment schemes as there are developers who use them. The method you choose will ultimately depend on your expertise and comfort level. Here are a few of the most common methods:

Transfer the Files with FTP

FTP (File Transfer Protocol) is one of the easiest ways to get your files from your local server to your InMotion Hosting server. We have several guides that can help refresh you on FTP setup and usage:

Using the above guides will help you determine if FTP is the right deployment method for you.

Use Git Version Control

Using Git is slightly more advanced, but it can save you some time as it becomes both your version control system and deployment method all wrapped in one. Check out these guides if you’re interested in learning how to use Git for your project:

You may find that while Git requires some learning the investment will pay dividends over the long run.

In order to use Git most effectively, you will need SSH access to your server with the cPanel user that owns the website, using the correct steps for your hosting plan: shared or VPS/Dedicated.

Import Your Database (if you’re using one)

If your site uses a database, you can import it into your server’s MySQL system by creating a new database and importing content into it. These guides will help you with that:

Remember, if you’re not using a database, these steps are not necessary. You can more easily manage static files with the previous two deployment methods.

Leave a Reply