Assistant Engineer
Assistant Engineer
  • UID622
  • Fans3
  • Follows0
  • Posts52

[Share]Simple Deployment of Caddy Web Server

More Posted time:Sep 18, 2016 11:05 AM
Caddy Server is an outstanding web server in Golang through with fewer domestic users. But I like it very much so I decide to make a simple manual for it.
It has many functions not available in Nginx and Apache. These functions are not only distinctive but also very practical such as simple .conf, HTTP/2 and markdown rendering. It does have a defect, in that it is not as efficient as the former two.
This text will introduce the Caddy Server from the perspectives below.
• Caddy Server installation and configuration files
• Basic composition modules
• Traditional PHP+MySQL for website building
• Caddy-distinctive markdown website building
• Reverse proxy introducing more back-end systems
1. Caddy installation and configuration files
To install Caddy Server, download the installer from Caddy official website: https://caddyserver.com/download. Check whichever modules you want.
PS: Put the zip package in a new folder before unzipping it.
Then execute ./caddy. Now you can visit localhost:2015 to test whether the site has been running. There are multiple ways to achieve persistent running.
The downloaded installer package of Candy Server already contains various system startup files.
I use Screen for protection. Of course, there are more professional software implementation, such as Supservisor.
Caddy boasts many ways to write a .conf file. First, you need to create a file named Caddyfile in the same directory of the executable file of Caddy. Or you should add the path ./caddy -conf=”/path/to/Caddyfile”at startup.
Next, I will introduce several ways to write a Caddy .conf file which is “amazing”.
For local access, 80:

Single site: (It will sign let’s encrypt certificate for you and enforce direction to https)
root /var/www/html
//More modules

Multiple website:
example.com {
  root /var/www/htm/
  //More modules

2. Frequently-used basic component modules
Included in the .conf file for the site.
1. Log
log /var/www/log/example.log

2. Directory access

3. Gzip compression

4. Independent SSL certificate
tls /path/ssl/example.com.crt /path/ssl/example.com.key

5. Git pull function (interval: 3,600 seconds)
git https://github.com/user/project.git/var/www/html/git/ {
  interval 3600

6. Access password authentication (User: emiria, password: abc123)
basicauth / emiria abc123

7. Cross-domain (Remember to check this at download)
cors / {  
    origin            https://alleysakura.com
    origin            http://alleysakura.pw
    methods           POST,PUT
    allow_credentials false
    max_age           3600
    allowed_headers   X-Custom-Header,X-Foobar
    exposed_headers   X-Something-Special,SomethingElse

8. IP address blocking (Remember to check this at download)
ipfilter / {  
    rule       block
    blockpage  /var/www/html/403.html

9. Navigation function (Directory rewrite function is similar)
redir http://example.com{url}

It is not as complicated as www re-direction of Nginx. You only need to write the domain name, original domain name (protocol not limited) and add this into the braces, which is marvelous.
10. Custom error pages
errors {
    404 404.html
    500 /var/www/html/500.html

3. Traditional PHP+MySQL for website building
Use the package manager to install MySQL+PHP and add
fastcgi / php

4. Caddy-distinctive markdown website building
Specific examples are available on the official wiki. It has powerful functions. This is a demo site the post owner builds https://caddy.rd.md
5. Reverse proxy introducing more back-end systems
Only one line is needed to introduce reverse proxy.
proxy / localhost:4000
SLB configurations are also similar.

proxy / {
        policy round_robin

Of course, Caddy Server also has many not-frequently-used but very interesting components. You can check them out on the official website and I will also keep exploring more.