PHP slim 部署Apache

Step 1: Install XAMPP

XAMPP 是一个集成了 Apache 服务器、MySQL 数据库和 PHP 的开放源代码软件包。

 

Step 2: Configure the Apache

打开Apache的配置文件httpd.conf,有效化 Apache Rewrite (mod_rewrite) 模块。
去掉#,#代表注释

LoadModule rewrite_module modules/mod_rewrite.so
LoadModule headers_module modules/mod_headers.so

允许.htaccess文件中的指令覆盖主配置文件中的设置。
允许所有主机访问。

《注意》apache的配置文件httpd.conf中,httpd-slim.conf要正确引用,否则Apache服务器会返回Not found 404错误

《注意》路径一定要包括public,否则Apache服务器会返回Forbidden 403错误

Alias "/slim" "C:/xamapp/htdocs/[my-app-name]/public"
<Directory "C:/xamapp/htdocs/[my-app-name]/public">
    Options -Indexes +FollowSymLinks
    AllowOverride All
    Require all granted
    DirectoryIndex index.php
</Directory>

 

Step 3: Install Composer

 下载 Composer并安装。
更改为阿里源。

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

 

Step 4: Install Slim

composer require --with-all-dependencies slim/slim:"4.*" slim/psr7 selective/basepath


创建Slim项目。

Composer create-project slim/slim-skeleton [my-app-name]

 

Step 5: Create .htaccess File

File: public/.htaccess

复制代码
Options -Indexes
IndexIgnore *

<Files ~ "\.(env|json|config.js|md|gitignore|gitattributes|lock|log)$">
    Order allow,deny
    Deny from all
</Files>

<IfModule mod_headers.c>
    Header set X-Robots-Tag "noindex,nofollow,noarchive,nosnippet"
</ifModule>

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ $1 [R=200,L]

    RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</ifModule>
复制代码

 

Step 6: Hello World

File: public/index.php

复制代码
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

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

$app = AppFactory::create();

$app->get('/', function (Request $request, Response $response, $args) {
    $response->getBody()->write("Hello world!");
    return $response;
});

$app->run();
复制代码

Step 7: Start Slim Application

systemctl restart apache2
php -S localhost:8080 -t public public/index.php

 

点击右上角即可分享
微信分享提示