This is not full documentation but in here I explain most useful things that you cant see in the code, and it's necessary.
I hope it will be useful for you.
For deploy go on linux server do these steps:
- Build your app with necessary environments:
env GOOS=linux CGO_ENABLED=0 go build -o blog ./cmd/api
- Create a systemd job file:
touch /etc/systemd/system/blog.service
- Copy these code into
blog.service
file:
Description= instance to serve blog api
After=network.target
[Service]
Environment="MODE=production"
User=root
Group=www-data
ExecStart=<Path of your builded go file>
[Install]
WantedBy=multi-users.target
NOTE: As you can see this file created with production mode environment
- Start service:
sudo systemctl start blog
- Check service status:
sudo systemctl enable blog
- Create blog config for nginx:
touch /etc/nginx/sites-available/blog
- Copy these codes into blog config file:
server {
server_name .com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:<Port Number>;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen 80;
listen [::]:80;
}
- Create soft link for sites-enables:
sudo ln -s /etc/nginx/sites-available/blog /etc/nginx/sites-enabled
- Restart nginx:
sudo systemctl restart nginx