andrewheiss / ath-cloud-caddy-simplified

4 stars 0 forks source link

How to install caddy? #1

Open andreashandel opened 8 months ago

andreashandel commented 8 months ago

Hi Andrew, Saw your message on Mastodon about your DO upgrade and it's super timely as I'm trying to set up a DO server that runs wordpress, ghost, shiny and ngnix for static pages. Seems like using Docker is ideal. I'm in the midst of fiddling with it. Unclear to me how to get caddy installed, is that another docker image? I found this https://www.digitalocean.com/community/tutorials/how-to-host-a-website-with-caddy-on-ubuntu-22-04 and I assume if I go through it it might work, but I also found a docker image and that seems easier.

Would love to read a future blog post that describes all those steps in detail :)

andrewheiss commented 7 months ago

The Docker Compose file here takes care of that: https://github.com/andrewheiss/ath-cloud-caddy-simplified/blob/main/docker/docker-compose.yml

Docker Compose lets you orchestrate multiple containers all at once and let them talk to each other and store files in persistent folders on your computer, so you can have separate containers for WordPress, Ghost, Shiny, and Caddy

andreashandel commented 7 months ago

Thanks! Tried it, just copied your files over on a DO droplet, installed docker and docker compose, but when I run the command you suggest to run, I couldn't get it to work. mariadb and php start, but caddy fails with this: Error response from daemon: driver failed programming external connectivity

Any thoughts what that might be due to? I'll continue fiddling with it, maybe I can get it to work.

andreashandel commented 7 months ago

Well, that problem was because apparently I still had a nginx server running. Once i shut it down, the docker compose command worked and Caddy started (so it seems). The website is still not showing though. I thought I changed things properly to my setup in Caddyfile but there's apparently still something I'm missing...

andrewheiss commented 7 months ago

Do you have a DNS alias pointed at your server?

andrewheiss commented 7 months ago

Actually it's a little weird to run WordPress with Docker Compose because you can use a standalone WordPress container, and then you have tell Caddy to serve it with php_fastcgi

Here's a complete working example with a couple static HTML sites + a WordPress site: https://github.com/andrewheiss/caddy-docker-compose-wp

andreashandel commented 7 months ago

Thanks! I wasn't actually trying to do wordpress yet, for now was just trying a static page following your example. Just tried it again on a local virtual Ubuntu box. I basically just installed Docker, Docker compose, pulled this repo, extracted, replaced your email with mine, deleted most sites, just kept thing1 and thing2, adjusted Caddyfile, and tried to fire it up. Getting: SSL_ERROR_INTERNAL_ERROR_ALERT I'm guessing I missed some Let's Encrypt setup step. I'll keep digging, Might need to revisit your 1st repo to see if there's some step I'm missing. I'm sure it's something simple and silly. And I think my DNS is set up right, but not even trying right now, just using IP address on DO or localhost on my virtual box.

andrewheiss commented 7 months ago

You shouldn't need to configure anything with Let's Encrypt - Caddy just handles that by itself

The error is likely because HTTPS certificates on local servers don't normally work. Let's Encrypt can't issue certificates for local servers, so Caddy uses self-signed certificates, which browsers don't normally like, so you have to bypass it (like here in Chrome):

image
andreashandel commented 7 months ago

Thanks! I realized one issue was that I didn't understand how the URL looks like, I just read part of your original blog and saw that I should go to thing2.localhost instead of localhost/thing2 and that now worked on the virtual box after the usual warning message. Still not working on my DO droplet. Something is messed up, I stopped caddy and tried to restart nginx to see if that still works and now that isn't working anymore either... I will just have to start digging more until I get it to work. Thanks for your help!

andrewheiss commented 7 months ago

On DO you need to change it so that it's the full URL. The caddyfile defines the full URL, whether that's a live server or a local server

So if you're working locally, you'd use something like

thing1.localhost {
   ...
}

On DO, you'd want this:

www.whatever.com {
   ...
}

or

thing1.whatever.com {
   ...
}

Like, on my actual server, this is what my Caddyfile looks like:

# bare version goes to www version
andrewheiss.com {
    redir https://www.{host}{uri}
}

www.andrewheiss.com {
    root * /var/www/html/andrewheiss.com/public
    encode gzip
    file_server

    handle_errors {
        rewrite * /{err.status_code}.html
        file_server
    }
}

But when I'm running that on my computer and not on DO, I change it to this:

# bare version goes to www version
andrewheiss.com.localhost {
    redir https://www.{host}{uri}
}

www.andrewheiss.com.localhost {
    root * /var/www/html/andrewheiss.com/public
    encode gzip
    file_server

    handle_errors {
        rewrite * /{err.status_code}.html
        file_server
    }
}

That way I can go to https://www.andrewheiss.com.localhost/ and see the site on the local Docker server there

andreashandel commented 7 months ago

It's working now! Though I probably should still learn a bit more to better understand what I'm actually doing :) But this was very helpful, thanks! Next, I'll try to add some more docker containers to my compose file.

andrewheiss commented 7 months ago

This video tutorial is pretty helpful for understanding Docker Compose in general: https://www.youtube.com/watch?v=DM65_JyGxCo

And I'd recommend using VS Code for it, since you can right click on the docker-compose.yml file (or use the command+shift+p command palette) and start/stop the compose file, both locally and on a remote server through SSH: https://code.visualstudio.com/docs/containers/docker-compose