在 Ubuntu 上测试 Laravel 的完整流程
一 环境准备
- 更新系统并安装所需扩展与工具(请选择与项目匹配的 PHP 版本,以下以常见扩展为例):
- sudo apt update && sudo apt install -y php php-cli php-fpm php-mysql php-json php-zip php-gd php-mbstring php-curl php-xml php-bcmath unzip
- 安装 Composer(全局):
- curl -sS https://getcomposer.org/installer | sudo php – --install-dir=/usr/local/bin --filename=composer
- 可选:安装 Laravel 安装器(便于新建项目)
- composer global require laravel/installer
- echo ‘export PATH=$PATH:~/.config/composer/vendor/bin’ >> ~/.bashrc && source ~/.bashrc
- 安装并初始化数据库(以 MySQL 8.0 为例):
- sudo apt install -y mysql-server
- sudo mysql_secure_installation
- mysql -u root -p -e “CREATE DATABASE laravel_test CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;”
- 建议:为当前项目创建专用数据库用户并授予权限,避免使用 root。
二 快速验证与本地服务器
- 新建或进入项目目录后,复制环境文件并生成应用密钥:
- cp .env.example .env
- php artisan key:generate
- 配置 .env 数据库连接(示例):
- DB_CONNECTION=mysql
- DB_HOST=127.0.0.1
- DB_PORT=3306
- DB_DATABASE=laravel_test
- DB_USERNAME=your_db_user
- DB_PASSWORD=your_db_password
- 执行迁移与基础检查:
- php artisan migrate --seed(如需填充测试数据)
- php artisan config:clear && php artisan cache:clear
- 启动内置开发服务器进行冒烟测试:
- php artisan serve --host=0.0.0.0 --port=8000
- 浏览器访问:http://localhost:8000 或 http://服务器IP:8000
三 编写并运行 PHPUnit 测试
- 创建示例测试:
- php artisan make:test ExampleTest
- 编辑文件 tests/Feature/ExampleTest.php(使用 RefreshDatabase 以便在测试中使用迁移后的数据库):
- 运行测试(二选一):
- php artisan test
- ./vendor/bin/phpunit
- 常用筛选:
- ./vendor/bin/phpunit --filter ExampleTest
- 提示:功能/单元/命令测试均位于 tests/ 目录,按需组织与命名。
四 使用 Nginx 或 Apache 进行浏览器测试
- Nginx 站点配置示例(/etc/nginx/sites-available/laravel.test):
-
server {
listen 80;
server_name laravel.test;
root /var/www/laravel/public;
index index.php;
add_header X-Frame-Options “SAMEORIGIN”;
add_header X-XSS-Protection “1; mode=block”;
add_header X-Content-Type-Options “nosniff”;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; # 按实际 PHP 版本调整
}
location ~ /.(?!well-known).* {
deny all;
}
}
-
启用站点并重启服务:
- sudo ln -s /etc/nginx/sites-available/laravel.test /etc/nginx/sites-enabled/
- sudo nginx -t && sudo systemctl reload nginx
- Apache 虚拟主机要点:
- DocumentRoot 指向 /path/to/laravel/public
- 启用 mod_rewrite,并确保 public/.htaccess 生效(AllowOverride All)
- 本地域名解析(/etc/hosts):
- 访问测试:http://laravel.test
五 持续集成与常见问题
- GitHub Actions 最小示例(.github/workflows/php.yml):
- name: PHP CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: laravel_test
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
ports:
- 3306:3306
options: --health-cmd=“mysqladmin ping” --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ‘8.1’
extensions: mbstring, dom, fileinfo, mysql, gd, bcmath, curl, xml, zip
coverage: none
- name: Install dependencies
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: Set env
run: |
echo “DB_CONNECTION=mysql” >> .env
echo “DB_HOST=127.0.0.1” >> .env
echo “DB_PORT=3306” >> .env
echo “DB_DATABASE=laravel_test” >> .env
echo “DB_USERNAME=laravel” >> .env
echo “DB_PASSWORD=secret” >> .env
- name: Run migrations
run: php artisan migrate --force
- name: Run tests
run: ./vendor/bin/phpunit
- 常见问题排查要点:
- 目录权限:确保 storage 与 bootstrap/cache 对 www-data 可写
- sudo chown -R www-data:www-data storage bootstrap/cache
- sudo chmod -R 775 storage bootstrap/cache
- 队列/邮件:本地测试可先使用 sync 驱动(在 .env 中设置 QUEUE_CONNECTION=sync)
- 日志定位:查看 storage/logs/laravel.log、Nginx 错误日志(/var/log/nginx/error.log)、PHP-FPM 日志(/var/log/php8.1-fpm.log 等)以快速定位 5xx/路由/权限等问题