php phalcon micro api endpoint return 404 except root url

52 views Asked by At

I am trying to setup very simple, 2 api endpoints using Php Phalcon Micro. I am getting 404 not found on endpoint route with "/api/healthcheck". But when I call root "/", I get proper response. I request to guide me on this issue. Thanking you in advance.

System architecture: Ec2 Server is under vpc and public subnet. Ubuntu: 22.04, PHP: 5.6, Apache 2.4.52

Request flow: domain.com -> nginx : 80 -> apache2 virtualhost : 8081 -> project folder with .htaccess

Api endpoint 1st:

 Request: domain.com/
 Response: status 200
      Array{
        "id": "1",
        "version": "1"
      }

Api endpoint 2nd:

 Request: domain.com/api/healthcheck
 Response: status 404 not found

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

/var/www/domain.com/index.php

<?php
// Phalcon
use Phalcon\Loader;
use Phalcon\Mvc\Micro;
use Phalcon\Di\FactoryDefault;
use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;

use Phalcon\Http\Response;
use Phalcon\Http\Request;

// Getting a request instance
$request = new Request();

// Set up Database Service
$di = new FactoryDefault();

// Set up the database service
$di->set('db', function () {
    return new PdoMysql(
        array(
            "host"     => DB_SERVER,
            "username" => DB_USER,
            "password" => DB_PASS,
            "dbname"   => DB_NAME
        )
    );
});

$app = new Micro($di);

$app->get(
    '/',
    function () use ($dbConn) {
        $sql = 'SELECT * FROM `dbversions` WHERE 1 Order By id desc LIMIT 1';
        $result_set = mysqli_query($dbConn, $sql);
        $row = mysqli_fetch_assoc($result_set);
        echo json_encode($row);
    }
);

$app->get(
    '/api/healthcheck',
    function () use ($dbConn) {
        $data = array("status"=> "success");
        echo json_encode($data);
    }
);

$app->handle();
?>

Apache2: /etc/apache2/apache.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

Virtual Host: /etc/apache2/site-available/000-default.conf

<VirtualHost *:8081>
    ServerName domain.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/domain.com/

    <Directory /var/www/domain.com>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Php8.1 Packages: sudo dpkg -l | awk '/^ii/{print $2}' | grep php8.1 | tee php8.1-packages.tx

libapache2-mod-php8.1
php8.1
php8.1-cli
php8.1-common
php8.1-curl
php8.1-dev
php8.1-fpm
php8.1-mbstring
php8.1-mysql
php8.1-opcache
php8.1-phalcon5
php8.1-readline

Php5.6 Packages: sudo dpkg -l | awk '/^ii/{print $2}' | grep php5.6 | tee php5.6-packages.tx

libapache2-mod-php5.6
php5.6
php5.6-cli
php5.6-common
php5.6-curl
php5.6-dev
php5.6-fpm
php5.6-json
php5.6-mbstring
php5.6-mysql
php5.6-opcache
php5.6-phalcon3
php5.6-readline

Nginx Access Logs: sudo tail -f /var/log/nginx/access.log

100.250.01.01 - - [26/Oct/2023:01:38:12 +0000] "GET / HTTP/1.1" 200 85 "-" "PostmanRuntime/7.33.0"
100.250.01.01 - - [26/Oct/2023:01:38:23 +0000] "GET /api/healthcheck HTTP/1.1" 404 240 "-" "PostmanRuntime/7.33.0"

Apcahe2 Access Logs: sudo tail -f /var/log/apache2/access.log

127.0.0.1 - - [26/Oct/2023:01:38:12 +0000] "GET / HTTP/1.0" 200 299 "-" "PostmanRuntime/7.33.0"
127.0.0.1 - - [26/Oct/2023:01:38:23 +0000] "GET /api/healthcheck HTTP/1.0" 404 450 "-" "PostmanRuntime/7.33.0"
0

There are 0 answers