Something I really like about developing with other languages / frameworks is that many of them have servers built in. This is very convenient, so it was frustrating not having this with WordPress. Over the years I have run WordPress in many different ways, MAMP, MAMP Pro, LAMP, XAMPP, Flywheel, Vagrant, Docker etc. But all methods I’ve used have had issues where I have had to spend a lot of times troubleshooting it. The built in servers that Ruby, Jekyll etc have make local dev simple and quick. You can get on with the actual coding and not focus to much on the server stuff. But PHP does have a inbuilt PHP server, since 5.4.0, it just doesn’t get mentioned very much. It’s actually a great way of running a local install of WordPress when you don’t need bells and whistles, when all you need is a basic WordPress install.
You’ll need PHP and MySQL
All you need is PHP installed and a MySQL server running, which I won’t cover in this tutorial, since it’s is platform specific. So from here on, I’m going to assume from here that you have them installed. Google it and come back when your setup. To check what you already have installed, type or paste the following into the terminal.
php -v mysql -V
To start, CD into your WordPress folder. This can be anywhere, you don’t have to stick to a specific folder as with other methods. I find this makes keeping my files in order a little easier. All you need is a folder with WordPress in it. You need to be at the root of your WordPress install, ie, where “wp-content” is. If you are starting from scratch, you can use this command to download WordPress, unzip it and cd into it.
wget wordpress.org/latest.zip && unzip latest.zip && cd wordpress
I have a “Dev” folder containing my projects. I’ll have a project folder that contains all relevant files for the project and I’ll have a WordPress folder that will contain my WordPress install for that project. You’ll need a database, to create one from the command line type:
mysqladmin -u root -p create databasename
You’ll need to provide your MySQL password. If you don’t know it, see this tutorial that covers setting and recovering your password.
Starting the server
Whilst in the WordPress folder you can type or paste to start your php server.
php -S localhost:8000
Now you can go to http://localhost:8000 to see your WordPress site and to run through the setup. When it comes to the database info, you use the same user name and password you used for setting up. Note: I don’t recommend using you root user and pass for a live site, only a local site not accessible to the public.
Running more than one
You can change the port number to something else if you run more then one at the same time or that port is in use.
php -S localhost:8001
You can also use port 80 which would mean that you don’t have to type the port for the URL but for me on Linux, that had to be done with sudo. Which is fine but messes up permissions, somewhat negating the simplicity of this method.
Stopping your server
To stop any running process, you can just close the terminal window ( or tab ) or press CTRL+C.
WP CLI provides a wrapper for this and has lot’s of commands that aids setup and install. See our tutorial on that.