Skip to content

Instantly share code, notes, and snippets.

@joshlevinson
Last active April 14, 2016 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlevinson/5ed65b12d1f739abf310 to your computer and use it in GitHub Desktop.
Save joshlevinson/5ed65b12d1f739abf310 to your computer and use it in GitHub Desktop.
Creating a VVV site with no down time
vagrant ssh
cd /srv/www/
mkdir site-name && cd site-name && mkdir htdocs && cd htdocs
wp core download
wp core config --dbname=site-name --dbuser=root --dbpass=root
# Before you install the site, you'll need to create the DB. I just pull up phpMyAdmin and create it there.
wp core install --url=site-name.dev --title=Site --admin_user=admin --admin_pass=password --admin_email=admin@example.dev
# Create the files "vvv-hosts" and "vvv-nginx.conf"
# Lastly, make the machine aware of the new site
sudo /srv/config/homebin/vagrant_kick
#!/bin/bash
# create this file in /srv/config/homebin and chmod +x it (while ssh'ed into the vagrant machine)
sudo cp /srv/config/nginx-config/nginx.conf /etc/nginx/nginx.conf
sudo cp /srv/config/nginx-config/nginx-wp-common.conf /etc/nginx/nginx-wp-common.conf
if [[ ! -d /etc/nginx/custom-sites ]]; then
sudo mkdir /etc/nginx/custom-sites/
fi
sudo rsync -rvzh --delete /srv/config/nginx-config/sites/ /etc/nginx/custom-sites/
# IMPORT SQL
#
# Create the databases (unique to system) that will be imported with
# the mysqldump files located in database/backups/
if [[ -f /srv/database/init-custom.sql ]]; then
mysql -u root -proot < /srv/database/init-custom.sql
echo -e "\nInitial custom MySQL scripting..."
else
echo -e "\nNo custom MySQL scripting found in database/init-custom.sql, skipping..."
fi
# Find new sites to setup.
# Kill previously symlinked Nginx configs
# We can't know what sites have been removed, so we have to remove all
# the configs and add them back in again.
sudo sh -c "find /etc/nginx/custom-sites -name 'vvv-auto-*.conf' -exec rm {} \;"
# Look for site setup scripts
for SITE_CONFIG_FILE in $(find /srv/www -maxdepth 5 -name 'vvv-init.sh'); do
DIR="$(dirname $SITE_CONFIG_FILE)"
(
cd "$DIR"
source vvv-init.sh
)
done
# Look for Nginx vhost files, symlink them into the custom sites dir
for SITE_CONFIG_FILE in $(find /srv/www -maxdepth 5 -name 'vvv-nginx.conf'); do
DEST_CONFIG_FILE=${SITE_CONFIG_FILE//\/srv\/www\//}
DEST_CONFIG_FILE=${DEST_CONFIG_FILE//\//\-}
DEST_CONFIG_FILE=${DEST_CONFIG_FILE/%-vvv-nginx.conf/}
DEST_CONFIG_FILE="vvv-auto-$DEST_CONFIG_FILE-$(md5sum <<< "$SITE_CONFIG_FILE" | cut -c1-32).conf"
DIR="$(dirname $SITE_CONFIG_FILE)"
sed "s#{vvv_path_to_folder}#$DIR#" "$SITE_CONFIG_FILE" | sudo tee /etc/nginx/custom-sites/"$DEST_CONFIG_FILE" > /dev/null
done
# Parse any vvv-hosts file located in www/ or subdirectories of www/
# for domains to be added to the virtual machine's host file so that it is
# self aware.
#
# Domains should be entered on new lines.
echo "Cleaning the virtual machine's /etc/hosts file..."
sed -n '/# vvv-auto$/!p' /etc/hosts > /tmp/hosts
mv /tmp/hosts /etc/hosts
echo "Adding domains to the virtual machine's /etc/hosts file..."
find /srv/www/ -maxdepth 5 -name 'vvv-hosts' | \
while read hostfile; do
while IFS='' read -r line || [ -n "$line" ]; do
if [[ "#" != ${line:0:1} ]]; then
if [[ -z "$(grep -q "^127.0.0.1 $line$" /etc/hosts)" ]]; then
echo "127.0.0.1 $line # vvv-auto" >> /etc/hosts
echo " * Added $line from $hostfile"
fi
fi
done < "$hostfile"
done
sudo service nginx reload
# create this file in /srv/www/site-name/
site-name.dev
#create this file in /srv/www/site-name/
################################################################
# Example configuration file for nginx
#
# To add a new local WordPress domain to your environment, copy
# this file using a filename that matches the domain you wish to
# setup. For example - mylocaldomain.com.conf would be an ideal
# filename for http://mylocaldomain.com
#
# Once copied, you will need to modify two settings in the server
# configuration provided:
#
# 1. server_name - Change the server_name parameter in the server
# configuration below to mylocaldomain.com
# 2. root - Change root to the full path that your WordPress
# site lives at inside Vagrant. An example would be
# /srv/www/mylocal-wordpress
#
# You do not need to worry about modifying the listen or include
# parameters as those are the same across all test sites for most
# basic WordPress configurations.
#
# Once your new domain has been added, make sure to restart the
# nginx process by running `vagrant provision` in your local
# environment or `sudo service nginx restart` after `vagrant ssh`
################################################################
server {
# Determines the port number that nginx will listen to for this
# server configuration. 80 is the default http port.
listen 80;
# Listen for HTTPS requests as well
listen 443 ssl;
# Tells nginx what domain name should trigger this configuration. If
# you would like multiple domains or subdomains, they can be space
# delimited here. See http://nginx.org/en/docs/http/server_names.html
server_name site-name.dev;
# Tells nginx which directory the files for this domain are located
root /srv/www/site-name/htdocs;
# Includes a basic WordPress configuration to help with the common
# rules needed by a web server to deal with WordPress properly.
include /etc/nginx/nginx-wp-common.conf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment