在Debian系统上安装和配置Laravel框架是一个相对直接的过程,但需要确保所有必要的软件包都已正确安装和配置。以下是详细的步骤:
更新系统包:
sudo apt update && sudo apt upgrade -y
安装PHP及其扩展:
sudo apt install php php-cli php-fpm php-json php-common php-mysql php-zip php-gd php-mbstring php-curl php-xml php-pear php-bcmath
安装Composer:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
安装Nginx或Apache(这里以Nginx为例):
sudo apt install nginx
创建新的Laravel项目:
composer create-project --prefer-dist laravel/laravel your_project_name
cd your_project_name
设置文件权限:
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 755 storage bootstrap/cache
配置环境变量:
.env.example
文件并重命名为 .env
:cp .env.example .env
.env
文件,并按照说明设置你的应用程序环境变量,例如数据库连接信息。生成应用密钥:
php artisan key:generate
配置Nginx:
sudo nano /etc/nginx/sites-available/your_project_name
server {
listen 80;
server_name your_domain.com;
root /path/to/your_project_name/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php index.html index.htm;
location / {
try_files uri uri/ /index.php?$query_string;
}
location \.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
}
location /\.ht {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled
sudo systemctl restart nginx
运行数据库迁移:
php artisan migrate
启用PHP OPcache:
php.ini
文件,确保以下配置项已启用:[opcache]
opcache.enable = 1
opcache.enable_cli = 1
opcache.jit = tracing
opcache.jit_buffer_size = 256m
opcache.memory_consumption = 512m
opcache.interned_strings_buffer = 64m
opcache.max_accelerated_files = 10000
opcache.revalidate_freq = 60
opcache.validate_timestamps = 1
opcache.fast_shutdown = 1
sudo systemctl restart php8.2-fpm
路由和配置缓存:
php artisan route:cache
php artisan config:cache
使用高效的缓存驱动:
.env
文件中设置缓存驱动为 redis
:CACHE_DRIVER=redis
SESSION_DRIVER=redis
优化数据库查询:
$users = App\Models\User::with('posts')->get();
DB::select('SELECT * FROM users WHERE age > ?', [18]);
使用队列处理耗时任务:
QUEUE_CONNECTION=redis
php artisan make:job SendEmail
使用Laravel Telescope进行性能监控:
composer require laravel/telescope
php artisan telescope:install
php artisan migrate
AppServiceProvider.php
中注册Telescope:use Laravel\Telescope\Telescope;
use Laravel\Telescope\TelescopeApplicationServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
Telescope::ignoreMigrations();
}
public function boot()
{
Telescope::night();
}
}
通过以上步骤,你应该能够在Debian系统上成功安装、配置和优化Laravel框架。