Hello, welcome to NETVN channel
You own a website and you want to keep it online
You want it to have a backup, when the main server fails the backup server will replace it immediately
In this video I will explain how to build a website with 2 servers
a primary web server and a backup web server
If anything happens to the primary server, the second server will be replaced
Data is always synchronized between two servers
Specifically in this video, the main server has an ip address of 10.11.32.180
and the secondary server has an ip address of 10.11.32.190
In addition, we will create a virtual ip address for the two servers that is 10.11.32.222
10.11.32.222
This ip address represents both servers, when any server is online it will use this ip normally main server will use this ip address,
if main server fails then this ip address will belong to secondary server
So the website will use the virtual ip address 10.11.32.222
Let's start
In this video I will use Wordpress to create my own website
To be able to get website with Wordpress I need to install apache, php and mariadb
So I will install apache, php and mariadb for both servers
You open the database configuration file and update the ip address corresponding to each server
As in this example, the main server's ip address is 10.11.32.180
And the ip address of the secondary server is 10.11.32.190
Data is added, edited, deleted on the main server,
the secondary server will back up all the same data on the main server
Next to define the database for the main server I assign the id as 100
and assign the identifier to the secondary server as 101
Note that the identifiers need to be different on each server, the same will cause an error
On the main server you create a username as wpuser
and grant replication permission to the database on the main server
We will use this username to sync the database between the two servers
This is the command to view the main database status
With this command you will get two parameters to allow the secondary server to connect to the primary server's database
I will write down this information to allow the secondary server to sync the database with the main server in the next step
On the secondary server
we synchronize the database with the main server
Here you use the information in the previous step to synchronize the database
The information includes the ip address of the main server, the username, the password,
and two parameters of the main database
Don't forget to enable slave mode for the database on the secondary server
When viewing the status, if you receive the above message, it is successful
On the main server I create a database for my website and name it wpdb
After creating a database on the primary server,
a new database named wpdb also appears on the secondary server
Thus, the databases on the two servers have been synchronized with each other
Any changes on the main server will be synchronized on the secondary server
But for the second server, I want to allow only read database and not add, update, delete data so on the secondary server
I create a wpuser username that has only read permission to the database
At this point we are done with configuring the database for the website as well as synchronizing the database between the two servers
Next step I will explain how to create virtual ip address for two servers
To generate virtual ip address for two servers I use keepalived
You install keepalived on both servers
Next open the sysctl config file and add a line like I'm doing
Next you create a config file for keepalived on both servers and configure it like I'm configuring
First you update your virtual ip address
As in this example virtual ip address is 10.11.32.222
First you update your virtual ip address
The priority of the main server is 100 and the secondary server is 99,
with this configuration the virtual ip address is always prioritized for the main server
When the primary server crashes, a virtual ip address will be given to the secondary server
Also you need to configure the password between the two servers so that they can connect with each other
Here the password is set as 123456
The virtual ip address has been configured,
the next step is to sync the Wordpress site storage directory
You open the hosts file and add two lines as above
Each line includes ip address and mnemonic name
You can copy from main server to secondary server
To sync two Wordpress folders I use glusterfs
You install glusterfs for both servers
In this video I use Ubuntu OS for both servers
And here are the commands to install glusterfs on Ubuntu
After installation you enable and start glusterfs on both servers
After the installation, we will configure Wordpress sync using glusterfs
First you connect the two servers with glusterfs
You can see the status after connecting to check if it was successful
After successful connection, create a shared folder on both servers called gluster storage
As you can see when creating on the main server the secondary server will also have a similar folder
Don't forget to start the newly created folder
Next I will mount the newly created gluster folder with the website hosting folder on the main server
I use apache's default directory to host my website
If you use another folder, customize it to your liking
But this is probably the simplest way
To avoid the error that when restarting the server,
the mount will be lost, you need to edit the fstab file and add the mount line as above
You do this on both servers
On the second server, I also use the default directory to sync my site directory
You need to edit the information accordingly for each of your servers
Otherwise, the mount will fail and the data will not be synchronized
Now I just need to install Wordpress on the main server, the data will be synchronized with the secondary server
On the main server I download the latest wordpress
As you can see on the second server the wordpress file is also present
You unzip the downloaded file
After extracting you copy the obtained files to the current directory
You will notice on both servers that the data is synchronized with each other
Then don't forget to give permission to the folder containing Wordpress with both servers
To allow the website to be accessible from anywhere you need a domain name and open port on your router
You can refer to how to open the port on the router on my NETVN channel
For this example, I pre-configured port forwarding on my router
The ports that need to be forwarded are 80 and 443
The ip address is the virtual address of the two servers
And this is also why we need virtual ip address because it is not possible to open port for two ips at the same time
In addition, I also created a free domain name for this website
I also updated the public ip address for this domain
If you want to know how to install duckdns on Ubuntu you can review the videos on my channel or follow the instructions on Duck DNS homepage
With the update of the public ip address for the domain name as well as port forwarding on the router,
you can access your website anywhere
You open a web browser and enter your domain name
The familiar Wordpress configuration page has appeared
You find information about the database on the main server in the previous step to configure Wordpress
Database name is wpdb, username is wpuser,
password is password
On the next configuration page you create a title,
an admin account and enter your email address to complete the installation
Next I will create a simple post to test the sync between two servers
As you can see the main server is using virtual ip address
10.11.32.222
To test the site redundancy I will shut down the main web server
The virtual ip address has appeared on the secondary server
10.11.32.222
As you can see the website is still working normally and the posts are synced even though the main server is down now
We will make the final configuration for the website which is to encrypt the communication between the server and the user
In other words, configure https
I will power on the main server and install lets encrypt
As you can see the virtual ip address has been reassigned to the main server
After installing letsencrypt you configure with your domain name
Just like my previous videos you enter your email address then enter your selections like I am doing
You select the ssl configuration file and enable the https redirect for your site
Back to the website, next to the address bar appeared a lock icon
To configure let's encrypt on the secondary server you need to turn off the main server
Because if the main server is running, the secondary server will give an error because the two servers are using the same domain name
So I finished building a website with two servers
One main server and one backup server
When the main server crashes, the secondary server will be replaced and your website will still work normally
Thank you for watching
Don't forget to like and subscribe to NETVN channel