Deploying a Go (Golang) project on an Ubuntu server requires several steps, including setting up the server environment, building the Go application, and configuring the server for production use. Here’s a step-by-step guide:
Make sure your Ubuntu server is up-to-date and has the necessary dependencies installed.
sudo apt update && sudo apt upgrade -yIf Go is not installed yet, you can install it as follows:
sudo apt install golang-go -yAlternatively, you can download the latest Go release from Go's official website and follow the instructions for installing the binary.
If you have your project in a Git repository, clone it to your server:
cd /home/username
git clone https://github.com/yourusername/your-golang-project.git
cd your-golang-projectIf your project uses third-party dependencies, you can install them using Go Modules:
go mod tidyThis will download and install any missing dependencies listed in your go.mod file.
Once the dependencies are installed, build your Go project.
go build -o yourapp .This will create an executable file named yourapp in the current directory.
You need to set environment variables, configure ports, databases, or other services depending on your application’s needs.
Create a .env file or export environment variables directly on the server:
export APP_ENV=production
export DB_HOST=localhost
export DB_PORT=5432
export DB_USER=myuser
export DB_PASSWORD=mypasswordYou can run the Go application directly:
./yourappIf your application runs on a specific port, for example port 8080, make sure to open the port in your firewall:
sudo ufw allow 8080Alternatively, for long-running production services, you can run the application in the background.
nohup ./yourapp &To make your application run as a service and automatically start on boot, you can create a systemd service.
Create a new service file in /etc/systemd/system/yourapp.service:
sudo nano /etc/systemd/system/yourapp.serviceAdd the following configuration:
[Unit]
Description=Go Application
After=network.target
[Service]
ExecStart=/home/username/your-golang-project/yourapp
WorkingDirectory=/home/username/your-golang-project
Restart=always
User=username
Group=username
Environment=APP_ENV=production
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable yourapp.servicesudo systemctl start yourapp.serviceYou can check the status of the service:
sudo systemctl status yourapp.serviceIf you want to expose your Go application to the web via Nginx, you can set up a reverse proxy.
sudo apt install nginx -yCreate a configuration file for your site:
sudo nano /etc/nginx/sites-available/yourappAdd the following configuration:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:8080; # Change to your application's 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;
}
}Create a symlink to enable the site:
sudo ln -s /etc/nginx/sites-available/yourapp /etc/nginx/sites-enabled/sudo systemctl restart nginxNow, Nginx will forward traffic to your Go application.
You can use tools like journalctl to check logs for your service:
sudo journalctl -u yourapp.serviceAlternatively, you can set up log files or use a service like logrotate to manage logs.
- Update Ubuntu and install Go.
- Clone your project and install dependencies.
- Build the Go application.
- Set environment variables for production.
- Run the application or set it up as a service.
- Optionally, set up Nginx as a reverse proxy.
Let me know if you need more detailed guidance on any of these steps!