在Debian系统上部署Laravel应用涉及几个步骤,包括安装必要的软件、配置Web服务器、设置数据库等。以下是一个基本的指南:
首先,确保你的Debian系统是最新的:
sudo apt update
sudo apt upgrade -y
Laravel需要PHP 7.3或更高版本。你可以使用以下命令安装PHP及其常用扩展:
sudo apt install -y 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是PHP的依赖管理工具,用于安装Laravel:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
你可以选择安装Nginx或Apache作为Web服务器。以下是安装Nginx的步骤:
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx
创建一个新的Nginx配置文件来托管你的Laravel应用:
sudo nano /etc/nginx/sites-available/yourdomain.com
添加以下内容(根据你的实际情况修改域名和项目路径):
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/public;
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/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
启用配置文件:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
确保Laravel应用的storage
和bootstrap/cache
目录可写:
sudo chown -R www-data:www-data /var/www/yourdomain.com
sudo chmod -R 755 /var/www/yourdomain.com
sudo chmod -R 777 /var/www/yourdomain.com/storage
sudo chmod -R 777 /var/www/yourdomain.com/bootstrap/cache
你可以选择安装MySQL或PostgreSQL。以下是安装MySQL的步骤:
sudo apt install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
登录到MySQL并创建数据库和用户:
sudo mysql -u root -p
在MySQL shell中:
CREATE DATABASE yourdatabase;
CREATE USER 'youruser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON yourdatabase.* TO 'youruser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
进入你的Laravel项目目录并编辑.env
文件:
cd /var/www/yourdomain.com
nano .env
设置以下变量:
APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:your_generated_key
APP_DEBUG=false
APP_URL=http://yourdomain.com
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=yourdatabase
DB_USERNAME=youruser
DB_PASSWORD=yourpassword
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
生成应用密钥:
php artisan key:generate
运行数据库迁移:
php artisan migrate
如果你需要使用Laravel Mix进行前端资源编译,可以安装Node.js和NPM:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
如果你有前端资源需要编译,可以运行:
npm install
npm run production
现在,你的Laravel应用应该已经在Debian系统上成功部署并可以访问了。