以下是Laravel与Linux服务器的集成方案,涵盖环境配置、部署流程及优化建议:
安装基础软件
php-fpm、php-mysql、php-curl等)。sudo apt update sudo apt install php-fpm php-mysql php-curl php-mbstring php-xml php-zip sudo apt install nginx sudo apt install mysql-server sudo mysql_secure_installation 配置数据库
CREATE DATABASE laravel_db; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES; 上传项目
/var/www/)。配置环境变量
.env.example为.env,修改数据库连接、APP_URL等参数。DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=laravel_user DB_PASSWORD=password 安装依赖与生成密钥
composer install --no-dev --optimize-autoloader php artisan key:generate 配置Web服务器
/etc/nginx/sites-available/default),指向项目public目录。server { listen 80; server_name example.com; root /var/www/laravel/public; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } } sudo systemctl restart nginx 设置权限
storage和bootstrap/cache目录可写:sudo chown -R www-data:www-data /var/www/laravel sudo chmod -R 755 /var/www/laravel/storage /var/www/laravel/bootstrap/cache 启用缓存
php artisan config:cache php artisan route:cache 配置队列(可选)
.env:QUEUE_CONNECTION=redis sudo apt install redis-server sudo systemctl start redis-server HTTPS支持(可选)
http://服务器IP或域名,确认应用正常运行。参考来源: