如何在Debian 8服务器部署并启动Go语言二进制程序?
Hey there! Let's get your Go program up and running smoothly on your Debian 8 server. Here's a step-by-step breakdown tailored to your setup:
Right now, your hello binary has permissions set to 0644, which only allows reading/writing—it doesn't have execute permission, so the system can't run it. Let's fix that:
chmod +x /Go/hello
You can also set it to 755 (read/write/execute for owner, read/execute for group/others) with chmod 755 /Go/hello if you want a more standard permission set for executable files.
First, run the binary directly to make sure it launches without errors:
/Go/hello
Now, test if it's accessible. If your program listens on a port (say, 8080 is common for dev setups), you can either:
- Run
curl http://localhost:[your-port]directly on the server to check for the "Hi there, I love hello!" output - Or from your local machine, open Safari and visit
http://[your-server-ip]:[your-port]
Important: If you can't access it from your local browser, check if Debian's firewall (iptables) is blocking the port. To open it:
iptables -A INPUT -p tcp --dport [your-port] -j ACCEPT # Save the rule so it persists after reboot iptables-save > /etc/iptables/rules.v4
If you just run the binary in your terminal, it'll stop as soon as you close the terminal. We have two solid options here:
Option A: Use systemd (recommended for reliability)
Debian 8 uses systemd, so we can create a service file to manage your program—this will let it run in the background, restart automatically if it crashes, and start on server boot.
- Create a service file:
nano /etc/systemd/system/hello.service
- Paste this content (adjust paths or port-related configs if needed):
[Unit] Description=Hello Go Web Service After=network.target [Service] ExecStart=/Go/hello Restart=always User=root WorkingDirectory=/Go [Install] WantedBy=multi-user.target
- Save and exit nano (press Ctrl+O, then Ctrl+X).
- Reload systemd to pick up the new service:
systemctl daemon-reload
- Start the service:
systemctl start hello.service
- Set it to start automatically on server boot:
systemctl enable hello.service
You can check the service status anytime with:
systemctl status hello.service
Option B: Use nohup (quick and simple)
If you don't need auto-restart or boot functionality right now, nohup will let you run the program in the background easily:
nohup /Go/hello > /Go/hello.log 2>&1 &
This redirects all program output to hello.log in the Go folder, and the program keeps running even after you close your terminal session.
After setting up either option, double-check:
- The program process is running with
ps aux | grep hello - You can access the "Hi there, I love hello!" message from your browser using the server's IP and port
内容的提问来源于stack exchange,提问作者sibert

