Contents
Step-by-Step Guide: Deploying a Golang API Backend on a Linux Server
Prerequisites:
- You have a Golang API project ready.
- You have access to a Linux server.
- Apache is installed and properly configured on the server.
Deployment Steps:
1. Check the .env
File:
Ensure your Golang project has a properly configured .env
file containing all the required environment variables.
2. Build the Binary:
a. If you're developing on Windows and wish to build a Linux binary, open a command prompt and set the GOOS
environment variable to "linux":
$env:GOOS = "linux"
b. Verify that GOOS
is set correctly in your Go environment by running:
go env
c. Build the binary using:
go build
The resulting binary will have the same name as your repository's root directory.
3. Copy Binary and .env to the Server:
Transfer the binary and the .env
file to your Linux server. You can use tools like SCP or SFTP to upload these files.
4. Create a Linux Service:
You can opt to run your Golang binary as a systemd service. Create a .service
file in /etc/systemd/system/
(e.g., yourapp.service
) with the following content:
[Unit]
Description=Your Golang API Service
After=network.target
[Service]
ExecStart=/path/to/your/binary
Restart=always
WorkingDirectory=/path/to/your/project
User=your_username
Group=your_group_name
[Install]
WantedBy=multi-user.target
ExecStart
: Specify the path to your Golang binary.WorkingDirectory
: Set this to the directory where your project is located.-
User
andGroup
: Specify the user and group under which the service should run.Save the file and enable the service:
sudo systemctl enable yourapp.service sudo systemctl start yourapp.service
5. Database Import and Migration:
-
Ensure that your database is imported and properly configured.
-
Make sure the "goose_db_version" table in the database contains data. If it's empty, delete the table to prevent migration issues, as an empty table can result in a "no version found" error.
-
If you have a separate "goose.go" file, build it into a binary and copy both the binary and the migration folder to the project root on the server. Here's an example of building the "goose.go" file into a binary:
go build -o do_migration ./migrations/goose.go
Using the example above, you can run the migration with
./do_migration
.
6. Configure Apache as a Reverse Proxy:
-
Ensure that the Apache modules
proxy
andproxy_http
are enabled:sudo a2enmod proxy sudo a2enmod proxy_http
-
Configure your Apache virtual host to act as a reverse proxy. Edit your virtual host configuration file, typically located in
/etc/apache2/sites-available/your-site.conf
, and include the following:< VirtualHost *:80> ServerName yourdomain.com # Proxy requests to the Go binary ProxyPass /api http://127.0.0.1:8080 ProxyPassReverse /api http://127.0.0.1:8080 # Additional Apache configuration settings... < Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride None Require all granted < /Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined < /VirtualHost>
-
Restart Apache to apply the changes:
sudo systemctl restart apache2
-
If you are using a VPS with HaProxy, ensure proper port forwarding for port 80/443.
7. HTTPS / Certbot:
Implement HTTPS using Certbot for SSL/TLS certificates, as needed.
8. Testing:
You can now access your Golang API at http://domain.com/api. Ensure that everything is working as expected.
9. Common Problems:
- Golang Backend Responds with a 404 Error for Existing Endpoints and Contains Double Trailing Slashes
//
:- Check your Apache proxy configuration, making sure the
ProxyPass
directive doesn't have a trailing slash at the end, likehttp://localhost:8080/
<-dont add '/' on the back.
- Check your Apache proxy configuration, making sure the
- Apache's New Configuration Is Not Loaded After Modification:
- Ensure you manually update the configurations for both HTTP and HTTPS if you've made changes. Certbot-generated configurations do not update automatically.
Your Golang API backend should now be successfully deployed on your Linux server and accessible via Apache's reverse proxy.