Skip to main content
View on YouTube to comment and like!

Syntax Video

Self Host 101 - Run Multiple Apps with Caddy | DNS, Static Sites, Reverse Proxies and Let's Encrypt

Chapter 1 0:00

Intro

have you ever wondered what happens when you type a URL into a web browser and press enter in this video we will answer that question by setting up and running multiple services on our own server we'll configure DNS records set up and install an HTTP server configure subdomains reverse proxies SSL certificates and we'll also talk about how to manage long running processes on the server now this is the third video in a series on self-hosting in the first video we lock down in secur VPS and if you haven't set up a VPS and you'd like to definitely check out that video in the second video I answered some of the questions that came up in the comments of the first video and also talked about where to find a VPS if you'd like to get one now even if you're not trying to run your own sites and services the server we set up today can be used to host all kinds of self- hostable services including things running in Docker containers now if you're enjoying this series and want to see more definitely let us know in the comments what you'd like to hear about next all right let's get into it I'm CJ welcome to

Chapter 2 0:56

Domain Purchase and DNS Setup

centax the first step in answering the question that we posed at the beginning of this video deals with DNS or the domain name system now this is a hierarchical and distributed naming system but essentially what it allows us to do is to take domains and turn them into IP addresses and for this tutorial I purchased the domain not local host.net so this is mine I bought it and we can actually go to it so if I go go to it in my browser here it loads but one of the first thing that needs to happen is my computer needs to turn turn this domain into an IP address now I purchased this domain not local host.net from Nam sheep.com there are a million in one places you can buy domains you don't have to use name chep you can use literally anything you want uh do your research find something that you like uh but once you've purchased a domain you will have a space on the dashboard of wherever you bought it that will let you specify DNS records and so in the next part of this tutorial we're going to be specifying these records so you need to make sure that you find that place in your dashboard now I've gone a step

further and instead of using name Che to manage my DNS directly I'm actually using a service called cloudflare so cloudflare has a bunch of other things that they provide but one of them is DNS management and they do have a free tier so I'm just using the free tier here um and this is where I'm going to be specifying all of my records but to set that up I actually set cloudflare as my name servers for this domain we're going to talk about what all this means in the next part of this video but if you're going to be using cloudflare follow their onboarding and they are going to have you set the name servers to point to Cloud flare for wherever you bought your domain so for me personally I'm on name cheep you can see I put my uh Cloud flare name servers right here and this actually sets it up so that now whenever I want to specify a DNS record I'm actually going to do it here in the cloud flare

Chapter 3 2:43

Creating DNS Records

dashboard now in order to point our domain name to the IP address of our server we need to set up what's called an a record So within our DNS dashboard here we're going to click add record the type is going to be a so an a record is specifically pointing to an IP address there are other types but for now we're going to use a and then for the name I'm going to use the at sign so this specifies the root for the apex of the domain so that's just notloc host.net with nothing in front of it right it's not www.not loal host.net or app. notloc host.net it's just not loal host.net and so this is the Apex then the value you want to specify here is the IP address of your server so I'm going to throw in my IP address here now if you're using cloudflare they have this SSL proxy we're going to disable this because we're going to be setting up SSL on our own server the last part here is super important and it's the TTL or time to live now when you're first setting up all of this stuff you want to set this super low I'm going to set this to one minute and the reason we do that is

because this is how long the values that we specify here will live in the cache of DNS and so we're going to talk about in a second how all of that resolves and how that works but if you set this to a high value like 30 minutes or an hour that means if you make a mistake if there is a cast record it's not going to check for the latest value until that that time has expired and so when we're first setting all of this stuff up we want to go super low like 60 seconds Max so that way if we make a mistake and we need to update a record or add a new one the value will no longer be in the cach and it will retrieve the latest value so we've got our a record it has a value of at and the IP address of our server so we're going to click save here now that this record exists we should be able to query for for it and so to do that we're going to use the command

Chapter 4 4:32

Query DNS Records with dig

line now I will mention it is important that before you try to put this domain name into your browser or before you try to make these requests from the command L like we're about to do you want to make sure that this TTL is set super low already because if you've made a mistake and this is too high you're going to have to wait for it to expire now from the terminal we can use a command called dig and so this command allows us to dig into the DNS records for a specific domain and so we're going to run dig on the domain that we uh specified here and so by default dig will show us all of the a records so if I do this it comes back and it tells us that there is an a record for this domain that points to this IP address and it has a time to live of 60 seconds so there we go so if this responds with this info then we know that we've set the record and we're getting back the right value now let's talk about how this actually happens and really talk about the hierarchy of the domain name system now when you have a domain you actually have some root domain in this case it's net so if you have a com that's your root domain if

you have aapp that's your root domain but for my domain the root is net and in order to resolve this when my computer makes a request it's going to first reach out to the root domain name servers which are the ones that are in charge of net from there they're going to respond with either further root servers or they're going to respond with oh hey I have an entry for not local host.net but it points to the name servers at cloudflare and then from there we need to then query the name servers at cloudflare that will eventually give us back this value here and if you want to see this in action you can actually do a DNS Trace so if we do dig with plus trace and then specify the record we want to find so we'll do an A reccord or not local host.net we can see this this process happen and so we can see that it starts at the root so it's grabbing the root servers for uh the net root server and then from there there are further root servers or there's gtld servers and then from there it's making a query to the uh name

server entries that it found at at one of these gtld servers and then from there it makes the query here which ultimately responds with that a record there so anytime you're typing in a a domain that isn't cashed meaning somebody hasn't gone there before it's going to have to go through this process every time and then when the cash expires it's going to have to go through this whole process again and so I'm actually going to wait 60 seconds to make sure that the the cash is expired and then we'll run it and we'll see that it's not instant cuz if I run it right now it's instant right it's instant because it's living in the cache of all of these servers but I'm going to wait 60 seconds and then when I run it again you'll see that it's not instant because it actually uh expired right because we set that TTL to 60 seconds so the next time someone looks up that domain it's going to have to query that hierarchy again to finally resolve to the IP address so let's wait 60 seconds then I'll run this command again okay it's been 60 seconds I'm going to run this command again but the other aspect of all of this caching and all these layers and hierarchy is that my machine my computer here also caches these entries so my machine whenever I make a query

for not local host.net if it if it if the time to live hasn't expired yet it's not even going to query these these external servers but okay it's been 60 seconds let's run this command and there we go so because that time to live had has expired it had to go through the whole process of of of figuring out the final IP address

Chapter 5 7:50

www subdomain record, CNAME records and TEXT records

again so from here I actually want to add one more a record now it's very common that when someone tries to visit your website they're going to type in www.local host.net now in order for that to work you need to also have an a record for the subdomain www uh so let's do just that so I'm going to add a record the type is a and the name here is dubdub duub and so that means when someone goes to dubdub dub.

notloc host.net we want it to resolve to this IP address so I'm going to turn off proxying we're going to set that TTL super low and then save the record now for this tutorial these are the only records that we're going to need uh but there are other types of uh DNS records so one of them I'll talk about is cname so if you're trying to point a domain to some service hosted somewhere you might they might have you specify a cname record so if you specify a c name a c name is a record that points to some other domain record so it doesn't point to an IP address so let's just add an example here I'm going to have banana so banana. notloc host.net but I want to actually point that to not local host.net and so this is a way of having an entry where you maybe the IP add address of the server changes and you just want this entry to always point to whatever IP address that this one is pointed to and so again we'll set this super low we'll save it and now I have this banana record here and when somebody looks it up it's going to resolve to The Domain not local host.net and then we would have to do a further

query on not local host.net to then find that it points to this IP address but but there are other types of Records there's also text records text records are very often used for like ver verifying your domain ownership and these Can Have Anything inside of them and so you can put them at the the the rout here and uh you can put anything so let's do this so anything goes here um and then I'm going to set it to a super low TTL we'll save it and then let's actually query for these records from the command line so if I query for dubdub dub. notloc host.net and I do an a record we should get back that IP address great if we query for banana. local host.net and we specifically are looking for a cname record we can see that it points to notloc host.net and then if we want to grab the text records for notloc host.net we can do this we can see that there's actually a record it says anything goes here and then there's this other record that's used for uh email servers and and I actually have email forwarding set up

through cloudflare uh so that's why that record is

Chapter 6 10:19

Email forwarding and a catch-all address

there now another thing you might want to consider is email routing now this is going to vary widely based on where you bought your domain and where you're configuring the DNS like sometimes when you buy a domain they give you like an email inbox and and different things like that um but for my domain I don't have that and I set up email routing with cloudflare other DNS providers uh support this as well but essentially what I did is I set up a catchall rule so any email sent to anything atlo host.net is going to be forwarded to my main email here and they walk you through the settings here you essentially just have to add some MX records and so now any mail that comes into some address that not local has net uh gets routed through the cloudflare servers and then they forward that email onto my main email here um and so yeah there's no email inboxes or anything that like that it's just literally forwarding all of the mail and this is nice because sometimes you might get emails for like uh TLS certificate expiration or something like that so this is good to have set up in case someone tries to send an email to your domain and you don't have any sort of

like mail server set

Chapter 7 11:23

DNS Resources

up now we just scratched the surface of DNS there's so much more that you can dive into definitely check out this Zen from Julie Evans It's called how DNS works you can actually check out a preview which dives into what we were talking about this idea of the DNS hierarchy and like delegating to various servers there's also this little comic here that talks about caching and how that works so this is a really great resource if you want to dive deeper and Julia also has this tool called mess with DNS so if you go here you will get a temporary domain that you can actually set records for um and you don't have to worry about messing anything up uh so this is super fun to to play around and learn from and then also Julia has this DNS lookup tool so if you don't want to use the command line and you want to just see what records are set for a domain you can plug it in here click it get me all the records and it'll spit out like a records you can see the like text records and stuff like

Chapter 8 12:13

SSH to your VPS with a Domain Name

that now at this point in answering the question that we posed at the beginning of the video we have an IP address and so a computer will then make a request to that IP address and so let's start working on the server itself now in the last video when we connected to our server we specified the IP address but now because we've set up that DNS record we can actually connect directly to that domain so we don't have to specify the IP address directly and as long as we've set that a record this should work now the first time you run this you're going to get this warning just like you did the first time you connected to your server but we'll allow this this is going to put that uh domain into our our known host file so that way if there's ever any sort of like man in the middle of attack this will uh let us know about that so that's going to put that in there and now we can connect to the server so that's one nicity of setting up a domain like this is not only for setting up websites but I can actually SSH using that same

Chapter 9 13:08

Responding to HTTP Requests from our Server

domain now at this point the domain of the URL that someone is trying to visit has been resolved to an IP address so the browser is now going to make a get request to that specific IP address and in order for that to work there needs to be something listening on the other side now the next part of this video we're going to set up a proper HTTP server but I kind of just want to show you what's happening under the hood before we do that and for that we're going to use this tool called netcat and so this is built into Linux and allows us to read and write to network connections so we're just going to have a little netcat command that is listening for connections on Port 80 because by default when somebody tries to visit HTTP colon slash some domain some path this is actually by default connecting on Port 80 80 is the default port for HTTP so our server needs to be listening on Port 80 now over here on my VPS I do have Port 80 open on the firewall now we talked about this in the last video you're just going to want to make sure that you have Port 8 open for for this part to work but we're going to use netcat and we're just going to listen on Port 80 Now by default you can't do that

because 80 is a privileged Port so you do have to run this with pseudo in order to listen on that Port now now at this point we have a process it's just sitting here and it's waiting for something to connect on Port 80 so back over in the web browser if I click enter here it has actually made that request to my server and if we go back to the server it literally received the request so we can see that we got a get request so this client is is saying hey I would like you to return whatever is at that the slash route here and they're negotiating this over HTTP 1.1 that's the the version of HTTP and then there's a couple of headers that are specified here it tells us like the the type of device that they're on what language they prefer all this kind of stuff and now our HTTP server can actually respond and with netcat we can actually just type this stuff in directly so I am going to say hello my client I am responding over HTTP 1.1 and the status code of this request is 200 and everything is okay and then hit enter so

that's actually responding to say hey everything's good to go here here it comes here comes the rest uh and then I need to specify some headers and so I'm going to specify the content type header to let the browser know that I'm sending it HTML so I'm going to do content type and in this case we'll just do text/html so now the browser knows that it's going to receive some text uh now I can just specify the body so I'm actually going to do this I'm going to do the body of the request I'm just going to do some nice little inline Styles and then I'll specify just an H1 inside the body here it'll say hello world and then I can close out the body and then finally I need to close the HTTP connection to say that that this is done so I'll do this and now the other side the browser should have actually received that response and it did and so we can actually see that the browser rendered out that little hello world here and if you open up the network tab in your browser you can kind of inspect this a little bit more you can see that it shows us what the status code is it shows us the version of HTTP it shows us that actual header that the server responded with now it would be very

cumbersome if we just had to sit at our VPS and respond to every request coming in and so that's what an HTTP server is for and so the next part of this video we're going to set up caddy uh which essentially we can have running on our machine 24/7 it can automatically respond to these requests and automatically route those requests to the correct application or folder depending on the request that comes in let's talk about web server software because there are a lot of options I'll link to this page that has a bunch of options and I'm going to talk about some of the more popular ones so Apache HTTP has been around for a very long time and uh it's the a in the lamp stack Linux Apache MySQL PHP there's also something called engine X I've been using engine X for years I probably have the most experience with engine X there's also something called traffic proxy it's nice because it has this built-in web dashboard you can see all of your running Services you can configure them from that dashboard as well and there's also caddy server and that's what we're going to use in this video now I'm new to caddy but so far I I'm loving it it's super easy to install super easy to

Chapter 10 16:27

Web Server Software

configure and it has this idea of secure websites by default so whenever you create a website configuration it can automatically procure a TLS certificate from either let's encrypt or zero SSL and it it does that seamlessly so it's super easy to get a site with https up and going um and these days every site is running on https so you definitely want that now uh in this tutorial I am going to talk about the concepts of the things that we configure with caddy in a general way as well because all the stuff we can do with caddy we can do with these other web servers things like serving up static websites reverse proxies uh url rewrites url redirects all that's possible with any web server software but we're specifically going to be doing it with caddy now another interesting thing about caddy is if you are coming from another web server they actually have config adapters so you can actually take your existing engine X configurations and then adapt them to run with caddy which is pretty cool um also check out episode 340 of syntax so the creator of caddy Matt Holt was

actually on and had a chat with Wes and Scott so if you want to learn about the origins of caddy definitely check that out but yeah in this tutorial we're going to be using caddy so let's get into

Chapter 11 18:12

Why do we need a Web Server?

it now the reason we want a web server like caddy running on our server is we want to be able to have multiple applications and websites running and caddy and web servers like it are essentially like the gatekeeper so cat is going to be listening on both Port 80 and 443 for HTTP and https traffic and depending on the request that comes in it will serve up the correct website so we might have a folder of static files that we want to serve at the root domain we might have a node.js API running on Port 5000 and we want to reverse proxy requests coming into api. rain.com to that noj app we might have a single page application running that is static files that need to be served up at some other subdomain but caddy is basically the GateKeeper listening on ad0 and 443 that's redirecting those incoming requests so that way we can have multiple sites running on the same

Chapter 12 19:05

How To Navigate The Caddy Docs

server now let's talk a bit about how to navigate the caddy website and also the different ways that you can run caddy so when you look at their website and you look at like getting started and also these tutorials typically they are walking you through using caddy directly from the command line so you install caddy it's a binary and then you can just run it from the command line and they also walk you through potentially like creating a caddy file in a folder and then using the caddy CLI to run that configuration with the caddy server and so the caddy command line tool has a lot of options and that's one way to run caddy but we are actually going to install caddy as a service so we typically will never be directly interfacing with this command line we're not going to be running any caddy commands we're going to do everything through config files so in the next section of this video we're going to run through the install and we're going to do the install that actually installs caddy as a service so it's always running in the background and then from there we're going to set up our configuration so that it lives in one spot and then anytime we want to change a website or add a website we just change our config in one spot and then

reload the caddy service so we're not running multiple instances of caddy and we're not really using caddy from the CLI uh the other thing is there are other ways to configure caddy they have this uh Json structure you can use to configure um we're specifically going to be using the caddy file and then also you could actually use the API so whenever you install caddy it's actually running this admin API in the back ground and you could make HTTP requests against that API to configure your website but everything we do is going to be done through caddy files and so specifically if you're following this tutorial here and you want to start to learn more about how to change the websites that we're configuring you're going to want to look in the catty file reference section so this shows you all of the various Concepts all of the various directives that you can use within a catty file and this is where you're going to find the stuff that you need if you're if you're in the tutorial it might get confusing because they say like run caddy from the command line we don't want that Caddy's running as a service we have all our configs in one location and if we want to change those configs this section of the docs here has everything we need to uh add new

websites or or change the behavior of those websites now the beginning of this tutorial is just going to focus on getting up and going with caddy and learning about the config file and and modifying it and things like that but we're going to focus on a static website to do all of those things after that we'll jump into hosting other kinds of websites like a nodejs application via a reverse proxy but initially we're going to focus on learning about the caddy config and just serving up a static

Chapter 13 21:32

Caddy Architecture / reloading configuration

website hello there CJ from the future here I have a quick correction to make so in this tutorial you're going to see me running pseudo service caddy restart and when you're first setting up the server this is fine because uh restart technically stops the caddy service and then restarts it so technically you have a a few milliseconds of downtime for your website the caddy architecture however does this reload command and what this does is it keeps the cadd service running and then dynamically reloads those configurations so you don't have any downtime for your websites so uh in the future as you're adding to your configurations and adding new websites make sure that you use the reload command I will link to the documentation that talks about managing configuration and and the idea of how they're able to do this config reload and then also in the section of the docs on keep the service running they talk about using that reload command as well so for this tutorial it's probably fine to use restart but you should know in the future especially once you have some a bunch of sites running on your server you should use the reload command

instead of the restart

Chapter 14 22:35

Install Caddy

command now let's install caddy if you go to caddys server.com you can click on documentation go to the install section and we're going to be installing for Ubuntu so we're going to click on this now they give us some commands we can run this will uh add the caddy repository to our package list we'll update our package list and then we will install CAD so I'm going to copy these commands and then run them on my VPS we do have to run this under pseudo so I'm going to type in my password great that has finished now when you install caddy this way it is going to install a service so it's always running in the background you can see that in the docs here it's telling us that it is running that system D service now to check that it is installed we can just do a caddy DV and that'll tell us which version we have and if we want to see if the caddy service is running we can do a pseudo service caddy status and we should see this uh Green Dot here and it should say that the service is running now at this point cadd actually automatically starts up just a a static file server and if

you go to your domain in the web browser it should load up this this caddy congratulation page and it actually tells us that we can put our files in here and this will actually serve up this static site and we can edit our configuration here so let's do just that we are going to edit this file and add some basic configurations so that we can get a a better setup here uh because one thing that will happen is if I attempt to visit the IP address of my server you'll see that it just shows the bare IP you'll notice it's not running over https so let's add some of those configurations so that by default we're getting https we're redirecting HTTP traffic to the https domain let's set that

Chapter 15 24:17

Set up https for the root domain

up in order to do this we need to edit the caddy file so I'll do Nano and then the name of the file now this file is actually in a privileged location we need to be root to be able to edit this and you'll see this error down here in in Nano so we need just to make sure that we run this command under Pudo so if you do pseudo bang bang that'll rerun your previous command uh using pseudo now uh we want to edit this and you can see right now um it just has Port 80 and it's saying uh we are going to have a static file server that is just running on Port 80 and what we want to do is we want to run this over https and we want it connected to our domain so the first thing we'll do is we'll just change this to be our domain name so I'm going to add not local host.net and that's literally all we have to do so I I add this here restart the service Caddy will provision a TLS certificate and then we should be able to go to not local host.net over https so let's give that a try so I'm going to save this file and

then I'm going to restart the caddy service and from there if I attempt to visit this you'll see that it redirects to the https site not local host.net so just like that I already have uh a a site running over https which is kind of wild if you're used to configuring things in other

Chapter 16 25:39

Redirect IP to the root domain

ways and one thing you'll notice is if you try to visit your website by putting in the IP address it's going to try to load that over https and we don't want that one thing we want to do is redirect that to the root domain so that way if for whatever reason someone's ever trying to put in this IP it just redirects to our so let's set up that rule so here in the cadd docs you can see that a website can have a reder directive so what we'll do is we'll actually edit the caddy file to add a new website and add this this reader directive on it now to set this up we can just actually modify the existing caddy file and if you go in here you can list as many websites as you want in this file now in a second I'll show you a better way to do this so it makes it easier to manage but for now let's just add another website so I'm going to add a website that corresponds to uh the domain here and I'm actually going to throw Port 80 on there so when a request comes in for this website over HTTP we're going to redirect to our not local host.net over https so I'm going to use that reader directive and I'm going to say https col slash and then specify the domain that I want to redirect to so

local host.net and then if for whatever reason they had like some path on the URL I can just append it here by doing URI and so if they went to like some path or had like a query string that's just going to append it over https now so we'll save this we'll will restart the service and now when we attempt it to visit the site over HTTP it should redirect us so let's give that a try it might still give us this error and if it still gives us this error we just need to make sure that it doesn't use the cach so if you open up the dev tools and then make sure you have disabled cach checked now if you try visiting that site again it'll won't load it from the cache and it'll actually get that redirect back to the root

Chapter 17 27:21

Multiple Caddyfile Setup

domain now we're going to set things up so that websites are easier to manage because I could put everything in that same caddy file but uh that's going to get hard to manage the more websites I have on this server so what we're going to do is create two directories and I'm going to need to be root to do this so we're actually just going to drop into a root shell if you do pseudo sou that is now going to drop you into a bash shell and all the commands you're running now are uh as root and we want to create two directories here we're going to create a directory called sites available this will have all of the caddy files for sites that we potentially want to enable and then we're also going to have a directory called sites enabled now with these two directories we're basically going to put all of our caddy files into sites available and then if we want to enable a website we're going to create a Sim link into the sites enabled folder so let's just for now copy that caddy file into sites available and then we're going to edit

that caddy file so if I just do a nano of sites available in that caddy file I'm going to remove everything except for the redirect here because I just this file is only going to be specifying redirect so I'm going to save that and actually I'm going to I'm going to rename it too so if we go into the sites available folder I'm going to uh move that fat cadd file into a file called HTTP redirects now you can call these files whatever you want but every file that exists in this folder it will potentially be loaded in as a website so I renamed it now the sites available folder has an HTTP redirects file if we look inside of it it has that one redirect inside of it so now I actually want to modify the main caddy file and remove that redirect so I'll use Nano again we'll go down here and actually just remove this section now in order to actually load those configurations that are in separate files here we can use the import directive so if I say import I can specify a directory name in this

case we're going to do sites enabled and then I can use a glob pattern so I can just do slst star so any file in the sites enabled folder should be imported in here and will actually load that website configuration so I'll close this save it out and now we're ready to create a link and so just I know I ran a bunch of of commands here but let's just talk walk through what your system should look like so if you the the root caddy file here should only have the definition of your main website and then an import of sites enabled and then inside of sites available you you should have a file called hgdb redirects and that should have just the one directive that redirects the IP address to the main site and then right now we don't have anything in sites enabled that directory is completely empty so if you're if you're following along so far now we can actually create a link to this file that lives in sites available and we're going to link that into the folder here inside of sites enabled so

we for that we can use the Ln command and this allows us to create symbolic links so I'm going to create a a link we do- s and we specify the file that we want to link to and I'm going to put the full path in so if I do Etsy catty sites available SL HTTP redirects I want to link that file to a file in this sites enable folder with the exact same name so we'll specify the full path Etsy caddy sites enabled and then we'll call it HTTP redirects now if you did this right if you do an ls- L you should see this file but you're going to see this little arrow and this means we have a symbolic link so this file is actually pointing to this file over here and so if I edit the file in either place I'm actually editing the real file that lives over here and what's nice about this is if I ever want to disable this website I just have to delete this file but I didn't lose my my settings that still exists over there in the sites available folder all right with all of these changes we can now restart the caddy service and it

should uh work in the same way it should it should have the redirect and everything else so let's just do a service caddy restart and now let's try visiting the site via IP address and it should still redirect us so if I try to go to the IP it still redirects so the main caddy file is actually loading in that other

Chapter 18 31:41

Redirect www subdomain to root domain

file now another redirect that I want to add is I want to redirect www.my domain to the root domain because right now uh if you go to not local host.net that redirects to H GPS that's perfect but if I go to www.local host.net this is actually a subdomain this is a separate domain from the root domain and you'll see that we're getting this very similar error that we got earlier uh with the IP address and that's because it's trying to load dubdub dub but the SSL certificate has not specified that dubdub is a valid name in the SSL certificate and what we really what we want to do is any request that comes in here we want to redirect it to the root domain now you can go in either direction you could set it up so dubdub dub. your website uh is the main domain and then you could redirect without dubdub duub to dub dubdub but we're going to we're going to do the opposite I like having the root domain here there's there's some U some uh opinions around which one you should prefer and which one you should do I like having

the domain without dubdub duub but that's just my preference so what we're going to do is if someone goes to dubdub duub we're going to redirect it to not loal host.net and for this to work we do need an a record here for dubdub duub right because someone goes to dubdub dub. your domain we want that to resolve to this IP address but then our server needs to redirect them to the https version without the ddb so to do this we'll edit that same HTTP RX file so here inside of sites available we're going to modify this file and you'll notice I'm still in a root shell so I don't have to use pseudo here and what we're going to do is just add another domain to the list here so you can actually have as many as you want and they can be uh space separated so I'll just do dubdub dub. notloc host.net and this this should work as I want so if you don't specify the port this now will work for both HTTP and https so if someone goes to https dd. loal host.net that's going to redirect it to the root domain and

similarly if they go to htdp that'll also redirect it over https to the root domain so I'll save this let's restart caddy and we should be good to go so let's visit dubdub duub sweet so the redirect happened and you'll notice if you look in the URL bar here there is no dubdub duub so when we went to dub dubdub it redirects to the route here now I realize I'm saying dub dubdub a lot if you've already commented that the fact that I'm saying dub dubdub then um

Chapter 19 34:15

Set up /var/www folder

great so at this point let's replace the default website that's loaded here by caddy now on the page you can see some details as to where they tell you you should put your files and and how you can update things so let's do just that so over here I'm in the Etsy caddy folder and if we take a look at the caddy file we can see our main host block here and it has the file server directive so this tells it that it should serve up static files and then right here it tells it where it should serve those static files from so right now our site is being served from user share caddy now if we go to user share caddy we should see an index. HTML and this is the actual file that's being served up at the default website um but we don't want that we're going to create our website in a common location so that way that's where we'll we'll put our files instead of here in the user share caddy folder so to do this I am going to create a directory at vdub dubdub so I'm going to CD into VAR and I need to make a directory called dubdub duub but I

will need to be root to do this because this is a a privileged folder so I'm going to do pseudo makeer dubdub duub and now if we go into dub dubdub we have this directory and this is where we want to put our websites so typically what I like to do is I have a separate folder for every static website I am serving and this folder is just for static websites for other types of websites we'll talk about that later and we'll put those in in a different spot but for static websites they're going to live in this folder and so let's say I want to create that static website that's hosted at the root I'm going to create a directory called uh the name of the do domain so in this case not local host.net and right now I will need to be root in order to make that directory uh because uh otherwise I wouldn't have privileges to create it but I want to be able to edit the files inside of here as my current user as CJ so I'm actually going to change the owner of this folder because I will be using the CJ user to manage this folder now the way you set

the permissions here is up to you I would say a best practice is to have varva dubdub duub owned by root and and the root group and then for each website you you'll set the settings accordingly one of the main things you don't want to do is you don't want to have this folder writable by the web server itself so the web server caddy is running under the caddy user and we do not want the cadd user to be able to modify the files inside of here because if the server ever gets compromised we don't want them to be able to modify these files so I'm going to change the owner to be my user CJ and then everyone else will have read access to these files but only I will have right access so to do this I'm going to do a studo Chone so change ownership and I'm going to change the ownership to CJ and the group to CJ so instead of root root it'll now become CJ CJ and I'm going to do this on the uh not to loal host.net folder now if we take a look at it we can see that CJ has rewrite and execute privileges the group

CJ has read and execute Privileges and everyone else on the machine has read and execute privileges so this setting right here is especially important because we want the caddy user to be able to read the files that are in this directory so now if we go in here uh because CJ is the owner of this current directory I should be able to create files in here without using pseudo so if I do a nano of index.html I don't see an error here in Nano that says I don't have permission so let's just try out a little hello world real quick so I'll throw in an H1 control X and then why to save it so I was able to create that file here in the uh not loal host.net directory and because I created it I'm the owner of it but uh right now my default umask makes it so that when I create files other users on the system do have read access so I I especially at least need to have read access for other users so that the cadd user can read this directory and read this file um so now that I have

this folder set up let's actually update the caddy config to point to this folder so to do that we'll go back up to Etsy caddy and then we want to modify that caddy file as the super user and right here where it says user share caddy we're going to update that to be VAR dubdub duub and then the folder that we created now I'll save this and then we will reload the cad server so if I do pseudo service hady reload that'll look at those configurations reload it without stopping and starting the server and now it should be reading from that directory so if we go back and we refresh you can see that it says hello world so we know that it's working it's now reading the files from that directory instead of the default

Chapter 20 39:05

Transfer files to the server with scp

directory now let's actually get our website files onto the server because we don't want to just be using Nano on the server and like copy paste all the files we actually want to transfer those files over and there are a couple of ways to do it the first way I'm going to show you is with a tool called SCP or secure copy protocol so here on my local machine um I'm going to use the SCP command to transfer the files and this does it over SSH so if you do SCP and then specify each of the files that you want to transfer and then specify an SSH connection so I'll do CJ notloc host.net this will transfer them to that host now we can also specify where to put them on the the host here so you use a colon and then specify where you want to put them and in this case we're going to put them in that folder we created so far dubdub duub not local host.net and so if we do this it is now going to transfer each of those files over SSH to to the server now it's important that we had the permissions set up like we did in in the previous section of this video because we were transferring these files under

the user CJ so the CJ user needs right access to that folder in order to create these files over SSH so you do need to make sure that whoever you're connecting as over SSH has right access to this folder if you want to load those files in but at this point we should be good so uh we don't actually have to restart or reload the server cuz uh we just changed the files in the directory here so if we go back and refresh the page we can see that the the new website is getting loaded with the latest files that I added

Chapter 21 40:35

Sync a folder with rsync

there now another tool we can use to transfer files is called rsync and so rsync is built into a Mac it's also built into most Linux distributions um and to demo it I'm actually just going to remove uh the files here because uh our sync is smart enough to only transfer files that have changed so we're going to start with nothing so we just have a compl completely empty directory now over here on my machine I'm going to do our sync then we'll specify some options so we have- a which means copy all of the file attributes including like Sim links and stuff like that Z will use compression capital P will show the progress of all the file transfers um we then want to specify where do we transfer the files from so I'm going to drop into a subshell here and say transfer the files from the current working directory so that'll give us the full path of the current working directory and then I'll do a slash now the slash is important because I'm saying copy the files in this directory not not don't create the directory on the other side but copy the files from in this directory to the destination and the destination is an SSH connection so we'll do CJ not local host.net and then a colon and we specify

where we want to put the files on that remote server and in this case we'll do VAR dubdub duub not local host.net now for the destination we do not put up trailing slash and what this means is all the files in this folder will be copied into the folder that exists on the server here now this is important because of like file permissions right so earlier we created this folder and we made sure that this CJ user can write to it uh that's why we're copying into the folder not trying to create it because we wouldn't have the permissions to do that uh and at this point we'll go go and look at that so we can see the file transfer progress of each thing if we go back over to the server and list it out we can see that the files got created but one of the nice things about rsync is it will only transfer files that have changed so if I run this command again you'll notice that it considered all the files but it didn't actually transfer anything over because none of the files are out of sync um and so this is really nice and it's a nice and quick and easy command that'll just sync up a

Chapter 22 42:31

Clone a public git repo

director now the next way I'll show you to sync files to the server is by using git now this is my preferred method of syncing things because I use git for all of my my codes and um if things change I can just push up to the repo and then pull down on the server now the first way I'll show you requires that the repository is public I'll show you how to do this for private repos next but if you have a public repo that you want to host on a server this is how you can get it going now the first thing you'll need is the Clone URL and you want to make sure that you grab the https URL because we're not going to be using an SSH key the next section of the video I'll show you how to set this up but for now we're just going to grab this and do a readon clone now I could attempt to clone but if that folder we're trying to clone into is not empty then git isn't going to be able to to clone into that directory so uh what I'm going to do is I'm going to delete this directory and just assume that we're kind of starting from scratch that we want to clone this directory from scratch so what we'll do is we will remove not loal host.net but I'm going to have to be root to do this because it's in vdub duub which is owned

by the root user so we'll use root to delete that folder now uh it's an empty directory we will use root to create an empty folder so we're going to do uh make directory of not local host.net so we're going to create the empty folder that we want to clone into now in order to clone into this uh as the CJ user the CJ user needs to have read and write privileges to this folder so to do that we will change the ownership of the folder so we'll use pseudo change the ownership of this empty directory to be our user and then the group so the owner will be CJ the group will be CJ and then specify the folder so now I have an empty directory that is owned by CJ and so if I go in here there's nothing in here and you actually CL can clone into an empty directory so now I have a completely empty directory it's owned by CJ which means I can clone into it as the CJ user so if I do a get clone here and then specify that https URL that we

grabbed it'll write it into that folder now if we go in there it exists so we should be good to go if we go back and try and visit the website it still loads just

Chapter 23 44:44

Hide files from Caddy file_server

fine now one of the issues of doing it this way is that dogit folder exists inside of that folder and because we haven't locked things down if someone were to request SL uh dogit SL say head for instance they can actually access all of the files inside of that git repo um and we don't want that now this is a public git repo so there actually is an issue with doing this but you especially don't want to let people access this if you are using for instance a a private repo so we can update our caddy config to actually ignore uh anything within that git folder here so over on the server if we're in the Etsy caddy folder and we uh edit the caddy file there is a section under file server called hide so we can create our block for file server and we can say hide and we're going to hide doget it should be as simple as that so I'll save this I will then uh reload the cadd server so pseudo

service caddy reload and now if I attempt to get that file uh nothing is returned because caddy is actually blocking access to that dogit folder so this is is important to set up especially if you have stuff in that folder that you don't want served up by caddy if you want to learn more check out the caddy docs specifically for the file server directive and you can see more info about the hide option

Chapter 24 46:08

handle_errors in the Caddyfile

here now for static sites there might be some more configuration we want to do especially for handling errors so right now since we hid that dogit folder whenever I try to request something inside of it the server is responding with the 404 but it's not actually displaying anything on the page and you can actually change the settings for that so within the caddy config there is a section you can have called handled errors and there's a few different things you can do in here so you can look specifically for like a 404 error like this so you can say um if there was an error code that matches within this range then just respond with with this text here um so there's a lot of different things you can do uh one of the easiest things you can do uh if you don't want like a super custom error page is to just add a a respond block here so I'm actually going to copy this handle errors block and then back over on the server we'll edit the cadd file again and right below our file server block We'll add the handle errors block and so now anytime an error occurs it's

actually just going to respond with the status code in the status text now this isn't going to be pretty I'll show you right now so if we uh reload the server and refresh the page over here and now it does say 404 not found and like I said it's not pretty U but if you do want to make pretty errors you can actually uh look in the docs here and serve up like a custom file you can you can Define like your 404 error file or your custom error file and automatically render that up instead of just the the status code and the message but for basic websites this should be fine and I definitely want to have this there just so I'm not getting back a blank page when an error

Chapter 25 47:46

Clone private git repos with an SSH deploy key

happens next I'll show you how to set things up if you have code inside of a private repo so in order to read from a private repo you need need to at least have an SSH key that has re access to the repo and specifically for GitHub you can do that with deploy keys so if you go into the settings tab here and then scroll down to deploy Keys you can add Keys here and give them readon access or read and write access and so what we can do is we can actually generate an SSH key on the VPS and then add it as a key here so the VPS will be able to clone from this repo and pull the latest changes from this repo now this is my preferred way of doing this essentially you set up a SS key for every repo that you want to pull from um you could however generate an SSH key and then just directly add it to your GitHub account I would avoid that though because uh if your VPS server gets compromised then they have complete readand write access to every single repo that you have on GitHub so I would avoid doing that I I would recommend that you use deploy keys so to set this

up you're going to need to generate an SSH key on the server I'll link to this documentation you specifically want to click the Linux tab because we'll be generating this on the VP s itself and then follow the steps for generating an SSH key now like I just mentioned you want to skip the step on adding the SSH key to your GitHub account because if you add that key to your GitHub account your VPS now has read and write access to every single GitHub repo that you have read and write access to and we don't want that we're going to lock it down to a specific repo so whenever you generate this SSH key under this step right here you should give it a unique name and in this case I'm going to generate a key and give it the name of the repo that I'm going to be cloning from because every repo that I want to give this server access to I'm going to generate a new key that has a unique name so I'm going to go through these steps I'll probably skip in the in the video um but I'll show you the relevant parts of where we're we're specifying a unique name so over here on the VPS I'm generating the key I specified an email that I do have access to and then for

the location I am giving it not the default name but I'm adding the name of the website this that this is going to be associated with so I recommend you use like the repo name just so you know specifically what that key is for because like I said for every site that you want to clone in this way we're going to generate a unique key so that way that key is the readon access to that specific repo so I'll generate this and now if you're following that doc I'll need to add it to the SSH agent and then the one part that's going to change is you're going to need to make sure that you actually specify the unique name of the file that you generated to add that to the SSH agent awesome at this point if we go into the SSH folder here on my VPS under the the user account that I created you can see that it has that private and public key so what we're going to want to do is we're going to want to uh add this public key as a deploy key so if I just cat this public key this is the key that we want we'll copy it and this is what we're going to add to GitHub now like I said

technically you could add this to your entire GitHub account but then this this user on This Server would have read right access to every single repo on GitHub that you have read right access to we don't want that so over here on the repo I'm going to go to settings I'm going to go down to deploy keys I'm going to add a key and we'll give it a name we'll call this uh not local host.net let's call it prod and then put that key in there now you can give it right access but uh let's remember the the principle of lease privilege this this uh VPS isn't going to need to write to this repo it literally is just going to need to uh pull the latest changes so I'm not going to give it right access I'm going to add that key and now at this point the server with the private key associated with this public key will'll actually be able to read from this repo so now if we go back and we grab the SSH clone URL This Server should be able to clone from the SSH URL instead of the https URL so now over here on the VPS I've gone through the process of just creating an empty

directory that my current user has re right access to and so now I'm going to clone into that empty directory so if I do a get clone specify the SSH URL uh now it's going to attempt a clone in there and because this is the first time that uh the VPS is trying to connect to GitHub I do have to say yes here to store the fingerprint but I should only have to do this the first time that this happens and there we go it cloned it into the folder and if we go in there all of the files are there and if we try to visit the website it loads just

Chapter 26 52:16

Install node.js with nvm

fine so so far we've only set up static websites let's set up a dynamic website and this site actually runs on nodejs so this is a simple little API that returns all the holidays from the Google Calendar API and I want to get this up and running on the machine but this API is built with node.js and it requires node.js to run so I need to install node on the machine now there's a lot of ways to do this you could install it from appt but I am going to use node version manager because it's nice and easy to pick the version that I want to install now uh on a VPS technically you don't have to install anything directly you could just be using Docker um we're not going to use Docker in this video but I have done a video on Docker if you'd like to learn about it and everything I show about Docker can run on a VPS as well but for this tutorial we're just going to install node.js directly on the machine so here on the NVM repo they have an install script so I'm going to grab this and then I'm going to run it here on my VPS now at this point that installed NVM or node version manager and I need to Source my bash RC so that way I can get

access to the NVM command so I'm going to do a source of that and now when I type NVM we get access to it so what I want to do is I want to install the latest LTS or the latest long-term support version of node.js so I'm going to do NVM install D dlts and just like that I have node version 20 so if I do a node - V I have 20 and if I do mpm DV I have version 10 of mpm now the way I install node.js it's actually just running here under the CJ user account um and I like that because I don't want to run node with super user privileges that way if the node server is compromised they still they only get access to my CJ account they don't get access to root um but all the node.js stuff just lives right here in this NVM folder and so every version you install just lives here and then we we're going to be running all the processes under this CJ user

Chapter 27 54:03

Set up Holidays API Example

account now that we have node installed I can set up this API on the VPS now earlier we were putting all of our static websites in vdub dubdub you can technically put these nodejs sites in there as well personally I like to just put them in my home folder because they they are running as the the CJ user so I like to make a directory called posts or sites and then inside of that directory all of the dynamic websites I have will live in this folder and uh they'll be running from there so I'm actually going to clone the repo into this folder now to do this I am going to set up another deploy key so we talked about deploy Keys earlier and I want to do this so that I can show you that we can actually have multiple keys so right now if I go into my SSH folder we have that uh not local host.net key that we set up earlier but I'm going to set up another key called holidays that will be specific to to this get repo there we go so I set up another key you can see it's living here in the same folder but now I can take this public key and add it as a deploy key so I'm going to grab the

public key we'll copy this and then over on the repo settings We'll add a deploy key so go into settings go down to deploy keys and then we're going to add a readon key here so we'll call this the uh not Local Host server go to the key in and just make it read only and then add it now it's important that we created a separate key because GitHub will not let you use the same public key as a deploy key for two separate repos so that's why for every repo we have read access to we're going to generate a new key so we've added it now I can clone it so we'll go over here and we'll grab that SSH URL and now because the CJ user running over here uh has that key it should be able to clone it so I'm going to go into that host folder and we're going to do the git clone just right here awesome so we got it and now we can set it up so I need to install dependen and this API requires an EnV so I'm going to go ahead and set that up so it has the EnV sample I'm going to copy that into EnV and then I'm going to add

my Google API key here I'm also going to set node EnV to production because I mean technically we're running in production we're running on a server here so so I'm going to grab my API key and then set it here great so I set that up I have dependencies installed I should just be able to do an mpm start hey the server starts up but remember we have a firewall So This Server is running on Port 5000 and right now my firewall only only has Port 80 and Port 443 open so there's no way for the outside world to make a request to the server running on Port 5000 so this is what a reverse proxy is

Chapter 28 56:37

Create a reverse_proxy with Caddy

for so to set this up we're first going to create some DNS records for the holidays API the first one will just be holidays. looc host.net so we're going to add a record it'll be an A type the name will be holidays the IP address will be our server we'll disable the prox and set it to one minute TTL the other DNS record we'll add is the dubdub duub subdomain of holidays.

notloc host.net so this is going to be www. holidays. notloc host.net and we're going to point it to the same server and the reason we do this is sometimes people put in dubdub duub when they don't need to um so we're setting up this record so that we can redirect it back to without the dubdub duub if someone were to type in dubdub duub so at this point we have those two records dubdub dub. holidays and Holiday and they both point to our server so let's set up the cadd configs that will look for these specific host names so over on the server I'm in Etsy caddy we're going to go into sites available and I'm going to go ahead and drop down into a root shell so I don't have to pseudo all these commands and I'm going to edit a file we're going to call this holidays API now inside of here I'll add my first server block which will just be holidays. not local host.net and uh to load the node API it's super simple just say reverse unor proxy and then we specify the port and specifically the holidays API is going to be running on Port 5000 that's it that's literally all we need uh the

other thing is we'll set up the redirect so I'll add dubdub dub. holidays. notloc host.net and I want to redirect that to uh https holidays. not localhost and then we'll pass in the URI in case that was specified uh this should be it so let's close this let's save it and now we need to link this caddy file into the sites enabled folder so we'll go up a directory let's go into sites enabled and we'll link it so link symbolic link we'll specify the full path so Etsy caddy sites available holidays API and then we're going to link that to Etsy caddy sites enabled um holidays API like this so now the holidays API caddy file exists within this s's enabled directory and it points to the one in s sit's available from here we can reload the server so if

we do uh service caddy reload should be good to go and let's try it out so in the browser if I visit holidays. notloc host.net it loads but it's a completely blank page if we open up the network tab we should see uh status yeah so 502 bad gateway because our no JS app is not running on Port 5000 so that reverse proxy right now is failing uh let's also try doing the dubdub duub and make sure that redirect works so if I do dubdub dub.

holidays. loal host.net it redirects to holidays. loal host.net cool now uh so far so good but we need to actually start up our nodejs app but before we do that let's actually show the error here because this is not great if we're just seeing a blank page so let's add the errors block all we're going to do is is this right here so if there are any errors we actually want to see what that status code and what that status text is so back over in our caddy file we're going to edit that holidays API and within this one we're going to throw in this handle errors block and so now if for instance a 502 bad gateway error occurs we should actually see that text instead of just a blank page so I'll save this then we'll reload the server and now when I visit this we actually see the text there so um you can get much much more custom with this you can serve up custom error files but at a minimum this is nice so that we're not just seeing a blank page now to get this work let's just start up our node.js app so I'm going to exit this root shell go into my home directory go into holidays

and then just do an mpm start and so now the service is running on Port 5000 and so if we make the request here it should reverse proxy that request to the node.js app running on Port 5000 and actually serve up the data

Chapter 29 1:00:45

Manage long running processes with pm2

awesome so now that we have the API running and it's reverse proxying and it's it's working the way we want it we want to keep it running right because right now I have this shell open and if I ever kill the app and refresh it the app is down right um so I need some way to run this now technically you could just use Linux command line Fu and just like run this as a background task what happens if it crashes right what happens if a bad request comes into the server and cause it to completely crash you want some sort of process manager so that if the servers crashes it will automatically restart and always stay up now there's a built-in way in Linux to do this and it's called systemd I'll link to this article that talks about how to set this up for nodejs but we've actually been interacting with systemd for caddy so caddy is installed as a systemd service and once you have something install as a service you can run service service name restart and it'll uh automatically restart it or service service name start and it'll keep it running in the background um so this article here talks about how to do this for node but you can do it for any kind of application that you want want to run in the background and this is

just built into Linux personally I like to use a tool called pm2 which is a process manager and uh this will work with things that aren't node.js as well but it works especially well with node.js applications and they even have this idea of an ecosystem config file that you can commit to your source code and it describes how the app should be run and you can just say pm2 start and it'll run it I also like it because it has this nice little uh command line dashboard which will show you all of your running Services you can start the processes you can stop them you can look at their logs it's pretty cool so I'm going to install this globally and we'll use this to run the app so I'm going to install this under the CJ user cuz that's where I installed node so if I do an mpm install - gpm2 that should install it globally and from there I can start the process so if I go into my hosts and holidays directory and then do a pm2 start I can specify the file that it should run in this case it should be in source index.js so this starts it up you can see that it's running if you do a pm2 list this will show you all of your

services if you do pm2 logs and then the ID or the name of the service it'll show you the request logs or just the logs for the the process so you can see there that it's listening on Port 5000 um and now the reverse proxy should work so if I if I refresh over here it actually loads the page because it is able to reverse proxy that request now there's a couple of other options you might want to do here so if we do a pm2 list we can see this is there I'm actually going to stop this so I'll do pm2 stop zero that'll stop that process and then I'm going to delete it so pm2 delete zero so that'll remove it from the list and whenever you do a pm2 start you can specify a name and so you want to do this just so you can keep track of all the things that you have running and I'm just going to call this the holidays API and then specify the file that it should run so if I do that now in the process list here you can see it has a nice friendly name and it's running in the background so we can still visit it and it still works now pm2 also supports this idea of an ecosystem file and it's just a Javascript file that exports the

configuration for how to run the app so you don't always want to specify Das Das name from the command line if you create this file now you can just do pm2 start it'll look at that file automatically give it that name and automatically choose the script or or whatever else there's a lot of other options you can specify in here and if for whatever reason you had two apps in a folder that need to be run you could have multiple apps listed in

Chapter 30 1:04:07

Run a TypeScript node.js API

here now the holidays API that I just showed you was actually just JavaScript code it didn't require a build process I do want to show you an app that requires a build process because it's more likely that you're going to have an app like that and I want to show you how we we should run that app so for this I'm just going to use my Express API starter typescript template and uh I'm not even going to create a custom API I'm just going to clone this directly uh we'll grab it over https and then um we'll clone it into the host folder so if I get clone this here we now have the express API starter TS we'll go in there we will install the dependencies and then for this app it actually has a start script but let me show it to you because that start script just runs TS node and we actually don't want to do this in production because TS node is dynamically transpiling these typescript files as requests come in and uh we don't want that we want to actually run the built app and so you can see here that we do have a build command and then we can run the built files from there so if I do an mpm run

build that's actually going to transpile all of my typescript into JavaScript codes and then from there I now have this dist folder and the dist folder has just JavaScript files that need to be run so I am going to run the code that's inside of there so I'm going to CD into disc and you can see that it is it's just the JavaScript and it has that Source directory there and I'm going to use pm2 to start it so I'll say pm2 start we'll give it a name of example API and then we'll specify source index.js cool so now that's running in the background let's set up a domain for it so over here I'm going to add some a records we're going to create uh example API point to that IP disable the proxy you know the deal you've seen me do this five times now um and then we're also going to add the the dubdub duub one so www. example API point to that IP one minute TTL save okay so we have our DNS records let's set up the caddy

file so uh honestly you should pause this video and try to do it yourself you've already seen me do this uh several times now I think you can do it without watching me so uh I'm I'm still going to walk through it but I think I think you can do it without watching me so go to Etsy caddy and then we need to create another file in the sites available folder and um in this case we actually can just copy the holidays API because it's going to be very similar um so yeah I'm going to drop down to a root shell and then I'm going to copy that holidays API into the example API folder or file and then we'll edit that and we'll update the host names here so um we will call this example api. uh not local host.net and then down here same thing we'll call this the exam dub du. example api. loal host.net and we want to redirect it to example API loal host.net now uh we need to choose a port I actually don't know what port the app runs on by default let's

look at the codes by default it's trying to run on Port 5000 as well so it probably has errored out I actually didn't didn't even check so uh I'm going to save this file for now exit this root shell and if I do a pm2 list yeah you can see that the example API errored out um and so this is a learning moment because we can just look at the logs so if I do pm2 logs for example API very likely it is having an issue binding to that Port it is so we need to change that so let me go into that host folder and then in this example we actually can specify uh the EnV just directly in this folder there's a couple ways to do it we actually could do it with an ecosystem file because we could specify our Port environment variable there but I'm just going to create a Dov right in this folder because I know my app actually to load it in so I'm going to set no EnV to production and I'm going to set my port to be something that's not in use let's just use port 6000 so I'll save that and now we can we can restart that process so if we list it out it's here if I do a

pm2 restart of example API we can also tell it to reload the environment so if I do D- update EnV that'll make sure that it's looking at the latest environment variables and now if we look at the logs for ID number one it's it's running on Port 6000 who so from here we can update our reverse prox proxy config to point to Port 6000 so let's go back to Etsy caddy drop into a root terminal we're going to go and do sites available edit that uh example API file and then point it at 6,000 cool and then we need to link this to the sites enabled folder so we'll do an ln- S um actually we can do reverse search so if you press controlr and then start searching ln- s um that will give you the command so we can update it so we want this to be example API we're going to link to example API cool and so if we list the sites

enabled folder we should see example API is pointing to the config that lives in sites available good to go so now if we do a uh service caddy reload it should have that config and I should be able to visit example API doot local host.net nice and uh it's just a a super simple node.js API uh I think we can go to uh API V1 and then emojis yeah look at that so uh really all I wanted to show you here is if you have an application that requires a build process you're going to want to make sure that you run that and then this the process that's actually running on the server should be running the built files not the typescript files next I want to show you an example of a static website that requires a build process so this is anything that might be using V or webpack or rollup or something that was made with like create react app because at the end of the day

Chapter 31 1:09:52

Run a static website that has a build process

those apps actually just are static websites even though you might have uh a Dev script and like you're using node.js to run things locally all of that is to turn your react files or your view file files or your spel files into actual JavaScript code that can run in the browser but when these kinds of apps are running in produ ction they're just static files so let me show you how to set that up so here I have just this timer it's a react app built with V Let's actually clone it down now there are a couple ways to do this you could build the files locally and then just transfer the built files to the server or you could build on the server itself and uh let's do just that so I'm going to go into the VAR dubdub duub folder because this is where our static websites live and I'm going to create a directory called a big timer and uh I don't have permissions so I'm going to have to do it with pseudo and then I'm going to change the owner of that folder to be C so that way I can clone into it so I'm going to Chone it for CJ CJ and I need to make sure I do this as rout cool so now that folder is owned by

me and I can clone into it so I'm just going to clone this here uh so it's by default it was trying to clone into react big timer I just want to tell it to go into the big timer folder cool so now if we go into the big timer folder we can see the actual source code and we need to create the actual built files so we are going to have to install the dependencies here now this is why you might want to build this app elsewhere and then just transfer the built files because now the server is having to do the work of of building our front end app but uh for the for this example that's all I'm going to do so we installed dependencies now we need to run the build script so if we look in this specific app it is a v app so it has a build script that's just going to build it so I'm going to do mpm run build and you can see that it generated these stat static assets so this is a v app it's at the end of the day it's literally just three static files it's an HTML file all my CSS and all my JavaScript but it lives in this disc folder so if we CD into disc we can see this is the actual website that I want

to serve up so what I'm going to do is I'm going to print this directory and we're going to create a static website config with caddy that serves up this directory for the big timer website so first we'll go create those DNS records like we did before so we're going to just call this big timer and uh point it at this IP address we'll do uh a dubdub dub record as well it's getting old I know but um I think repetition is good for learning so we add another record point it to this IP disable this save it up good to go so I got those uh DNS records let's create the caddy configs so we're going to specifically create a static file website that points to this directory we're going to go into Etsy caddy and then I am going to drop down to a root shell and then create a inside of sites available we're going to call this big timer now we're going to put the domain so big timer. local host.net and uh we have to grab the directives

for a static file server which I don't have memorized so I'm going to go to the docs in a second but I'll set up the other stuff so dubdub dub. bigtim doot loal host.net should redirect to Big timer so reader https Go slash big timer doot local host.net great uh and then let's go to the cadd Dos so again we talked about this earlier but specifically we should go to directives we're going to want to find that static serve directive which is the file server and then we can see some examples so yeah we want file server specified here and then above that we'll specify the root say grab any file from that directory there so this is going to be serving up the stattic files that live in that dis directory for big timer so we'll save this we're going to need to link it to the sites enabled folder like this cool now that we've

linked it we'll Le reload caddy so we'll do service caddy reload and let's try it out so if I go to big timer. notloc host.net uh we got a big timer and uh the way this website works is you can pass in let's say like minutes uh five and that will just create a big five minute timer nice now the main takeaway from this is if you have an application that is ultimately just static files you should be serving it up as a static website you wouldn't use pm2 for this react app to like run the dev server uh that's just not what you want to do you specifically want to build those files and then just serve it up as a static

Chapter 32 1:14:44

Caddyfile concepts and Logging

website now that's it for all the example sites I'm going to show you there's so much more we can do but I do want to dive into some of the other Concepts that you should take a look at at for the caddy file uh first of all if you check out the concept section here on the site uh they give a nice breakdown of what's inside of a caddy file and what I have not shown is this idea of a global options block so at the top most level of your main caddy file you can add your main email that's used for like certificate renewal information and you can also set your ca now I didn't talk about this whenever we were setting things up but you could set your ca to be the staging server of let encrypt in case you're trying to test things out because let encrypt actually does rate limit your requests and so if you mess things up you might be locked out from setting up your TLS certificates so uh here is where you can set that CA URL and then just practice with staging if you want to do that now there's a bunch of other directives one I want to point your attention to is the log directive because by default logging is not enabled for any of your caddy

websites so you can specify the log directive you can give it a name and so in the logs it'll have that name instead of the generated like log zero log one and then you can specify an output so you can just output to standard out or standard error but probably the best thing you want to do is set up file output so if you do output file that will use a tool called Lumberjack to automatically output to a log file and it automatically does log file rotation so it doesn't just fill up your hard drive with logs um as they get to a certain size it'll delete the old ones and overwrite the write them to new ones if you scroll down to the example section you can see how they set this up so for any uh site block you can have the log directive and then say output file and then give it a a directory to log to you can also specify how big they should get before they roll over now I'll also link you to their docs on how logging works because they have some interesting ideas about it so for for most HTP servers they kind of have this standard log format or sorry the common

log format uh and you've probably seen this before in server logs where it's just like IP address and then info about the request but uh caddy likes to log things as Json and this gives you a lot more info about the request and you can use tools like like JQ which will let you look at these files that have all of these uh Json logs inside of them and actually query them in a useful way and so whenever you set up output logging with caddy it's actually going to Output uh these Json logs to your file by

Chapter 33 1:17:12

Common Caddyfile patterns including SPA redirects

default now the last place I'll point you to is the common cadd file patterns documentation and this has a bunch of just things that you might want to do inside of that caddy file and one specifically that you might be interested in is the single page applications one so uh so far of all the static sites that I showed you they didn't have like client side routing but if you have a static website that has client side routing you're going to want to set up this config right here and so anytime you have a single page application with a client side router uh it's potential that request to the app will 404 because those requests are handled by the client side they're not handled by the server and so this Tri files directive right here says any 404s should go to the index.html and that's essentially how you set up client side routing for single page applications so be sure to do this uh the other thing I didn't show and it's an example for us right here is you can actually set up compression so if you set encode to Gip that will make sure that all of your

static file responses are using the gzip compression just like all the other videos I've only scratched the surface there's so much more we can do with the web server and the caddy config and DNS records and everything else so please please please let me know down in the comments what do you want to learn about next what should I dive deeper into now I do want to say congrats to Matt kilcup uh because he won a free t-shirt so in the last self-host video um I showed the IP address of the server that I'm working on here and if you visited that IP address in the web browser it would just load the website that I've been showing you today not local host.net but there was a link to a form where you could get a coupon code for a free t-shirt and can you believe that Matt was the only person to do this uh and so congrats to Matt he got a free t-shirt now there's another place hidden in this video where you can find instructions on getting a coupon code for a free t-shirt the first person to do it will get that uh but I do want to note that it's hidden in plain sight you don't need to hack into anything you don't need to do anything malicious there's something

Chapter 34 1:18:15

Thanks!

I've done today where you can see the instructions on how to get that free t-shirt and the first person to do it will get it all right that's all I have for you please please please let me know there's there's so much I wasn't able to cover and so much more that that I want to talk about so let me know what do you want to see next and uh I'll see you in the next [Music] one

Quote it with credit and a link. Quoting Syntax