MyRaffaell Blog

demo

Migrate your web hosting to Synology & Cloudflare


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:

  1. I have multiple domain and hosted under web hosting provider.
  2. I have Synology NAS box with the latest DSM 6.0
  3. I have stable 10mbps line with symetric Up and Down for 10mbps.
  4. 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 :

  1. You don’t have IP public dont worry, we will use CloudFlare for this.
  2. We will do several port forwarding on the router.
  3. Access to your domain cpanel
  4. 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:

  1. Web Station / web services, i have set my path to default as /volumex/web
  2. PHP
  3. MariaDB / once installed i reset the root password
  4. 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:

  1. You able to open the main page http:///
  2. 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:

  1. Logon to your webhosting cpanel
  2. For database:
    1. Access the PHP MyAdmin
    2. Select your database
    3. Choose export
    4. Export to SQL files and download the file
  3. For your web files
    1. Use FTP or Backup feature on your webhosting
    2. Download all your files
    3. 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:

  1. Ensure your hosting account has activated ssh
  2. Logon to your server by using putty
  3. Execute this command:
    1. 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
    2. Once you press ENTER it will ask your password.
    3. If the file to big you can compress it with this command:
      • zip export.zip export.sql

3. Register Cloudflare account and add your first domain.

  1. Goto cloudflare and sign up an account over there.
  2. 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.
  3. Once you click next cloudflare will ask the plan (Choose the free plan, for now) and then click next.
  4. 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.

  1. Login to the domain cpanel
  2. Manage your domain
  3. Select custom name server, and put:
    • hank.ns.cloudflare.com
    • nora.ns.cloudflare.com
  4. Save and logout

Once this complete, you need to verify whether cloudflare already activate your website:

  1. Go back to your Cloudflare page and goto “Overview
  2. Click on “recheck nameservers
  3. 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:

  1. Make sure you have activated SSH on your Synology. (Control Panel / Advanced Mode / Terminal & SNMP / Enable SSH service)
  2. 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.
  3. On the console follow this steps:

    1.  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

    2. If you have multiple domain please follow all of the steps, if not please skip the step on number 2:

      1. I need to distinguish each of the domain name, so I have rename the scripts to respective as below:
        1. cloudflare_myraffaell.com.sh
        2. cloudflare_mandarin.web.id.sh
        3. etc…
      2. Use this command below
        1. cd /sbin/
        2. cp cloudflaredns.sh cloudflare_myraffaell.com.sh
        3. cp cloudflaredns.sh cloudflare_mandarin.web.id.sh
      3. Create the cloudflare configuration at Synology webstation, follow this steps:

        1. cat >> /etc.defaults/ddns_provider.conf << ‘EOF’
        2. For first site, type below (please be aware with “modulepath“):

          [CLF_Myraffaell] modulepath=/sbin/cloudflare_myraffaell.com.sh

          queryurl=https://www.cloudflare.com/api_json.html?a=rec_edit&tkn=__PASSWORD__&email=__USERNAME__&z=__HOSTNAME__&content=__MYIP__

        3. For second site, type below:

          [CLF_Mandarin] modulepath=/sbin/cloudflare_mandarin.web.id.sh

          queryurl=https://www.cloudflare.com/api_json.html?a=rec_edit&tkn=__PASSWORD__&email=__USERNAME__&z=__HOSTNAME__&content=__MYIP__

        4. Continue the same for your next sites:

        5. Type “EOF” (without the trailing slash). and enter.

        6. To ensure this is correct when you type cat /etc.defaults/ddns_provider.conf you will see your [CLF_*]

    3. 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:

      1. Visit CloudFlare account settings page and copy your API key
      2. Still the same on your terminal SSH type:
        1. curl https://www.cloudflare.com/api_json.html \ (press enter)
        2. -d ‘a=rec_load_all’ \ (press enter)
        3. -d ‘tkn=4fff816bb538ca2724b98623aae03e3282acd‘ \  (please change to your API KEY then press enter)
        4. -d ’[email protected]’ \ (your email cloudflare account then press enter
        5. -d ‘z=myraffaell.com’ (change to your domain and press enter)
    4. Once it completed you should have a respond with complicated text like below:

    5. 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.

    6. Now we need to go back to the scripts that we copied earlier and edit those scripts:

      1. cd /sbin
      2. vi myraffaell.com.sh
        1. Press i
        2. Point the cursor to _\__LOGFILE__ and change the log path:
        3. Point the cursor to _\_RECID__ and put the “_rec_id_” earlier
        4. Point the cursor to _\_RECNAME__ and put the zone name
        5. 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).
        6. press esc
        7. press :wq! (enter)
      3. 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:

  1. DSM 5.0
    1. Goto control panel > Web services
    2. Tick enable web station
    3. click on virtual host button
      1. Click create and add new virtualhost
      2. Put the hostname as your domain hostname, ( you might need to set this 2x for myraffaell.com and also www.myraffaell.com)
      3. Put the port as port 80
      4. Put the folder as the folder you kept the website files under the /web folder. (same folder different domain allowed)
  2. DSM 6.0
    1. Click on Start menu choose “Web Station” set these menu
    2. General Settings
      1. Choose NGINX (why ? faster response, less memory footprint)
      2. PHP 5.x
    3. Virtual Host
      1. Create
      2. Hostname = your domain name (make 2x for non-www. and www.)
      3. Port =80
      4. Document root = point to your website folder
      5. 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:

  1. Click on Control Panel > External Access
  2. on DDNS tab click Add
  3. Service Provider = [CLF*] that we configured earlier
  4. Hostname = your Domain name
  5. Username = your cloudflare email account
  6. Password = your cloudflare API key
  7. 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.

  8. 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.

  1. Logon to your router
  2. Goto Advanced > Port forwarding
  3. Put the names of the rules
  4. Put the Synology IP address for Local IP
  5. Put public port as 80
  6. On the allowed public ip leave it blank.
  7. Save

In order for you to check whether your port already open and connected you can try this step:

  1. Goto http://www.canyouseeme.org/
  2. Enter the port number “80”
  3. 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

comments powered by Disqus