Skip to content

Instantly share code, notes, and snippets.

@newloong
Created January 27, 2018 10:14
Show Gist options
  • Save newloong/b26ab8765307362dfa5cc28ce01367aa to your computer and use it in GitHub Desktop.
Save newloong/b26ab8765307362dfa5cc28ce01367aa to your computer and use it in GitHub Desktop.
This is a script designed to streamline the process of setting up and WordPress installation with the help of Bedrock (https://roots.io/bedrock/) and Trellis (https://roots.io/trellis/)

WP Setup

This is a script designed to streamline the process of setting up and WordPress installation with the help of Bedrock and Trellis

To run, copy this script where you want you would like to create the project and run:

chmod -x setup-wp.sh && setup-wp.sh

Dependancies

This script is design to be used on Mac OS X and uses the following:

  • Virtual box (brew cask install virtualbox)
  • Vagrant (brew cask install vagrant)
    • vagrant-manager (brew cask install vagrant-manager)
    • vagrant-bindfs (vagrant plugin install vagrant-bindfs)
    • vagrant-hostmanager (vagrant plugin install vagrant-hostmanager)
  • Composer (brew install composer)
  • sed (brew install sed)
  • Perl
#!/bin/bash
# install some deps
echo "REQUIRED:"
echo "pip, passlib and ansible 2.0.2. These can be installed like so:"
echo "sudo easy_install pip"
echo "pip install ansible==2.0.2.0"
echo ""
echo "Whats the git URL? (eg. [email protected]:example/example.com.git)"
read GITURL
export GITURL
echo "Whats the vault password?"
read VAULTPWD
export VAULTPWD
echo "Whats the theme name?"
read THEMENAME
export THEMENAME
echo "Whats the prod URL? (eg. example.com)"
read PRODURL
export PRODURL
echo "Whats the staging URL? (eg. staging.example.com)"
read STAGINGURL
export STAGINGURL
echo "Whats the dev URL? (eg. example.dev)"
read DEVURL
export DEVURL
echo "Whats the database hosts? (eg. localhost)"
read DBHOST
export DBHOST
echo "Whats the database name? (eg. social-traders)"
read DBNAME
export DBNAME
echo "Whats the database username? (eg. root)"
read DBUSER
export DBUSER
echo "Whats the dev database password? (eg. root)"
read DEVDBPASSWORD
export DEVDBPASSWORD
echo "Whats the production database password? (eg. root)"
read PRODDBPASSWORD
export PRODDBPASSWORD
# Create new Wordpress site
echo "Installing trellis and bedrock..."
mkdir $PRODURL && cd $PRODURL
git clone --depth=1 [email protected]:roots/trellis.git && rm -rf trellis/.git
git clone --depth=1 [email protected]:roots/bedrock.git site && rm -rf site/.git
cd trellis && ansible-galaxy install -r requirements.yml
### Update git repo, host, admin email
echo "Updating wordpress config..."
perl -i -pe 's/example.dev/$ENV{DEVURL}/g' group_vars/development/wordpress_sites.yml
perl -i -pe 's/example.com/$ENV{DEVURL}/g' group_vars/development/wordpress_sites.yml
perl -i -pe 's/staging.example.com/$ENV{STAGINGURL}/g' group_vars/staging/wordpress_sites.yml
perl -i -pe 's/staging.example.com/$ENV{STAGINGURL}/g' group_vars/staging/wordpress_sites.yml
perl -i -pe 's/example.com/$ENV{STAGINGURL}/g' group_vars/staging/wordpress_sites.yml
perl -i -pe 's/example.com/$ENV{PRODURL}/g' group_vars/production/wordpress_sites.yml
perl -i -pe 's/[email protected]:example\/example.com.git/$ENV{GITURL}/g' group_vars/staging/wordpress_sites.yml
perl -i -pe 's/branch: master/branch: staging/g' group_vars/staging/wordpress_sites.yml
perl -i -pe 's/[email protected]:example\/example.com.git/$ENV{GITURL}/g' group_vars/production/wordpress_sites.yml
### Update passwords
echo "Updating passwords..."
perl -i -pe 's/example.com/$ENV{DEVURL}/g' group_vars/development/vault.yml
perl -i -pe 's/example.com/$ENV{STAGINGURL}/g' group_vars/staging/vault.yml
perl -i -pe 's/example.com/$ENV{PRODURL}/g' group_vars/production/vault.yml
perl -i -pe 's/example_dbpassword/$ENV{DEVDBPASSWORD}/g' group_vars/development/vault.yml
perl -i -pe 's/example_dbpassword/$ENV{DEVDBPASSWORD}/g' group_vars/staging/vault.yml
perl -i -pe 's/example_dbpassword/$ENV{PRODDBPASSWORD}/g' group_vars/production/vault.yml
perl -i -pe 's/devpw/$ENV{DEVDBPASSWORD}/g' group_vars/development/vault.yml
perl -i -pe 's/stagingpw/$ENV{DEVDBPASSWORD}/g' group_vars/staging/vault.yml
perl -i -pe 's/productionpw/$ENV{PRODDBPASSWORD}/g' group_vars/production/vault.yml
### Update user
echo "Updating passwords..."
perl -i -pe 's/admin/root/g' group_vars/staging/vault.yml
perl -i -pe 's/admin/root/g' group_vars/production/vault.yml
## install wp
echo "Installing WP..."
cd ../site && composer install
## env config
echo "Configuring WP..."
cp .env.example .env
perl -i -pe 's/database_name/$ENV{DBNAME}/g' .env
perl -i -pe 's/database_user/$ENV{DBUSER}/g' .env
perl -i -pe 's/database_password/$ENV{DBPASSWORD}/g' .env
perl -i -pe 's/database_host/$ENV{DBHOST}/g' .env
perl -i -pe 's/example.com/\${HTTP_HOST}/g' .env
## install the generic theme
cd web/app/themes/
git clone [email protected]:conduct/conduct_generic_wptheme.git $THEMENAME && rm -rf $THEMENAME/.git
## load server
cd ../../../../trellis
# Replace your_server_hostname with host name for env
perl -i -pe 's/your_server_hostname/$ENV{STAGINGURL}/g' hosts/staging
perl -i -pe 's/your_server_hostname/$ENV{STAGINGURL}/g' hosts/staging
perl -i -pe 's/your_server_hostname/$ENV{PRODURL}/g' hosts/production
perl -i -pe 's/your_server_hostname/$ENV{PRODURL}/g' hosts/production
# Set sshd_permit_root_login to false in group_vars/all/security.yml
# perl -i -pe 's/sshd_permit_root_login: true/sshd_permit_root_login: false/g' group_vars/all/security.yml
## protect passwords etc in git
echo $VAULTPWD >> .vault_pass
chmod 600 .vault_pass
# Add to trellis/ansible.cfg in [defaults]
perl -pi -e 'print "vault_password_file = .vault_pass\n" if $. == 2' ansible.cfg
# Define sudoer_passwords in group_vars/<environment>/vault.yml
# Update trellis/roles/deploy/hooks/finalize-after.yml -> shell: sudo service php7.0-fpm reload
# shell: sudo service php7.0-fpm restart
perl -i -pe 's/fpm reload/fpm restart/g' roles/deploy/hooks/finalize-after.yml
# encrypt the passwords
ansible-vault encrypt group_vars/all/vault.yml group_vars/development/vault.yml group_vars/staging/vault.yml group_vars/production/vault.yml
# spin up the server
vagrant up
# generating salts
echo "Generating WP salts"
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current && wp package install aaemnnosttv/wp-cli-dotenv-command'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current && wp dotenv salts generate'
## Install plugins
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install worker'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install secure-wordpress'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install w3-total-cache'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install login-lockdown'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install simple-custom-post-order'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install google-analytics-for-wordpress'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install all-in-one-seo-pack'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install tinymce-advanced'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install widget-visibility'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin install wp-google-search'
# activate plugins
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin activate tinymce-advanced'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin activate all-in-one-seo-pack'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin activate simple-custom-post-order'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin activate widget-visibility'
vagrant ssh -- -t 'cd /srv/www/$DEVURL/current/ && wp plugin activate wp-google-search'
# complete
echo "Complete! Double check your config files should anything be wrong"
echo "Remote deployment instructions found in the comments of this file"
# Remote deploy:
# MAYBE: Install on remote box mysql-python
# sudo apt-get install python-pip -y
# pip install python-mysqldb
#
# Generate the root password on the box and add to the vault yml for each env
# python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())"
# ansible-playbook server.yml -e env=<environment>
# ./deploy.sh <environment> <root_domain>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment