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.
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.dmgit'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
Let's now set up a Vagrant Machine within this directory.
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.
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.
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
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
and the second one is
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.
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!
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
Let's create a new directory on the desktop called vagrant_site
In that folder lets just create a basic hello world with PHP
<?php echo "Yo world"; ?>
Lets go back to our Vagrantfile in our text editor and uncomment
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.
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".
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.
Create a new user and grant all priveledges
CREATE USER 'ubuntu'; GRANT ALL PRIVILEGES ON *.* TO ubuntu;`
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
SSH Host enter
SSH User enter
SSH Key click the little key icon in the field, navigate to
our_vagrant_machine/.vagrant/machines/default/virtualbox/ and select
SSH Port enter
and now press Connect and we should now be logged into our database.
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.
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.
Create the new conf file.
sudo vim hello.world.conf
and add the following to the new file.
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName hello.world DocumentRoot /var/www/html/ </VirtualHost>
Save it. (
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.