Instantly share code, notes, and snippets.

Embed
What would you like to do?
Vagrant provision script for php, Apache, MySQL, phpMyAdmin, Laravel, and javascript helpers. Tested with Ubuntu 16.04.
#! /usr/bin/env bash
###
#
# install_mysql.sh
#
# This script assumes your Vagrantfile has been configured to map the root of
# your application to /vagrant and that your web root is the "public" folder
# (Laravel standard). Standard and error output is sent to
# /vagrant/vm_build.log during provisioning.
#
###
# Variables
DBHOST=localhost
DBNAME=dbname
DBUSER=dbuser
DBPASSWD=test123
echo -e "\n--- Mkay, installing now... ---\n"
echo -e "\n--- Updating packages list ---\n"
apt-get -qq update
echo -e "\n--- Install base packages ---\n"
apt-get -y install vim curl build-essential python-software-properties git >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Add Node 6.x rather than 4 ---\n"
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Updating packages list ---\n"
apt-get -qq update
# MySQL setup for development purposes ONLY
echo -e "\n--- Install MySQL specific packages and settings ---\n"
debconf-set-selections <<< "mysql-server mysql-server/root_password password $DBPASSWD"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $DBPASSWD"
debconf-set-selections <<< "phpmyadmin phpmyadmin/dbconfig-install boolean true"
debconf-set-selections <<< "phpmyadmin phpmyadmin/app-password-confirm password $DBPASSWD"
debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/admin-pass password $DBPASSWD"
debconf-set-selections <<< "phpmyadmin phpmyadmin/mysql/app-pass password $DBPASSWD"
debconf-set-selections <<< "phpmyadmin phpmyadmin/reconfigure-webserver multiselect none"
apt-get -y install mysql-server phpmyadmin >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Setting up our MySQL user and db ---\n"
mysql -uroot -p$DBPASSWD -e "CREATE DATABASE $DBNAME" >> /vagrant/vm_build.log 2>&1
mysql -uroot -p$DBPASSWD -e "grant all privileges on $DBNAME.* to '$DBUSER'@'localhost' identified by '$DBPASSWD'" > /vagrant/vm_build.log 2>&1
echo -e "\n--- Installing PHP-specific packages ---\n"
apt-get -y install php apache2 libapache2-mod-php php-curl php-gd php-mysql php-gettext >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Enabling mod-rewrite ---\n"
a2enmod rewrite >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Allowing Apache override to all ---\n"
sed -i "s/AllowOverride None/AllowOverride All/g" /etc/apache2/apache2.conf
echo -e "\n--- Setting document root to public directory ---\n"
rm -rf /var/www/html
ln -fs /vagrant/public /var/www/html
echo -e "\n--- We definitly need to see the PHP errors, turning them on ---\n"
sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php/7.0/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php/7.0/apache2/php.ini
echo -e "\n--- Restarting Apache ---\n"
service apache2 restart >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Installing Composer for PHP package management ---\n"
curl --silent https://getcomposer.org/installer | php >> /vagrant/vm_build.log 2>&1
mv composer.phar /usr/local/bin/composer
echo -e "\n--- Installing NodeJS and NPM ---\n"
apt-get -y install nodejs >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Installing javascript components ---\n"
npm install -g gulp bower >> /vagrant/vm_build.log 2>&1
echo -e "\n--- Updating project components and pulling latest versions ---\n"
cd /vagrant
if [[ -s /vagrant/composer.json ]] ;then
sudo -u vagrant -H sh -c "composer install" >> /vagrant/vm_build.log 2>&1
fi
if [[ -s /vagrant/package.json ]] ;then
sudo -u vagrant -H sh -c "npm install" >> /vagrant/vm_build.log 2>&1
fi
if [[ -s /vagrant/bower.json ]] ;then
sudo -u vagrant -H sh -c "bower install -s" >> /vagrant/vm_build.log 2>&1
fi
if [[ -s /vagrant/gulpfile.js ]] ;then
sudo -u vagrant -H sh -c "gulp" >> /vagrant/vm_build.log 2>&1
fi
echo -e "\n--- Creating a symlink for future phpunit use ---\n"
if [[ -x /vagrant/vendor/bin/phpunit ]] ;then
ln -fs /vagrant/vendor/bin/phpunit /usr/local/bin/phpunit
fi
@notflip

This comment has been minimized.

Show comment
Hide comment
@notflip

notflip Mar 24, 2014

Looking good! What's the dev/null 2>&1 for? Is it silencing the output of the installation?

notflip commented Mar 24, 2014

Looking good! What's the dev/null 2>&1 for? Is it silencing the output of the installation?

@rrosiek

This comment has been minimized.

Show comment
Hide comment
@rrosiek

rrosiek Apr 15, 2014

Sorry for the late response, but yea, it's basically to shut everything up. A lot of the 'silent' flags on things aren't actually silent when vagrant is provisioning. It's probably not the best idea for debugging, but once setup, it's pretty damn consistent.

Owner

rrosiek commented Apr 15, 2014

Sorry for the late response, but yea, it's basically to shut everything up. A lot of the 'silent' flags on things aren't actually silent when vagrant is provisioning. It's probably not the best idea for debugging, but once setup, it's pretty damn consistent.

@AmundsenJunior

This comment has been minimized.

Show comment
Hide comment
@AmundsenJunior

AmundsenJunior Oct 10, 2014

Thanks for this! I'm writing my first Vagrantfile and provisioning shell script, and this is helping me out tremendously. Do you have resources on how you pass responses to installations (such as in lines 26-32)? It works for my script as well, but I'd appreciate learning how and why.

AmundsenJunior commented Oct 10, 2014

Thanks for this! I'm writing my first Vagrantfile and provisioning shell script, and this is helping me out tremendously. Do you have resources on how you pass responses to installations (such as in lines 26-32)? It works for my script as well, but I'd appreciate learning how and why.

@st-f

This comment has been minimized.

Show comment
Hide comment
@st-f

st-f Jan 28, 2015

Thanks a lot for this, very helpful ! Works like a charm, but I found it easier to setup phpmyadmin by adding a simple symlink : sudo ln -fs /usr/share/phpmyadmin /var/www/phpmyadmin

st-f commented Jan 28, 2015

Thanks a lot for this, very helpful ! Works like a charm, but I found it easier to setup phpmyadmin by adding a simple symlink : sudo ln -fs /usr/share/phpmyadmin /var/www/phpmyadmin

@mnori

This comment has been minimized.

Show comment
Hide comment
@mnori

mnori Apr 28, 2015

Really liking the mysql/phpmyadmin bit that bypasses the annoying GUI.

mnori commented Apr 28, 2015

Really liking the mysql/phpmyadmin bit that bypasses the annoying GUI.

@rvlasenko

This comment has been minimized.

Show comment
Hide comment
@rvlasenko

rvlasenko commented Jun 8, 2015

Why rm rf

@byaussy

This comment has been minimized.

Show comment
Hide comment
@byaussy

byaussy Jul 13, 2015

rm -rf
Removes any existing symlinks before creating a new one.
Probably only useful for a forced re-provisioning, as the symlink won't be there the first time you provision a new vagrant box.

byaussy commented Jul 13, 2015

rm -rf
Removes any existing symlinks before creating a new one.
Probably only useful for a forced re-provisioning, as the symlink won't be there the first time you provision a new vagrant box.

@alogins

This comment has been minimized.

Show comment
Hide comment
@alogins

alogins Feb 23, 2016

Thanks :)

alogins commented Feb 23, 2016

Thanks :)

@esmaily

This comment has been minimized.

Show comment
Hide comment
@esmaily

esmaily commented Mar 6, 2016

ok!

@DiegoGallegos4

This comment has been minimized.

Show comment
Hide comment
@DiegoGallegos4

DiegoGallegos4 Mar 14, 2016

On debian. There are some issues for example:

  1. Instead of using service use systemctl
  2. NPM installation commands are no longer working due that the url is no longer there. Use :
    curl --silent -0 -L https://npmjs.org/install.sh | sh > /dev/null 2>&1

DiegoGallegos4 commented Mar 14, 2016

On debian. There are some issues for example:

  1. Instead of using service use systemctl
  2. NPM installation commands are no longer working due that the url is no longer there. Use :
    curl --silent -0 -L https://npmjs.org/install.sh | sh > /dev/null 2>&1
@andrius-senulis

This comment has been minimized.

Show comment
Hide comment
@andrius-senulis

andrius-senulis commented Apr 27, 2016

Thanks 👍

@asinode

This comment has been minimized.

Show comment
Hide comment
@asinode

asinode May 2, 2016

Thanks a lot for this.

I got a small issue while this script ran.

==> default: --- Updating project components and pulling latest versions ---
==> default: /tmp/vagrant-shell: line 103: cd: /vagrant/client: No such file or directory

Not sure if running this script required me to add a directory inside /vagrant with the name client. I guess all other steps ran successfully.

Please help.
Thanks.

asinode commented May 2, 2016

Thanks a lot for this.

I got a small issue while this script ran.

==> default: --- Updating project components and pulling latest versions ---
==> default: /tmp/vagrant-shell: line 103: cd: /vagrant/client: No such file or directory

Not sure if running this script required me to add a directory inside /vagrant with the name client. I guess all other steps ran successfully.

Please help.
Thanks.

@naneri

This comment has been minimized.

Show comment
Hide comment
@naneri

naneri May 18, 2016

Hey Rrosiek could you write an artical with some details concerning provisioning. For instance, I don't understand the last command that ads env vars to artisan. Also, concerning the phpmyadmin - how can I access it later? Also where can I see the .vagrantfile (i assume that shared folders are placed inside the /vagrant folder on host machine.) Also wanted to know - does this provision create the apache virtual host for future project?

naneri commented May 18, 2016

Hey Rrosiek could you write an artical with some details concerning provisioning. For instance, I don't understand the last command that ads env vars to artisan. Also, concerning the phpmyadmin - how can I access it later? Also where can I see the .vagrantfile (i assume that shared folders are placed inside the /vagrant folder on host machine.) Also wanted to know - does this provision create the apache virtual host for future project?

@rrosiek

This comment has been minimized.

Show comment
Hide comment
@rrosiek

rrosiek May 21, 2016

Thanks for the responses everyone; I wish Gist would actually notify me when there are comments, so I try to check on it once in a while. I'll look into explaining this a little further, fixing a few issues, adding more comments, and also updating it for Ubuntu 16.04 (or at least note the changes). Will try to test and update over the next week, thanks!

Owner

rrosiek commented May 21, 2016

Thanks for the responses everyone; I wish Gist would actually notify me when there are comments, so I try to check on it once in a while. I'll look into explaining this a little further, fixing a few issues, adding more comments, and also updating it for Ubuntu 16.04 (or at least note the changes). Will try to test and update over the next week, thanks!

@naneri

This comment has been minimized.

Show comment
Hide comment
@naneri

naneri Jun 5, 2016

Concerning line 103, script does work without it. And with it it works also, buth throws an error.

naneri commented Jun 5, 2016

Concerning line 103, script does work without it. And with it it works also, buth throws an error.

@BentCoder

This comment has been minimized.

Show comment
Hide comment
@BentCoder

BentCoder Jun 25, 2016

Could please someone explain why environment variables are added into 000-default.conf and .bashrc files? What happens if I don't add them?

BentCoder commented Jun 25, 2016

Could please someone explain why environment variables are added into 000-default.conf and .bashrc files? What happens if I don't add them?

@rrosiek

This comment has been minimized.

Show comment
Hide comment
@rrosiek

rrosiek Jul 8, 2016

Updated the script to use the latest Vagrant and Ubuntu 16.04. Also cleaned up quite a bit since Laravel uses .env files rather than actual environment variables (although those are still valid). The old script was biased to my project containing a "client" folder, which isn't necessary using Laravel Elixir.

Owner

rrosiek commented Jul 8, 2016

Updated the script to use the latest Vagrant and Ubuntu 16.04. Also cleaned up quite a bit since Laravel uses .env files rather than actual environment variables (although those are still valid). The old script was biased to my project containing a "client" folder, which isn't necessary using Laravel Elixir.

@amiceli

This comment has been minimized.

Show comment
Hide comment
@amiceli

amiceli Jul 28, 2016

When I run ths script I've this following error : Syntax error: redirection unexpected on line 36.
Somebody have had this error ?

amiceli commented Jul 28, 2016

When I run ths script I've this following error : Syntax error: redirection unexpected on line 36.
Somebody have had this error ?

@rrosiek

This comment has been minimized.

Show comment
Hide comment
@rrosiek

rrosiek Jul 30, 2016

Hmm, are you building an Ubuntu VM? Try using this at the top:

#!/bin/bash

instead of

#! /usr/bin/env bash

Seems like it's not running in a bash environment, although what's there has been tested.

Owner

rrosiek commented Jul 30, 2016

Hmm, are you building an Ubuntu VM? Try using this at the top:

#!/bin/bash

instead of

#! /usr/bin/env bash

Seems like it's not running in a bash environment, although what's there has been tested.

@nerdoc

This comment has been minimized.

Show comment
Hide comment
@nerdoc

nerdoc Dec 7, 2016

BTW: bash is needed. the <<< HERE statement is not supported in Ubuntu's default shell (/bin/sh -> /bin/dash), and echo -e doesn't work neither there.

nerdoc commented Dec 7, 2016

BTW: bash is needed. the <<< HERE statement is not supported in Ubuntu's default shell (/bin/sh -> /bin/dash), and echo -e doesn't work neither there.

@zalog

This comment has been minimized.

Show comment
Hide comment
@zalog

zalog May 30, 2017

I'm not sure about -yes flag. It's apt-get -y install or apt-get install -y?

zalog commented May 30, 2017

I'm not sure about -yes flag. It's apt-get -y install or apt-get install -y?

@rrosiek

This comment has been minimized.

Show comment
Hide comment
@rrosiek

rrosiek Jun 12, 2017

Hi @zalog, I think both versions will work, but from the command reference, flags technically go before the command:

apt-get [options] [-o config=string] [-c=cfgfile] command [pkg]

Owner

rrosiek commented Jun 12, 2017

Hi @zalog, I think both versions will work, but from the command reference, flags technically go before the command:

apt-get [options] [-o config=string] [-c=cfgfile] command [pkg]

@michaelfresco

This comment has been minimized.

Show comment
Hide comment
@michaelfresco

michaelfresco Mar 20, 2018

@ amiceli

When I run ths script I've this following error : Syntax error: redirection unexpected on line 36.
Somebody have had this error ?

With ubuntu16.04 i managed to get it running by typing bash install_mysql.sh

michaelfresco commented Mar 20, 2018

@ amiceli

When I run ths script I've this following error : Syntax error: redirection unexpected on line 36.
Somebody have had this error ?

With ubuntu16.04 i managed to get it running by typing bash install_mysql.sh

@joshddavis

This comment has been minimized.

Show comment
Hide comment
@joshddavis

joshddavis Jul 30, 2018

@rrosiek

Can someone explain to me why he sets "phpmyadmin phpmyadmin/reconfigure-webserver multiselect none"? His script works and I'm able to load phpmyadmin, but just so I can understand whats happening here why doesn't he choose the apache2 flag instead? How does it still get configured to work in this case? Sorry im completely new at this.

joshddavis commented Jul 30, 2018

@rrosiek

Can someone explain to me why he sets "phpmyadmin phpmyadmin/reconfigure-webserver multiselect none"? His script works and I'm able to load phpmyadmin, but just so I can understand whats happening here why doesn't he choose the apache2 flag instead? How does it still get configured to work in this case? Sorry im completely new at this.

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