You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Golang(Beego)生产环境部署咨询:Ubuntu+Nginx等相关疑问

Beego Deployment on Ubuntu + Nginx: Answers to Your Questions

Hey there! As someone who's made the jump from PHP to Go/Beego deployment, I totally get where these questions are coming from—PHP's "upload and go" workflow is super straightforward, but Go-based frameworks have their own rhythm. Let's unpack your questions one by one:

1. How to deploy a Beego project on Ubuntu + Nginx?

Here's a step-by-step workflow I've used repeatedly for production deployments:

Step 1: Prepare the server environment

First, make sure your Ubuntu server has Go installed (you only need this if you're compiling on the server; alternatively, compile locally and just upload the binary). Install Go with:

sudo apt update && sudo apt install golang-go -y

Then, install the Beego framework and Bee tool (if you haven't already):

go get github.com/astaxie/beego
go get github.com/beego/bee/v2

Step 2: Compile your Beego project

If working on the server, clone your project repo first, then navigate to the project root and run:

bee build -o my-beego-app

This generates a standalone binary named my-beego-app (you can rename it as needed). For production, I recommend compiling locally on your dev machine—just run the same command, then use scp to transfer the binary and your static/views folders to the server (no need to install Go on production this way).

Step 3: Configure Nginx as a reverse proxy

Nginx handles incoming requests, serves static files directly (way more efficient than letting Beego do it), and proxies dynamic requests to your app. Create a new Nginx config file:

sudo nano /etc/nginx/sites-available/my-beego-app.conf

Paste this config (adjust paths and domain to match your setup):

server {
    listen 80;
    server_name your-domain.com;

    # Serve static files directly via Nginx
    location /static/ {
        root /path/to/your/project;
        expires 30d;
    }

    location /views/ {
        root /path/to/your/project;
        expires 30d;
    }

    # Proxy dynamic requests to Beego app
    location / {
        proxy_pass http://127.0.0.1:8080; # Match Beego's default port
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Enable the config and restart Nginx:

sudo ln -s /etc/nginx/sites-available/my-beego-app.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx

Step 4: Run the Beego app as a system service

To ensure your app starts on boot and restarts if it crashes, create a systemd service file:

sudo nano /etc/systemd/system/my-beego-app.service

Add this content (adjust paths and user):

[Unit]
Description=My Beego Application
After=network.target

[Service]
User=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/project/my-beego-app
Restart=always

[Install]
WantedBy=multi-user.target

Reload systemd and start the service:

sudo systemctl daemon-reload
sudo systemctl start my-beego-app
sudo systemctl enable my-beego-app

2. Do I need to compile the Beego project before deployment?

Absolutely. Unlike PHP (which is interpreted at runtime), Go is a compiled language—Beego apps are just Go applications under the hood. Compiling turns your code into a single, standalone binary that runs on the server without needing Go or extra dependencies installed (other than basic system libraries).

The bee run command you might use in development is great for hot-reloading and debugging, but it’s not built for production. Compiling gives you an optimized, stable binary ready to handle real traffic.

3. Should I use Nginx/Apache or Beego's built-in run command for deployment?

Always use Nginx (or Apache) in production—Beego's built-in server is designed for development and debugging, not real-world traffic. Here's why:

  • Performance & Stability: Nginx is built to handle thousands of concurrent connections efficiently, while Beego's built-in server is lightweight but not optimized for high load.
  • Static File Handling: Nginx serves CSS, JS, and images way faster than Beego can—offloading this to Nginx frees up your app to focus on dynamic requests.
  • SSL/TLS Support: Configuring SSL with Nginx (using Let's Encrypt, for example) is straightforward and more secure than handling SSL directly in your app.
  • Load Balancing: If you ever scale to multiple servers, Nginx can act as a load balancer to distribute traffic evenly.
  • Security: Nginx handles rate limiting, request filtering, and hiding sensitive server info—adding an extra layer of protection between your app and the internet.

Save bee run for local development only—stick to the compiled binary behind Nginx/Apache in production.

内容的提问来源于stack exchange,提问作者Jake Pucan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:07:24