In this example, we're going to create a fully functional LAMP environment with vagrant. The apache web server will also be accessible from a browser of the host machine. We'll also have phpMyAdmin installed and accessible from the host machine.


Configuration


I'm assuming that you already have installed vagrant and Oracle VM software. I also assume that the ubuntu/trusty64 box is already added to your filesystem with vagrant box add ubuntu/trusty64 command. If you're not sure, you can confirm it with ls -l ~/.vagrant.d/boxes/ command.


Create a new project folder


$ mkdir ubuntu-trusty-lamp
$ cd ubuntu-trusty-lamp/

Create a new shared folder


Do it when you're in ubuntu-trusty-lamp folder.


$ mkdir public

Create a bootstrap file


Do it when you're in ubuntu-trusty-lamp folder. Name the file as bootstrap.sh.


#!/usr/bin/env bash

# BEGIN ########################################################################
echo -e "-- ------------------ --\n"
echo -e "-- BEGIN BOOTSTRAPING --\n"
echo -e "-- ------------------ --\n"

# VARIABLES ####################################################################
echo -e "-- Setting global variables\n"
APACHE_CONFIG=/etc/apache2/apache2.conf
PHP_INI=/etc/php5/apache2/php.ini
SITES_ENABLED=/etc/apache2/sites-enabled
PHPMYADMIN_CONFIG=/etc/phpmyadmin/config-db.php
DOCUMENT_ROOT=/var/www/html
APPLICATION_HOST=localhost
VIRTUAL_HOST=localhost
MYSQL_DATABASE=lamp
MYSQL_USER=root
MYSQL_PASSWORD=root

# BOX ##########################################################################
echo -e "-- Updating packages list\n"
apt-get update -y -qq

# APACHE #######################################################################
echo -e "-- Installing Apache web server\n"
apt-get install -y apache2 > /dev/null 2>&1

echo -e "-- Adding ServerName to Apache config\n"
grep -q "ServerName ${VIRTUAL_HOST}" "${APACHE_CONFIG}" || echo "ServerName ${VIRTUAL_HOST}" >> "${APACHE_CONFIG}"

echo -e "-- Allowing Apache override to all\n"
sed -i "s/AllowOverride None/AllowOverride All/g" ${APACHE_CONFIG}

echo -e "-- Updating vhost file\n"
cat > ${SITES_ENABLED}/000-default.conf <<EOF
<VirtualHost *:80>
ServerName ${VIRTUAL_HOST}
DocumentRoot ${DOCUMENT_ROOT}

<Directory ${DOCUMENT_ROOT}>
Options Indexes FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/${VIRTUAL_HOST}-error.log
CustomLog ${APACHE_LOG_DIR}/${VIRTUAL_HOST}-access.log combined
</VirtualHost>
EOF

echo -e "-- Restarting Apache web server\n"
service apache2 restart

# MYSQL ########################################################################
echo -e "-- Installing MySQL server\n"
debconf-set-selections <<< "mysql-server mysql-server/root_password password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password ${MYSQL_PASSWORD}"

echo -e "-- Installing MySQL packages\n"
apt-get install -y mysql-server > /dev/null 2>&1
apt-get install -y libapache2-mod-auth-mysql > /dev/null 2>&1
apt-get install -y php5-mysql > /dev/null 2>&1

echo -e "-- Setting up a dummy MySQL database\n"
mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} -h ${APPLICATION_HOST} -e "CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE}"

# PHPMYADMIN ###################################################################
echo -e "-- Installing phpMyAdmin GUI\n"
debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true"
debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/admin-pass password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/app-pass password ${MYSQL_PASSWORD}"
debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2"

echo -e "-- Installing phpMyAdmin package\n"
apt-get install -y phpmyadmin > /dev/null 2>&1

echo -e "-- Setting up phpMyAdmin GUI login user\n"
sed -i "s/dbuser='phpmyadmin'/dbuser='${MYSQL_USER}'/g" ${PHPMYADMIN_CONFIG}

echo -e "-- Restarting Apache web server\n"
service apache2 restart

# PHP ##########################################################################
echo -e "-- Fetching PHP 5.6 repository\n"
add-apt-repository -y ppa:ondrej/php5-5.6 > /dev/null 2>&1

echo -e "-- Updating packages list\n"
apt-get update -y -qq

echo -e "-- Installing PHP modules\n"
apt-get install -y python-software-properties > /dev/null 2>&1
apt-get install -y libapache2-mod-php5 > /dev/null 2>&1
apt-get install -y php5 > /dev/null 2>&1
apt-get install -y php5-cli > /dev/null 2>&1
apt-get install -y php5-mcrypt > /dev/null 2>&1

echo -e "-- Enabling PHP mcrypt module\n"
php5enmod mcrypt

echo -e "-- Turning PHP error reporting on\n"
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" ${PHP_INI}
sed -i "s/display_errors = .*/display_errors = On/" ${PHP_INI}

# TEST #########################################################################
echo -e "-- Creating a dummy index.html file\n"
cat > ${DOCUMENT_ROOT}/index.html <<EOD
<html>
<head>
<title>${HOSTNAME}</title>
</head>
<body>
<h1>${HOSTNAME}</h1>
<p>This is the landing page for <b>${HOSTNAME}</b>.</p>
</body>
</html>
EOD

echo -e "-- Creating a dummy index.php file\n"
cat > ${DOCUMENT_ROOT}/index.php <<EOD
<?php
phpinfo();
EOD

# END ##########################################################################
echo -e "-- ---------------- --"
echo -e "-- END BOOTSTRAPING --"
echo -e "-- ---------------- --"

Create vagrant file


Do it when you're in ubuntu-trusty-lamp folder. Name the file as Vagrantfile.


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

config.vm.box = "ubuntu/trusty64"

config.vm.define :lamp do |lamp_config|
lamp_config.vm.hostname = 'lamp'
lamp_config.vm.network "private_network", ip: "192.168.50.10"
lamp_config.vm.provision :shell, path: "bootstrap.sh"
end

config.vm.provider :virtualbox do |vb_config|
vb_config.name = "Vagrant - Ubuntu 14.04 LAMP"
end

config.vm.synced_folder "public", "/var/www/html"

end

Run vagrant box


If you ever change your "bootstrap.sh" file, you'll need to run vagrant reload --provision command instead.



==> lamp: ......
==> lamp: ......
==> lamp: ......
==> lamp: -- ------------------ --
==> lamp: -- BEGIN BOOTSTRAPING --
==> lamp: -- ------------------ --
==> lamp: -- Setting global variables
==> lamp: -- Updating packages list
==> lamp: -- Installing Apache web server
==> lamp: -- Adding ServerName to Apache config
==> lamp: -- Allowing Apache override to all
==> lamp: -- Updating vhost file
==> lamp: -- Restarting Apache web server
==> lamp: * Restarting web server apache2
==> lamp: ...done.
==> lamp: -- Installing MySQL server
==> lamp: -- Installing MySQL packages
==> lamp: -- Setting up a dummy MySQL database
==> lamp: -- Installing phpMyAdmin GUI
==> lamp: -- Installing phpMyAdmin package
==> lamp: -- Setting up phpMyAdmin GUI login user
==> lamp: -- Restarting Apache web server
==> lamp: * Restarting web server apache2
==> lamp: ...done.
==> lamp: -- Fetching PHP 5.6 repository
==> lamp: -- Updating packages list
==> lamp: -- Installing PHP modules
==> lamp: -- Enabling PHP mcrypt module
==> lamp: -- Turning PHP error reporting on
==> lamp: -- Creating a dummy index.html file
==> lamp: -- Creating a dummy index.php file
==> lamp: -- ---------------- --
==> lamp: -- END BOOTSTRAPING --
==> lamp: -- ---------------- --

Access the box


$ vagrant ssh
vagrant@lamp:~$

Check installed services


vagrant@lamp:~$ uname -mrs
Linux 3.13.0-88-generic x86_64

vagrant@lamp:~$ apache2 -v
Server version: Apache/2.4.7 (Ubuntu)
Server built: May 4 2016 17:05:10

vagrant@lamp:~$ php -v
PHP 5.6.23-1+deprecated+dontuse+deb.sury.org~trusty+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

mysql> SELECT VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 5.5.49-0ubuntu0.14.04.1 |
+-------------------------+
1 row in set (0.00 sec)

Access webserver from host browser


You need to go to http://192.168.50.10/ in the browser.


Access php info from host browser


You need to go to http://192.168.50.10/index.php in the browser.


Access phpMyAdmin from host browser


You need to go to http://192.168.50.10/phpmyadmin in the browser. Credentials: root:root.