Skip to content

Instantly share code, notes, and snippets.

@asmerkin
Last active June 13, 2022 12:45
Show Gist options
  • Save asmerkin/df919a6a79b081512366 to your computer and use it in GitHub Desktop.
Save asmerkin/df919a6a79b081512366 to your computer and use it in GitHub Desktop.
A simple LAMP Vagrantfile and Bootstrap file for Vagrant and ubuntu/trusty64 box. It uses mpm-workers and php5-fpm + some extra tools like grunt, gulp and composer.
#!/usr/bin/env bash
# ---------------------------------------
# Virtual Machine Setup
# ---------------------------------------
# Adding multiverse sources.
cat > /etc/apt/sources.list.d/multiverse.list << EOF
deb http://archive.ubuntu.com/ubuntu trusty multiverse
deb http://archive.ubuntu.com/ubuntu trusty-updates multiverse
deb http://security.ubuntu.com/ubuntu trusty-security multiverse
EOF
# Updating packages
apt-get update
# ---------------------------------------
# Apache Setup
# ---------------------------------------
# Installing Packages
apt-get install -y apache2 libapache2-mod-fastcgi apache2-mpm-worker
# linking Vagrant directory to Apache 2.4 public directory
rm -rf /var/www
ln -fs /vagrant /var/www
# Add ServerName to httpd.conf
echo "ServerName localhost" > /etc/apache2/httpd.conf
# Setup hosts file
VHOST=$(cat <<EOF
<VirtualHost *:80>
DocumentRoot "/var/www/public"
ServerName localhost
<Directory "/var/www/public">
AllowOverride All
</Directory>
</VirtualHost>
EOF
)
echo "${VHOST}" > /etc/apache2/sites-enabled/000-default.conf
# Loading needed modules to make apache work
a2enmod actions fastcgi rewrite
service apache2 reload
# ---------------------------------------
# PHP Setup
# ---------------------------------------
# Installing packages
apt-get install -y php5 php5-cli php5-fpm curl php5-curl php5-mcrypt php5-xdebug
# Creating the configurations inside Apache
cat > /etc/apache2/conf-available/php5-fpm.conf << EOF
<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
# NOTE: using '/usr/lib/cgi-bin/php5-cgi' here does not work,
# it doesn't exist in the filesystem!
<Directory /usr/lib/cgi-bin>
Require all granted
</Directory>
</IfModule>
EOF
# Enabling php modules
php5enmod mcrypt
# Triggering changes in apache
a2enconf php5-fpm
service apache2 reload
# ---------------------------------------
# MySQL Setup
# ---------------------------------------
# Setting MySQL root user password root/root
debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'
# Installing packages
apt-get install -y mysql-server mysql-client php5-mysql
# ---------------------------------------
# PHPMyAdmin setup
# ---------------------------------------
# Default PHPMyAdmin Settings
debconf-set-selections <<< 'phpmyadmin phpmyadmin/dbconfig-install boolean true'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/app-password-confirm password root'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/mysql/admin-pass password root'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/mysql/app-pass password root'
debconf-set-selections <<< 'phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2'
# Install PHPMyAdmin
apt-get install -y phpmyadmin
# Make Composer available globally
ln -s /etc/phpmyadmin/apache.conf /etc/apache2/sites-enabled/phpmyadmin.conf
# Restarting apache to make changes
service apache2 restart
# ---------------------------------------
# Tools Setup.
# ---------------------------------------
# These are some extra tools that you can remove if you will not be using them
# They are just to setup some automation to your tasks.
# Adding NodeJS from Nodesource. This will Install NodeJS Version 5 and npm
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
sudo apt-get install -y nodejs
# Installing Bower and Gulp
npm install -g bower gulp
# Installing GIT
apt-get install -y git
# Install Composer
curl -s https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
# -*- 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|
config.vm.box = "ubuntu/trusty64"
config.vm.hostname = "benchmark"
config.vm.network :forwarded_port, host: 8080, guest: 80
config.vm.provision :shell, path: "Vagrant.bootstrap.sh"
end
@mtpultz
Copy link

mtpultz commented Dec 31, 2015

Wow, that's super simple. I've been wrestling with how to configure a LAMP stack using PHP 5.4. Had no idea this could be so precise without using some of the other provisioners like Puppet or Ansible, which just adds a level of complexity I can't seem to figure out yet. Vagrant by itself with shell scripts are very comfortable, though I worry about things like the shell not being idempotent unlike Puppet and Ansible. Though I understand what that means I'm not sure what would happen on reprovisions using the shell provisioner.

@es6Test you can change the last line to use inline then add the shell script using something like the example below, but a separate file is much cleaner.

config.vm.provision "shell", inline: <<-SHELL
    ...
SHELL

@mtpultz
Copy link

mtpultz commented Dec 31, 2015

Though in your blog you mention this installs PHP 5.4, it appears to install PHP 5.5.9. Were you able to install PHP 5.4 at some point? I've tried to do this several ways and I always get 5.5.9 (last attempt is pasted below):

sudo apt-get update
sudo apt-get -y install python-software-properties
sudo add-apt-repository ppa:ondrej/php5-oldstable

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y php5 php5-cli php5-fpm curl php5-curl php5-mcrypt

@asmerkin
Copy link
Author

asmerkin commented Apr 5, 2016

sorry, Ubuntu must have updated their packages. That was installing php 5.4 at the moment of writting.

@xie67
Copy link

xie67 commented Mar 28, 2017

I'm a beginner of vagrant. Could you please tell me why I cannot open http://localhost:8080 after I set all this up?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment