Setting Up a LAMP Server on a Vagrant Machine
Tags: vagrant, lamp, php, server
Published: 2019-04-28 22:10:55

---
title: Setting Up a LAMP Server on a Vagrant Machine
published: true
description: Guide to spinning up a vagrant machine running LAMP
tags: Vagrant, LAMP, PHP, Server
cover_image: https://untitledone.io/public/img/blog/lamp-vagrant/header.jpg
canonical_url: https://untitledone.io/blog/setting-up-a-lamp-server-on-a-vagrant-machine
---


When I first started developing with PHP, I used XAMPP on a Windows machine. A few years later, I landed my first job and moved on to using a Mac. Using XAMPP on a Mac doesn't feel right, so I found Vagrant. Since then all new developers at my company are given a MacBook with a configured Vagrant machine, I always have to reference my notes when setting their machines up and so I thought I could share these notes to the world as a guide to setting up LAMP on a Vagrant machine.

In this guide, we will be using Mac OSX, Vagrant, Virtual Box, Ubuntu, and [Sequel Pro](https://www.sequelpro.com/). On our Vagrant machines, we'll be installing, Apache, PHP 7, MySQL & PDO. We'll also be configuring, a Virtual Host and an SSH connection.


_Before writing this I was not aware of [https://box.scotch.io/](https://box.scotch.io/), this may fit your needs and make setup faster._


## Installation

> As of writing this, VirtualBox's SSL does not seem to be configured correctly and is not accessible by the link below. [Reddit Post about it](https://www.reddit.com/r/virtualbox/comments/bhu0w6/why_cant_i_download_virtualbox/), As a workaround, you can go to [https://download.oracle.com/virtualbox/6.0.6/](https://download.oracle.com/virtualbox/6.0.6/) click `VirtualBox-6.0.6-130049-OSX.dmg` it'll then bring you to a page that says "An unauthorized request." just remove the `https://` from the URL and it should start to download. 

First, we'll need to install [VirtualBox](https://www.virtualbox.org/wiki/Downloads)

Next, we'll need to install [Vagrant](https://www.vagrantup.com/) for MacOS.

In this guide, we'll be using **ubuntu/xenial64** you can find other Vagrant Boxes from their catalog [here](https://app.vagrantup.com/boxes/search). 

Now lets create a directory that will contain our Vagrant configuration files and the machine. I like to keep mine on my Desktop. 

```bash
cd ~/Desktop
mkdir our_vagrant_machine
```

Let's now set up a Vagrant Machine within this directory.
```bash
cd ~/Desktop/our_vagrant_machine
vagrant init ubuntu/xenial64
```

Once the machine is done setting up, we can turn it on and SSH inside of it.
```bash
vagrant up
vagrant ssh
```

`vagrant up` - _This command creates and configures guest machines according to your Vagrantfile._
`vagrant ssh` - _This will SSH into a running Vagrant machine and give you access to a shell._

Once you're inside we can begin installing the basics, Apache, MySQL, PHP and the PDO extension.
```shell
sudo apt-get install apache2
sudo apt-get install mysql-server # This will ask for a root password, I leave it blank. 
sudo apt-get install php
sudo apt-get install php7.0-mysql
```

## LocalHost

Now that we've gotten Apache on there we can now configure a local server to connect with our Vagrant Machine. 

>The Vagrantfile describes the type of machine required for a project, and how to configure and provision these machines.

Inside your _our_vagrant_machine/_ directory, there is a file called Vagrantfile. Open this file in a text editor, inside there are two lines we'll be uncommenting out. 

The first one is 
```ruby
config.vm.network "forwarded_port", guest: 80, host: 8080
```

and the second one is 
```ruby
config.vm.network "private_network", ip: "192.168.33.10"
```

Now let's restart our machine, go back to our terminal and exit the shell with `exit` then restart the machine, SSH back into it and restart Apache as well. 

```shell
vagrant reload
vagrant ssh
sudo /etc/init.d/apache2 start
```

In your browser, navigate to `http://192.168.33.10/` and you should see Apache's welcome page. 

This is fantastic we have our vagrant machine up and running!

## Synced Folders

But we need something other than Apaches default welcome page to display. So how do we get our own files onto the Vagrant machine? Well in our Vagrantfile there is config line called `config.vm.synced_folder`. Excellent.

Let's create a new directory on the desktop called **vagrant_site**
In that folder lets just create a basic hello world with PHP

`index.php`
```php
<?php echo "Yo world"; ?>
```

Lets go back to our Vagrantfile in our text editor and uncomment 
```ruby
config.vm.synced_folder "../data", "/vagrant_data"
```

We now need to change `"../data"` to our hello world folder and `"/vagrant_data"` to the path where our files will live within the Vagrant Machine. 

```ruby
config.vm.synced_folder "../vagrant_site/", "/var/www/html/"
```

Save the changes, `vagrant reload` the machine, refresh `http://192.168.33.10/` and you should see "Yo world".

## MySQL and Sequel Pro

Now let's configure our database connection with MySQL and setup Sequel Pro as a GUI to our database. 

SSH into the machine if not already and start MySQL.

```shell
sudo mysql
```

Create a new user and grant all priveledges 

```mysql
    CREATE USER 'ubuntu';
    GRANT ALL PRIVILEGES ON *.* TO ubuntu;`
```

Cool. Now `quit` MySQL, `exit` the shell and enter the command `vagrant ssh-config`. Here you can see your configuration info for SSH'ing which you can use to login into your database from Sequel Pro. 

Open Sequel Pro, navigate to the SSH.
**Name** enter whatever you want
**MySQL Host** enter `127.0.0.1`
**username** enter `ubuntu`
**password** blank
**database** blank
**port** blank

**SSH Host** enter `127.0.0.1`
**SSH User** enter `vagrant`
**SSH Key** click the little key icon in the field, navigate to `our_vagrant_machine/.vagrant/machines/default/virtualbox/` and select `private_key`.
**SSH Port** enter `2222`

and now press **Connect** and we should now be logged into our database.


## Virtual Host

In this part we'll be setting up a Virtual Host, so instead of navigating to `http://192.168.33.10/` we can navigate to a URL of your choice. I personally like to use `hello.world` on my local machine. 

To do this we must find our `hosts` file. I typically use my **Finder**, from Finder's menu press **Go**, **Go to Folder**, type in `/etc/hosts`, and press "Go".
Now that we have found it, let's open it in a text editor and add a new line at the bottom. 

The vagrant machines private network IP + __space__ + the URL of your choice. 
`192.168.33.10 hello.world`

Save the Hosts file.

Now we need to make a conf file for our virtual host.

SSH into the Vagrant Machine once again, navigate to Apache's "sites-available" directory. 

```shell
cd /etc/apache2/sites-available/
```

Create the new conf file. 

```shell
sudo vim hello.world.conf
```

and add the following to the new file.

```apache
<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName hello.world
  DocumentRoot /var/www/html/
</VirtualHost>
```

Save it. _( `:wq!` )_

Run: 

```shell
sudo a2ensite hello.world.conf
sudo service apache2 reload
```

and there you go!

A running Vagrant Machine with Ubuntu, Apache, PHP, MySQL, a Sequel Pro connection and a Virtual Host.

I hope this was helpful this is my first post ever, so any tips, constructive criticism and/or critiquing is much appreciated. Thanks.