Skip to content

Instantly share code, notes, and snippets.

@rnickmccandless
Last active March 7, 2016 22:34
Show Gist options
  • Select an option

  • Save rnickmccandless/d7bb59a2a0dd28448148 to your computer and use it in GitHub Desktop.

Select an option

Save rnickmccandless/d7bb59a2a0dd28448148 to your computer and use it in GitHub Desktop.
Vagrantfile - Centos 6.7 WordPress Provision & Hosted
# Vagrantfile.rb
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Edit these values
wp_vm_options = {
:wp_admin_email => 'your_email@example.com',
:vm_name => 'centos67-wp',
:wp_port => 8080, #port use on host
:vm_defined => 'default'
}
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = 'bento/centos-6.7'
config.vm.hostname = "wp-#{wp_vm_options[:vm_name]}" unless wp_vm_options[:vm_name].empty?
config.vm.define wp_vm_options[:vm_defined]
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network 'forwarded_port', :guest => 80, :host => wp_vm_options[:wp_port]
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
config.vm.synced_folder '.', '/vagrant' #, :type => 'nfs'
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider :virtualbox do |vb|
# Display the VirtualBox GUI when booting the machine
# vb.gui = true
vb.name = "wp-#{wp_vm_options[:vm_name]}" unless wp_vm_options[:vm_name].empty?
# Customize the amount of memory on the VM:
vb.memory = '4096'
vb.cpus = 4
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# Wordpress NGINX Config
wordpress_config = %q{
##
server {
listen 80;
server_name wordpress.localhost.com;
root /var/www/html/wordpress;
# index index.html index.htm index.php;
error_log /var/log/nginx/wordpress.localhost.com.error_log.log;
location / {
index index.html index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
# REQUIREMENTS : Enable PHP Support
location ~ \.php$ {
try_files $uri =404; # SECURITY : Zero day Exploit Protection
# ENABLE : Enable PHP, listen fpm sock
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_intercept_errors on;
#expires off;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
# PLUGINS : Enable Rewrite Rules for Yoast SEO SiteMap
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
}
}
wp_functions = %q{
######################
## Custom Functions ##
######################
# Allows HTML in category descriptions
remove_filter('pre_term_description', 'wp_filter_kses');
# Auto login for Wordpress: http://wordpress.localhost.com:#{wp_vm_options[:wp_port]}/?autologin=1
function autologin() {
if (isset($_GET['autologin']) && $_GET['autologin'] == '1') {
$creds['user_login'] = 'admin';
$creds['user_password'] = 'Password1';
$creds['remember'] = true;
$autologin_user = wp_signon( $creds, false );
if ( !is_wp_error($autologin_user) )
wp_redirect(admin_url());
}
}
add_action( 'after_setup_theme', 'autologin' );
}
#XDebug for remote debugging
xdebug_config = %q{
[xdebug]
zend_extension='/usr/lib64/php/modules/xdebug.so'
xdebug.remote_enable = 1
xdebug.remote_host = 10.0.2.2
xdebug.remote_port = 9000
}
# Color output for better visibility during provisioning
color_out = %q{
color_output() {
printf '\033[%sm%s\033[m\n' "$@"
# usage color "31;5" "string"
# 0 default
# 5 blink, 1 strong, 4 underlined
# fg: 31 red, 32 green, 33 yellow, 34 blue, 35 purple, 36 cyan, 37 white
# bg: 40 black, 41 red, 44 blue, 45 purple
}
}
#############################################################
# Root provisioning
config.vm.provision 'shell', :inline => <<-SHELL
#{color_out}
echo "\n\n======================================================"
color_output '36;40;5;1;4', "Starting Wordpress Server provisioning"'!'
echo ''
color_output '36;40;1', "\nUpdating Repo & Installing Packages"
# yum -y -q update ## !! Breaks vagrant provisioning
yum -y -q install epel-release
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm
yum -y -q --enablerepo=remi install php php-common
yum -y -q --enablerepo=remi,remi-php56 install php php-common
yum -y -q --enablerepo=remi,remi-php56 install php-fpm php-pecl-apcu php-cli php-devel php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-soap
yum -y -q install nginx vim pv gcc gcc-c++ autoconf automake htop
echo ''
color_output '36;40;1', "Adding XDebug"
pecl install xdebug
echo "#{xdebug_config}" >> /etc/php.ini
echo ''
color_output '36;40;1', "Downloading, Installing, & Starting MySQL"
wget http://dev.mysql.com/get/mysql57-community-release-el6-7.noarch.rpm
yum -y -q localinstall mysql57-community-release-el6-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
yum -y -q install mysql-community-server
echo "default_password_lifetime=0" >> /etc/my.cnf
service mysqld start
echo ''
color_output '36;40;1', 'Resetting MySQL root password to null'
service mysqld stop
mysqld_safe --skip-grant-tables &
sleep 5
mysql -u root -e "update mysql.user set mysql.user.authentication_string=PASSWORD('') where mysql.user.User='root'; flush privileges;"
mysql -u root -e "ALTER USER USER() IDENTIFIED BY '';" --connect-expired-password
service mysqld stop
service mysqld start
mysql -uroot -e "CREATE DATABASE wordpress"
color_output '36;40;1', "Wordpress database is 'wordpress'"
echo ''
echo ''
color_output '36;40;1', "Starting NGINX & enabling for boot"
service nginx start
chkconfig nginx on
echo ''
color_output '36;40;1', "Starting PHP-FPM & enabling for boot"
service php-fpm start
chkconfig php-fpm on
echo ''
color_output '36;40;1', "Creating Wordpress NGINX config file & restarting NGINX"
echo '#{wordpress_config}' > /etc/nginx/conf.d/wordpress.localhost.com.conf
service nginx restart
echo ''
color_output '36;40;1', "Changing PHP-FPM Config & restarting"
sed -i -e 's/apache/nginx/g' /etc/php-fpm.d/www.conf
sed -i -e 's%127.0.0.1:9000%/var/run/php-fpm.sock%g' /etc/php-fpm.d/www.conf
sed -i -e 's%;listen.owner%listen.owner%g' /etc/php-fpm.d/www.conf
sed -i -e 's%;listen.group%listen.group%g' /etc/php-fpm.d/www.conf
sed -i -e 's%nobody%nginx%g' /etc/php-fpm.d/www.conf
service php-fpm restart
echo ''
color_output '36;40;1', "Download and install Wordpress"
wget --quiet https://wordpress.org/latest.tar.gz
mkdir -p /var/www/html
mv latest.tar.gz /var/www/html/
cd /var/www/html/ && tar xzf latest.tar.gz
chown nginx:vagrant -R /var/www/html/*
find /var/www/html/ -type d -exec chmod 775 {} \\;
find /var/www/html/ -type f -exec chmod 664 {} \\;
echo ''
color_output '36;40;1', "Installing WP-CLI"
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/bin/wp
SHELL
# User vagrant provisioning
config.vm.provision 'shell', :privileged => false, :inline => <<-SHELL
#{color_out}
echo ''
color_output '36;40;1', "-------------------------------------"
color_output '36;40;1', "== Provisioning WP as user vagrant =="
echo ''
color_output '36;40;1' "Using email #{wp_vm_options[:wp_admin_email]} for WP admin"
echo ''
color_output '36;40;1', "Create symbolic links & set up aliases"
echo "alias ll='ls -l'" >> /home/vagrant/.bashrc
echo "alias l='ls -la'" >> /home/vagrant/.bashrc
source /home/vagrant/.bashrc
ln -s /vagrant /home/vagrant/project
echo ''
color_output '36;40;1', "Setting up WP Config"
wp core config --dbname=wordpress --dbuser=root --locale='127.0.0.1' --path=/var/www/html/wordpress
## Uncomment to add debugging
# wp core config --dbname=wordpress --dbuser=root --locale='127.0.0.1' --path=/vagrant --extra-php <<-PHP
# define( 'WP_DEBUG', true );
# define( 'WP_DEBUG_LOG', true );
# PHP
echo ''
color_output '36;40;1', "Installing WP"
wp core install --url='wordpress.localhost.com:#{wp_vm_options[:wp_port]}' --title='Wordpress' --admin_user='admin' --admin_password='Password1' --admin_email='#{wp_vm_options[:wp_admin_email]}' --path=/var/www/html/wordpress
echo "WP Admin\n-u admin -p Password1" > /home/vagrant/admin.readme
wp option update uploads_use_yearmonth_folders 0 --path=/var/www/html/wordpress
echo ''
color_output '36;40;1', "Setting up WP DB pristine backup"
mysqldump -uroot wordpress > /var/www/html/wordpress/pristine.db
echo "#!/bin/bash\necho 'Restoring Wordpress database to pristine copy.'\nmysql -uroot wordpress < pristine.db" > /var/www/html/wordpress/revert_db.sh
# Make sure everything has correct permissions
sudo chown nginx:vagrant -R /var/www/html/*
sudo find /var/www/html/ -type d -exec chmod 775 {} \\;
sudo find /var/www/html/ -type f -exec chmod 664 {} \\;
# Make changes to WP functions in each theme - create an auto-login
echo '#{wp_functions}' >> /var/www/html/wordpress/wp-content/themes/twentysixteen/functions.php
echo '#{wp_functions}' >> /var/www/html/wordpress/wp-content/themes/twentyfifteen/functions.php
echo '#{wp_functions}' >> /var/www/html/wordpress/wp-content/themes/twentyfourteen/functions.php
echo ''
color_output '36;40;4;1;5', 'Finished provisioning!'
echo '======================================================'
color_output '33;41;4;1', " => Don't forget to add '127.0.0.1 wordpress.localhost.com' to your hosts file so you can access it @ http://wordpress.localhost.com:#{wp_vm_options[:wp_port]}/ from your browser. "
echo '======================================================'
echo ''
SHELL
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment