DEV Community

Cover image for Setting Up a LAMP Server on a Vagrant Machine
UntitledOne
UntitledOne

Posted on • Updated on • Originally published at untitledone.io

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. 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/, 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, As a workaround, you can go to 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

Next, we'll need to install Vagrant for MacOS.

In this guide, we'll be using ubuntu/xenial64 you can find other Vagrant Boxes from their catalog here.

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

cd ~/Desktop
mkdir our_vagrant_machine
Enter fullscreen mode Exit fullscreen mode

Let's now set up a Vagrant Machine within this directory.

cd ~/Desktop/our_vagrant_machine
vagrant init ubuntu/xenial64
Enter fullscreen mode Exit fullscreen mode

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

vagrant up
vagrant ssh
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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

config.vm.network "forwarded_port", guest: 80, host: 8080
Enter fullscreen mode Exit fullscreen mode

and the second one is

config.vm.network "private_network", ip: "192.168.33.10"
Enter fullscreen mode Exit fullscreen mode

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.

vagrant reload
vagrant ssh
sudo /etc/init.d/apache2 start
Enter fullscreen mode Exit fullscreen mode

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 echo "Yo world"; ?>
Enter fullscreen mode Exit fullscreen mode

Lets go back to our Vagrantfile in our text editor and uncomment

config.vm.synced_folder "../data", "/vagrant_data"
Enter fullscreen mode Exit fullscreen mode

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.

config.vm.synced_folder "../vagrant_site/", "/var/www/html/"
Enter fullscreen mode Exit fullscreen mode

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.

sudo mysql
Enter fullscreen mode Exit fullscreen mode

Create a new user and grant all priveledges

    CREATE USER 'ubuntu';
    GRANT ALL PRIVILEGES ON *.* TO ubuntu;`
Enter fullscreen mode Exit fullscreen mode

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.

cd /etc/apache2/sites-available/
Enter fullscreen mode Exit fullscreen mode

Create the new conf file.

sudo vim hello.world.conf
Enter fullscreen mode Exit fullscreen mode

and add the following to the new file.

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName hello.world
  DocumentRoot /var/www/html/
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Save it. ( :wq! )

Run:

sudo a2ensite hello.world.conf
sudo service apache2 reload
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
scrolltumi profile image
Tumiso Marebane

THANK YOU!!!! ...

This saved me a lot of pain!