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
-
Remove the old website files from computer, default:
public\
In command prompt, we can delete the
public\
folder just by1
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
-
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.
-
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
-
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
-
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
file1
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.
-
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
- $Zone_ID
- user@example.com
- $Global_API_Key
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
|
|
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.