rafaelmsantos rafaelmsantos - 1 month ago 22
Apache Configuration Question

How do I deploy a golang app with Apache installed on Ubuntu 16.04 on digitalocean?

I am learning Go at the moment and I have built really simple webapps following some tutorials with the net/http package. I have created a simple wishlist, where I add an item and than it does to a simple table of things I want, pretty simple.

Now I want to deploy this app to my Digital Ocean droplet, but I just don't know how. I have some php websites with different domains already with Apache behind it.

I am really a begginer on this "servers configuration" thing, usually with php is pretty easy on webhosts and I didn't need this much experience. Can you point me on the right direction to make my Go app available at a domain I own, without the ports bit? Preferably with Apache.

Thanks :)

Answer

Note: Almost everything in this answer needs to be customized to your specific circumstances. This is written with the assumption that your Go app is called "myapp" and you have made it listen at port 8001.

You should make a systemd unit file to make your app start up automatically at boot. Put the following in /etc/systemd/system/myapp.service (adapt to your needs):

[Unit]
Description=MyApp webserver

[Service]
ExecStart=/www/myapp/bin/webserver
WorkingDirectory=/www/myapp
EnvironmentFile=-/www/myapp/config/myapp.env
StandardOutput=journal
StandardError=inherit
SyslogIdentifier=myapp
User=www-data
Group=www-data
Type=simple
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enable automatic startup:

systemctl enable myapp

Start it:

systemctl start myapp

Check that it is ok:

systemctl status myapp

Then it is time to configure Apache virtualhost for your app. Put the following in /etc/apache2/sites-available/myapp.conf:

<VirtualHost *:80>
        ServerName myapp.example.com
        ServerAdmin webmaster@example.com
        DocumentRoot /www/myapp/public
        ErrorLog ${APACHE_LOG_DIR}/myapp-error.log
        CustomLog ${APACHE_LOG_DIR}/myapp-access.log combined

        ProxyPass "/" "http://localhost:8001/"
</VirtualHost>

Enable the configuration:

a2ensite myapp

Make sure you did not make mistake in Apache configuration:

httpd -t

Reload Apache configuration:

systemctl reload apache2

Remember to make the name myapp.example.com available in DNS.

That's it!