在 Debian 上测试 Laravel 接口,一般分 环境准备 → 启动服务 → 接口测试 三步。下面给你一套实用、常用方案。
sudo apt update
sudo apt install -y php php-cli php-fpm php-mysql php-mbstring php-xml php-curl unzip git
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer create-project laravel/laravel example-app
cd example-app
php artisan serve --host=0.0.0.0 --port=8000
访问接口:
curl http://localhost:8000/api/test
php -S 0.0.0.0:8000 -t public
# GET
curl http://localhost:8000/api/users
# POST
curl -X POST http://localhost:8000/api/users \
-H "Content-Type: application/json" \
-d '{"name":"test","email":"test@test.com"}'
php artisan make:test ApiTest
示例:
public function test_example()
{
$response = $this->get('/api/test');
$response->assertStatus(200);
}
运行:
php artisan test
http://<debian-ip>:8000/api/xxxsudo snap install postman
sudo apt install nginx
server {
listen 80;
root /var/www/example-app/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
}
}
sudo systemctl restart nginx
| 问题 | 解决 |
|---|---|
| 404 | 检查路由 routes/api.php |
| 500 | 看 storage/logs/laravel.log |
| 权限 | chmod -R 775 storage bootstrap/cache |
| 跨域 | 安装 fruitcake/laravel-cors |
composer install
cp .env.example .env
php artisan key:generate
php artisan serve
curl http://localhost:8000/api/test
如果你愿意,可以告诉我:
我可以直接给你对应命令或配置。