Caddy Web Server

ArticlesBlog

Written by:


Assalamualaikum and a very good afternoon
everyone My name is Zulfa. I’m here to talk about Caddy. So Caddy is a web server and I’m going to
show you guys how to set up. So first and foremost you’ll have to a VPS. I have a VPS here. As you can see I’m running on AWS. And here’s my public IP address. Alright, and from that public IP Address I
point it to aws02.zulfajuniadi.com because Caddy uses the DNS to generate the
certificates so you’ll need to point the DNS first. So today I’m going to show you how to Install
Caddy on to your Ubuntu Server or any Linux Server. So first you got caddyserver.com, click download. So here I’ll show you how to do Git deploy. So I’ll have that ticked. Then click download based on your platform. Since I’m running on Linux 64 bit so I’ll
just click this one. So In my downloads, I’ll just extract that
so I’ll have these. So on my server I’ll create a new: I’ll create a new directory
and name it Caddy. So I’ll transfer those files. I’ll just upload the whole directory to my
server. Won’t take long. So we have this caddy executable. To run Caddy, just type in ./caddy and you
can see that it’s listening on port 2015. So if you go to aws02.zulfajuniadi.com:2015
now you can see Caddy is running but it says 404 not found. Next, let’s create a virtualhost that points
aws02 to a folder. As you can see my /var/www/aws02 I’ve already git cloned my github repo, which is this one “caddy-tutorial”. So you want aws02.zulfajuniadi.com to point
to this caddy folder. So how do we do that? We go back to /opt/caddy, let’s create a new
file called Caddyfile. Of course you can name this file anything
and you can place this configuration at any location, but for the ease of this
tutorial I’ll just create it in the same directory as the caddy executable. So to create a virtualhost first you need
to put in your subdomain and we need to tell caddy where the index file is. So: root /var/www/aws02/caddy. So let’s run caddy and tell caddy to use Caddyfile
as the configuration file. So it’s saying that http and https both are
working. So if you go to port 2015, it’s no longer
listening on 2015 it automatically listens on port 80 and 443. Alright, so, as you can see HTTPS is working
and it’s a valid certificate. But it’s saying that 404, not found. Why? because default Caddy installation, it
will try to find the index.html file so since in our directory, in our caddy
directory there’s an index.php, let’s change that. mv index.php to index.html. So in our server, in our /opt/caddy,
let’s rerun caddy. Right, so will echo out whatever is inside
that file. So next we are going to. Let’s change this back to index.php. Next we’re going to tell Caddy to handle
the PHP file. So to do that, first you have to install,
obviously PHP. apt install php7.0. So since I’ve already have it installed. So you can see here that it’s already installed. To check whether it’s running: service php7.0-fpm
status. So it’s saying that the FastCGI
Process Manager is running. The socket is in /var/run/php/php7.0-fpm.sock. So this is the socket. So to tell caddy to use that socket to handle
PHP files, it’s like this. So we’ll edit our Caddyfile. We’ll just need to add in one line: fastcgi
/ /var/run/php/php7.0-fpm.sock php. Let’s run it again. So now it’s passing the file to PHP FPM and
FPM is returning this HTML. So next we’re going to do git deploy. So if you look under Caddy Server documentation
under Git. You see git, repo, clonepath, then we want
a hook, path and secret. So git deploy means that every time a push
event is done on your git repository Github will tell your server to refresh the files. Or sent the event to your server, basically. Let’s setup our webhook. In Github under settings, webhooks, let’s
delete the old one. Add a webhook https://aws02.zulfajuniadi.com/git-update. Of course this one can be anything. Secret let’s just put password. Just send the push event. Alright. Next we’re going to update
the Caddyfile, git, the repo is the repo URL, which is, this one, where we want to git deploy
to. Since the git has these three folders:
caddy, caddy2 and Caddyfile, we’re not going to
deploy it in the caddy folder, we’re going to deploy it on aws02, so it’s a relative
path, since we’re now in caddy, we want to point
it to aws02, we’ll just use ../. Next we want to have the hook, so hook /git-update password. Save that. So during the first initiation of Caddy,
it will pull from the repository and say that the files been pulled, basically. So, let’s cd
into my Desktop. So I’ve already cloned the same repo on my
desktop, so let’s edit the file: vi caddy/index.php so instead of phpinfo we want to echo “hello
there!”. Then git commit -am “test webhook” then git
push. So as you can see once the files has been
pushed, Github will notify the server that theres a new update. Ah, see there’s a conflict. Let’s try this again. You can see pull notification, and it’s saying
that one file changed. And when we refresh this we see “Hello there
devs!” so that’s how we do deployment.

One Reply to “Caddy Web Server”

  1. onehans66 says:

    somehow when i installed it on my qnap it does not have any configuration window or probably does not work

Leave a Reply

Your email address will not be published. Required fields are marked *