Laravel
- OpenLiteSpeed 已安装 PHP 8.2+(参见 安装 PHP)
- 所需 PHP 扩展:
mbstring、xml、bcmath、curl、zip、tokenizer、json、openssl - 已安装 Composer
- MySQL、MariaDB、PostgreSQL 或 SQLite
安装 Laravel
Section titled “安装 Laravel”cd /var/wwwcomposer create-project laravel/laravel example.comcd example.com
# 设置权限chown -R nobody:nobody storage bootstrap/cachechmod -R 775 storage bootstrap/cache配置 .env 文件
Section titled “配置 .env 文件”cp .env.example .envphp artisan key:generate编辑 .env:
APP_NAME="My App"APP_ENV=productionAPP_DEBUG=falseAPP_URL=https://example.com
DB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306DB_DATABASE=laravelDB_USERNAME=laraveluserDB_PASSWORD=strong_password_here
CACHE_DRIVER=redisSESSION_DRIVER=redisQUEUE_CONNECTION=redis虚拟主机配置
Section titled “虚拟主机配置”Laravel 的入口文件是 public/index.php。OLS 的文档根目录必须指向 public/ 目录:
virtualhost example { vhRoot /var/www/example.com configFile conf/vhosts/example/vhconf.conf allowSymbolLink 1 enableScript 1 restrained 1
docRoot $VH_ROOT/public/
index { indexFiles index.php, index.html }
scripthandler { add lsapi:lsphp php }
rewrite { enable 1 autoLoadHtaccess 1 }}Laravel 需要将所有请求路由到 index.php。
方式 A:OLS 原生重写(在 vhconf.conf 中)
Section titled “方式 A:OLS 原生重写(在 vhconf.conf 中)”rewrite { enable 1 rules <<<END_rules RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] END_rules}方式 B:.htaccess(使用 LiteHTTPD 模块)
Section titled “方式 B:.htaccess(使用 LiteHTTPD 模块)”Laravel 自带 public/.htaccess 文件,可与 LiteHTTPD 模块配合使用:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L]</IfModule>以及 public/.htaccess 中:
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L]</IfModule>静态资源上下文
Section titled “静态资源上下文”直接提供静态资源,无需 PHP 处理:
context /build/ { location $VH_ROOT/public/build/ allowBrowse 1
extraHeaders { set Cache-Control "public, max-age=31536000, immutable" }}
context /storage/ { location $VH_ROOT/storage/app/public/ allowBrowse 1}创建存储符号链接:
php artisan storage:linkLaravel PHP 设置
Section titled “Laravel PHP 设置”memory_limit = 256Mmax_execution_time = 300upload_max_filesize = 64Mpost_max_size = 64M
; OPcacheopcache.enable = 1opcache.memory_consumption = 256opcache.max_accelerated_files = 20000opcache.validate_timestamps = 0使用 LiteHTTPD 模块,还可以在 .htaccess 中设置按目录覆盖:
php_value memory_limit 512Mphp_value max_execution_time 600生产环境优化
Section titled “生产环境优化”# 缓存配置php artisan config:cache
# 缓存路由php artisan route:cache
# 缓存视图php artisan view:cache
# 优化自动加载composer install --optimize-autoloader --no-dev队列工作进程
Section titled “队列工作进程”对于 Laravel 队列,使用 systemd 管理工作进程:
[Unit]Description=Laravel Queue WorkerAfter=network.target
[Service]User=nobodyGroup=nobodyWorkingDirectory=/var/www/example.comExecStart=/usr/local/lsws/lsphp84/bin/php artisan queue:work redis --sleep=3 --tries=3 --max-time=3600Restart=alwaysRestartSec=5
[Install]WantedBy=multi-user.targetsystemctl enable --now laravel-worker添加到 nobody 用户的 crontab:
* * * * * cd /var/www/example.com && /usr/local/lsws/lsphp84/bin/php artisan schedule:run >> /dev/null 2>&1500 内部服务器错误:
- 检查
storage/logs/laravel.log - 确认
storage/和bootstrap/cache/目录对 OLS 用户可写 - 如果配置缓存过期,运行
php artisan config:clear
所有路由返回 404:
- 确认重写规则已生效(在 vhconf.conf 或 .htaccess 中)
- 确保
docRoot指向public/目录
会话/缓存问题:
- 如果使用 Redis 驱动,确认 Redis 正在运行
- 检查
storage/framework/sessions/和storage/framework/cache/的文件权限