Keep a hugo application up and running on the server permanently

Hello with everyone I am new using HUGO framework. What I did was a web application in hugo and I uploaded it to my aws server in amazon, I did the configurations in ngnix and when I execute the command:

sudo hugo server --watch --baseUrl ='my url' --appendPort = false

the application rises correctly, but when I close the aws console the application is dropped. So how can I make the application stay up and running on the server? Thank you very much for your help

You are not supposed to have the command running. You just have to run it once to have your static site built on the public/ directory. After that you can upload it to your server. If you are using AWS, take a look here https://gohugo.io/hosting-and-deployment/hosting-on-aws-amplify/

yes, what I did was to execute the hugo command to generate the public directory and upload it to the server. I’m using Ligthsail from aws.
Is there any other configuration that I may be missing?

The command you used is meant for development. To build, just run hugo without any further arguments. It will check for those in your config.toml file.

yes, just run hugo and generate the public directory, then in the config.toml file add the parameter publishDir = “public” and upload the project to the server. And when I load the application in the browser I get a 502 Bad Gateway. Only when I run the following command on the server sudo hugo server --watch - baseUrl = ‘my url’ --appendPort = false, the application is correctly running and I can observe it from the web browser.
I hope to make myself understood, my English is not very good.

That is beginning to sound more like a misconfiguration of Nginx. Do you have access to the error log so I can see what is causing the 502 error?

yes, this appears in the error.log

Alright, here’s what I think it’s happening. Your nginx configuration may not be right and may be pointing to the wrong location or not enabled for that site.

When you run the command, Hugo takes over nginx, listening on port 80 (because you are running it with sudo).

Double check that your nginx config is fine and use a simple index.html saying hello world to make sure it’s working. Then upload your files to replace it.

I have never used aws, so this is as far as I can reach.

2 Likes

I concur with what @brunoamaral said. Lightsail lets you pick a distro or their pre-configured “nginx” selection. If you chose the latter it should just work. But you need to copy the files to the correct place.

If I were doing this, I would be doing the dev on my local machine like:

  • do dev, and test with hugo server and whatever switches, accessing via localhost:1313.
  • periodically git commit
  • when I’m ready to publish, run hugo with whatever switches, to generate my public somewhere.
  • use rsync to push my newly generated public to the correct location on the lightsail server (assumes I have all the needed ssh keys and so on set up)

Then all lightsail does is host my copied files. If you’re unable to use a local machine for some reason, then you can indeed do the dev on the server, but maybe just let it use the default 1313 but open that port in the firewall, so that hugo server is not clobbering your web server.

Either way, it is easiest to make a script to run hugo to generate in some location, then rsync your public to where it is needed. I’m doing this in zsh functions usually. Something like:

hugodeploy-mysite () {
  _hugobin="$HOME/gocode/bin/hugo"
  _gitbin="/usr/local/bin/git"
  _rsyncbin="/usr/bin/rsync"
  _workingdir="$HOME/dev/mysite"
  _targetdir="/path/to/webserver/htdocs/"
  _datetime=$(date +'%Y%m%d-%H%M%S')
  cd ${_workingdir}
  ${_hugobin} --gc --minify
  ${_rsyncbin} --verbose --compress --archive --recursive --partial --progress --checksum --delete --exclude '.git' ${_workingdir}/public/ ${_targetdir}
}

Or, you can keep something like that in a script in your site’s repo, for instance.

I made a correction to the nginx configuration file, apparently it should point to the public directory of the hugo project. But I can only load my application in incognito chrome browser. In Firefox I have this error: “It has a security policy called HTTP Strict Transport Security (HSTS), which means that Firefox can only connect to it safely, you can not add an exception to visit this site.” any ideas?

The server’s config has an add_header setting regarding something like, forcing https I would imagine? When you set it up, did you choose the pre-configured lightsail nginx template? If so, it probably has a bunch of settings pre configured, and you should consult their docs for that.

Also, while you can indeed point nginx at hugo’s public folder, you need to make other settings including permissions to make sure that works. I recommend copying public into the structure they have provided, that is, into the correct directory per their instructions or manual.