Php_fastcgi failed to specify PHP root path

1. Caddy version (caddy version):

Docker Caddy 2.2.1
Docker wordpress 5.5.1-php7.4-fpm-alpine
Docker mysql 8.0.21

2. How I run Caddy:

Domain names

a. System environment:

Docker version 19.03.13

b. Command:

docker-compose up -d

c. Service/unit/compose file:

#【docker-compose.yml】
version: "3.7"
services:

  Mysql:
    image: mysql:8.0.21
    networks:
      - netcaddy
    volumes:
      - $PWD/mysql:/var/lib/mysql
    restart: always
    environment:
     MYSQL_ROOT_PASSWORD: ***********
    container_name: db

  Wordpress:
    depends_on:
      - Mysql
    image: wordpress:5.5.1-php7.4-fpm-alpine
    restart: always
    networks:
      - netcaddy
    volumes:
      - $PWD/wordpress/web:/var/www/html
    container_name: wp

  Caddy2:
    depends_on:
      - Wordpress
    image: caddy:2.2.1
    networks:
      - netcaddy
    #command: -conf /home/Caddyfile -agree
    volumes:
      - $PWD/caddy_data/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/caddy_data:/data
      - $PWD/wordpress/web:/usr/share/caddy
    restart: always
    ports:
      - "80:80"
      - "443:443"
    container_name: caddy2

networks:
  netcaddy:

d. My complete Caddyfile or JSON config:

#【Caddyfile】
domain.name

# Set this path to your site's directory.
root * /usr/share/caddy

# Enable the static file server.
file_server

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000
php_fastcgi wp:9000 {
  root * /var/www/html  
}

4. Error messages and/or full log output:

【docker-compose logs】
caddy2 | run: adapting config using caddyfile: parsing caddyfile tokens for ‘php_fastcgi’: /etc/caddy/Caddyfile:35 - Error during parsing: unrecognized subdirective /var/www/html

5. What I already tried:

【Modify Caddyfile】
1、php_fastcgi wp:9000 #Website------>“File not found.”
2、php_fastcgi unix//run/php/php-7.4-fpm.sock #Website------>HTTP ERROR 502
3、php_fastcgi wp:9000 {
root * /usr/share/caddy
} #docker-compose logs------>Error during parsing: unrecognized subdirective /usr/share/caddy
4、php_fastcgi wp:9000 {
root /usr/share/caddy
} #Succeed :v: :v: :v:

You should be using the root directive here, not the root subdirective of the php_fastcgi directive. There’s a pretty significant difference in how they behave (the directive will apply to everything, the subdirective will only apply to that directive, which matters here because you’re using file_server and php_fastcgi also comes with a file matcher which uses root as well). You have both defined in your Caddyfile, you should only use one of them.

Your Caddyfile should look like this:

domain.name {
	root * /var/www/html
	php_fastcgi wp:9000
	file_server
}

You’ll also need to change this line in your docker-compose.yml:

- $PWD/wordpress/web:/usr/share/caddy

to this:

- $PWD/wordpress/web:/var/www/html

Because the filesystem paths need to match between Caddy and the php-fpm service, so that they both refer to the same place on disk.

Also, it’s not safe to reuse the caddy_data directory for two purposes. You should change these:

      - $PWD/caddy_data/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/caddy_data:/data

To something like this instead:

      - $PWD/caddy/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/caddy/data:/data

Things can break when you have one volume inside of another.

1 Like

@francislavoie, Francis Lavoie, thank you very much for your patience. I am trying to build caddy2 + wordPress + nextcloud through docker. I hope to get your opinions.

This topic was automatically closed after 30 days. New replies are no longer allowed.