In this part of the tutorial, we will deploy our example PHP application, WordPress, and a private DNS:
Your users will access your application over HTTPS via a domain name, e.g. “https://www.example.com”, that points to the load balancer. The load balancer will act as a reverse proxy to the application servers, which will connect to the database server. The private DNS will enable us to use names to refer to the private network addresses of our servers which ease the process of configuration of our servers.
We will set up the components that we just discussed on six servers, in this order:
Let’s get started with the DNS setup.
Using names for addresses helps with identifying the servers you are working with and becomes essential for the maintenance of a larger server setup, as you can replace a server by simply updating your DNS records (in a single place) instead of updating countless configuration files with IP addresses. In our setup, we will set up our DNS so we can reference the private network addresses of our servers by name instead of IP address.
We will refer to the private network address of each server by a hostname under the “nyc3.example.com” subdomain. For example, the database server’s private network address would be “db1.nyc3.example.com”, which resolves to it’s private IP address. Note that the example subdomain is almost completely arbitrary, and is usually chosen based on logical organization purposes; in our case, we “nyc3” indicates that the servers are in the NYC3 datacenter, and “example.com” is our application’s domain name.
Set this up by following this tutorial, and adding DNS records for each server in your setup:
After completing the DNS tutorial, you should have two BIND servers: ns1 and ns2. If you already know the private IP addresses of all of the servers in your setup, add them to your DNS now; otherwise, add the appropriate DNS records as you create your servers.
Now we’re ready to set up our database server.
Because we want to load balance the our application servers, i.e. the ones running Apache and PHP, we need to decouple the database from the application servers by setting it up on a separate server. Decoupling the database from the application is an essential step before horizontally scaling many types of applications, as explained in this blog post: Horizontally Scaling PHP Applications: A Practical Overview.
This section covers all of the necessary steps to set up our database server, but you can learn more about setting up a remote, decoupled MySQL database server for a PHP application in this tutorial: How To Set up a Remote MySQL Database.
On the database server, db1, install MySQL Server:
Enter your desired MySQL root password at the prompt.
Now run:
You will have to enter the MySQL administrator’s password that you set in the steps above. Afterwards, it will ask if you want to change that password. Type “N” for no if you’re happy with your current password. Answer the rest of the questions with the defaults.
Open the MySQL configuration file:
Find the bind-address
setting, and change it to the address of the private network address of your database server:
bind-address = db1.nyc3.example.com
Save and exit.
Restart MySQL:
Now we need to create the database and database users that the application servers will use to connect.
Enter the MySQL console:
Enter the MySQL root password at the prompt.
At the MySQL prompt, create the database for your application:
MySQL associates its users to the servers that they should be connecting from. In our case, we have two application servers that will be connecting, so we should make a user for each of them. Create a database user, “appuser” in our example, that can be connected to from private network address of each of your application servers (app1 and app2). You should use the same password for each user:
We will configure the final database user privileges later, but let’s give appuser full control over the app database:
These relaxed privileges ensure that the application’s installer will be able to install the application in the database. If you have more than two application servers, you should create all the necessary database users now.
Exit the MySQL prompt now:
The database server setup is complete. Let’s set up the application servers.
The application servers will run the code of our application, which will connect to the database server. Our example application is WordPress, which is a PHP application that is served through a web server such as Apache or Nginx. Because we want to load balance the application servers, we will set up two identical ones.
This section covers all of the necessary steps to set up our application servers, but the topic is covered in detail in the following tutorial, starting from the Set Up the Web Server section: How To Set Up a Remote Database.
On both application servers, app1 and app2, install Apache and PHP:
We will be using HAProxy, on the load balancer server, to handle SSL termination, so we don’t want our users accessing the application servers directly. As such, we will bind Apache to each server’s private network address.
On each application server, app1 and app2, open your Apache ports configuration file. By default, this is the ports.conf
file:
Find the line that says Listen 80
, and add your private IP address to it, like so (substitute in the actual IP address of your server):
Listen private_IP:80
Save and exit. This configures Apache to listen only on the private network interface, which means it cannot be accessed by the public IP address or hostname.
Restart Apache to put the changes into effect:
Apache is now accessible via only the private network address of your application servers. We will configure the load balancer to send user requests here, in a moment.
In our example, we are using WordPress as our application. If you are using a different PHP application, download it and perform any relevant configuration (e.g. database connection information), then skip to the next section.
On the first application server, app1, download the WordPress archive:
Extract the WordPress archive:
Change to the extracted directory:
WordPress needs a directory to be created for its uploads, wp-content/uploads
. Let’s do that now:
We will use the sample WordPress configuration file as a template. Copy it to the proper location:
Now open the configuration file for editing:
Configure the WordPress database connection by changing the highlighted information in the following lines:
/** The name of the database for WordPress */
define('DB_NAME', 'app');
/** MySQL database username */
define('DB_USER', 'appuser');
/** MySQL database password */
define('DB_PASSWORD', 'password');
/** MySQL hostname */
define('DB_HOST', 'db1.nyc3.example..com');
Because we are going to use TLS/SSL encryption on the load balancer server, we must add the following lines so WordPress will be aware that it is behind a reverse proxy that is using SSL:
define('FORCE_SSL_ADMIN', true);
if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS']='on';
You will also want to update the keys and salts, so you can invalidate cookies when you want. We won’t cover this here but make sure that they are identical on all of your application servers.
Save and exit.
WordPress is now configured, but its files must be copied to the proper location to be served by our web server software.
Now that we have our application configured, we need to copy it into Apache’s document root, where it can be served to visitors of our website.
The default location of Apache’s DocumentRoot is /var/www/html
, so we will use that in our example.
First, delete the default index.html
file:
Then use rsync to copy the WordPress files to /var/www/html
, and make www-data
(the user that Apache runs as) the owner:
Our first application server, app1, is ready. We will set up the other application server.
In order to keep your application’s files consistent across your various application servers, you should set up file replication of your web server’s document root. In the case of WordPress, using the web interface to upload files and install plugins will store the files on the particular server that processes the request. If these files are not replicated to all of your application servers, some of your users will be served pages with missing images and broken plugins. If your PHP application is not WordPress and does not store any of its data (e.g. uploaded files or downloaded plugins) on the application server, you can just copy the application files manually, once. If this is the case, use rsync to copy your application files from app1 to app2.
GlusterFS can be used to create a replicated volume of the necessary files, and it is demonstrated in the Synchronize Web Application Files section of this tutorial: How To Use HAProxy as a Load Balancer for WordPress Application Servers. Follow the instructions (skip the Edit Hosts File section, as our DNS takes care of that) and set up replication between app1 and app2.
Once your replication is set up properly, both of your application servers should be configured properly. Let’s set up our load balancer now.
Our load balancer server will run HAProxy, which will serve as a reverse proxy load balancer for our application servers. Your users will access your application through the the load balancer server via a URL such as https://www.example.com
.
This section covers all of the necessary steps to set up our load balancer server, but the subject is covered in detail in the following tutorials:
Perform these steps on the load balancer server, lb1.
In the directory that contains your SSL certificate (one of the prerequisites from the part 1), combine your certificate, any intermediate CA certificate, and your certificate’s key into a single .pem
file. For example (our certs are in /root/certs
:
Then copy the pem file to /etc/ssl/private
:
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This 6-part tutorial will show you how to build out a multi-server production application setup from scratch. The final setup will be supported by backups, monitoring, and centralized logging systems, which will help you ensure that you will be able to detect problems and recover from them. The ultimate goal of this series is to build on standalone system administration concepts, and introduce you to some of the practical considerations of creating a production server setup.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi, running the mysql-server installation returns an error: FATAL ERROR: Neither host ‘MFD-FRA-DB1’ nor ‘localhost’ could be looked up with /usr/bin/resolveip
This comment has been deleted
Well the problem was way stranger… I used the most recent Ubuntu version 15.x. totally not working. With the LTS 14.x everything is fine!!
Wow, this is such an amazing guide! You could charge for this easily. Thanks for the freebies :D
I’m loving this high quality tutorial series, thanks for sharing it!
I think there is a typo in the “Restart HAProxy” section, the command proposed restarts rsyslog instead of HAProxy.
Does an application have to have these many servers when it starts out? I currently use one droplet for the entire application I’m developing.
What a great tutorial series. It’s such a relief to go through all these steps without hitting a brick wall where you end up spending hours looking for a solution.
Nice work!
Hi,
I have query regarding domain that we are using to access our web application in this tutorial.
Let’s say I have purchased domain xyz.do. I have pointed that domain to DO nameservers ( By navigating to Networking ->Domain Tab in DO control panel). Also added the IP of Load balancer droplet in A record.
Now By following this tutorial I have created 2 droplets for nameserver(ns1,ns2) for zone “xyz.do”. In ns1 droplet, I am creating zone file for “xyz.do”, where I will be giving private IP address of all the other servers(lb1,db1,app1,app2).
This creates confusion for me. DO nameservers (i.e. ns1.digitalocean.com) are managing “xyz.do” domain and same domain is also managed by ns1 droplet(created for this tutorial). In final, I will access my application through “xyz.do” which is pointed to public ip of Load balancer droplet.
Please correct me.
Great tutorial series! Thanks for all the information. Quick question, so you’re spinning up 6 total droplets for each server correct?
Good stuff. Thank you, sir.