nik2208 / php-crud-api-quick-start

A ready to go, customizable, docker stack for PHP-CRUD-API
6 stars 0 forks source link

404 when accessing http://127.0.0.1:8080/openapi #3

Open mevdschee opened 9 months ago

mevdschee commented 9 months ago
maurits@n100dc:~/projects/php-crud-api-quick-start$ docker-compose up
Starting mysql_treeqlqs ... done
Starting php-fpm_treeqlqs ... done
Starting nginx_treeqlqs   ... done
Starting dbadmin_treeqlqs ... done
Starting swagger_treeqlqs ... done
Attaching to mysql_treeqlqs, php-fpm_treeqlqs, nginx_treeqlqs, dbadmin_treeqlqs, swagger_treeqlqs
dbadmin_treeqlqs | [Thu Oct 12 17:45:49 2023] PHP 7.4.33 Development Server (http://[::]:8080) started
nginx_treeqlqs |   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
nginx_treeqlqs |                                  Dload  Upload   Total   Spent    Left  Speed
mysql_treeqlqs | 2023-10-12 17:45:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.1.0-1.el8 started.
100  480k  100  480k    0     0  1596k      0 --:--:-- --:--:-- --:--:-- 1600k
nginx_treeqlqs | 2023/10/12 17:45:49 [warn] 1#1: server name "http://127.0.0.1:8080/" has suspicious symbols in /etc/nginx/conf.d/default.conf:3
nginx_treeqlqs | nginx: [warn] server name "http://127.0.0.1:8080/" has suspicious symbols in /etc/nginx/conf.d/default.conf:3
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: using the "epoll" event method
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: nginx/1.25.2
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: OS: Linux 6.5.3-060503-generic
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: start worker processes
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: start worker process 10
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: start worker process 11
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: start worker process 12
nginx_treeqlqs | 2023/10/12 17:45:49 [notice] 1#1: start worker process 13
php-fpm_treeqlqs | [12-Oct-2023 17:45:48] NOTICE: fpm is running, pid 1
php-fpm_treeqlqs | [12-Oct-2023 17:45:48] NOTICE: ready to handle connections
mysql_treeqlqs | 2023-10-12 17:45:48+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_treeqlqs | 2023-10-12 17:45:48+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.1.0-1.el8 started.
mysql_treeqlqs | '/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
swagger_treeqlqs | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
swagger_treeqlqs | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
mysql_treeqlqs | 2023-10-12T17:45:48.998140Z 0 [System] [MY-015015] [Server] MySQL Server - start.
mysql_treeqlqs | 2023-10-12T17:45:49.228034Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
mysql_treeqlqs | 2023-10-12T17:45:49.229705Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.1.0) starting as process 1
swagger_treeqlqs | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
mysql_treeqlqs | 2023-10-12T17:45:49.240752Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_treeqlqs | 2023-10-12T17:45:49.437918Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_treeqlqs | 2023-10-12T17:45:49.697991Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_treeqlqs | 2023-10-12T17:45:49.698019Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_treeqlqs | 2023-10-12T17:45:49.703899Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
swagger_treeqlqs | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
mysql_treeqlqs | 2023-10-12T17:45:49.733495Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql_treeqlqs | 2023-10-12T17:45:49.733774Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.1.0'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
swagger_treeqlqs | 10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
swagger_treeqlqs | /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
swagger_treeqlqs | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
swagger_treeqlqs | 20-envsubst-on-templates.sh: Running envsubst on /etc/nginx/templates/default.conf.template to /etc/nginx/conf.d/default.conf
swagger_treeqlqs | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
swagger_treeqlqs | /docker-entrypoint.sh: Launching /docker-entrypoint.d/40-swagger-ui.sh
swagger_treeqlqs | /docker-entrypoint.sh: Configuration complete; ready for start up
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: using the "epoll" event method
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: nginx/1.25.2
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r10) 
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: OS: Linux 6.5.3-060503-generic
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: start worker processes
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: start worker process 57
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: start worker process 58
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: start worker process 59
swagger_treeqlqs | 2023/10/12 17:45:50 [notice] 1#1: start worker process 60
php-fpm_treeqlqs | ERROR: Passing INI directive through FastCGI: unable to set 'root'
php-fpm_treeqlqs | 172.18.0.4 -  12/Oct/2023:17:45:58 +0000 "GET /api.php" 404
nginx_treeqlqs | 172.18.0.1 - - [12/Oct/2023:17:45:58 +0000] "GET /openapi HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0"

Can you see why I get a 404 on http://127.0.0.1:8080/openapi? I think it is about these lines:

php-fpm_treeqlqs | ERROR: Passing INI directive through FastCGI: unable to set 'root'
php-fpm_treeqlqs | 172.18.0.4 -  12/Oct/2023:17:50:09 +0000 "GET /api.php" 404

Can you confirm? What is causing that?

nik2208 commented 9 months ago

fixed here https://github.com/nik2208/php-crud-api-quick-start/commit/3fe5ae4f444c1f1079bf6d23b28acdc106dbb4b3 just missed to put api.php in the right position (it worked on my machine for the exact reason I asked you to try it 😅 )

would u give it a try again?

nik2208 commented 9 months ago

there's still something missing.

this

web:
    image: nginx:latest
    container_name: nginx_treeqlqs
    restart: always
    environment:
      SERVER_NAME: ${SERVER_NAME}
      PHP_CRUD_API_PORT: ${PORT}
      PHP_CRUD_API_ADDRESS: ${SERVER_NAME}
      PHP_CRUD_API_DATABASE: ${MYSQL_DATABASE}
      PHP_CRUD_API_USERNAME: ${MYSQL_USER}
      PHP_CRUD_API_PASSWORD: ${MYSQL_PASSWORD}
      PHP_CRUD_API_DEBUG: ${PHP_CRUD_API_DEBUG}
    volumes:
      - ./nginx/default.conf.template:/etc/nginx/conf.d/default.conf.template
      - ./php:/var/www/html
    ports:
      - "${PORT}:80"
    command: /bin/bash -c "curl -o /var/www/html/api.php https://raw.githubusercontent.com/mevdschee/php-crud-api/main/api.php && envsubst '$$SERVER_NAME' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
    depends_on:
      - php
    networks: 
      - php-api-network

is the code setting up the nginx container.

all the env variables are properly set and available in the nginx container shell (I've checked their values one by one) but I'm getting

PDOException occurred (enable debug mode)

I found the exception in api.php but I'm not able to understand what causes it.

As long as I was using a static version (copy-pasted) of api.php turned into template and configured via envsubst everything was working Using the env variables somethings wrong happens.

UPDATES After further investigations, even if the env variables are properly set in the system, they aren't used by api.php: if I leave a copy of api.php and set the config values, everything works correctly.

Enabling the debug flag, the previous PDO exception error turns into { "code": 9999, "message": "SQLSTATE[HY000] [2002] No such file or directory" }

Screenshot 2023-10-13 at 13 11 08

mevdschee commented 9 months ago

SQLSTATE[HY000] [2002] No such file or directory

Probably you have no access to /var/lib/mysql/mysql.sock or you should connect on 127.0.0.1 instead of localhost.

nik2208 commented 9 months ago

It's something related to the env variables. If I properly set the configuration fields it works. If I leave the config section as is counting on the docker compose env variables it doesn't.

nik2208 commented 9 months ago

Do u have any hint/idea on why the api.php is unable to catch the env variables in the nginx image even if the are there as shown by the screenshot above?

mevdschee commented 9 months ago

I think the PHP_CRUD_API_ADDRESS should be the hostname of the database, not the URL.

nik2208 commented 9 months ago

I realised that and I fixed it, but the problem persists.

mevdschee commented 8 months ago

Can you post another dump of the env variables?

jaanbv commented 3 months ago

Hi, I had the same issue and solved it by some changements in the docker-compose file. The debug var and some other things were not on the right place. docker-compose.yml.txt

blungu commented 1 week ago

I think the PHP_CRUD_API_ADDRESS should be the hostname of the database, not the URL.

Yes, you are correct, current container_name is "mysql_treeqlqs" so:

$config = new Config([
        // 'driver' => 'mysql',
        'address' => 'mysql_treeqlqs',
        // 'port' => '3306',
        ...

This error is not impactful:

php-fpm_treeqlqs | ERROR: Passing INI directive through FastCGI: unable to set 'root'
php-fpm_treeqlqs | 172.18.0.4 -  12/Oct/2023:17:50:09 +0000 "GET /api.php" 404