I have several sites online through web hosting and the yearly bill sometimes not really nice to pay especially when your site is down and not much earn for this. My site has been attack by lots of malware, SQL injection even got hacked several times and in the end I realize I should have a full control on my site on my own server. I have Synology Nas at home, basically it’s have some capability to run web server. And now I will share how do we move our hosting sites to Synology with the latest DSM 6.0.
Current condition:
- I have multiple domain and hosted under web hosting provider.
- I have Synology NAS box with the latest DSM 6.0
- I have stable 10mbps line with symetric Up and Down for 10mbps.
- I have DLINK DIR-615 router for my internet access
General solution
In order to migrate your site to your local Synology you need several ingredients :
- You don’t have IP public dont worry, we will use CloudFlare for this.
- We will do several port forwarding on the router.
- Access to your domain cpanel
- Time & Coffee
Lets start the migration:
1. Install Synology web service, MariaDB and their friends.
Login to your DSM, goto package manager / package center and install several application below:
- Web Station / web services, i have set my path to default as /volumex/web
- PHP
- MariaDB / once installed i reset the root password
- PhpMyadmin / used to manage the DB later on.
Once you completed all of this activities you can start go to the next steps, but before that you have to ensure:
- You able to open the main page http://
/ - You able to open PhpMyAdmin, logon and administrate some DB operation.
2. Export and download
On this steps, you need to export all of your database from the current web hosting to your local drive. Here’s how:
- Logon to your webhosting cpanel
- For database:
- Access the PHP MyAdmin
- Select your database
- Choose export
- Export to SQL files and download the file
- For your web files
- Use FTP or Backup feature on your webhosting
- Download all your files
- put inside your synology web folder (/volumex/web/Mysite1).
In my case I have very large database (approx 350MB) and I cannot use export from PhpMyadmin, therefore I have to do this from SSH with below steps:
- Ensure your hosting account has activated ssh
- Logon to your server by using putty
- Execute this command:
- mysql -u USERNAME -p-h HOSTNAMEORIP DATABASENAME > export.sql
- USERNAME is your database username
- HOSTNAME is your domain hosting name
- DATABASENAME is your database.
- export.sql is the file that will be exported
- Once you press ENTER it will ask your password.
- If the file to big you can compress it with this command:
- zip export.zip export.sql
- mysql -u USERNAME -p-h HOSTNAMEORIP DATABASENAME > export.sql
3. Register Cloudflare account and add your first domain.
- Goto cloudflare and sign up an account over there.
- Setup a websites, please put your first domain that you will migrate
- On this step Cloudflare will try to find all your domain configuration information.
- Once it completed, cloudflare will save your, A record, MX record and also CNAME and TXT record.
- Once you click next cloudflare will ask the plan (Choose the free plan, for now) and then click next.
- Then cloudflare will ask you to change the pointer for your DNS record to:
- hank.ns.cloudflare.com
- nora.ns.cloudflare.com
At this stage now you need to login to your “domain” cpanel and change your DNS pointer to the Cloudflare.
- Login to the domain cpanel
- Manage your domain
- Select custom name server, and put:
- hank.ns.cloudflare.com
- nora.ns.cloudflare.com
- Save and logout
Once this complete, you need to verify whether cloudflare already activate your website:
- Go back to your Cloudflare page and goto “Overview“
- Click on “recheck nameservers“
- Once clicked the status now should be “active“
4. Cloudflare integration with Synology
On Synology we can use DYDNS to point our domain to our site, but unfortunately Synology doesnt have a built in Cloudflare DYDNS support. But to achieve this thanks to Tehmantra who created some script for CloudFlare dynamic dns updater module for Synology. Here’s the steps:
- Make sure you have activated SSH on your Synology. (Control Panel / Advanced Mode / Terminal & SNMP / Enable SSH service)
- SSH (use Putty) to your Synology.
- DSM 5.x
- Login as Root
- Use your admin password
- DSM 6.x
- Login as admin
- Use your admin password
- On terminal type sudo su – root
- Type your admin password again.
- DSM 5.x
On the console follow this steps:
Paste below command and press enter:
wget https://gist.github.com/tehmantra/f1d2579f3c922e8bb4a0/raw/706f8db7d8506e62b17a5568951544e5c5ee0644/cloudflaredns.sh -O /sbin/cloudflaredns.sh
It will download the cloudflaredns.sh and save it to your /sbin/cloudflaredns.sh
If you have multiple domain please follow all of the steps, if not please skip the step on number 2:
- I need to distinguish each of the domain name, so I have rename the scripts to respective as below:
- cloudflare_myraffaell.com.sh
- cloudflare_mandarin.web.id.sh
- etc…
- Use this command below
- cd /sbin/
- cp cloudflaredns.sh cloudflare_myraffaell.com.sh
- cp cloudflaredns.sh cloudflare_mandarin.web.id.sh
Create the cloudflare configuration at Synology webstation, follow this steps:
- cat >> /etc.defaults/ddns_provider.conf << ‘EOF’
For first site, type below (please be aware with “modulepath“):
[CLF_Myraffaell] modulepath=/sbin/cloudflare_myraffaell.com.sh
For second site, type below:
[CLF_Mandarin] modulepath=/sbin/cloudflare_mandarin.web.id.sh
Continue the same for your next sites:
Type “EOF” (without the trailing slash). and enter.
To ensure this is correct when you type cat /etc.defaults/ddns_provider.conf you will see your [CLF_*]
- I need to distinguish each of the domain name, so I have rename the scripts to respective as below:
Configuration files has been created, you’re close to go now, but now you need to test whether cloudflare respond from your Synology request, please follow this steps:
- Visit CloudFlare account settings page and copy your API key
- Still the same on your terminal SSH type:
- curl https://www.cloudflare.com/api_json.html \ (press enter)
- -d ‘a=rec_load_all’ \ (press enter)
- -d ‘tkn=4fff816bb538ca2724b98623aae03e3282acd‘ \ (please change to your API KEY then press enter)
- -d ’[email protected]’ \ (your email cloudflare account then press enter
- -d ‘z=myraffaell.com’ (change to your domain and press enter)
Once it completed you should have a respond with complicated text like below:
Find the “rec_id” number and also “zone_name” this is not difficult as it only on the first line, copy and paste those value on the notepad.
Now we need to go back to the scripts that we copied earlier and edit those scripts:
- cd /sbin
- vi myraffaell.com.sh
- Press i
- Point the cursor to _\__LOGFILE__ and change the log path:
- Point the cursor to _\_RECID__ and put the “_rec_id_” earlier
- Point the cursor to _\_RECNAME__ and put the zone name
- Point the cursor to _\_SERVICEMODE__ and put to number “1″. (this is to ensure all the incoming request will go thru Clourdlare each time IP change on Synology).
- press esc
- press :wq! (enter)
- Repeat steps 2 for the next script.
4. Configure the virtual host on Synology
Once the steps of the preps cloudflare configuration done, the next steps we will setup the virtual host on Synology. Virtual host are very important on Synology, with this feature basically you will be able to have multiple site and domain name within 1 local ip address. here’s the steps:
- DSM 5.0
- Goto control panel > Web services
- Tick enable web station
- click on virtual host button
- Click create and add new virtualhost
- Put the hostname as your domain hostname, ( you might need to set this 2x for myraffaell.com and also www.myraffaell.com)
- Put the port as port 80
- Put the folder as the folder you kept the website files under the /web folder. (same folder different domain allowed)
- DSM 6.0
- Click on Start menu choose “Web Station” set these menu
- General Settings
- Choose NGINX (why ? faster response, less memory footprint)
- PHP 5.x
- Virtual Host
- Create
- Hostname = your domain name (make 2x for non-www. and www.)
- Port =80
- Document root = point to your website folder
- HTTP Backend = NGINX and PHP
5. Configure the DYDNS
At this point now we will set up the Cloudflare DYDNS connection. This steps will explain further:
- Click on Control Panel > External Access
- on DDNS tab click Add
- Service Provider = [CLF*] that we configured earlier
- Hostname = your Domain name
- Username = your cloudflare email account
- Password = your cloudflare API key
Press test connection
At this rate, you should be able to see the result is normal, but if its not, please ask on the comment page below.
Save
6. Last steps… Port forwards…
The last steps to make this further complete is to set port forwarding on your router. This mean cloudflare from the internet would be able to communicate with your Synology DSM. To perform this its quite simple, currently I’m using Dlink DIR 615 on my internet router but the basic concept is the same.
- Logon to your router
- Goto Advanced > Port forwarding
- Put the names of the rules
- Put the Synology IP address for Local IP
- Put public port as 80
- On the allowed public ip leave it blank.
- Save
In order for you to check whether your port already open and connected you can try this step:
- Goto http://www.canyouseeme.org/
- Enter the port number “80”
- and see the result make sure its “Open”
Once all of this settings completed, your website should be online now, try access your site from browser, there are some pre cautions on the new NGINX settings on DSM 6.0 that will be posted on the next article. If you have further question please fill on the comment form.
Cheers,
Raff