Provisioning script for spinning up a vagrant box with a fresh instance of Drupal 8 on it.
- Download VirtualBox
- Download Vagrant
- Clone or download this git repository: git clone git@github.com:genradley/Drupal_Vagrant_Provision_Script.git
- Make sure the start and provision scripts have execution permissions: chmod a+x
- In unix shell, run the start script: ./start.sh
The start script: (1) initializes an ubuntu 16.04 VirtualBox, and (2) tells the Vagrantfile to use the provisioning script and what port or ip to run on
Check to see if a Vagrantfile exists:
if [ ! -f Vagrantfile ]; then
Initialize bento/ubuntu-16.04:
vagrant init -m bento/ubuntu-16.04
Add config settings to Vagrantfile: (you can set the network to a port on localhost or to a private ip, as-is this script does both)
grep -v 'end' Vagrantfile > temp
mv temp Vagrantfile
echo ' config.vm.hostname = "web-dev"' >> Vagrantfile
echo ' config.vm.provision "shell", path: "provision.sh"' >> Vagrantfile
echo ' config.vm.network "forwarded_port", guest: 80, host: 8080, id: "apache", auto_correct: true' >> Vagrantfile
echo ' config.vm.network "private_network", ip: "192.168.33.111"' >> Vagrantfile
echo 'end' >> Vagrantfile
fi
Start vagrant. This will run the provision script the first time the vagrant box is brought up.
vagrant up
Before we do anything, we need to make sure apt-get is up-to-date.
apt-get update
From Drupal 8 System Requirements: "Drupal 8 works on any web server with PHP version of 5.5.9 or greater... Apache is the most commonly used web server for Drupal."
I am using a LAMP (Linux, Apache, MySql, PHP) stack here.
Set up the script to automatically insert MySql password:
apt-get install -y debconf-utils
debconf-set-selections <<< "mysql-server mysql-server/root_password password $DB_PASS"
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $DB_PASS"
Install LAMP:
apt-get install -y lamp-server^
apt-get install -y php-xml php7.0-gd php7.0-mbstring zip unzip php7.0-zip #Drupal dependencies not installed by lamp-server
wget https://getcomposer.org/composer.phar
chmod +x composer.phar
mv composer.phar /usr/local/bin/composer
wget https://github.com/drush-ops/drush-launcher/releases/download/0.3.1/drush.phar
chmod +x drush.phar
mv drush.phar /usr/local/bin/drush
From Drush docs: "It is recommended that Drupal 8 sites be built using Composer, with Drush listed as a dependency." The project they recommend to start with is drupal-composer/drupal-project. That is what this script does.
cd /var/www/html
rm -rf *
mkdir $APP_NAME
composer create-project drupal-composer/drupal-project:8.x-dev $APP_NAME --stability dev --no-interaction
Drush site-install docs
This script creates the drupal database using the root user. If you want to create a drupal database user, you'll have to create that user before running site-install. That requires a password insert which is why this script uses root. Here's a good tutorial for creating a drupal database user. There is probably a way to automate the password requirement. I just haven't looked for it, yet.
cd $APP_NAME/web
drush site-install -y standard --account-name=$ACCT_NAME --account-pass=$ACCT_PASS --account-mail=$ACCT_EMAIL --db-url=mysql://$DB_USER:$DB_PASS@$DB_HOST/$DB_NAME --site-name=$APP_NAME
Drupal 8 enables Clean URLs by default so Apache’s rewrite module must also be enabled:
a2enmod rewrite
Specify the DocumentRoot:
sed -i "s+DocumentRoot /var/www/html+DocumentRoot /var/www/html/$APP_NAME/web+" /etc/apache2/sites-available/000-default.conf
Specify the rewrite conditions for DocumentRoot in Apache’s configuration file:
sed -i 's/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf
Change ownership of Apache’s DocumentRoot from the system’s root user to Apache. This allows you to install modules and themes, and to update Drupal, all without being prompted for FTP credentials.
chown --recursive vagrant:www-data /var/www/html/$APP_NAME
chmod --recursive g+w /var/www/html/$APP_NAME
Restart Apache so all changes are applied:
service apache2 restart
apt-get install -y git
Depending on what the start script put in the Vagrantfile for the network config, you'll point your browser there. The way the script is, you can go to localhost:8080 or 192.168.33.111
Installing modules/themes - You may still not be able to install modules and themes through the drupal gui because of ftp credentials. You can download and install them with drush or composer. Run your commands from the web/ directory. My preferred way is: drush -y en --pm-force module_or_theme_name. You need the --pm-force because the project is composer based.
Git - your git root will be /var/www/html/$APP_NAME/web. The drupal-composer/drupal-project creates a .gitignore file for you.
Vagrant commands - you will mostly use vagrant up (start the box), vagrant ssh (ssh into the box), and vagrant halt (stop the box).
If something doesn't go right and you need to modify the scripts and start over: (1) vagrant destroy, (2) rm -rf .vagrant Vagrantfile, (3) make your script changes, (4) then run ./start.sh again.
(please ignore this line... it is for seo purposes) Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley. Genell Radley.