Question

The correct slim framework settings on nginx

I am a very weird situation. Below is what I have done on my centos 7 box. I have nginx which works perfectly fine with a normal php application. Next via composer I just tried to install slim framework.

composer create-project slim/slim-skeleton app. Next I tried to change the ownership via this command

chown nginx:nginx app/ next I did the chmod mod command chmod 755 app/ next step I cd into the app folder and ran this command chmod 644 *

Below is my nginx.conf for now.

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
#include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    server_tokens off;
    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

  include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/blockuseragents.rules;
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        server_tokens off;

        #root         /usr/share/nginx/html;
        root /var/www/html/;
        index index.php index.html home.html;
        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;
        include /etc/nginx/default.d/*.conf;


        location ~ ^/app {
       
          try_files $uri $uri/ /app/public/index.php$is_args$args;
        }

        location ~ .php$ {
         include fastcgi.conf;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_intercept_errors on;
         fastcgi_pass 127.0.0.1:9000;
        }
 limit_conn addr 3;


       

    }



The weird part now when I type like this

http://myip/apiv5 I get 403 forbidden but when I type example like this.

http://myip/apiv5/token . I am suspecting some permission error is causing this to mess up. How can I enable it to work perfectly?


<?php
if (PHP_SAPI == 'cli-server') {
    // To help the built-in PHP dev server, check if the request was actually for
    // something which should probably be served as a static file
    $url  = parse_url($_SERVER['REQUEST_URI']);
    $file = __DIR__ . $url['path'];
    if (is_file($file)) {
        return false;
    }
}

require __DIR__ . '/../vendor/autoload.php';

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);

// Set up dependencies
$dependencies = require __DIR__ . '/../src/dependencies.php';
$dependencies($app);
// Register middleware
$middleware = require __DIR__ . '/../src/middleware.php';
$middleware($app);

// Register routes
$routes = require __DIR__ . '/../src/routes.php';
$routes($app);

// Run app
$app->run();
Subscribe
Share

Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello,

Your configuration seems correct and the permissions seem fine as well. I’ve seen a similar issue due to SELinux. Have you tried moving the application in your /var/www/ folder and see how it goes?

Another thing you could try is running this command to allow rw access for Nginx:

chcon -R -t httpd_sys_rw_content_t /app

Let me know how it goes! Bobby