在PHP中,如果你只需要提供API接口,而不需要处理静态文件或复杂的Web页面,可以选择以下几种服务器:
bash
php -S localhost:8000
配置示例: ```nginx server { listen 80; server_name api.example.com;
location / {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
} ```
mod_php
模块可以直接处理PHP脚本。配置示例:
```apache
<Directory /var/www/api>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
```
示例代码: ```php $http = new Swoole\Http\Server("0.0.0.0", 9501);
$http->on("request", function ($request, $response) { $response->header("Content-Type", "application/json"); $response->end(json_encode(["message" => "Hello, World!"])); });
$http->start(); ```
yaml
http:
address: 0.0.0.0:8080
middleware: ["headers"]
bash
composer require laravel/octane
php artisan octane:start
根据你的具体需求和项目规模,选择合适的服务器配置。