Quick Local WordPress Setup with Valet

Often you just want to do a quick WordPress setup locally on your Mac for trying something out. I always do a quick local WordPress setup with Valet. Laravel Valet is an application that is created by the developers behind Laravel. It can be used to run all sorts of apps and WordPress is one of them. Thanks to the Evan Mattson’s CLI Valet Command package you can use Valet to create WordPress sites with a single command.

Prerequisites

So what do you need? You need to setup Laravel Valet and for that you need Homebrew (MacOs package manager) including PHP and MariaDB. You also need Composer to install the Laravel Valet package itself.. Let’s go through settings all these up.

Homebrew

So what do you need? You need to setup Laravel Valet and for that you need Homebrew (MacOs package manager). So install Homebrew using:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Homebrew Packages

Then you need to install PHP 7.2 (currently).

brew install homebrew/php/php72

And you also need to install MariaDB

brew install mariadb

Composer & Laravel Valet

Then you can install Laravel Valet with composer. Most of you using Laravel should have it already, but just in case. To install Composer do the following in a directory of your choice

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Next step is to install the composer package for Laravel Valet

composer global require laravel/valet

WP CLI & WP CLI Valet

You should have WordPress Command Line Interface installed too. You can read about it here. But on top of that you need to install the WP Valet package. This package does all the magic of using the coolness of Laravel Valet and WP CLI. You can do the installation using this wp command:

wp package install git@github.com:aaemnnosttv/wp-cli-valet-command.git

That will get you going.

WordPress Website Installation

Once that is done you can install WordPress using Valet and Bedrock as a setup using:

wp valet new my-project --project=bedrock

Bonus: Local Site Copy

So what if you quickly want to set up an existing website? This so you can make changes for your client? There are many options. You could do a general WordPress Valet setup, replace the database and add the wp-content directory. That would do the trick. You could also do a general Valet WordPress setup and than do a BackupBuddy restore.

To start a new project as a base you simply run:

wp valet new my-project --project=wp

Then inside the newly created directory my-project run

valet link new-project

Using valet links you can check if it was created, but of course also by surfing to https:/my-project.test. You can then login at https://my-project.test/wp-login.php with username admin and password admin.

Local Restore

Now let’s say I want to go the BackupBuddy restore way. How do I proceed? I install the BackupBuddy plugin and import the backup using the backup file and the import file. Issue is that BackupBuddy refuses to run backups or restores due to errors with wp-cron and others.

Error: Error #9038: Loopback test error: cURL error 6: Could not resolve: site.test (Domain name not found). URL: https://site.test/wp-admin/admin-ajax.php?action=itbub_http_loop_back_test&serial=96ytxigaj612suq. If you need to contact your web host, tell them that when PHP tries to connect back to the site at the URL https://site.test/wp-admin/admin-ajax.php?action=itbub_http_loop_back_test&serial=96ytxigaj612suq via curl (or other fallback connection method built into WordPress) that it gets the error cURL error 6: Could not resolve: site.test (Domain name not found). This means that WordPress' built-in simulated cron system cannot function properly, breaking some WordPress features & subsequently some plugins. There may be a problem with the server configuration (eg local DNS problems, mod_security, etc) preventing connections from working properly.

We do have 1024MB as a memory limit already at /usr/local/etc/php/7.3/conf.d/php-memory-limits.ini and we also made execution time 120 instead of 30 at /usr/local/etc/php/7.3/php.ini

But with or without SSL we still got stuck updating via BackupBuddy’s interface. So what you can better do is replace wp-content and import the database as a whole. We imported with WP Database Backup.

WP Database Backup

We did however had to re-create the database and using utf8mb4. Basic WP Valet database setup caused the import to fail and even using different import settings than Greek Windows did not help. Still got stuck at

[ERROR in query 1] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PK   ' at line 1 [ERROR in query 2] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '??4?A?? ??G?ж??O ?????? ? fW??n?,?m??e?x? ? ?

With new database under the same name we could import. Warning shots were fired and a database update was needed by WordPress but things did work in the end. That and with home url and site url changed to work with .test . Of course image would still load remote site images, but for local code tweaks that did not matter.

WP CLI Installation of WordPress

To do a WP CLI Installation of WordPress on a server where WordPress Command Line Interface is installed is quite easy. Let me explain here how you can get WordPress installed with it in no time.

WP CLI Installation

To install WP CLI itself do a

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar 
sudo mv wp-cli.phar /usr/local/bin/wp

then do a wp –info to see that it works.

WP Core Installation Parameters

You just need to log into the server via shell using ssh user@domain.com. Then you go to the web root or directory where your site should reside. There you run the command wp core with the following parameters:

wp core install --url=staging.domain.com --title=Staging --admin_user=uberlord --admin_password=toughpassword --admin_email=jasper@staging.domain.com

This will make wp-cli install WordPress for you with:

  • url – domain where the site will be served from
  • title – site’s Title
  • admin_user – admin’s username
  • admin_password – the administrator’s password
  • admin_email – the email address of the administrator

WP CLI Automation

Once you have started the command it will download the latest WordPress version, install it using the parameters you fed it.

Alternatives

I also wrote about installing WordPress from the command line before without any tool. This works just fine too. But these days I prefer Laravel Valet or WP CLI. These tools are just too nice to live without.

Backing up WordPress from the Command Line

We often have to back up our website and we do not always want to use plugins or control panel options to do this. Often using the command line is a lot easier and quicker. With the help of your terminal you can ssh into your website and fire a few simple commands to backup your WordPress website files and database. I did write about all this briefly before here, but this post is more detailed and includes database backup examples too.

Backing up files

To backup your files you can use tar. Here is the command I use to backup all and exclude the BackupBuddy backups which I don’t need and which tend to take up way too much space.

tar cvzf  domain.nl-16-06-2017.tar.gz domain.nl --exclude=domain.nl/wp-content/uploads/backupbuddy_backups

Tar starts out details on what should be done, then indicates your destination and finally the source directory. So I first tell tar to:

  • (c)ompress
  • use (v)erbose out
  • use (g)unzip
  • and all this done on the (f)ile

Then I tell the destination domain.nl-16-06-2017.tar.gz which I tend to call domain.com-day=month-year.tar.gz. And then finally I tell it to grab all from directory domain.nl and exclude all my backupbuddy backups using the exclude command. If I would have added those the backup would have been several 100 MB larger than without! And as I often just want to backup and SFTP to my home box or another server I prefer to keep the backup as small as possible.

Database Backup

We have not backed up the database yet, which is also very important. To do that I use:

mysqldump --opt --user=user --password=password --host=mysql-site.domain.com databasename > domain.com-dd-mm-yy.sql

Afterwards you can tar the whole file to make it smaller too. See above tar command for that.

WP-CLI Database Backup

Now, if you have wp-cli available or can get it installed you can use it for backing up as well. This command will backup the file in the root of your WordPress website:

wp db export

Now you do have to make sure you are in the WordPress project root. Especially if you like me have multiple WordPress installations. Too, using this command creates the file in the same directory with no specifics in the file name. You may prefer to use something like:

wp db export domain.com_db-dd-mm-yy.sql

 

Changing Site URL with WP-CLI

If you quickly need to change the WordPress site url on a VPS or dedicated server with WP CLI installed or installable WP-CLI is your friend. It is a great command line based option dealing with url changes during site migration. There are of course great plugins to help out too, but believe me with full server access or VPS access a command line tool like wp-cli can deal with the url issues in no time!

Development to Production

If you just need to move the database from development to production as you will go live with the content you can do this. You replace the database on the production server with the one on the development box. Then you use WP-CLI and then change the urls in a jiffy. Just ssh into your production server and run the search-replace command like this:

wp search-replace 'example.dev' 'example.com' --skip-columns=guid

Afterwards all will work well. All content will be loaded properly with the right urls. Images should be loaded too as long as you moved those to the server as well. I do this often for all new Trellis LEMP box setups at Digital Ocean and I can tell you it works really well. A lot better than the WordPress Importer tool from the Dashboard. Though the importer does work quite well from the command line you still need to deal with images.

Dry Run – Better Safe than Sorry

You can also do a dry run first with –dry-run added. This to see what urls will be changed and to make sure you are running the correct command. So the command will then be

wp search-replace 'example.dev' 'example.com' --skip-columns=guid --dry-run

Actually pretty smart to use. Especially if you are talking a lot data and or complicated url. Better safe than sorry. Will safe you restoring it all with or without backup.

Backup, backup!

Do remember it is always good to backup the database before you do this. Just in case you do not fill in the urls properly once you run things live. Sequel Pro is a great OSX database management tool for backups, changes and replacements of databases.

There is more..

You can do way more with wp-cli than just searching and replacing urls. You can set up full WordPress installations, install themes, plugins, add fields, backup setups and more. I will be sure to write some other applications again as soon as I have the time. Stay tuned!

WP CLI Call to undefined function apply_filters()

Doing a BackupBuddy backup from the commandline using WP CLI on a GoDaddy server the other day I ran into a fatal error. A WP CLI Call to undefined function apply_filters() error to be precise. It did not allow me to backup from the command line at all. The site was running fine.

Anyways, the error I got running the command:

wp backupbuddy backup 2

was the following one:

Fatal error: Call to undefined function apply_filters() in /home/user/public_html/wordpress/wp-includes/load.php on line 317

Troubleshooting

Googling the issue I found out that many websites were displaying similar issues. And no solutions really but to remove the core files and replace them with a fresh set or turning off all plugins and checking if that helps. The basics. But then I stumbled upon this SO thread. There the suggestion was to see if wp cli could be updated. And then I remembered wp cli having some issues with Trellis and WordPress 4.6. And I thought, yeah, that could be it!

Updating WP CLI to 0.24.1

So I ran the updater using the following command:

wp cli update

and it worked updating it all:

 wp cli update

You have version 0.22.0. Would you like to update to 0.24.1? [y/n] y

Downloading from https://github.com/wp-cli/wp-cli/releases/download/v0.24.1/wp-cli-0.24.1.phar...

New version works. Proceeding to replace.

Success: Updated WP-CLI to 0.24.1

WP CLI BackupBuddy Re-Run

After that I ran the command to backup the site again:

wp backupbuddy backup 2

It did not spit out the same error again, but it did take its sweet time showing some feedback. After like 2 minutes it mentioned zip functionality it could not find as usual and then proceeded to do the backup.

Happy ending!

NB Reasons for using wp cli for BB backups on GoDaddy and installation instructions are mentioned here if you need them.

WP CLI Error Running BackupBuddy Backup

Updating a site using BackupBuddy from the command line using WP CLI I ran into the following error related to the LayerSlider 5.6.2 plugin on WordPress 4.4.1.

WP CLI Error Running BackupBuddy Backup

The WP CLI Error running BackupBuddy Backup on a GoDaddy Ultimate Linux hosted site was:

Fatal error: Class 'WP_Widget' not found in /home/user/public_html/wordpress/wp-content/plugins/LayerSlider/wp/widgets.php on line 6

Code Scan

When you check that line inside the LayerSlider widgets.php file you will see

class LayerSlider_Widget extends WP_Widget {

Here the plugin builds upon the WP_Widget. That is pretty common when you want to create your own custom widget. See the Codex article on the WP_Widget here. But looking through the code all seemed to be loaded well and there were no issues with including classes.

WP CLI Upgrade

On contacting the author of the plugin I was told it was an issue with the WP CLI version I was using. So following the instructions in my own post I upgraded WP CLI to 0.22.0 and then I was able to backup all again properly.

BackupBuddy and GoDaddy

BackupBuddy and GoDaddy have never really been buddies really. Been hard to run BackupBuddy on GoDaddy to say the least, but I am here to tell you there are options to make it work on GoDaddy as running backups from the Dashboard fail most if not all the time. The option is running backups from the shell. This  you can of course only do if can access the site from shell and know how to use shell commands and or follow this short tutorial.

WP CLI

First of all you need to install WP CLI, the wonderful WordPress command line that is completely open source. GoDaddy explains how this can be done here. FYI this guide can be followed to upgrade WP CLI as well.

You first access GoDaddy using shell like so:

 ssh user@domain.com

Then you use curl to download WP CLI:

 curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Shell - Download WP CLI
Then you make sure the file can be executed

 chmod +x wp-cli.phar

And to check the functionality of WP CLI you can run the following command:

 php wp-cli.phar --info

Then you create an alias so you can use wp instead of the entire path to the file. Mind you that you make sure you have the PHP phar file in your home directory or else move it there.

 

NB If you were following this guide for updating WP CLI you should be done now.

 alias wp='~/wp-cli.phar'

Once the alias is created add it to .bashrc:

 echo "alias wp='~/wp-cli.phar'" >> .bashrc

and make sure it is taken up in the current Bash session:

 source .bashrc

Once that is done the wp cli command line tool should be working and we can move on to running BackupBuddy from the commandline.

Backup with BackupBuddy from the Command Line

Now from the command line you can do the following command to do a full backup:

 wp backupbuddy backup 2

Number two is related to a FULL backup of the site. Once that command is given a full backup will be run without I/O or RAM issues and you will see that it can be done!

Extra Tip

You could also add a crontab job from GoDaddy (see document at GD here) to run the script automatically on certain times, but that is for another tutorial.