How to deploy the website
You can setup zsh first.
Guide for setup this website:
- build website binary
- Nginx for reverse proxy
- get SSL certificate with Certbot
- setup auto git pull with webhook
Requirements
- vps
- domain name
- SSL certificate
config cloudflare DNS
- Go to Cloudflare dashboard and select your domain
- Go to DNS settings
- Add two record with your domain name pointing to your VPS IP address
-
Type: A
- Name: @
- IPv4 address: your VPS IP address
- TTL: Auto
- Proxy status: Proxied
-
Type: A
- Name: www
- IPv4 address: your VPS IP address
- TTL: Auto
- Proxy status: Proxied
- SSL/TLS settings
- SSL/TLS encryption mode: Full (strict)
- Always use HTTPS: ON
- Automatic HTTPS Rewrites: ON
Build website binary
- Clone the repository to your VPS
git clone https://github.com/sokinpui/website.git
- build the website
cd website
go clean
go build -o website.o
Setup Nginx for reverse proxy
- Install Nginx
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
- Configure Nginx
sudo -e /etc/nginx/sites-available/www.skpstack.uk
Add the following configuration:
server {
listen 80;
server_name www.skpstack.uk;
location / {
proxy_pass http://localhost:12352;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
- Enable the server block
sudo ln -s /etc/nginx/sites-available/www.skpstack.uk /etc/nginx/sites-enabled/
- Test Nginx configuration and restart
sudo nginx -t
sudo systemctl restart nginx
Get SSL certificate with Certbot
Detail guide can be found on certbot official
- Install Certbot
sudo apt update
sudo apt install python3 python3-dev python3-venv libaugeas-dev gcc
sudo apt remove certbot
- Setup Certbot
sudo python3 -m venv /opt/certbot/
sudo /opt/certbot/bin/pip install --upgrade pip
sudo /opt/certbot/bin/pip install certbot certbot-nginx
sudo ln -s /opt/certbot/bin/certbot /usr/bin/certbot
- Obtain SSL certificate
sudo certbot --nginx
- Set up automatic renewal
echo "0 0,12 * * * root /opt/certbot/bin/python -c 'import random; import time; time.sleep(random.random() * 3600)' && sudo certbot renew -q" | sudo tee -a /etc/crontab > /dev/null
Setup auto git pull with webhook
Setup webhook listener
sudo apt install git webhook
modify hooks.json file in website directory:
example:
[
{
"id": "redeploy-website",
"execute-command": "/home/<username>/website/deploy.sh",
"command-working-directory": "/home/<username>/website",
"response-message": "Deployment initiated.",
"trigger-rule": {
"and": [
{
"match": {
"type": "payload-hmac-sha256",
"secret": "abc",
"parameter": {
"source": "header",
"name": "X-Hub-Signature-256"
}
}
},
{
"match": {
"type": "value",
"value": "refs/heads/main",
"parameter": {
"source": "payload",
"name": "ref"
}
}
}
]
}
}
]
create webhook systemd service
- create a systemd service to run webhook
sudo -e /etc/systemd/system/webhook.service
[Unit]
Description=Webhook
After=network.target
[Service]
User=<username>
Group=<username>
ExecStart=webhook -hooks /home/<username>/website/hooks.json -verbose
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
- Start and enable the service
sudo systemctl daemon-reload
sudo systemctl start webhook.service
sudo systemctl enable webhook.service
Create website systemd service
- create a systemd service to serve the website binary
sudo -e /etc/systemd/system/my-website.service
[Unit]
Description=My Go Website
After=network.target
[Service]
User=<username>
Group=<username>
WorkingDirectory=/home/<username>/website
ExecStart=/home/<username>/website/website.o
Restart=on-failure
[Install]
WantedBy=multi-user.target
- Start and enable the service
sudo systemctl daemon-reload
sudo systemctl start my-website.service
sudo systemctl enable my-website.service
add Webhook to nginx config:
sudo -e /etc/nginx/sites-available/www.skpstack.uk
location /hooks/ {
proxy_pass http://127.0.0.1:9000/hooks/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
sudo nginx -t
sudo systemctl reload nginx
allow run restart my-website.service without sudo
sudo visudo
add this line at the bottom
<username> ALL=NOPASSWD: /usr/bin/systemctl restart my-website
You should be able to access the website now
Development
Refresh the website:
- Chrome, Firefox, or Edge (Windows/Linux):
Ctrl + Shift + RorCtrl + F5 - Chrome, Firefox, or Edge (Mac):
Cmd + Shift + R - Safari (Mac):
Cmd + Option + R
Restart the website:
cd ~/website
./deploy.sh
clear cluodflare cache
sometimes after deploy the website, the old cache is still there. You can clear the Cloudflare cache by following these steps:
- Log in to Cloudflare: Go to the Cloudflare dashboard https://dash.cloudflare.com and log in to your account.
- Select Your Website: On the dashboard home, click on the domain name you want to manage (e.g., xrayxrtas.space ).
- Navigate to Caching: In the left-hand sidebar, find and click on the Caching icon (it looks like a cylinder or database).
- Go to Configuration: Within the Caching section, make sure you are on the Configuration tab.
- Purge Everything: You will see a section called "Purge Cache". For your situation, the simplest and most effective option is to purge everything. • Click the "Purge Everything" button. • A confirmation dialog will appear. Confirm that you want to purge the entire cache.