Skip to content

Instantly share code, notes, and snippets.

@clavery
Forked from joehinkle/Vagrantfile
Last active March 1, 2018 20:14
Show Gist options
  • Save clavery/7cec1a3107a976ada41b to your computer and use it in GitHub Desktop.
Save clavery/7cec1a3107a976ada41b to your computer and use it in GitHub Desktop.
Simple PHP vagrant box for ubuntu trusty 64 w/ Mysql
#!/usr/bin/env bash
DOCUMENT_ROOT='/vagrant/public' # change if using a subdirectory in your project
MYSQL_ROOT_PASSWORD='foobar'
DROP_DB_IF_EXISTS=0 # Set to 1 to drop databases that exist
DATABASES=(crafttest) # Space delimited
SMTP_RELAY='1.2.3.4'
### Bookeeping ###
mkdir -p /root/.provisioning
### Apache + PHP ###
apt-get update -y
apt-get install -y apache2
apt-get install -y php5 libapache2-mod-php5 php5-cli php5-mcrypt php5-gd php5-curl
cat <<CONFIG > /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot ${DOCUMENT_ROOT}
<Directory />
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory ${DOCUMENT_ROOT}>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
</VirtualHost>
CONFIG
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/
sudo sed -i 's/^User.*/User vagrant/g' /etc/apache2/apache2.conf
php5enmod mcrypt
service apache2 restart
update-rc.d apache2 defaults
### MYSQL ###
DEBIAN_FRONTEND=noninteractive apt-get -y install php5-mysql mysql-server mysql-client
if [ -f /root/.provisioning/mysql_root_password ]; then
_old_pass=$(cat /root/.provisioning/mysql_root_password)
mysqladmin -u root --password=${_old_pass} password ${MYSQL_ROOT_PASSWORD}
else
mysqladmin -u root password ${MYSQL_ROOT_PASSWORD}
fi
echo -n ${MYSQL_ROOT_PASSWORD} > /root/.provisioning/mysql_root_password
sed -i "s/^bind-address.*/bind-address = 0.0.0.0/g" /etc/mysql/my.cnf
service mysql restart
update-rc.d mysql defaults
if [ ${DROP_DB_IF_EXISTS} -eq 1 ]; then
for DB in ${DATABASES[@]}; do
echo "Dropping database if exists: ${DB}"
mysql -u root --password=${MYSQL_ROOT_PASSWORD} <<-DROPMSQL
DROP DATABASE IF EXISTS ${DB};
DROPMSQL
done
fi
for DB in ${DATABASES[@]}; do
echo "Create database if not exists: ${DB}"
mysql -u root --password=${MYSQL_ROOT_PASSWORD} <<-MSQL
CREATE DATABASE IF NOT EXISTS ${DB};
MSQL
done
mysql -u root --password=${MYSQL_ROOT_PASSWORD} <<-MSQL
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';
FLUSH PRIVILEGES;
MSQL
### SMTP RELAY ###
DEBIAN_FRONTEND=noninteractive apt-get -y install mailutils
sed -i "s/^relayhost.*/relayhost = ${SMTP_RELAY}/g" /etc/postfix/main.cf
service postfix restart
update-rc.d postfix defaults
# vi: set noexpandtab:
# -*- 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.provision :shell, path: "bootstrap.sh"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 3306, host: 3306, host_ip: "127.0.0.1"
config.vm.provider "virtualbox" do |v|
v.memory = 1024
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment