How To Set Up WordPress with a Remote MySQL Database on Ubuntu 14.04

Introduction

WordPress has taken over the world as the most popular Content Management System. Not only is WordPress easy to use, it's easy to install and implement. Ubuntu is one of the most popular Linux operating systems. It's user friendly which makes it the ideal operating system for this tutorial.

Sometimes it's necessary to separate your application and your database, whether you want to split the load, or you always want to keep your databases separate. By the end of this guide, you will have a fully working WordPress installation in one server and its fully functional database in a remote server. We will go through the basic steps of setting up LAMP, downloading, configuring, and installing WordPress. We will be installing all of this in two servers running Ubuntu 14.04.

Prerequisites

Before we get started, you will need:

  1. Two Servers (VPS) running Ubuntu 14.04 - one server to house your WordPress installation, the other to house your database
  2. Non root account on the servers with sudo privileges - it's always best practice to install and run applications from a non-root account for increased security
  3. SSH Client - PuTTY is the recommend SSH client - download it Here - SuperPutty may be even more useful since we are dealing with multiple servers and this software allows you to have multiple tabs open at once. Download SuperPutty Here.
  4. Browser (Firefox or Chrome recommended) - to view, configure, and use your finished product
  5. A Domain Name - For the purposes of this tutorial, a domain name is optional. The servers' IP Addresses will be sufficient. In this tutorial, domain.tld will be used to reference your domain name.

If you have everything you need, we're ready to proceed.

LAMP

LAMP stands for Linux, Apache, MySQL, and PHP. These are the basic tools we need in order for WordPress to function properly. If you have Ubuntu 14.04 installed, you're a quarter of the way there. Apache is the web server which will allow you and the rest of the world to view your site. MySQL is the database management system which will house all of the content that we will see in our WordPress installation, including but not limited to, Posts, Comments, Plugins, and even Settings. And PHP is the system which will allow WordPress to properly run since it's coded in PHP.

If your servers are LAMP ready, feel free to skip to the WordPress section below.

If you need LAMP, for the purposes of this tutorial, we will install LAMP on both servers to ensure that all necessary components are installed in both servers. You can either follow these steps on both servers simultaneously, or go through the steps twice. Once you have logged into your server using an account with sudo privileges, continue...

LAMP Step 1 - Install Apache

It's time to install Apache as our web server. It's an easy package to install and Ubuntu makes it even easier because Apache can be installed using Ubuntu's package manager, known as apt.

In the command line, type:

sudo apt-get update
sudo apt-get install apache2

The apt-get update command will ensure the package manager is fully up to date. Then install apache2 will search for the proper package and take care of the full installation for you. When installing apache2, it will ask for you to confirm that you want to use additional disk space to install Apache. Type y to continue with the installation.

That's it! You've installed Apache. There are two ways to test to make sure that Apache is indeed install and running. The first way is very easy and it requires you to remain in your SSH window. Just type the following command:

service apache2 status

The output should read

apache2 is running

If you don't see the above output, and instead, it says unrecognized service, go back a few steps and make sure you installed apache2 AND you said yes to the prompt. Also double check your spelling to ensure you didn't miss anything. You can also start/restart the Apache service with the following command if the service is stopped:

sudo service apache2 restart

The second way to tell if Apache is installed is to go to your Browser and visit your website. If you have a domain name, visit your domain.tld. For the purposes of this tutorial, our IP address will serve as our gateway. And you should see a screen similar to the following:

Apache Success

It's recommended to use both methods of testing because even though the apache2 service may be running, a bad configuration could throw an error only visible when viewing through a browser.

LAMP Step 2 - Install MySQL

In this part of the tutorial, we will be installing MySQL server. Even though our database will be housed in our second server, feel free to install mysql-server on both servers. To install MySQL and its necessary components, type:

sudo apt-get update
sudo apt-get install mysql-server php5-mysql

If you're working on your second/new server, you will need to run sudo apt-get update followed by the actual MySQL install command. The php5-mysql component will ensure that our PHP and MySQL components will work properly.

Note: If you choose not to install MySQL on your primary server housing your WordPress installation, you will still need to install the php-mysql component on that server to allow the two to function properly. So it may be best to install all components of LAMP on both servers.

During the installation process, it will prompt you for a root MySQL password in a UI similar to the screenshot below. Enter your desired password to continue with the installation.

MySQL Password

After you've confirmed your new root password, MySQL should be installed and running. Just like Apache, it's easy to check if MySQL is running. To check its status, run the following command:

service mysql status

The reason the sudo command isn't necessary when checking for service status is because we're only looking to see if a service is running, we are not attempting to write or access anything restricted. This does not require root privileges. After you run the command, you should see something similar to the following:

mysql start/running, process 978

If you don't see the above output, make sure you followed the steps properly and have no typos.

Now we need to take a few additional steps to ensure the security of our MySQL installation. First, tell MySQL to create its database directory structure by typing:

sudo mysql_install_db

After running the above command, run one more command to further secure your MySQL installation:

sudo mysql_secure_installation

At this step, it will ask you to enter your current root password, this is the root MySQL password you entered above.

In order to allow a remote WordPress installation to connect to our database, we need to make a quick tweak to the MySQL configuration. The configuration lies in a file called my.cnf. To edit this file, type:

sudo nano /etc/mysql/my.cnf

Scroll down until you find bind-address and comment it out with a # symbol as follows:

#bind-address         = 127.0.0.1

This will also ensure that it allows external connections.

Quick Tip: To quickly find this line, press ctrl+w and type in bind-address and press enter, it will take you to this line directly.

Your MySQL installation should now be ready.

LAMP Step 3 - Install PHP

It's time to install PHP. This time, we won't need the sudo apt-get update because we already ran it before installing Apache earlier in this tutorial. So this time, run the following:

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt php5-gd libssh2-php

We are actually installing 5 components at once with the above command. It's more efficient because it saves time and it's easier to run one command rather than 3 different ones. In this command we install php5 which is the basic PHP5 component. Next, we install libapache2-mod-php5 which is a module for the Apache web server designed to make it easier for PHP and Apache to work together. Thephp5-mcrypt component is a library necessary to install many types of CMS'. The php5-gd and libssh2-php components are also necessary for WordPress to work properly. If you did not install php5-mysql as shown in the MySQL section of this guide, add it to the end of the above command line. For more information on these components, visit the PHP manual at PHP.net.

PHP is not a service like Apache. So testing to see if PHP is installed properly will be a bit different. To do this, we want to create a file that will run the PHP info function and tell us if PHP is installed properly and which components of PHP are available. To do this, navigate to the html directory by typing:

cd /var/www/html

This is the root document library for your public facing files. Ubuntu makes it easy to create and save new files because it comes installed with the nano editor. We'll name our new file info.php. To create this file type:

sudo nano info.php

Remember to type sudo in order to make sure you are creating this file with the proper permissions. You will see the GNU nano editor interface. Within this editor, type the following lines:

<?php 
    phpinfo();
?>

Don't worry about the spacing. The phpinfo(); command is a built in PHP function designed to output everything about the PHP installation on the local server. To save and close this file by pressing ctrl+x and press y to confirm changes. To confirm that your new file was created, you can type ls or dir to get a directory listing of all files within the /var/www/html folder.

Now go to your browser, type in http://domain.tld/info.php and you should see something similar to the following screen:

PHP Success

It's highly recommended that you delete the info.php file after you verify that PHP is installed and working. Why? If you see the page, it gives a LOT of information about your server and it could be the opening a hacker needs to gain entry into your server. To remove the file, type:

sudo rm info.php

To confirm that it's removed, view your directory listing by typing ls. If you ever need this PHP info page, you can always easily recreate it.

WordPress

Once all the components of LAMP are installed, we can move forward with creating our database and installing WordPress.

WordPress Step 1 - Create Your Database

The database should be created in the second server created to house the database. Once logged in, type the following line to access your MySQL Root account:

mysql -u root -p

You will be prompted for the MySQL root password. Once you are successfully authenticated, create your database with the following command:

CREATE DATABASE wordpress;

Note: All MySQL statements must be terminated with a semi-colon (;) which signifies the end of a particular statement.

Next, we need to create a user account which will govern the newly created database. We will call this user wpuser and assign it the password p@ssw0rd. When creating the user, it's highly recommended to use a much more secure password because this is the key to your entire database. To create a user, type:

CREATE USER wpuser@IP_ADDRESS IDENTIFIED BY 'p@ssw0rd';

Replace the IP_ADDRESS with your primary server's IP address. This will ensure that a remote connection can be made, otherwise, your WordPress installation will not be able to connect to your database server. Adding the IP address will also tell MySQL that only this IP should be able to connect to his user. After our user has been created, we need to tell our database that this user has authorization, also known as privileges, to read and write to the database. To grant privileges, type:

GRANT ALL PRIVILEGES ON wordpress.* TO wpuser@IP_ADDRESS;

Once again, remember to replace the IP_ADDRESS with your primary server's IP. This command will allow the user wpuser to do anything and everything on the database wordpress. Run the flush command below to let MySQL know about the privileges changes:

FLUSH PRIVILEGES;

We're now done with the MySQL prompt, exit this part of the interface by typing:

exit

WordPress Step 2 - Download WordPress

It's time to go back to our first server in which we installed Apache and PHP. We will now download WordPress, to do this, type:

cd ~
wget http://wordpress.org/latest.tar.gz

This command takes us to our root directory where we will download the latest version of WordPress. Thankfully, WordPress makes it easy for us to always download the latest version of their CMS by keeping the download URL the same. The .tar.gz file is the compressed version of WordPress, we need to extract it by typing:

tar xzvf latest.tar.gz

This will extract the contents of the compressed file into a directory called wordpress.

WordPress Step 3 - Configure WordPress

Once WordPress is installed, it's time to dive into the configuration. First, navigate into the wordpress directory to locate the wp-config file:

cd wordpress

To see a list of all files within the directory, type ls. By default, the configuration file is titled wp-config-sample.php. We will make a copy of this file and call the new file wp-config.php. To do this, type following command:

cp wp-config-sample.php wp-config.php

Now we can edit the configurations. To edit the file, type:

nano wp-config.php

We only need to make a few edits to allow WordPress to connect to our remote MySQL server, in the wp-config.php file, locate the following lines and change fields next toDB_NAME, DB_USER, DB_PASSWORD, and DB_HOST:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wpuser');

/** MySQL database password */
define('DB_PASSWORD', 'p@ssw0rd');

/** MySQL hostname */
define('DB_HOST', 'IP_ADDRESS');

Change the above information to the appropriate values. The username does not require the @IP_ADDRESS however, make sure in DB_HOST you replace IP_ADDRESS with the actual IP Address or the domain name of your database server.

Once you are done with the configurations, save and close the file by pressing ctrl+x and accepting the changes.

WordPress Step 4 - Make WordPress Public

It's time to move the WordPress directory into Apache's document root so that we may access it from our browser. To do this, type:

sudo mv ~/wordpress/ /var/www/html/

This will copy all files within and including the wordpress directory to /var/www/html which is the Apache document root.

Now let's enter the document root directory by typing:

cd /var/www/html

Then type ls to confirm that the wordpress directory is listed.

WordPress Step 5 - Install WordPress

Now we're finally ready to install WordPress. To do this, we will escape from our SSH client and head to the browser. You can navigate to your new WordPress site by visiting http://domain.tld/wordpress. If you are using your IP address, use http://ip_address/wordpress.

Your page should look something similar to this:

WordPress Install Page

Fill out the required information on the site and click the Install WordPress button at the bottom. If installation is successful, you will see the screen below, click the Log In button to login:

WordPress Install Success

Successful login will display a similar screen to the following:

WordPress Dashboard

Conclusion

We have gone through the steps to successfully Set Up WordPress with a Remote MySQL Database on Ubuntu 14.04. Your WordPress installation should now be complete with the CMS running on one server while the database is housed in another. The site itself can now be managed directly from the admin panel. Updates, upgrades, addition of new themes, plugins, and even editing the actual code of your WordPress files can be done through /wp-admin/. For security reasons, it's always recommended to keep your blog, plugins, and themes up to date. This will decrease the likelihood of vulnerability exploitation.

Your next steps should be configuring WordPress itself, such as the site's permalinks, appearance, and plugins. Jetpack is a plugin worth installing because it is a bundle of many useful tools including analytics, design, social networking, and more. The Growmap Anti Spambot plugin is also a very useful plugin to keep your blog free of spambots.

Happy Blogging!