Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 542
rafaelmorais Avatar asked Oct 27 '16 10:10

rafaelmorais


People also ask

Is httpd and Apache the same?

"httpd" is the name of the deamon/service that runs in the background and processes all requests. "Apache Web Server" is the name of the software, which includes httpd.


1 Answers

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 (and many others).

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

For documentation of these settings see: man systemd.unit, man systemd.service and man systemd.exec

Start it:

systemctl start myapp

Check that it is ok:

systemctl status myapp

Enable automatic startup:

systemctl enable 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 [email protected]
        DocumentRoot /www/myapp/public
        ErrorLog ${APACHE_LOG_DIR}/myapp-error.log
        CustomLog ${APACHE_LOG_DIR}/myapp-access.log combined

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

Documentation of the proxy related settings: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

Enable the configuration:

a2ensite myapp

Make sure you did not make mistake in Apache configuration:

apachectl configtest

In case the proxy modules are not previously enabled you will get an error at this point. In that case enable the proxy modules and try again:

a2enmod proxy
a2enmod proxy_http
apachectl configtest

Reload Apache configuration:

systemctl reload apache2

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

That's it!

EDIT: Added pointers to documentation and instructions for enabling Apache modules if needed. Use apachectl for config test.

like image 124
snap Avatar answered Sep 20 '22 23:09

snap