安裝 Caddy Web Server
Caddy 安裝
- 本文與前一篇文章 ”安裝 phpmyadmin“ 有連貫,即,已安裝了某些必要套件,本文不再提及。
- 筆者希望網頁統一收管在 /var/web 底下,即所謂 doc root
- 並也打算一些測試的網頁在 /var/web/html 下操作
- 而 phpmyadmin 則以 /var/web/dbadmin 來代表
- 為了彰顯個人伺服器的彈性,所以打算安裝兩套 wordpress,乃例如,一個當作測試用,另一個則是正式發佈用,故有 /var/web/wp
- 另一個 /var/web/wordpress
- 又或是,一台電腦上架兩個不同網址的 wordpress 網站也行。
- 部份路徑,已在前文提及建立。若前後文都未提及,請自行建立。接著,照著以下安裝命令,即可安裝完成 Caddy
sudo mkdir /etc/caddy sudo mkdir /var/log/caddy sudo mkdir /etc/ssl/caddy sudo chown -R root:www-data /etc/caddy sudo chown -R www-data:root /var/log/caddy sudo chown -R www-data:root /etc/ssl/caddy sudo chmod 770 /var/log/caddy sudo chmod 770 /etc/ssl/caddy sudo touch /etc/caddy/Caddyfile sudo chown root:www-data /etc/caddy/Caddyfile curl https://getcaddy.com | bash -s personal sudo chown root:root /usr/local/bin/caddy sudo chmod 755 /usr/local/bin/caddy sudo setcap 'cap_net_bind_service=+eip' /usr/local/bin/caddy curl -L https://github.com/mholt/caddy/raw/master/dist/init/linux-systemd/caddy.service | sudo tee /etc/systemd/system/caddy.service ###curl -L https://github.com/mholt/caddy/raw/master/dist/init/linux-systemd/caddy.service | sed "s/;CapabilityBoundingSet/CapabilityBoundingSet/" | sed "s/;AmbientCapabilities/AmbientCapabilities/" | sed "s/;NoNewPrivileges/NoNewPrivileges/" | sudo tee /etc/systemd/system/caddy.service sudo chown root:root /etc/systemd/system/caddy.service sudo chmod 644 /etc/systemd/system/caddy.service sudo systemctl daemon-reload sudo systemctl enable caddy.service sudo systemctl start caddy.service ###接下來做相關文件的建立與設定,如下 測試 一節。完成後,執行下二命令 ###sudo systemctl restart php7.3-fpm.service ###sudo systemctl restart caddy.service
測試
首先,建立 /var/web/html,並在底下建立三個檔案:index.html, index.php, phpinfo.php
index.html
<!DOCTYPE html>
<html>
<head>
<title>This page is served by Caddy Web Server</title>
</head>
<body>
<h3>This page is served by Caddy Web Server. LCMP creation.</h3>
<p>This is a paragraph.</p>
</body>
</html>
index.php
<!DOCTYPE html>
<html>
<head>
<title>This page is served by Caddy Web Server</title>
</head>
<body>
<h3>This page is served by Caddy Web Server. LCMP creation.</h3>
<p>This is a paragraph.</p>
<?php echo("Hello, world!"); ?>
</body>
</html>
phpinfo.php
<?php phpinfo(); ?>
建立html目錄及置入三個檔案後下達:sudo chown -RL root:www-data /var/web/html
最後,我們設置 Caddy 的設定檔 /etc/caddy/Caddyfile 完成後,重啟 Caddy 便大功告成了
Caddyfile
# for general purpose tests
localhost:8080 {
tls off
root /var/web/html
log /var/log/caddy/test_access.log
errors /var/log/caddy/test_errors.log
# PHP-FPM Configuration for Caddy
fastcgi / /run/php/php7.3-fpm.sock php
}
# for phpmyadmin
localhost:8081 {
tls off
root /var/web/dbadmin
log /var/log/caddy/dbadmin_access.log
errors /var/log/caddy/dbadmin_errors.log
# PHP-FPM Configuration for Caddy
fastcgi / /run/php/php7.3-fpm.sock php
}
卡關
至此,一台網頁伺服器於焉完成。但事實是網頁無法顯示!筆者經日夜寒暑不斷地與谷神搏鬥,最終。。。雖查不到,不過仍試通關了,尚需以下回復的一步:
- sudo gedit /etc/php/7.3/fpm/php.ini
- 尋找 doc_root =
- 原本我們已在前文設定成 doc_root = /var/web,但因 php-fpm 始終無法找到網頁,故將此設定回復,參數設為空,便能將問題解決。即 doc_root = 並存檔離開。
最後,重啟:
- sudo systemctl restart php7.3-fpm.service
- sudo systemctl restart caddy.service
- 之後,可用 sudo systemctl status service-name 查看服務器的執行狀況;按Q離開
好菜上桌
打開 firefox 網址分別輸入,以測試幾種網頁:
- http://localhost:8080/
- http://localhost:8080/index.html
- http://localhost:8080/index.php
- http://localhost:8080/phpinfo.php
- http://localhost:8081/
- https://localhost:8080/ 這個嘗試會是失敗的,但 https 正是 Caddy 的強項,後續會再介紹到。
- 參考資料
- https://www.digitalocean.com/community/tutorials/how-to-host-a-website-with-caddy-on-ubuntu-16-04
- https://blog.wu-boy.com/2017/04/1-line-letsencrypt-https-servers-in-golang/
20210918 更新
在 caddy 出了 v2 版後,其 config file 改得更系統化結構化簡單化且應是有相容 v1 版的;其只要按照 migration 步驟便可移植。筆者現用的是 caddy v1.0.4,也不再使用 v2 版了後續轉而使用 nginx,其在後面文章有提到並且連同 ssl 憑證,也不會太難安裝。建議可 nginx。
Caddy 官網已不支援 V1 版了/應是說都建議改用 V2 版。將自己現用的 bin file 1.0.4 版 (h1:wwuGSkUHo6RZ3oMpeTt7J09WBB87X5o+IZN4dKehcQE=) 放於此備存。
發佈留言