Despite the fact that WordPress comes with its now famous 5-minute installation process, there is quite some boilerplate work to be done in order for those 5 minutes to be effective. So in today’s tutorial, I will teach you how to properly install WordPress on the server we created and configured last week.

In the second part of the tutorial I will also teach you how to migrate an existing WordPress website to your shiny new server! The steps are quite similar, so I recommend you read this tutorial top to bottom, then decide which part fits your needs best.

Note: For the purpose of these tutorials, I’ve registered a domain wp-kickstart.com, which I’ll use in configuration files, as directory names, as usernames,… etc. When you notice this domain, make sure you replace it with your own!

Getting started

First, you need to understand the two main components (building blocks, if you will) that form pretty much every web-based project, and WordPress is no exception: files and data (stored in a database).

Files are the components in charge of the functionality: logging in, posting an article, making a comment, all this is handled via files. Most of the files we’re be dealing with end in .php.

The second component is the data; In WordPress, we have data objects such as an article, a user, a category, tag,… That data is stored in a database (or several), which is handled by what we call a database engine - an application, running on the server, making sure files can communicate with the database via CRUD operations, which stands for Create, Read, Update and Delete. There are also more complex operations, of course, but for the purpose of this tutorial, the basic ones will do.

So when your browser requests a particular article (via URL in the address bar), WordPress’s index.php connects to other files which in turn talk to the database to extract the article from the database, prettify it (our theme does that) and present it back to the user.

Note: Most of the steps in this tutorial can be achieved in two very different ways, either using your browser and an FTP client or using the terminal (with SSH access). I would strongly encourage you to use the latter, not only because it is faster, but you’ll also get to practice server commands, getting gradually better at it. You will need to in every case, since not all speed and security optimizations can be done from within the WordPress. So why not do it now, while we’re handling the basic stuff? :)

Creating a database user

Last week we installed our database engine and in the process, also created our root user. As a refresher, the root user is the god-like user that can do everything to and on the server, and this applies to the database root user as well, it can do everything to any database. Obviously allowing WordPress to connect to the database using the root user would be a big security risk, which is why we need to create a regular user.

Note: the term user might confuse you as it’s used quite often, and in different contexts. It almost never represents a real person, but rather a set of credentials (such as username and password) that we can use to access different parts of the system. So at some point, we will have a database user, an FTP user, a web server user, etc. Even most of the programs running on the server have their own users. Furthermore, each user also belongs to a group, which is just a bucket that holds characteristics and permissions for the users in that group.

In order for WordPress to be able to connect to the database, we need to create it, and a corresponding user that will be able to access just that database. First, let’s connect to the server.

$ ssh Playground

(we configured the Playground shortcut in our [previous tutorial][1], so refer to it)

Once connected, we first need to login to our database engine (MariaDB or MySQL) by entering the following command:

$ mysql -u root -p

Note that we are connecting with the root user as it’s currently the only user with sufficient privileges to create more users and databases.

Next, we need to create a database, I’ll call it wpkickstart, but feel free to name it differently - I’d suggest something like wpkickstartproduction, because it’s easier to see what it belongs to.

$ CREATE DATABASE wpkickstart;

With our database created, it’s now time to create a user that will be able to access it. As with the database name, something descriptive is recommended. And make sure you choose a complex password. That can never hurt :)

$ CREATE USER wpkickstartuser@localhost IDENTIFIED BY "password";

Now that we have a user and a database, it’s time to connect them, or rather allow the user to access it:

$ GRANT ALL PRIVILEGES ON wpkickstart.* TO wpkickstartuser@localhost;

Finally, let’s reset the privileges so that our database engine reloads the configuration changes we just made:

$ FLUSH PRIVILEGES;

That’s it! type exit to return to the server shell. And run the following command which will install the package needed by php to connect to MySQL:

$ sudo apt-get install php5-mysql

And while, we're at it, let's also install GD library that will be used to resize our images:

$ sudo apt-get install -y php5-gd

Download and prepare WordPress

Whether you’re setting up a fresh installation of WordPress or migrating it to a new host, there is a number of steps that overlap, so we’ll install it from scratch here;

First, we need to create a directory that will hold our WordPress files. In order to keep things simple, we will just use the home directory of the webmaster user. I recommend you create a directory with the same name as your domain - it’s easier to locate. First go to the home directory by entering:

$ cd

You could also use $ cd /home/webmaster or cd ~ (the tilda character means home).

Then, create a directory called www (this directory will hold our WordPress install and any other we might have in the future), and go into it:

$ mkdir www
$ cd www

(the command cd stands for change directory, it’s essentially like moving a cursor over a folder and double clicking it).

Now it’s time to download WordPress:

$ wget http://wordpress.org/latest.tar.gz

Once downloaded, you will need to uncompress the file by entering the following command:

$ tar -zxf latest.tar.gz

In order to make sure you’ve done everything correctly, enter

$ ll

and you should see the file latest.tar.gz and a folder wordpress. See the screenshot below. Let’s now rename wordpress to something more specific, I like to use domain names, for clarity:

$ mv wordpress www.wp-kickstart.com

(don’t forget to use your own domain name here)

Install WordPress directory
Creating necessary WordPress directories

One last thing we need to do with our fresh WordPress is create a folder for our uploaded files and set proper permissions on it, so our webserver (nginx) will have proper access (with write permissions).

$ mkdir www.wp-kickstart.com/wp-content/uploads
$ sudo chown :www-data www.wp-kickstart.com/wp-content/uploads/
Install WordPress create uploads
Creating uploads directory with proper permissions

Smashing magazine published an article about WordPress permissions a couple of months ago, if you want to learn more about them.

Our WordPress is now in place, but we can’t access it yet, for that we will need to

Configure Nginx

Note: Before you start with this part, make sure you have a domain or a subdomain available and that it is pointing to the IP address of your VPS (you can find it on the dashboard of your cloud provider). Registering a domain is not part of this tutorial, but it’s really easy if you use a provider like DNSimple. This is what our A record looks like:

Install WordPress DNS records
DNS records (on DNSimple)

If you already have a domain, I recommend you use a subdomain to follow these tutorials, such as tutorials.your-domain.com or something similar.

Last week we installed Nginx on the server, but we didn’t add any virtual hosts. Those are special configuration blocks that define a host. A host corresponds to a domain and configures it’s behaviour based on that domain. For example, WordPress requires php to process the files, so we will need to add that in.

To get started, switch to the directory nginx is installed in:

$ cd /etc/nginx

If you list that directory’s files and folders by entering ll (see screenshot below), you’ll notice two directories: sites-available and sites-enabled. Let’s switch the directory to the former:

$ cd sites-available

Inside we need to create our very first virtual host:

$ sudo nano www.wp-kickstart.com

This command will open an empty text file inside the nano editor. Paste in the following code:

server {
  server_name www.wp-kickstart.com;
  listen 80;

  root /home/webmaster/www/www.wp-kickstart.com;
  index index.php index.html;
  location / {
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ .php$ {
    try_files $uri /index.php;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_index index.php;
  }
}

In this file, we instruct nginx to listen on port 80 for the domain www.wp-kickstart.com, and we configure that our index file is located in the directory /home/webmaster/www/www.wp-kickstart.com.

The block in line 8 allows us to use pretty permalinks and the block in line 12 configures nginx to connect to our php processor when it encounters php files.

That’s it, now exit the editor (CTRL+X, shift+Y, enter). Before we can actually access our server via browser, we need to make this configuration available. In order to do that, let’s change the directory to sites-available and create a symlink (a shortcut to that file), using following commands:

$ cd ../sites-enabled/
$ sudo ln -s ../sites-available/www.wp-kickstart.com

All you have to do now is restart nginx and you should be all set:

$ sudo service nginx restart

Using your browser now visit the domain you just configured, and if you see a WordPress logo with some instructions below, pat yourself on the back for a job well done!

Install WordPress

The first screen of the WordPress installer just gives some basic instructions with no action required on our part yet, so just click next.

WordPress Install welcome screen
WordPress Install welcome screen

Now, we need to enter the database credentials we created earlier, when setting up our database.

Install WordPress database credentials
Database credentials input screen

Once done, we will be asked to copy wp-config.php. Switch to your terminal, and change the directory to where your WordPress is:

$ cd ~/www/www.wp-kickstart.com
$ nano wp-config.php
Install WordPress php config
wp-config.php contents
Install WordPress source files
WordPress source files

In here, just paste the contents of the file that installer gives you, exit the nano editor, and click Run the install in the browser.

Almost there!

The last step in the installer requires you to insert some basic information about your WordPress, and also to create the first user, which is usually named admin (not required though, name it differently if you want to):

Install WordPress site configuration
WordPress site configuration

After you’re done, just click Install WordPress and you’re finished! Your WordPress is now properly installed! Log in and smell the fresh installation you masterfully created :)

FTP configuration

As I mentioned in the previous tutorial, I’m not a fan of FTP, but I find it really useful for one purpose, installing plugins. Before we can do that, we need to configure vsFTPd:

$ sudo nano /etc/vsftpd.conf

There are many options in this file and no worries if you don’t understand them all, you don’t need to, really. Just make sure the following ones are set (if they are missing in the file, just add them):

local_enable=YES 
write_enable=YES 
chroot_local_user=YES 
allow_writeable_chroot=YES

Exit the editor and restart your FTP daemon, by entering the following command:

$ sudo service vsftpd restart

The last thing, as usually, is to test whether our configuration is working. To do that, let’s log in to our new WordPress and install a plugin (whichever you like, I’ll use WordPress SEO by Yoast, as it’s on my list of must-haves).

First, visit the plugins page, and click Add New at the top:

Install WordPress plugins list
WordPress plugin management screen

Then, enter Yoast in the search bar on the right:

Install WordPress SEO by Yoast
Install WordPress SEO by Yoast

Most likely the first hit will be the correct one, so click Install Now. The moment of truth! Enter the hostname (www.wp-kickstart.com in my case), and your webmaster credentials that we used all along, then click Proceed. If your FTP configuration is in order, you should now see the following screen:

WordPress plugin installed
WordPress plugin installed

That’s it, our WordPress is now fully operational!

(For all you WP ninjas out there, I’m planning to cover the secure FTP configuration next week as a part of securing our WordPress tutorial)

Migrating/Transferring WordPress from another host

Now that we know our new server is fully capable of running WordPress we will migrate an existing website on top of our fresh installation. If you want to do it fresh, the same steps apply as well, so just follow along. First we need to export all the files and the database.

Migrating files

Note: The process of exporting data and files from the server is exactly the same like when we’re doing a backup (which we will automate in one of our future tutorials), so I’ll use this terminology going forward.

It’s difficult to cover every possible scenario of downloading your existing files since many hosting companies provide their own solutions of doing that, but most of them support downloading files via FTP. What you should do is connect to your host and just copy files over to your computer.

Install WordPress ftp transmit
Transmit, a MacOS FTP client

Some hosting providers allow you to download a backup directly through an admin panel, which will give you a zip or a tar.gz file of all WordPress files.

If you downloaded all the files separately, zip (compress) them into one file before you proceed. Uploading them one by one might take a while, and now that we’re mastering SSH, why not use it to our advantage.

Upload your zip file through FTP into your www/www.your-domain.com (or something similar, just create it with FTP) directory that you created while installing WordPress. Let’s call this file archive.zip

Now, SSH to the server (in case you exited), and change directory to this new folder, install unzip, then unzip the compressed file:

$ cd ~/www/www.your-domain.com
$ sudo apt-get install unzip
$ unzip archive.zip

You now hopefully have the index.php in this directory. Enter the following command to verify:

$ ll

Optional: In case you have another directory inside (let’s call it website), and that directory holds our WordPress files, just move all the files from that directory to it’s parent dir, like this:

$ mv website/* .

(The asterisk stands for every file or directory and the dot just means current directory)

Also don’t forget to edit the /etc/nginx/sites-available/www.your-domain.com (or whatever you named it) and fix the root option to this new directory, to make sure that nginx serves correct files (see the section on installing WordPress for the correct commands).

Backing up database

Now that our files are in order, we need to backup the database on our existing host and transfer it to our new server. The easiest way to accomplish this is to export it with PhpMyAdmin, which is a tool that many hosting companies provide out of the box.

So log in to the PhpMyAdmin, select the database on the left pane, then click the export button in the main navigation bar.

Install WordPress PHPMyAdmin
PHPMyAdmin

You’ll be presented with two export methods, choose custom and scroll all the way down to the section titled Object creation options. Here, check the second option that adds DROP TABLE…. This is useful when you’re importing the database over an existing one. If you’re importing to a new database, however, this isn’t required.

Install WordPress PhpMyAdmin export
PhpMyAdmin export

Scroll to the bottom of the page and click Go, which will download the sql file to your hard drive.

Importing database

Just like we did with all other files in the previous section, upload the sql file (let’s call it backup.sql) to your www directory via FTP. Once there, return to the terminal and navigate to that directory:

$ cd ~/www

Now it’s time to import the data. If you followed along with the installation, then you can either reuse the existing database (wpkickstart, in my case) or create a new one, following the database creation steps above.

To import the sql file, just run the following command:

$ mysql -u wpkickstartuser -p wpkickstart < backup.sql

Don’t forget to change the user and the database to whatever you chose when creating them!

You’ll be prompted for the password you assigned to the database user and once entered the data will be inserted in the database - a process which may take a while, depending on how big your database is.

Depending on whether you used an existing database or created a new one, you must now update the ‘wp-config.php’ with the right database credentials.

That’s it, you should now have a migrated WordPress!

Troubleshooting

As the process of migration depends on many factors, such as hosting providers’ environments, your server configuration, software versions, there are many cases in which this process can go wrong, so if the migration didn’t work for you, check the following:

  • are WordPress files in correct location, such as /home/webmaster/www/www.your-domain.com
  • is the nginx virtual host file properly configured? I.e. is the root directive pointing to the correct directory?
  • if you can’t upload files, does the upload folder exist and has the correct permissions?
  • is your wp-config.php updated with proper set of database credentials?
  • is your DNS properly configured and pointing to the right IP address?

Conclusion

One more tuesday, one more tutorial to make your life with WordPress easier :)

As you can see, it’s fairly easy to install WordPress on a preconfigured server, despite the fact this tutorial is not the shortest around. And trust me, once you do it a couple of times, it’ll become muscle memory - just don’t give up on using terminal commands as you’ll need them in more important tutorials that we’re preparing.

One of them is coming next tuesday where I’ll cover securing access to our new WordPress; We will install and configure an SSL certificate so that we’ll serve our pages through the HTTPS protocol. Also, we will switch from using the regular FTP to a more secure version (sFTP).

Like always, if you have any questions, ask in the comments below.

New Call-to-actionQuality: The Codeable Differene