Local WordPress Development with Vagrant

The purpose of this post is to show you how to set up an isolated WordPress development environment on your local machine with Vagrant. The benefits of using Vagrant for development is that all your dependencies/packages are installed in an isolated, reproducible, & disposable development environment that doesn’t effect your local machine.

Learn more about what Vagrant is here.

This tutorial assumes that you have both Vagrant and Virtual Box install on your local machine. If not here are the links to both:

  1. https://www.vagrantup.com/
  2. https://www.virtualbox.org/

Once both are installed you will be able to complete the rest of this tutorial.

Step 1 – Fetch WordPress Source Code

Clone the latest WordPress source code from their Github:

git clone https://github.com/WordPress/WordPress.git
Terminal

When git is done fetching the source code you should have a new WordPress folder in your working directory which contains all the WordPress source code files.

Step 2 – Create a Vagrantfile

Run the vagrant init command to generate a new Vagrantfile in the same directory as you cloned the WordPress source code.

Terminal

The Vagrantfile generated tells VirtualBox how to provision and run the Ubuntu virtual machine we will spin up. We’ll edit this in a bit.

For a bit of house cleaning, let’s create a parent folder called wp and put both the WordPress source code and Vagrantfile inside it.

mkdir wp
Code directory

Step 3 – Quick Configuration Changes

Configure the Vagrantfile

Open the wp folder with your favorite text editor and edit the Vagrantfile to look like the following:


Vagrant.configure("2") do |config|
# The base disk image will be Ubuntu 16.04 LTS
config.vm.box = "ubuntu/xenial64"

# Set the host name of the machine
config.vm.hostname = "wordpress"

# A friendly message to appear on machine start up
config.vm.post_up_message = "Welcome to WordPress mate!"

# The private IP to access the virtual machine on your local
config.vm.network "private_network", ip: "192.168.33.10"

# Syncs the shared folder WordPress source code to
# /var/www/Wordpress on the virtual machine
config.vm.synced_folder "Wordpress", "/var/www/Wordpress"

# On start up the machine will run these commands to install
# Nginx
# PHP 7.0 packages we need to run WordPress
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y nginx
apt-get install -y php7.0 php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-fpm
SHELL
end

Configure WordPress

  1. In the WordPress source code folder rename the wp-config-sample.php to wp-config.php.
  2. In the wp-config.php update the salts with salts generate from this website https://api.wordpress.org/secret-key/1.1/salt/
  3. Update define( 'DB_NAME', 'database_name_here' ); with whatever name you want your database to be
  4. Update define( 'DB_USER', 'username_here' ); to be define( 'DB_USER', 'root' );
  5. Update define( 'DB_PASSWORD', 'password ); to be define( 'DB_PASSWORD', 'root' );
Wordpress configuration file

NOTE: For live environments I would highly recommend not using the root user for MySQL nor such a simple password.

Step 4 – Enter the Machine

With these configurations done it’s time to start your virtual machine with vargant up. When the machine is done provisioning and installing all dependencies you’ll see your friendly message in the terminal.

Terminal

To check that everything is working so far navigate to http://192.168.33.10/ on your local machine. You should see the default Nginx page.

Nginx default page

Congratulations, you now have a high performance web server running on your local machine! Just a few more steps to go.

Step 5 – Install MySQL

ssh into your machine with vagrant ssh and install MySQL on your virtual machine with

sudo apt-get install -y mysql-server

After which you will be prompted to enter and reconfirm a password. Above we made out database password root so we will use that input for both prompts.

MySQL Password Prompt
MySQL Confirm Password Prompt

Now we will create our database with the same name as specified in our wp-config.php file. Mine was called wp_db. From inside the machine log into your MySQL instance as the root user with:

mysql -u root -p

Once in the MySQL command line, create your database with:

CREATE DATABASE wp_db;

And then exit the MySQL command line with:

\q
Terminal of virtual machine

Step 6 – Configure Nginx

In your virtual machine, create a wordpress file in /etc/nginx/sites-available with the following contents:

server {
listen 80;
root /var/www/Wordpress;
index index.php index.html;
server_name example.com;

location / {
try_files $uri $uri/ =404;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}

location ~ /\.ht {
deny all;
}
}

Remove the default file from /etc/nginx/sites-enabled and symlink the wordpress file you created in the sites-available directory to sites-enabled

Terminal of virtual machine

Confirm that your Nginx configuration is valid with:

sudo nginx -t

Restart your Nginx web server

sudo nginx -s reload

You’re Done!

Reload http://192.168.33.10 and you should WordPress!

Wordpress settings page

Please let me know how I can improve this post and I’ll be happy to update it!