Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Vagrant

 Vagrant

Using vagrant to host WordPress locally

Tom J Nowell

March 19, 2014
Tweet

More Decks by Tom J Nowell

Other Decks in Technology

Transcript

  1. Vagrant
    Virtual Machines + automation =
    win
    Tom J Nowell, @tarendai, tomjn.
    com

    View Slide

  2. ● What is Vagrant
    ● Installing Vagrant
    ● Using Vagrant
    ● Moving to Vagrant

    View Slide

  3. Installing Vagrant
    Install Virtualbox
    https://www.virtualbox.
    org/wiki/Downloads

    View Slide

  4. Installing Vagrant

    View Slide

  5. Installing Vagrant
    Install Vagrant
    http://vagrantup.com

    View Slide

  6. Installing Vagrant

    View Slide

  7. Installing Vagrant
    Install the Hosts Updater:
    $ vagrant plugin install vagrant-hostsupdater
    $ vagrant plugin install vagrant-vbguest

    View Slide

  8. Using Vagrant
    What now?

    View Slide

  9. Using Vagrant
    Grab Varying Vagrant
    Vagrants

    View Slide

  10. Using Vagrant
    VVV is now your web
    environment

    View Slide

  11. Using Vagrant
    Open the VVV folder in the
    terminal and type
    vagrant up --provision
    to start the server for the
    first time

    View Slide

  12. Using Vagrant

    View Slide

  13. Using Vagrant
    To stop the server:
    vagrant halt

    View Slide

  14. Using Vagrant
    To start it again:
    vagrant up

    View Slide

  15. Using Vagrant
    To destroy the server:
    vagrant destroy

    View Slide

  16. Using Vagrant
    To login to the server:
    vagrant ssh

    View Slide

  17. Using Vagrant
    To check if it’s running:
    vagrant status

    View Slide

  18. Using Vagrant
    vagrant up # start
    vagrant halt # stop
    vagrant destroy # destroy
    vagrant ssh # login
    vagrant status # is it up?

    View Slide

  19. Using Vagrant
    This gets you:
    ● Ubuntu Virtual Machine
    ● PHPMyAdmin
    ● Nginx
    ● WordPress Dev & stable
    ● WP CLI
    ● all listed at http://vvv/

    View Slide

  20. Using Vagrant

    View Slide

  21. Moving to Vagrant
    To add a site, go to the
    www folder and create:
    ● A subfolder for your site
    ● a vvv-nginx.conf file
    ● a vvv-hosts file

    View Slide

  22. Moving to Vagrant
    vvv-nginx.conf contains an
    Nginx configuration file

    View Slide

  23. Moving to Vagrant
    vvv-hosts is a hosts file
    with the list of hosts to
    add

    View Slide

  24. Example vvv-nginx.conf
    server {
    listen 80;
    listen 443 ssl;
    # CHANGE THE DOMAINS BELOW AND SAVE
    server_name tomjn.com www.tomjn.com;
    root {vvv_path_to_folder};
    include /etc/nginx/nginx-wp-common.conf;
    }

    View Slide

  25. Example vvv-hosts
    tomjn.com
    www.tomjn.com

    View Slide

  26. Moving to Vagrant
    When you’ve done that, put
    your sites files in that
    folder, and re-provision
    the server:
    vagrant halt
    vagrant up --provision

    View Slide

  27. Moving to Vagrant
    ● All my sites are in MAMP
    ● I change files and FTP
    them up to see the changes
    ● What about our existing
    sites?
    ● We dont have the time to
    switch over

    View Slide

  28. Moving to Vagrant
    vvv-init.sh
    Gets ran when vagrant
    provisions itself.
    Lives with vvv-nginx.conf
    and vvv-hosts

    View Slide

  29. Moving to Vagrant
    Example vvv-init.sh
    wp core download
    wp core install --url=”etc..
    wp plugin install wpseo
    wp plugin activate wpseo
    wp import content.xml

    View Slide

  30. #!/bin/sh
    SUSER="alantaifirestar"; # SSH user
    SHOST="tomjn.com"; # remote server
    SDIR="/home/alantaifirestar/tomjn.com/"; # remote folder
    LDIR="/srv/www/tomjn.com" #local folder
    echo 'starting rsync'
    rsync -e "/usr/bin/ssh" --compress --stats --bwlimit=2000 -rlDHS [email protected]$SHOST:$SDIR
    $LDIR
    echo 'finished rsync, grabbing database'
    WHOST=`cat $LDIR/wp-config.php | grep DB_HOST | cut -d \' -f 4`; # get the DB details
    WNAME=`cat $LDIR/wp-config.php | grep DB_NAME | cut -d \' -f 4`;
    WUSER=`cat $LDIR/wp-config.php | grep DB_USER | cut -d \' -f 4`;
    WPASS=`cat $LDIR/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`;
    ssh [email protected]$SHOST "mysqldump -q -u $WUSER -h $WHOST -p$WPASS $WNAME | gzip -9 > backup.
    sql.gz"
    scp [email protected]$SHOST:./backup.sql.gz . # copy all the files to backup server
    ssh [email protected]$SHOST rm ./backup.sql.gz # delete files on db server
    gunzip -d backup.sql.gz # unzip it
    mysql -u root -e "CREATE DATABASE IF NOT EXISTS $WNAME" # create the database locally
    mysql -u root -e "GRANT ALL PRIVILEGES ON $WNAME.* To 'wp'@'localhost'"
    mysql -u wp -pwp $WNAME < backup.sql # pull in the DB dump
    sed -i "/DB_HOST/s/'[^']*'/'localhost'/2" wp-config.php # change wp-config.php values
    sed -i "/DB_USER/s/'[^']*'/'wp'/2" wp-config.php
    sed -i "/DB_PASSWORD/s/'[^']*'/'wp'/2" wp-config.php

    View Slide

  31. Questions?
    Tom J Nowell
    @tarendai
    tomjn.com

    View Slide