Beginner struggling to reload/start Caddy (listen tcp :80: bind: address already in use)

1. Caddy version (caddy version):

2.5.1

2. How I run Caddy:

To host a couple websites, and I was working toward hosting a Huginn instance.

a. System environment:

Debian VPS

b. Command:

caddy start
caddy reload

c. Service/unit/compose file:

n/a, I think?

d. My complete Caddyfile or JSON config:

# The Caddyfile is an easy way to configure your Caddy web server.
#
# Unless the file starts with a global options block, the first
# uncommented line is always the address of your site.
#
# To use your own domain name (with automatic HTTPS), first make
# sure your domain's A/AAAA DNS records are properly pointed to
# this machine's public IP, then replace the line below with your
# domain name.

natehn.com {
        root * /home/nathan/blog/public

        # Enable the static file server.
        file_server

        encode zstd gzip

        header Access-Control-Allow-Origin
        header Strict-Transport-Security "max-age=31536000; includeSubDomains"
        #       header Content-Security-Policy "default-src 'none'; font-src https://fonts.gstatic.com; img-src 'self' https://i.imgur.com; object-src 'none'; script-src 'self'; style-src 'self'"
        header X-Content-Type-Options nosniff
        header X-Frame-Options DENY
        header Referrer-Policy no-referrer-when-downgrade
        header Permissions-Policy interest-cohort=()
        header -Server
        header X-XSS-Protection "1; mode=block"

        route {
                header Cache-Control max-age=3600
                header /static/* Cache-Control max-age=31536000
        }

photos.natehn.com {
        root * /home/nathan/photos/_build

        # Enable the static file server.
        file_server

        encode zstd gzip

        header Access-Control-Allow-Origin
        header Strict-Transport-Security "max-age=31536000; includeSubDomains"
        #       header Content-Security-Policy "default-src 'none'; font-src https://fonts.gstatic.com; img-src 'self' https://i.imgur.com; object-src 'none'; script-src 'self'; style-src 'self'"
        header X-Content-Type-Options nosniff
        header X-Frame-Options DENY
        header Referrer-Policy no-referrer-when-downgrade
        header Permissions-Policy interest-cohort=()
        header -Server
        header X-XSS-Protection "1; mode=block"

        route {
                header Cache-Control max-age=3600
                header /static/* Cache-Control max-age=31536000
        }

        handle_errors {
                rewrite * /404/
                file_server
        }
}

3. The problem I’m having:

I updated Caddy through Homebrew (I was focused on something else and let it happen) and now I can’t start or reload Caddy and I have no idea why. I get the below error message and, tbh, I don’t know where to begin.

4. Error messages and/or full log output:

2022/06/30 23:26:18.067	INFO	using adjacent Caddyfile
reload: sending configuration to instance: performing request: Post "http://localhost:2019/load": dial tcp 127.0.0.1:2019: connect: connection refused

5. What I already tried:

At first I thought the Caddyfile was the issue because I made some changes, but I undid the changes and still had the same problem. Is this a Caddy issue or something else outside the scope of this forum?

Here’s what I get when I run systemctl start caddy:

Failed to start caddy.service: Access denied
See system logs and 'systemctl status caddy.service' for details.

And here’s what I get when I run systemctl status caddy.service:

â—Ź caddy.service - Caddy
   Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2022-06-30 18:51:27 UTC; 5h 9min ago
     Docs: https://caddyserver.com/docs/
  Process: 483 ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile (code=exited, st
 Main PID: 483 (code=exited, status=1/FAILURE)

The timing lines up with me updating Caddy, I"m pretty sure.

Hi :wave:

You seem to be missing a } here:

                 header /static/* Cache-Control max-age=31536000
         }
+ }

 photos.natehn.com {
         root * /home/nathan/photos/_build

You can validate your config via caddy validate before applying :slight_smile:


Please don’t use caddy start but instead caddy run. Or even better, the systemd service, which you seem to have since you shared some systemctl status caddy.service output.
Please see Problem with with caddy - #2 by francislavoie


That isn’t actually a caddy error but instead systemd trying to tell you, that you are missing the necessary permissions and might need to use sudo (sudo systemctl start caddy.service) or user root.

To actually view the full logs of caddy (when running as systemd service), you would use something like
journalctl --unit caddy.service --no-pager | less +G (and press q if you want to exit again).


Also, with both root * /home/nathan/blog/public and /home/nathan/photos/_build you might be running into file permission issues.
I am more or less in the process of doing a proper write-up about common file permission with caddy (or any webserver really) which would explain that in detail.
But in the meantime, you might want to read:

tldr is basically: Don’t use /home/ but instead /srv/ or /var/www/.


Feel free to ask any additional questions, especially because if anything is unclear or if you encounter any issue :innocent:

3 Likes

Hi @emilylange I’m still struggling with this. Here’s what I’ve tried, although I must admit I don’t feel very confident about it:

nathan@hugo:/etc/caddy$ sudo systemctl start caddy
nathan@hugo:/etc/caddy$ sudo systemctl status caddy
â—Ź caddy.service - Caddy
   Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2022-07-01 03:47:55 UTC; 8s ago
     Docs: https://caddyserver.com/docs/
  Process: 17351 ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile (code=exited, 
 Main PID: 17351 (code=exited, status=1/FAILURE)

Jul 01 03:47:55 hugo caddy[17351]: {"level":"info","ts":1656647275.879891,"msg":"using provided conf
Jul 01 03:47:55 hugo caddy[17351]: {"level":"info","ts":1656647275.8873043,"logger":"admin","msg":"a
Jul 01 03:47:55 hugo caddy[17351]: {"level":"info","ts":1656647275.8879642,"logger":"http","msg":"se
Jul 01 03:47:55 hugo caddy[17351]: {"level":"info","ts":1656647275.88822,"logger":"http","msg":"enab
Jul 01 03:47:55 hugo caddy[17351]: {"level":"info","ts":1656647275.8922503,"logger":"tls.cache.maint
Jul 01 03:47:55 hugo caddy[17351]: {"level":"info","ts":1656647275.8925762,"logger":"tls.cache.maint
Jul 01 03:47:55 hugo systemd[1]: Started Caddy.
Jul 01 03:47:55 hugo caddy[17351]: run: loading initial config: loading new config: http app module:
Jul 01 03:47:55 hugo systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE
Jul 01 03:47:55 hugo systemd[1]: caddy.service: Failed with result 'exit-code'.
caddy reload
2022/07/01 03:44:02.283	INFO	using adjacent Caddyfile
reload: sending configuration to instance: performing request: Post "http://localhost:2019/load": dial tcp 127.0.0.1:2019: connect: connection refused
caddy stop
2022/07/01 03:47:17.982	WARN	failed using API to stop instance	{"error": "performing request: Post \"http://localhost:2019/stop\": dial tcp 127.0.0.1:2019: connect: connection refused"}
stop: performing request: Post "http://localhost:2019/stop": dial tcp 127.0.0.1:2019: connect: connection refused
nathan@hugo:~$ sudo systemctl reload caddy
caddy.service is not active, cannot reload.

I feel like every single caddy command - except caddy validate, thank you for that one! - throws an error.

Should I consider doing a fresh install?

I would argue a fresh install isn’t needed :slight_smile:

The caddy reload (stop and reload too) fails, because caddy isn’t running.
It crashed immediately after you ran sudo systemctl start caddy.
The sudo systemctl status caddy might be a bit hard to parse at first, but basically:

An up and running systemd service would usually read something like

   Active: active (running) since Fri 2022-07-01 03:47:55 UTC; 8s ago

The logs you shared in the systemctl status output are truncated, but we need to see the whole lines :slight_smile:

Could you please print the (last few) lines of journalctl --unit caddy.service --no-pager | less +G?
So we can see what’s actually going on :innocent:

2 Likes

Here you go! Thank you very much for walking through this with me, by the way.

nathan@hugo:~$ sudo systemctl status caddy
â—Ź caddy.service - Caddy
   Loaded: loaded (/lib/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2022-07-01 11:44:05 UTC; 55s ago
     Docs: https://caddyserver.com/docs/
  Process: 28502 ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile (code=exited, status=1/FAILURE)
 Main PID: 28502 (code=exited, status=1/FAILURE)

Jul 01 11:44:05 hugo caddy[28502]: {"level":"info","ts":1656675845.869066,"msg":"using provided configuration","config_file":"/etc/caddy/Caddyfile","config_adapter":""}
Jul 01 11:44:05 hugo caddy[28502]: {"level":"info","ts":1656675845.877141,"logger":"admin","msg":"admin endpoint started","address":"tcp/localhost:2019","enforce_origin":false,"origins":["//[::1]:2019","//127.0.0.1:2019","//localhost:2019"]}
Jul 01 11:44:05 hugo caddy[28502]: {"level":"info","ts":1656675845.8776665,"logger":"http","msg":"server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS","server_name":"srv0","https_port":443}
Jul 01 11:44:05 hugo caddy[28502]: {"level":"info","ts":1656675845.877912,"logger":"http","msg":"enabling automatic HTTP->HTTPS redirects","server_name":"srv0"}
Jul 01 11:44:05 hugo caddy[28502]: {"level":"info","ts":1656675845.880124,"logger":"tls.cache.maintenance","msg":"started background certificate maintenance","cache":"0xc0001accb0"}
Jul 01 11:44:05 hugo caddy[28502]: {"level":"info","ts":1656675845.8801503,"logger":"tls.cache.maintenance","msg":"stopped background certificate maintenance","cache":"0xc0001accb0"}
Jul 01 11:44:05 hugo caddy[28502]: run: loading initial config: loading new config: http app module: start: tcp: listening on :80: listen tcp :80: bind: address already in use
Jul 01 11:44:05 hugo systemd[1]: Started Caddy.
Jul 01 11:44:05 hugo systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE
Jul 01 11:44:05 hugo systemd[1]: caddy.service: Failed with result 'exit-code'.
3 Likes

Great!
The important bit is:

Perhaps another webserver is already running on that port :80.
Even if you don’t remember installing anything besides caddy, some VPS provider preinstall for example apache2 and put it in autostart.

We can continue to speculate, or just print whatever process is using port :80.
There are multiple ways to achieve that, pick one and maybe try another one if you get an error like command not found.
Also, use need sudo again, if you aren’t user root.

  1. lsof -i :80 -s TCP:LISTEN
  2. netstat -lnp | grep :80
  3. ss -lnp 'sport = :80'

Next step would be to stop whatever process that is and stop it from autostarting again (systemctl stop <service> and systemctl disable <service>, or systemctl disable --now <service>).

For example, in case of apache, use systemctl disable --now apache2

Feel free to post the output from one of the lsof/netstat/ss commands above, and we can figure out the service name together if you need help :innocent:

After that, caddy should start just fine :tada:

4 Likes

@emilylange you are a life-saver! It was apache2. Such generosity. I hope you have an incredible weekend.

3 Likes

Aww, thank you :innocent:
Glad I could help :slight_smile:

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.