phpapacheslim

Running slim framework on existing apache web server


I am new to slim framework. Currently on my existing webserver which is on centos 7 I have other php based application running. So currently my directory structure is like this.

var/www/html
    phpapp1
    phpapp2
    apislim

The folder for the apislim I created was for slim framework. Below are the exact steps I did was

  1. composer create-project slim/slim-skeleton

  2. I rename the slim-skeleton folder to apislim

  3. I make sure the owner is apache chown -R apache:apache apislim

  4. In the httpd.conf I ensure this AllowOverride is enabled to be All

    <Directory "/var/www"> AllowOverride All # Allow open access: Require all granted Options -Indexes

Also below I enabled All

<Directory "/var/www/html">   
    Options -Indexes -FollowSymLinks
    AllowOverride All
</Directory>
  
  1. In the apislim/public folder I have this .htaccess file as below.
<IfModule mod_rewrite.c>
  RewriteEngine On

  
  RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
  RewriteRule ^(.*) - [E=BASE:%1]


  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
</IfModule>
  1. The I have also the index.php file in the public folder which also links up to the src for the routes and the main folder apislim I created another .htaccess file and added this.

    RewriteEngine on RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L]

The issue now I want my existing application to work along with this REST api which is based on Slim framework. So when I go to this link

http://*.*.*.*/apislim/
http://*.*.*.*/apislim/public/
http://*.*.*.*/apislim/public/index.php

Neither of it works all are giving me 403 forbidden. Then I check the error log it show error regarding FollowSymLinks So I added

Options -Indexes +FollowSymLinks 

into <Directory "/var/www/html">

So next error I get now is 500 interval server error.


Solution

  • The following steps are necessary for your Slim 3 application to work within subdirectories.

    Directory structure:

    The content of the file: .htaccess:

    RewriteEngine on
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
    

    The content of the file: public/.htaccess:

    # Redirect to front controller
    RewriteEngine On
    # RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L] 
    

    Add this container entry to the file: dependencies.php:

    // Activating routes in a subfolder
    $container['environment'] = function () {
        $scriptName = $_SERVER['SCRIPT_NAME'];
        $_SERVER['SCRIPT_NAME'] = dirname(dirname($scriptName)) . '/' . basename($scriptName);
        return new Slim\Http\Environment($_SERVER);
    };
    

    Edit: In Slim 4 you should use the $app->setBasePath($basePath); method. More details