A super-simple Vagrant LAMP stack bootstrap (installable with one command)

As I have to setup servers inside Vagrant quite often, sometimes 10 times per day, I started to use provisioning: Using a list of commands that will be executed automatically when Vagrant sets up a new box. This saves a shitload of time. To do so, I’m using a reduced-to the max Vagrantfile that
- sets up a Ubuntu 14.04 LTS “Trustry Thar” 64bit box
- makes the box accessable by the host at IP
192.168.33.22
- syncs the current folder with
/var/www/html
inside the box (permanently, in both directions) - automatically perform all the commands in bootstrap.sh directly after setting up the box for the first time
and a bootstrap.sh that holds your chosen password and your chosen project folder name and does this:
- update, upgrade
- create the project folder inside /var/www/html
- install Apache 2.4, PHP 5.5, MySQL, PHPMyAdmin, git and Composer
- sets the pre-chosen password for MySQL and PHPMyAdmin
- activates mod_rewrite and add AllowOverride All to the vhost settings
The files
The Vagrantfile looks like this:
# -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "ubuntu/trusty64" # Create a private network, which allows host-only access to the machine using a specific IP. config.vm.network "private_network", ip: "192.168.33.22" # Share an additional folder to the guest VM. The first argument is the path on the host to the actual folder. # The second argument is the path on the guest to mount the folder. config.vm.synced_folder "./", "/var/www/html" # Define the bootstrap file: A (shell) script that runs after first setup of your box (= provisioning) config.vm.provision :shell, path: "bootstrap.sh" end
and the bootstrap.sh that looks like this
#!/usr/bin/env bash # Use single quotes instead of double quotes to make it work with special-character passwords PASSWORD='12345678' PROJECTFOLDER='myproject' # create project folder sudo mkdir "/var/www/html/${PROJECTFOLDER}" # update / upgrade sudo apt-get update sudo apt-get -y upgrade # install apache 2.5 and php 5.5 sudo apt-get install -y apache2 sudo apt-get install -y php5 # install mysql and give password to installer sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $PASSWORD" sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $PASSWORD" sudo apt-get -y install mysql-server sudo apt-get install php5-mysql # install phpmyadmin and give password(s) to installer # for simplicity I'm using the same password for mysql and phpmyadmin sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true" sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password $PASSWORD" sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/admin-pass password $PASSWORD" sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/app-pass password $PASSWORD" sudo debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2" sudo apt-get -y install phpmyadmin # setup hosts file VHOST=$(cat <<EOF <VirtualHost *:80> DocumentRoot "/var/www/html/${PROJECTFOLDER}" <Directory "/var/www/html/${PROJECTFOLDER}"> AllowOverride All Require all granted </Directory> </VirtualHost> EOF ) echo "${VHOST}" > /etc/apache2/sites-available/000-default.conf # enable mod_rewrite sudo a2enmod rewrite # restart apache service apache2 restart # install git sudo apt-get -y install git # install Composer curl -s https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer
Installation
To use this, simply get both files here in the repo, put them inside a folder and do a
vagrant up
Make sure you already have the ubuntu/trusty64 box loaded, if not, do
vagrant box add ubuntu/trusty64
Voila, 5mins later you’ll have a fully installed box, syncing with your local folder.
Thank you! I was looking for this to create a simple (and old) environment! Work it at first
Thank you! This is everything a lamp developer needs. It’s almost sexy to have everything up with just “vagrant up”
This is great and quite a bit simpler than what I’ve been looking at and trying to understand. If we wanted to use PHP 5.4 would it take much more than
$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php5-oldstable
$ sudo apt-get update
$ sudo apt-get install -y php5
Not sure what to do for Apache, but I’m trying to mimic our production server that is a LAMP with Apache 2.2, MySQL, MyPhpAdmin, and PHP 5.4.
Did you ever figure this one out?
This is awesome. Thank you so much. I went through a whole lot of different ‘systems’ for creating vagrant boxes, and none of them were simple enough to really understand what was going on.
I kind of took it and ran, making a bit more robust version that uses a simple yaml file. It will also automatically check out a sub repo to the web root. That way someone can use the same exact repo for the vagrant machine while keeping the actual working files unique to that one website.
Question – do you have a license for this? Thinking of releasing mine on github under the MIT license, not sure if you would mind that… I put a link to this site and your repo in a ‘credits’ section of the readme.
Hey, thanks too! I’ve gone the exact same way, everything was too complicated and totally messy, so I’ve built this one. Feel free to do whatever you want with the code, it’s totally free! As it’s so simply I think it doesn’t even need a licence, you know…
Can you drop a link to your Git repo to see your example?
It’s still not totally done, I have some local changes like adding a shared “databases” folder, trying to figure out how I (at one point) was able to get it to pause partway through and give you the github ssh key so the repo can check out private repos during the first provision.
Windows kind of borked my local machine with a recent update, though, so I haven’t been able to do much with fresh / blank boxes. I can use ssh user@existing.box.ip, but not vagrant ssh boxnumber. Which means I can’t reprovision.
Anyway, here’s what I have up online so far. I’d really appreciate help cleaning it up and extending it.
https://github.com/herent/vagrant-bootstraps-php
Great job and Thanks a lot ..
Great! Thank you it’s simple & perfect
Thanks @devmetal:disqus great script!
Do you have one with php5.6?
It works like a charm!
Thanks for the well written instructions.
Does it work with any php applications like prestashop or symfony ?
Can I use docker instead of virtualbox in my netbook ?
Thanks for this very easy tutorial.
He, this just install the tools described and prepares mod_rewrite, so it has nothing to do with the scripts you want to use. It depends, so follow the install tutorials of the script you want to use. Regarding Docker, hmm, i cannot say this, no idea to be honest.