Deploy website to VPS with CMD script

Brief Introduction

The official website of Hugo introduces several ways to deploy the website, most of them are talking about how to deploy the website to the Online Services, such as GitHub.

Some methods, such as Git or Rsync, it’s essential to firstly install some softwares before we deploy it to the VPS.

As a lazy guy, there’s always a question: “Is there any way to deploy the built websites to VPS just by one click??”

Yes! Here I introduce you a method, to deploy the website to VPS just with a bat script.

Environment: Windows 10 2004

The procedure of the deployment can be detailed in several steps:

  • Remove the old website files from computer, default: public\
  • Build the new website with Hugo
  • Remove the old website files from server
  • Copy the new website from computer to server
  • Restart NGINX
  • Optional: purge the Cloudflare cache

Procedure step-by-step

  1. Remove the old website files from computer, default: public\

    In command prompt, we can delete the public\ folder just by

    1
    
    rd /s /q public
    

    where we got 3 parameters:

    • /s: Delete the subfolder and files
    • /q: Quite, delete the files without confirmation
    • public: The folder you want to delete
  2. Build the new website with Hugo

    The new website can be built by Hugo with

    1
    
    hugo
    

    The command need to be run in website root folder.

  3. Remove the old website files from server

    We can control the server via ssh, in windows, openssh is already integrated in CMD.

    1
    
    ssh -i identityFile -p port name@host "rm -r path_to_website/* && exit"
    

    where

    • -i: indicates the Keyfile for identity
    • -p: indicates the ssh port to connect the server, by default it’s 22
    • “*": the command we want to run in the server, here we want to delete the files
  4. Copy the new website from computer to server

    We can easily copy the files to server with scp.

    1
    
    scp -i identityFile -P port -r ./public/* name@host:path_to_website
    

    where

    • -i: indicates the Keyfile for identity
    • -P: indicates the ssh port to connect the server, by default it’s 22, note the P is a capital letter
    • ./public/*: the filepath in computer
    • path_to_website: the website path in server
  5. Restart NGINX

    we can restart NGINX in command line with password

    1
    
    sudo service nginx force-reload
    

    to avoid the request of password, we can edit the sudoer file

    1
    
    sudo visudo
    

    and add a new line at the end of the file

    1
    
    $User ALL = NOPASSWD: /usr/sbin/service nginx force-reload
    

    replace $User with your own username.

    Then we can restart the NGINX service without the request of password.

  6. Optional: purge the Cloudflare cache

    If we use the Cloudflare CDN, we also need to purge the cache after deploying the website to VPS.

    The Cloudflare API offers us an easy way to purge the cache with command line.

    1
    2
    3
    4
    5
    
    curl -X POST "https://api.cloudflare.com/client/v4/zones/$Zone_ID/purge_cache" \
         -H "X-Auth-Email: user@example.com" \
         -H "X-Auth-Key: $Global_API_Key" \
         -H "Content-Type: application/json" \
         --data '{"purge_everything":true}'
    

    replace

    we can create a bash script in server:

    1
    2
    3
    
    cat>purge_cache.sh<<EOF
    curl -X POST "https://api.cloudflare.com/client/v4/zones/$Zone_ID/purge_cache" -H "X-Auth-Email: user@example.com" -H "X-Auth-Key: $Global_API_Key" -H "Content-Type: application/json" --data '{"purge_everything":true}'
    EOF
    

    and then make it executable

    1
    
    chmod +x purge_cache.sh
    

One Key Script for deployment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@echo off
set identityFile=$identityFile
set port=$port
set computer_path=./public
set server_path=$path_to_website

echo Remove files in Public
rd /s /q public
echo.
echo Create website files by hugo
hugo
echo.
echo Deploying website to VPS
echo.
echo Step 1: delete old files in VPS
ssh -i %identityFile% -p %port% $name@$host "rm -r %server_path%/* && exit"
echo.
echo Step 2: copy new files to VPS
scp -i %identityFile% -P %port% -r %computer_path%/* $name@$host:%server_path%
echo.
echo Step 3: restart nginx in VPS and purge cache in Cloudflare
ssh -i %identityFile% -p %port% $name@$host "sudo service nginx force-reload && bash ~/purge_cache.sh && exit"
echo.
Pause

place the script in website root folder.

replace

  • $identityFile
  • $port
  • $path_to_website
  • $name
  • $host

check if you have already place the purge_cache.sh script in server.

Enjoy

updatedupdated2020-09-022020-09-02