WordPress is the most popular content management system (CMS) based on PHP and MySQL. When your WordPress CMS grows in traffic and you have outgrown your current server resources, a professional solution is to host your database on a separate database server. With this solution, you can optimize your database and your web server independently and both servers can grown on it’s own machine. In this tutorial we will guide you on how to configure WordPress to use a remote database, on a CentOS 7 based VPS.
Requirements
In order to run WordPress on your CentOS 7 VPS and configure it to use a remote database, we need the following requirements pre-installed:
- A Web VPS on which we will install the WordPress instance.
- A Database VPS with MariaDB or MySQL installed on it. The database will be hosted on this VPS.
WordPress requires the following:
- Web server: Apache, Nginx
- PHP version 7.2 or newer, with JSON support, mbstring, zip and GD2 extensions.
- MariaDB version 10.0 or greater or MySQL database server version 5.6 or newer
Step 1: Log in via SSH on both servers:
Log in to each VPS via SSH as user root
Step 2: Update all packages
Once you are logged, run the following command on both servers to make sure that all installed RPM packages are up to date
Step 3: Install MariaDB server on the Database VPS
Step 4: Install LAMP stack on a Web VPS
As mentioned in the requirements section of the tutorial, a LAMP stack (Apache, MySQL/MariaDB and PHP) is required to run WordPress on the server. We will start with installing the Apache web server:
After the Apache installation is complete, start the web server and enable it to start upon server boot:
PHP 5.4 is installed on CentOS 7 by default.
In order to install and use PHP 7.2, we need to install EPEL and REMI repositories:
Next, install PHP 7.2 along with the required PHP extensions:
And finally, complete the LAMP installation by installing MariaDB client package:
Start the service and set it to start on reboot
To accept remote connections, edit the MariaDB configuration file (/etc/my.cnf.d/server.cnf) and change the following line:
with:
Do not forget to replace ‘web_server_IP_address’ with the public IP of the web server.
If you want to configure MariaDB to listen on all interfaces on the web VPS, set:
Restart MariaDB for the changes to take effect:
Step 5: Create a MariaDB database for WordPress on the Database VPS
Log in to MariaDB console with the root user account:
Create a new MariaDB database for WordPress using the following query:
Create a new MariaDB user for WordPress using the following query:
Then execute the following query to add a separate user for WordPress that will interact with the MariaDB database:
Do not forget to replace database_VPS_IP with the actual IP address of the database VPS .
Execute the following command to apply the privileges we set:
Now we can exit the MariaDB session:
Step 6: Configure the MariaDB server on database VPS to listen on public IP (or all interfaces)
Edit the MariaDB configuration file (/etc/my.cnf.d/server.cnf) and change the following line:
with:
Or, configure MariaDB to listen on all interfaces on the database VPS:
Restart MariaDB for the changes to take effect:
Step 7: Install WordPress on the Web VPS
Download the latest WordPress version available at https://wordpress.org/download/ and extract it in a directory on your server:
Set proper permissions on WordPress files and directories:
Rename wp-config-sample.php WordPress configuration file to wp-config.php:
Edit the wp-config.php file and modify the following lines
Step 8: Configure Apache to serve WordPress
Now we will have to setup the Apache configuration so it can serve the WordPress directory. Add the contents below in the /etc/httpd/conf.d/wordpress.conf file using vi or your favorite editor:
Add the following lines:
<VirtualHost *:80>
ServerAdmin admin@your-domain.com
DocumentRoot /var/www/html/wordpress
ServerName your-domain.com
ServerAlias www.your-domain.com
Alias /matomo “/var/www/html/wordpress/”
<Directory /var/www/html/wordpress/>
Options +FollowSymlinks
AllowOverride All
</Directory>
ErrorLog /var/log/httpd/wordpress-error_log
CustomLog /var/log/httpd/wordpress-access_log common
</VirtualHost>
Save the changes and restart Apache for the changes to take effect:
Open http://your-domain.com in your favorite web browser and finish the WordPress installation.
Congratulations. You have successfully configured WordPress to use a remote database on a CentOS 7 server.
Of course you don’t have to configure WordPress to use a remote database on CentOS 7, if you use one of our Managed CentOS VPS Hosting Solutions, in which case you can simply ask our expert Linux admins to set up your WordPress to use a remotely hosted database for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on how to configure WordPress to use a remote database on a CentOS 7 server, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.