简体   繁体   中英

Deploying a Golang (Beego) in production

I came from a PHP background w/c is you can easily throw a PHP file in a server and that's it! How about in Golang, to be specific is the Beego framework because I already created a website using Beego but my stopper is (Q1)How to deploy in Ubunto and NGINX environment? (Q2)Do i need to compile the Beego project before deploying? (Q3)Do i need NGINX/Apache as server or use the built-in "run" of Beego?

Thank You.

How to deploy in Ubuntu and NGINX environment?

  1. Install Golang and run your Golang app on a particular port at localhost (say localhost:9000).
  2. Install Nginx ( https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-16-04 )
  3. Choose a domain or subdomain that you wish to point to your Golang App.
  4. Create Nginx server block ( https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04 )

    Example Server block pointing to Golang App running at localhost:9000:

     server { listen 80; server_name my.domain.com; location / { proxy_pass http://localhost:9000; proxy_http_version 1.1; 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; proxy_cache_bypass $http_upgrade; proxy_pass_request_headers on; proxy_read_timeout 150; } } 
  5. To run your program efficiently, you may use supervisor ( https://www.digitalocean.com/community/tutorials/how-to-install-and-manage-supervisor-on-ubuntu-and-debian-vps )

1 - Pretty much install golang (Using the built-in version: sudo apt-get install golang-go or Detailed guide ) and build the project or simply copy the built project and their libs if needed;

2 - Ideally, the production environment shouldn't include the source codes on most scenarios, therefore a built server could be better.

3 - Although not required, most people recommend to run golang behind a nginx. The reason for that is due to several optimizations (eg:static files serving) made on NGinx/Apache, which you would be missing on direct running your bin on server 80.

Side notes:
- the go run ... is the same thing as the go build ... + run the built executable
- If you are just deploying the binary, build using the same OS / configurations.
- Remember to follow the basic security guides, just open the ports needed, secure the server before deploying, limit users access, etc.

浏览以下链接: link1link2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM