Beego Deployment and Usage Guide
Prerequisites
- Go programming language (version 1.16 or higher)
- Go Modules (enabled by default in Go 1.16+)
- Git for version control
- Text editor or IDE (VS Code, GoLand, etc.)
Installation
Install Beego Framework
# Download and install beego v2.0.0
go get github.com/astaxie/beego@v2.0.0
# Verify installation
go list -m github.com/astaxie/beego
Install Bee Tool (Optional but recommended)
Bee is a powerful development tool for Beego applications:
go get github.com/beego/bee/v2@latest
Configuration
Environment Variables
Beego uses configuration files by default. You can set environment variables for customization:
# Application configuration
export BEEGO_RUNMODE="dev" # or "prod"
export BEEGO_PORT="8080"
# Database configuration (if using ORM)
export BEEGO_DB_DRIVER="mysql"
export BEEGO_DB_USER="your_username"
export BEEGO_DB_PASSWORD="your_password"
export BEEGO_DB_HOST="localhost"
export BEEGO_DB_PORT="3306"
export BEEGO_DB_NAME="your_database"
Configuration Files
Create a conf/app.conf file in your project directory:
appname = your_app_name
runmode = dev
httpport = 8080
# Database configuration
db.driver = mysql
db.user = your_username
db.password = your_password
db.host = localhost
db.port = 3306
db.name = your_database
Build & Run
Create a New Beego Project
# Create a new project using bee tool
bee new myproject
# Or manually create project structure
mkdir myproject && cd myproject
go mod init myproject
Build and Run Locally
# Navigate to your project directory
cd myproject
# Build the application
go build main.go
# Run the application
./myproject
# Or run directly without building
go run main.go
Run in Development Mode
# Use bee tool for hot reload
bee run
# Or set runmode explicitly
BEEGO_RUNMODE=dev go run main.go
Run in Production Mode
# Build for production
go build -o myproject main.go
# Set production environment
BEEGO_RUNMODE=prod ./myproject
Deployment
Deploy to Cloud Platforms
Heroku
# Create Heroku app
heroku create your-app-name
# Set Go buildpack
heroku buildpacks:set heroku/go
# Deploy
git push heroku master
Google Cloud Run
# Build container
gcloud builds submit --tag gcr.io/PROJECT-ID/your-app
# Deploy to Cloud Run
gcloud run deploy your-app --image gcr.io/PROJECT-ID/your-app --platform managed
AWS Elastic Beanstalk
# Initialize EB application
eb init your-app-name
# Create environment
eb create your-app-env
# Deploy
eb deploy
Deploy to Traditional Servers
# Build binary
go build -o beego-app main.go
# Transfer to server
scp beego-app user@your-server:/path/to/app
# Run as service
sudo tee /etc/systemd/system/beego-app.service > /dev/null <<EOF
[Unit]
Description=Beego Application
After=network.target
[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/app
ExecStart=/path/to/app/beego-app
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# Enable and start service
sudo systemctl enable beego-app
sudo systemctl start beego-app
Troubleshooting
Common Issues and Solutions
Port Already in Use
# Check what's using the port
lsof -i :8080
# Kill the process
kill -9 <PID>
Module Not Found
# Clear module cache
go clean -modcache
# Download dependencies again
go mod download
Database Connection Issues
# Check database configuration
cat conf/app.conf
# Test connection
go run main.go --check-db
Hot Reload Not Working
# Ensure bee tool is installed
bee version
# Check file permissions
chmod +x $(which bee)
Build Errors
# Check Go version
go version
# Update Go if needed
# For Ubuntu/Debian:
sudo apt-get update && sudo apt-get install golang
# For macOS:
brew install go
Performance Issues
# Enable profiling
BEEGO_RUNMODE=prod go run main.go --pprof
# Check logs
tail -f logs/your-app.log
Useful Commands
# Check beego version
go list -m github.com/astaxie/beego
# Generate API documentation
bee generate docs
# Run tests
go test ./...
# Check for race conditions
go test -race ./...
Getting Help
- Official Documentation: http://beego.me
- Examples: https://github.com/beego-dev/beego-example
- Community: http://beego.me/community
- Slack: https://beego.slack.com
- QQ Group: 523992905
For issues not covered here, check the Beego documentation or create an issue in the GitHub repository.