Thursday, May 25, 2023

Some notes about caddy webserver installation on Linux



sudo apt update

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https

curl -1sLf '' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc

curl -1sLf '' | sudo tee /etc/apt/sources.list.d/caddy-stable.list

sudo apt update

sudo apt install caddy

# the code sits in /var/www

# create link to the local directory

ln -s /var/www/ /home/xxx/myprojects/web

# set the user to be xxx and the group caddy

sudo chown -R xxx:caddy ./www  

# add us to caddy groups

sudo usermod -a -G caddy xxx

sudo usermod -a -G www-data xxx

Install php-fpm

sudo apt install php-cli php-fpm php-mysql php-mbstring php-curl

configure caddy with php

sudo nano /etc/php/8.1/fpm/pool.d/www.conf

#change users to caddy (user, group, listen.owner,

#take the socket name: listen = /run/php/php8.1-fpm.sock

nano php.ini # set E_ALL view all errors

sudo systemctl restart php8.1-fpm

#set socket in Caddyfile: php_fastcgi unix//run/php/php8.1-fpm.sock

sudo service caddy stop && sudo service caddy start


Caddyfile {
#:80 {
tls /var/www/fullchain.pem /var/www/privkey.pem

# Set this path to your site's directory.
root * /var/www/
php_fastcgi unix//run/php/php7.4-fpm.sock

# Enable the static file server.
# file_server
rewrite /symbol/* s.php?s={path}
# rewrite /symbol/ {
# r ^/(\w+)/?$
# to /symbol.php?symbol={1}
# }

encode gzip

log {
output file /var/log/caddy/ {
roll_size 3MiB
roll_keep 5
roll_keep_for 48h
format console
# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000

#RewriteRule ^symbol/jscss/([^/]*)\.([^/]*)$ /jscss/$1.$2 [L] # no need for those since we used <base href="../" />
#RewriteRule ^symbol/images/([^/]*)\.([^/]*)$ /images/$1.$2 [L]
#rewrite /symbol/* / symbol.php?symbol={query}



I had the same problem whith my docker php-fpm and I fixed it by modifing parameters

 in these files: /usr/local/etc/php-fpm.d/www.conf /usr/local/etc/php-fpm.d/www.conf.default

The new parameters are :

pm = dynamic
pm.max_children = 25
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500



- ADD how to enabled logs in PHP