Publish: 2016-12-06 | Modify: 2016-12-06
DDNS maps a user's dynamic IP address to a fixed domain name resolution service. Every time the user connects to the internet, the client program will transmit the dynamic IP address of the host to the server program located on the service provider's host, and the server program is responsible for providing DNS services and implementing dynamic domain name resolution.
Currently, there are many domestic vendors that support DDNS resolution, such as Peanut Shell. If you don't want to use the DDNS service provided by a third-party vendor, you can set it up yourself, for example, using the CloudXNS API to implement DDNS.
As I know a little bit about PHP, I chose the CloudXNS API PHP SDK. So you need to set up the PHP environment in advance, and then execute the following commands in the site's root directory:
### Download CloudXNS API PHP SDK
wget https://github.com/CloudXNS/CloudXNS-API-SDK-PHP/archive/master.zip
### Unzip
unzip master.zip
### Move
mv CloudXNS-API-SDK-PHP-master/* ./
CloudXNS-API-SDK-PHP requires Composer. If you haven't installed Composer yet, please refer to: Installing Composer on CentOS. Then execute composer install
in the site's root directory to complete the SDK installation. Complaining: SDK is located overseas, and the installation speed is too slow.
The CloudXNS API PHP SDK contains a complete DEMO, which can be referred to if needed. Save the following code as ddns.php
, and please modify setApiKey
and setSecretKey
to your own in the CloudXNS backend. Also, please modify test.hixz.org
to the record you need to update in CloudXNS.
<?php
// Get public IP
$ip = file_get_contents("https://blog.xiaoz.org/ip/userip.php");
require_once './vendor/autoload.php';
$api = new \CloudXNS\Api();
$api->setApiKey('xxxxxxxx');
$api->setSecretKey('xxxxxxxx');
$api->setProtocol(true);
/**
* Quickly modify the resolution record for DDNS
* @param string $domain Domain name containing host records
* @param string $ip IP value, multiple values separated by |, such as 1.1.1.1|2.2.2.2, can be empty
* @param integer $line_id Line ID, default is 1, can be empty
*/
echo $api->ddns->ddns('test.hixz.org.', $ip, 1);
echo $ip;
?>
We can use the crontab scheduled task of Linux to run the script every hour and update the public IP in a timely manner, as follows:
### Add scheduled task
crontab -e
### Add the task to the schedule, running the script every hour
10 * * * * cd /data/wwwroot/test.xiaoz.top/ && /usr/local/php/bin/php ddns.php >> /home/ddns.log 2>&1
### Reload crontab
service crond reload
10 * * * *
means 10 minutes past every hour, /data/wwwroot/test.xiaoz.top/
is the website's root directory, and /usr/local/php/bin/php
is the installation path of PHP.
After the successful execution of the script, due to DNS caching, it generally takes about 10 minutes to take effect. We can use the ping command to test whether it is consistent with our own public IP. If it is consistent, it means that the DDNS resolution is successful, as shown in the screenshot below.
This article refers to: CloudXNS API PHP SDK, original article, please indicate the source when reprinting.
I come from China and I am a freelancer. I specialize in Linux operations, PHP, Golang, and front-end development. I have developed open-source projects such as Zdir, ImgURL, CCAA, and OneNav.