github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/docs/assets/js/27.9c5b36f2.js (about)
1 (window.webpackJsonp=window.webpackJsonp||[]).push([[27],{399:function(e,t,a){"use strict";a.r(t);var o=a(25),n=Object(o.a)({},(function(){var e=this,t=e.$createElement,a=e._self._c||t;return a("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[a("h1",{attrs:{id:"deployment"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#deployment"}},[e._v("#")]),e._v(" Deployment")]),e._v(" "),a("p"),a("div",{staticClass:"table-of-contents"},[a("ul",[a("li",[a("a",{attrs:{href:"#introduction"}},[e._v("Introduction")])]),a("li",[a("a",{attrs:{href:"#application-configuration"}},[e._v("Application configuration")])]),a("li",[a("a",{attrs:{href:"#build"}},[e._v("Build")])]),a("li",[a("a",{attrs:{href:"#deamon"}},[e._v("Deamon")]),a("ul",[a("li",[a("a",{attrs:{href:"#systemd"}},[e._v("Systemd")])])])])])]),a("p"),e._v(" "),a("h2",{attrs:{id:"introduction"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#introduction"}},[e._v("#")]),e._v(" Introduction")]),e._v(" "),a("p",[e._v("There are some important details to think about before deploying your Goyave application to production. We are going to cover them in this section of the guide, and make sure your applications are deployed properly.")]),e._v(" "),a("h2",{attrs:{id:"application-configuration"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#application-configuration"}},[e._v("#")]),e._v(" Application configuration")]),e._v(" "),a("p",[e._v("Be sure to deploy your application with a "),a("code",[e._v("config.production.json")]),e._v(" config file containing the correct values for your production environment and "),a("strong",[e._v("set the "),a("code",[e._v("GOYAVE_ENV")]),e._v(" environment variable to "),a("code",[e._v("production")]),e._v(".")])]),e._v(" "),a("ol",[a("li",[e._v("Ensure that the "),a("code",[e._v("app.environment")]),e._v(" entry is "),a("code",[e._v("production")]),e._v(".")]),e._v(" "),a("li",[e._v("The "),a("code",[e._v("server.host")]),e._v(" entry should be "),a("code",[e._v("0.0.0.0")]),e._v(" if you want to open access to your service from anywhere. If you're using Apache or Nginx as a proxy on the same machine, keep it at "),a("code",[e._v("127.0.0.1")]),e._v(" so the server will only be accessible through the proxy.")]),e._v(" "),a("li",[e._v("Change the "),a("code",[e._v("server.domain")]),e._v(" entry to your domain name, if you use one.")]),e._v(" "),a("li",[e._v("The "),a("code",[e._v("server.port")]),e._v(" and "),a("code",[e._v("server.httpsPort")]),e._v(" will very likely require a change. Most of the time, you need "),a("code",[e._v("80")]),e._v(" and "),a("code",[e._v("443")]),e._v(" respectively.")]),e._v(" "),a("li",[e._v("If you use "),a("code",[e._v("https")]),e._v(", be sure to provide the paths to your "),a("code",[e._v("server.tls.cert")]),e._v(" and "),a("code",[e._v("server.tls.key")]),e._v(". Learn more "),a("RouterLink",{attrs:{to:"/guide/configuration.html#setting-up-https"}},[e._v("here")]),e._v(".")],1),e._v(" "),a("li",[a("code",[e._v("server.debug")]),e._v(" "),a("strong",[e._v("must")]),e._v(" be set do "),a("code",[e._v("false")]),e._v(". You don't want anyone to get important information about your internal errors and therefore your code when an error occurs.")]),e._v(" "),a("li",[e._v("Change your database connection credentials. "),a("code",[e._v("database.autoMigrate")]),e._v(" should be set to "),a("code",[e._v("false")]),e._v(".")])]),e._v(" "),a("h2",{attrs:{id:"build"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#build"}},[e._v("#")]),e._v(" Build")]),e._v(" "),a("p",[e._v("Of course, don't run your application with "),a("code",[e._v("go run")]),e._v(" in production. Build your application using "),a("code",[e._v("go build")]),e._v(" and deploy the executable, alongside the config files and resources directory.")]),e._v(" "),a("p",[e._v("The following Dockerfile is an example of a goyave application called "),a("code",[e._v("docker-goyave")]),e._v(":")]),e._v(" "),a("div",{staticClass:"language-docker extra-class"},[a("pre",{pre:!0,attrs:{class:"language-docker"}},[a("code",[a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("FROM")]),e._v(" golang"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(":")]),e._v("alpine as builder\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("WORKDIR")]),e._v(" /app\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("COPY")]),e._v(" . .\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("RUN")]),e._v(" go build "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("-")]),e._v("ldflags "),a("span",{pre:!0,attrs:{class:"token string"}},[e._v('"-w -s"')]),e._v("\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("FROM")]),e._v(" alpine"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(":")]),e._v("3.9\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("WORKDIR")]),e._v(" /app\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("COPY")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("-")]),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("-")]),e._v("from=builder /app/docker"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("-")]),e._v("goyave ./docker"),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("-")]),e._v("goyave\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("COPY")]),e._v(" resources /app/resources\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("COPY")]),e._v(" config.production.json ./config.production.json\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("EXPOSE")]),e._v(" 80\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("ENV")]),e._v(" GOYAVE_ENV=production\n\n"),a("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("ENTRYPOINT")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("[")]),e._v(" "),a("span",{pre:!0,attrs:{class:"token string"}},[e._v('"./docker-goyave"')]),e._v(" "),a("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("]")]),e._v("\n")])])]),a("h2",{attrs:{id:"deamon"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#deamon"}},[e._v("#")]),e._v(" Deamon")]),e._v(" "),a("p",[e._v("Goyave applications are standalone and don't require any web server (such as Apache or Nginx) to function. However, this also means that you will need some additional installation steps on your production server.")]),e._v(" "),a("p",[e._v("When running a web service, it is important that it stays online. Creating a "),a("strong",[e._v("deamon")]),e._v(" is necessary so your server can run in the background, have the correct filesystem permissions, and restart automatically after a crash or a reboot.")]),e._v(" "),a("p",[e._v("You can achieve this with any supervisor program, but in this guide, we will use systemd as it's the most common one.")]),e._v(" "),a("h3",{attrs:{id:"systemd"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#systemd"}},[e._v("#")]),e._v(" Systemd")]),e._v(" "),a("p",[e._v("First, we need to create a user and its group for our application:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("sudo addgroup goyave\nsudo useradd -r -s /bin/false goyave\nsudo usermod -a -G goyave goyave\n")])])]),a("p",[e._v("You may use the following systemd unit file to deploy your Goyave application:")]),e._v(" "),a("p",[a("code",[e._v("/etc/systemd/system/my-goyave-application.service")]),e._v(":")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v('[Unit]\nDescription=Goyave application deamon\n\n# If you are using a database, mysql for example, make sure the application\n# service starts AFTER the database service.\n# After=mysqld.service\nAfter=network.target\nStartLimitIntervalSec=0\n\n[Service]\nEnvironment=GOYAVE_ENV=production\nWorkingDirectory=/path/to/goyave-application\nExecStart=/path/to/goyave-application/executable\n\n# Process management\n####################\n\nType=simple\n\n# Can be changed to "on-failure"\nRestart=always\nRestartSec=1\nTimeoutStopSec=600\n\n# Run as goyave:goyave\nUser=goyave\nGroup=goyave\n\n# Hardening measures\n####################\n\n# Provide a private /tmp and /var/tmp.\nPrivateTmp=true\n\n# Mount /usr, /boot/ and /etc read-only for the process.\nProtectSystem=full\n\n# Deny access to /home, /root and /run/user\nProtectHome=true\n\n# Disallow the process and all of its children to gain\n# new privileges through execve().\nNoNewPrivileges=true\n\n# Use a new /dev namespace only populated with API pseudo devices\n# such as /dev/null, /dev/zero and /dev/random.\nPrivateDevices=true\n\n# Deny the creation of writable and executable memory mappings.\nMemoryDenyWriteExecute=true\n\n[Install]\nWantedBy=multi-user.target\n')])])]),a("div",{staticClass:"custom-block warning"},[a("p",{staticClass:"custom-block-title"},[e._v("WARNING")]),e._v(" "),a("p",[e._v("Replace "),a("code",[e._v("my-goyave-application")]),e._v(" with the name of your application and "),a("code",[e._v("/path/to/goyave-application")]),e._v(" with the "),a("strong",[e._v("absolute")]),e._v(" path to your application. This is important to have unique application names in case you run multiple goyave applications on the same server. You should also create a separate user and group for each application.")])]),e._v(" "),a("p",[e._v("Now let's run the service and enable it on system startup:")]),e._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[e._v("sudo systemctl start my-goyave-application\nsudo systemctl enable my-goyave-application\n")])])])])}),[],!1,null,null,null);t.default=n.exports}}]);