The homestead virtual machine box for vagrant is a great way to set up your local testing and sandbox development environments. The cool thing about homestead is that not only can you use it for all of your Laravel specific development, but you can also make use of it for other projects like Slim, Symfony, Drupal, or WordPress. In this tutorial, we will set up WordPress on an existing homestead installation using composer.
Remind yourself of shared folder locations
Typically you have a directory on the host machine which contains all of your various projects you may be working on. For example, in our tutorial, we have a directory that lives at C:localdev> (/c/localdev in a git client) where we place our projects. This corresponds to some settings that we will configure in the Homestead.yaml file later on. Your local directory very well may be different, just know that this is where we will install WordPress.
Use composer to install WordPress
Now that we know where we want to store the WordPress files, we can use composer to download the latest version of WordPress. Manually downloading software is so yesterday! At the command prompt navigate into your local folder and type this command.
composer create-project johnpbloch/wordpress wordpresstutorial
This installs all the files we need for WordPress with the root directory for the installation living at /c/localdev/wordpresstutorial/wordpress on the host machine. The guest machine has these files located in /home/vagrant/Code/wordpresstutorial/wordpress. Here, we open the local files in PHP Storm:
Set a local domain in hosts file
In this step, you can choose any name you like to work with your local WordPress installation. We will use the name of wordpresstutorial.dev. Find your hosts file located in C:WindowsSystem32driversetc and open the file using notepad with Administrator privileges. For the domain you choose, go ahead and add the following line to the hosts file and save it.
The yaml file is the way you configure the Homestead box to tell the system what ip address to use, how much memory the virtual machine should have, how many cpu’s the vm has, and which hypervisor to use among other things. The two main things we want to make note of in this tutorial are the folders section and the sites section of the yaml file. Like we discussed earlier, we can see how folders on the host machine map to the guest machine. For our installation, files in /c/localdev on the house map to /home/vagrant/Code on the guest. Great.
Under the sites section, we specify which domain maps to what location on the virtual machine. So when we type wordpresstutorial.dev into our web browser, requests will be sent to /home/vagrant/Code/wordpresstutorial/wordpress on the virtual machine. These two lines are highlighted in the yaml file below.
- map: C:/localdev
- map: wordpresstutorial.dev
- map: phpmyadmin.dev
# - id: foo
# token: bar
# client-id: foo
# client-token: bar
# - send: 50000
# to: 5000
# - send: 7777
# to: 777
# protocol: udp
We’re not ready just yet! With our configuration updated, we need to now re provision our running homestead box. This tutorial assumes you already have your development environment set up for using homestead. Navigate to your /c/homestead directory from the command prompt and type
You may see something like this on your screen.
Create a database for WordPress
In a prior tutorial we had set up the homestead database to work with an installation of Laravel. In that tutorial we also configured a fresh install of phpMyAdmin so that we could make use of a simple graphical user interface to easily work with the database rather than the command line. Check out that tutorial if you would like to set up the same. Here we will go ahead and set up a new database named
wordpresstutorial for our WordPress Installation.
With all of this in place, we should now be able to visit wordpresstutorial.dev in our broswer and go through the standard initial WordPress set up. Let’s try it out.
Upon clicking continue, we get this helpful message:
Welcome to WordPress. Before getting started, we need some information on the database. You will need to know the following items before proceeding.
Table prefix (if you want to run more than one WordPress in a single database)
We’re going to use this information to create a wp-config.php file. If for any reason this automatic file creation doesn’t work, don’t worry. All this does is fill in the database information to a configuration file. You may also simply open wp-config-sample.php in a text editor, fill in your information, and save it as wp-config.php. Need more help? We got it.
In all likelihood, these items were supplied to you by your Web Host. If you don’t have this information, then you will need to contact them before you can continue. If you’re all ready…
We know that the database name in our case is wordpresstutorial, the database username is homestead, the password is secret, the host is localhost, and we will use wp_ as the table prefix. We click Let’s Go.
Here we make a few small edits to match the configuration information we just spoke about and click on Submit. We get the cool message of “All right, sparky! You’ve made it through this part of the installation. WordPress can now communicate with your database. If you are ready, time now to…”
After clicking on Run The Install, we get a message of “Welcome to the famous five-minute WordPress installation process! Just fill in the information below and you’ll be on your way to using the most extendable and powerful personal publishing platform in the world.” Let’s fill out the basic information and then click on Install WordPress.
Once we click to install WordPress, if things go right we should get a message that says “Success! WordPress has been installed. Thank you, and enjoy!”
We can now sign in to this WordPress installation.
As well as view the homepage of our new WordPress Installation.
How To Install WordPress on Laravel Homestead Summary
Installing WordPress on the Homestead virtual machine was pretty easy. In this tutorial we showed you exactly how to do it. We configured our shared folders, used composer to install the WordPress files, set up a local domain name, configured the yaml file, provisioned our vagrant environment, and set up a database for WordPress. Finally, we followed the navigation to go through the standard install process of WordPress once database connectivity has been established. We can see that it went quite well so great work!