dou5454954610 2016-10-27 10:49
浏览 65
已采纳

如何在Digitalocean的Ubuntu 16.04上部署安装了Apache的golang应用程序?

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

  • 写回答

1条回答 默认 最新

  • douke3007 2016-10-27 12:17
    关注

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊