Skip to main content

Install WordPress from CLI


Published: April, 26 2022 | DevDrawer

At my agency, we do a lot of work with WordPress. If you are also a WordPress developer, you may be in the same situation. When you are creating a new local development site with WordPress, it could take time...more time than you want to spend. If this is the case, try to install it all from CLI. It is easy to do if you are able to SSH into your local or remote server.

View This On YouTube

SSH Into Your Server

This entire tutorial relies on you using SSH and having an already existing server. So, stop and set that up if you have not already done so. Also, keep in mind you can use things like Docker to do this but if you normally do this manually, it may save you some time.

Open your CLI and type the following command (change out the username and server ip address to match your setup.

ssh username@192.168.0.1
Enter fullscreen mode Exit fullscreen mode

Enter your password and create your new virtual host / folder. I use Roverwire Virtualhost Manage Script. It makes it as to create virtual hosts on a Linux server using only SSH. You can view more about it here: https://github.com/RoverWire/virtualhost

If you use Roverwire, you can simply type:

sudo virtualhost create sample.local
Enter fullscreen mode Exit fullscreen mode

Once that is done, we can get WordPress set up.

Download and Extract WordPress

First things first, create a new folder for your website. In this tutorial, I will be using sample.local.

The following commands will install a folder called wordpress in your newly created server folder.

Switch to the folder you created on the server

cd /var/www/samplelocal/
Enter fullscreen mode Exit fullscreen mode

Download latest Version

sudo wget  http://wordpress.org/latest.tar.gz
Enter fullscreen mode Exit fullscreen mode

Extract WordPress

sudo tar xfz latest.tar.gz
Enter fullscreen mode Exit fullscreen mode

Move Folders

Once you have the WordPress folder, you will need to move it and then remove the extractable file you downloaded previously.

Move WordPress folder to the parent

sudo mv wordpress/* ./
Enter fullscreen mode Exit fullscreen mode

Remove WordPress folder

sudo rmdir ./wordpress/
Enter fullscreen mode Exit fullscreen mode

Remove downloaded file

sudo rm -f latest.tar.gz
Enter fullscreen mode Exit fullscreen mode

Finally, after setting this up, you can move on to setting up the database using the MySQL instructions below. Once the database is set up, you can go to the domain you created above and run the typical WordPress installation.

Setup Database

The username and password combination for your MySQL server should already be set up. You can log in by using the following command:

mysql -p -u mysqlusername
Enter fullscreen mode Exit fullscreen mode

You will then be prompted for the password.

Once you have logged in successfully, you should now see a MySQL prompt instead of the normal SSH prompt. It will look like this:

mysql>
Enter fullscreen mode Exit fullscreen mode

Create Your Database

create database databasename;
Enter fullscreen mode Exit fullscreen mode

Exit MySQL

exit
Enter fullscreen mode Exit fullscreen mode

Lastly, you may need to set up shared or global permissions to your new website. You can do this a few different ways, but I found that a shared user works best:

sudo chown -R shareuser /var/www/samplelocal
Enter fullscreen mode Exit fullscreen mode

or for a global user

sudo chown -R www-data:www-data /var/www/samplelocal
Enter fullscreen mode Exit fullscreen mode

Modify Hosts File

Now your WordPress website is ready to go. To view your new website locally, you will need to modify your hosts file. You can do this using the following instructions:

Windows is typically located at
%SystemRoot%\System32\drivers\etc\hosts

OSX and Unix/Linux is typically located at
/etc/hosts

Add the following to your host file. domainname.fdg should be changed to the virtual host that was created above.

192.168.0.1 sample.local
Enter fullscreen mode Exit fullscreen mode

Open Your Website

Now that you have successfully created your virtual host, installed WordPress, and set up your hosts. You can browse the website URL to finish the installation in the WordPress installer.

Here is a quick video on how to set up WordPress once you get to this step.

Good luck.

Read more articles on DevDrawer


1 9


Share this Article

Like what you see, support the channel.
Buy Me a Coffee