Running Your Web Server And Database Servers On Different VPSs
Previously we looked at the advantages of splitting your various server functions between multiple VPSs rather than combining everything onto a single larger server. Here, we’ll look at a practical application of this by looking at how you’d go about configuring two servers, one as database and one as a web server for your website.
Getting Started With Multiple VPSs
We’ll start with 2 bare Linux VPSs. In this case we’ll be looking at the commands to configure this on Debian and Ubuntu, but there’s no reason why you couldn’t use another distribution.
First we’ll install Apache and PHP to the web server:
sudo apt-get update
sudo apt-get install apache2 php libapache2-mod-php php-mcrypt php-mysql
For the most part, Ubuntu auto-configures things to work for you. So once installed you should be able to navigate to your web server’s IP address in a web browser and see the default Ubuntu holding page.
On the server create a test file for checking the PHP configuration:
sudo nano /var/www/html/phptest.php
Now paste in the following:
<?php
phpinfo();
?>
Save and exit the file, then navigate to http://<your vps IP>/phptest.php and you should see a page detailing the PHP configuration on your server. If that works then you are all good and ready to go with your web server.
Configuration
The next step is to configure the database server. To do that, we’ll log into that server and install MySQL:
sudo apt-get update
sudo apt-get install mysql-server
You will be prompted to set a password for the MySQL root user during the installation. The next step is to configure MySQL to listen for connections from your web server, as by default it is configured to only listen for connections from the local server:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Now identify the line that looks like this:
bind-address = 127.0.0.1
You will need to change the IP address from the one shown in the example above to the IP address of your VPS. Save and exit the file, then restart MySQL:
sudo systemctl restart mysql
At this point it’s worth noting that you’ll want to limit access to your server’s MySQL server to only your web server. Assuming you plan to use the iptables firewall on your server, then a command like the one below can be used. Note that you’ll need to change the IP from 192.168.0.1 to the IP of your web server.
sudo iptables -I INPUT -m state –state NEW -p tcp –dport 3306 -s 192.168.0.1 -j ACCEPT
With that done, your web server and database server are configured to work together.
Important Notes
Going forward, when you are installing a web application on your web server that uses your database, things will be a little more complicated than they were previously. Normally when you add a user to MySQL, it is done in the format of ‘user’@’localhost’ to specify the username and the host from which you will be connecting. In future, when adding users for applications you will need to replace ‘localhost’ with the IP address of your web server as MySQL checks the source host of the connection before deciding whether to allow access for a user. Also, when you configure your web application you need to ensure that when providing the username and password for your database user, you change the default value (usually localhost) in the host section to the IP address of your database server. Unfortunately this isn’t a simple thing to provide guidance on as web applications use many different ways to configure their database access. Normally the documentation for the application should help with this kind of configuration.
While many web applications assume that your web server and database server will be the same server, it’s fortunately quite simple to split the two up. As you can see from this guide the configuration can be done quite quickly and isn’t much more time consuming to configure than on a single server.