banner



How To Create Nameservers On Vps

How To Set Up Your Nameservers On Separate VPS's (Centos Web Panel)

Posted by The Curious Website Designer | Posted on Sat 29 Dec 2018

How To Set Up Your Nameservers On Separate VPS's (Centos Web Panel)

I have two seperate VPS server setups each with one public ip address. One is my main production server and the other is used as an offsite disaster backup.

In this article, I will show you how I set up the DNS server so that ns1 is on the production server, ns2 is on the backup server and how I keep the DNS information synchronised.

Here are the details of the two servers I will be setting up:

Primary Server Secondary Server

IP Address: 91.238.164.188

Hostname: svr1.mynewdomain.co.uk

Nameservers:

  • ns1.mynewdomain.co.uk
  • ns2.mynewdomain.co.uk

Domains:

  • mynewdomain.co.uk
  • mydomain.co.uk
  • myotherdomain.co.uk

IP Address: 80.211.112.201

Hostname: svr2.mynewdomain.co.uk

Nameservers:

  • ns1.mynewdomain.co.uk
  • ns2.mynewdomain.co.uk

Domains:

  • backup.mynewdomain.co.uk

For the purposes of this article, I am assuming you have already installed Centos Web Panel on your main or primary server.

Step 1. Configure The Primary Server.

Looking at my primary server, these are the DNS zones that have been set up (from the left menu select DNS Functions > List DNS Zones):

As both the primary and secondary servers are going to be set up as sub domains of the Top Level Domainmynewdomain.com, we're going to edit the dns zone for mynewdomain.com.

Click on theEdit File button formynewdomain.co.uk.db. You will see a record along the lines of:

  1. ; Generated by CWP
  2. ; Zone file for mynewdomain.co.uk
  3. $TTL 14400
  4. mynewdomain.co.uk.      86400        IN      SOA     ns1.mynewdomain.co.uk. email.gmail.com. (
  5. 2013071600      ; serial, todays date+todays
  6. 86400           ; refresh, seconds
  7. 7200            ; retry, seconds
  8. 3600000         ; expire, seconds
  9. 86400 )         ; minimum, seconds
  10. mynewdomain.co.uk. 86400 IN NS ns1.mynewdomain.co.uk.
  11. mynewdomain.co.uk. 86400 IN NS ns2.mynewdomain.co.uk.
  12. mynewdomain.co.uk. IN A 91.238.164.188
  13. localhost.mynewdomain.co.uk. IN A 127.0.0.1
  14. mynewdomain.co.uk. IN MX 0 mynewdomain.co.uk.
  15. mail IN CNAME mynewdomain.co.uk.
  16. www IN CNAME mynewdomain.co.uk.
  17. ftp IN CNAME mynewdomain.co.uk.
  18. ; Add additional settings below this line
  19. _dmarc 14400 IN TXT "v=DMARC1; p=none"

To the end of this file, we're going to add the following lines:

  1. ns1 14400 IN A 91.238.164.188
  2. ns2 14400 IN A 80.211.112.201
  3. svr1 14400 IN A 91.238.164.188
  4. svr2 14400 IN A 80.211.112.201

This adds the hostname and nameserver subdomains that we need to the DNS zone. Note the ip addresses associated with each subdomain - ns1 and svr1 are the primary server and ns2 and svr 2 go to the secondary server.

We're also going to add one other line to the file:

  1. backup 14400 IN A 80.211.112.201

This will direct any web enquiries for backup.mynewdomain.co.uk to our secondary server.

The resulting file should look like this:

  1. ; Generated by CWP
  2. ; Zone file for mynewdomain.co.uk
  3. $TTL 14400
  4. mynewdomain.co.uk.      86400        IN      SOA     ns1.mynewdomain.co.uk. email.gmail.com. (
  5.     2013071600      ; serial, todays date+todays
  6.     86400           ; refresh, seconds
  7.     7200            ; retry, seconds
  8.     3600000         ; expire, seconds
  9.     86400 )         ; minimum, seconds
  10. mynewdomain.co.uk. 86400 IN NS ns1.mynewdomain.co.uk.
  11. mynewdomain.co.uk. 86400 IN NS ns2.mynewdomain.co.uk.
  12. mynewdomain.co.uk. IN A 91.238.164.188
  13. localhost.mynewdomain.co.uk. IN A 127.0.0.1
  14. mynewdomain.co.uk. IN MX 0 mynewdomain.co.uk.
  15. mail IN CNAME mynewdomain.co.uk.
  16. www IN CNAME mynewdomain.co.uk.
  17. ftp IN CNAME mynewdomain.co.uk.
  18. ; Add additional settings below this line
  19. _dmarc 14400 IN TXT "v=DMARC1; p=none"
  20. ns1 14400 IN A 91.238.164.188
  21. ns2 14400 IN A 80.211.112.201
  22. svr1 14400 IN A 91.238.164.188
  23. svr2 14400 IN A 80.211.112.201
  24. backup 14400 IN A 80.211.112.201

Make sure there is an empty line at the end of the file otherwise the file will fail to load and your DNS server will not run.

Save the file and restart the Bind DNS Server (from the webpanel dashboard):

Step 2. Basic Configuration of The Secondary Server

  1. Make sure the hostname of the server is svr2.mynewdomain.co.uk
  2. Set up the nameservers to be ns1.mynewdomain.co.uk and ns2.mynewdomain.co.uk
  3. Create a new user account forbackup.mynewdomain.co.uk

Use your own domain details instead of those I've used here of course.

Step3. Synchronise The DNS Zones Across Both Servers

Next we're going to create a script that:

  • copies the zone files from svr1 to svr2
  • checks whether the DNS server configuration file is set up to load the zone file and update it where the file reference is missing
  • restarts the DNS server on svr2

We will then set up a CRON job to run this process every 30 minutes so that the DNS zones are synchronised within 30 minutes of any of the zones on svr1 being updated.

#The Script

The script will reside on svr2 and will first of all copy the DNS zone files from svr1:

  1. $remote = 'root@91.238.164.188';
  2. exec('scp -P4567 ' . $remote . ':/var/named/*.db /var/named/');

The first line sets the variable$remote to contain the address of user root on svr1.

The second line performs the file copy using 'scp'. The syntax is copy using port 4567 from'root@91.238.164.188:/var/named/*' to the folder '/var/named/' on this (svr2) server. You would need to change the value of$remote on your server.

Next, the script checks and, if necessary, updates the DNS Server configuration file (/etc/named.conf).

  1. $files = scandir('/var/named/');
  2. $conf = file_get_contents('/etc/named.conf');
  3. foreach($files as $file)
  4.   if(substr($file, -3) == '.db') {
  5.     if(strpos($conf, '/var/named/' . $file) === false) {
  6.       $domain = substr($file, 0, -3);
  7.       $fp = fopen('/etc/named.conf', 'a');
  8.       fwrite($fp, " ");
  9.       fwrite($fp, '// zone ' . $domain . " ");
  10.       fwrite($fp, 'zone "' . $domain . '" {' . " ");
  11.       fwrite($fp, '        type master;' . " ");
  12.       fwrite($fp, '        file "/var/named/' . $file . '";};' . " ");
  13.       fwrite($fp, '// zone_end ' . $domain . " ");
  14.     }
  15.   }
  16. }

Line 1 puts the file names in the folder '/var/named/' into an array $files (the zone files).

Line 2 puts the contents of the DNS server configuration file into a variable for comparison with the filenames in$files

Line 3 loops through all the zone filenames

Line 4 checks that the filename is a zone file (ie ends in.db). If not, it skips lines 5 - 14.

Line 5 checks the DNS server configuration file for a reference to the zone file. If there is no reference, lines 6 - 13 append the correctly formatted reference to the file.

Finally, the DNS server is restarted:

  1. exec('systemctl restart named.service');

This is what the final file looks like:

  1. <?php
  2.   // ns2_sync.php
  3.   //  * copies all of the .db files from svr1 (ns1) to svr2 (ns2)
  4.   // * updates named.conf if there is not an entry corresponding to the .db file
  5.   // Copy DNS Zone Files from svr1 to svr2 ***  EDIT THE NEXT LINE TO REFLECT THE CORRECT IP ADDRESS  ***
  6.   $remote = 'root@217.61.22.130';
  7.   exec('scp -P47255 ' . $remote . ':/var/named/*.db /var/named/');
  8.   // Check & update named.conf
  9.   $files = scandir('/var/named/');
  10.   $conf = file_get_contents('/etc/named.conf');
  11.   foreach($files as $file) {
  12.     if(substr($file, -3) == '.db' && $file != 'co.uk.db') {
  13.       if(strpos($conf, '/var/named/' . $file) === false) {
  14.         $domain = substr($file, 0, -3);
  15.         $fp = fopen('/etc/named.conf', 'a');
  16.         fwrite($fp, " ");
  17.         fwrite($fp, '// zone ' . $domain . " ");
  18.         fwrite($fp, 'zone "' . $domain . '" {' . " ");
  19.         fwrite($fp, '        type master;' . " ");
  20.         fwrite($fp, '        file "/var/named/' . $file . '";};' . " ");
  21.         fwrite($fp, '// zone_end ' . $domain . " ");
  22.       }
  23.     }
  24.   }
  25. exec('systemctl restart named.service');
  26. ?>

Save the file as 'ns2_sync.php' and upload it to a new folder '/cron/'

#Setup SCP.

If you're not familiar with using Secure Copy (scp), checkout this article: How To Use Secure Copy (SCP) To Copy Files From One Server To Another.

The main points to bear in mind are:

  • Copy the contents of the file '/root/.ssh/id_rsa.pub' on thePrimaryserver to the file '/root/.ssh/authorized_keys' on theSecondary server, adding the content at the end of the file if it already exists.
  • Make sure to add the IP address of the 'other' server to the firewall whitelist onboth servers.
  • If you have setup the SSH configuration file (/etc/ssh/sshd_config) so thatPermitRootLogin isno, this needs to be changed towithout-password

You can check the file is working correctly by logging in to the Secondaryserver (svr2) via PuTTY, usingsuto become thr root user and entering:

# /usr/local/bin/php -vvv /cron/ns2_sync.php

The '-vvv' means that debug data is output to the console so you can establish where the script is going wrong if it fails.

#Set Up The CRON Job

Once you know that the script works correctly, all that needs to be done is set the CRON Job so it all happens in the background without you having to manually update the secondary server.

In the Control Panel for svr2go toServer Settings > Crontab for root. Find the section at the bottom of the page Add Full Custom Cron Jobs and enter:

7,37 * * * * /usr/local/bin/php /cron/ns2_sync.php

Then save the changes.

This will set the Cron job to run at 7 minutes and 37 minutes past the hour, every hour every day.

You can find out more about Cron Jobs here: https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

How To Create Nameservers On Vps

Source: https://thecuriouswebsitedesigner.co.uk/how-to-set-up-your-nameservers-on-separate-vpss-centos-web-panel

Posted by: englesdoony1936.blogspot.com

0 Response to "How To Create Nameservers On Vps"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel