Creating your own PHP dev-env in Vagrant: Part 3

June 3, 2016

Table of Contents

Part 1

Part 2

Bonus 1

Bonus 2

Full Code Base on GitHub

In the previous post we’ve set up the apache web server successfully. Now it’s time to add MySQL and PHP to finish the LAMP stack.

Let’s start with MySQL first and add a mysql.sh file to the sh directory. We write the following lines into that file.

The first two lines set values for the upcoming installation dialog of MySQL. The installation dialog will ask us for a password and to repeat it. Because we want to setup MySQL automatically without any interaction, we preset those fields with our password of choice. In this case my_pw. Afterwards we install MySQL and an extra package for apache.

Afterwards we copy over the config file of MySQL onto the guest system.

We do it exactly the same way, as we did with the apache.conf. First we place the my.cnf in our conf directory. Afterwards we add a file provisioner to our Vagrantfile.

This time it’s important to replace a custom config file with the original, because in our modified version we switch the following line from:

to

Before it was only possible to access the database on the guest machine itself. We changed this, so we can connect from the host machine as well.

Finally we need to restart the MySQL demon.

With which we end up in our final mysql.sh file, which looks like this:

Final thing we need to do is adding the shell script as a shell provisioner to our Vagrantfile.

Now let’s install PHP as well. This is pretty straight forward. Our php.sh looks like this:

Afterwards we also add it as a provisioner

Which results in the following Vagrantfile

This is it. Our LAMP stack is complete. Now we want to check out our environment. Switch into the directory of the Vagrantfile and execute the following command

A lot of stuff is happening now and for the first time it will take some time to fire up the vagrant machine. If vagrant booted up successfully, we add an index.php in the directory above our Vagrantfile. This is our project directory. I won’t show anything big here,  just a proof of concept

If you browse to 192.168.33.10 on your host system, you should see an info page about the running PHP version, similar to this

phpinfo

Congratulations! You’ve done it! To suspend your vagrant machine just type the following in the same spot, where you did the vagrant up

This saves the state of your machine and will be the way to shut it down in daily business. If you want to completely reset your machine and install everything again, you execute

To start your vagrant machine, just do vagrant up again.

You could start from here and put your own PHP project in the parent directory of the Vagrantfile and start programming. But if you want to know some nifty stuff, like how to access your website via an address like mysite.dev instead of 192.168.33.10 or how you can access your database with the workbench software on your host machine, you should definitely check out the next posts in this series.

Go to Bonus 1