如何在CloudPanel中安装npm及部署Laravel 10应用?
在CloudPanel部署Laravel 10应用的完整指南
解决npm安装权限问题
CloudPanel创建的普通用户默认没有sudo执行权限,必须先切换到root用户操作:
su - root
输入服务器root密码(可通过DigitalOcean控制台重置)。
安装Node.js与npm
推荐用nvm安装(支持多版本切换,适配Laravel版本需求):
- 安装nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
- 加载bash配置:
source ~/.bashrc
- 安装LTS版本Node.js(Laravel 10推荐18.x/20.x):
nvm install --lts
- 验证安装:
node -v && npm -v
若偏好apt安装:
apt update && apt install -y nodejs npm
完成Laravel项目部署
切换回你的应用用户(如myuser):
su - myuser
进入项目根目录(CloudPanel站点默认路径:/home/myuser/htdocs/your-domain.com):
cd /home/myuser/htdocs/your-domain.com
项目配置步骤
- 安装PHP依赖:
composer install --optimize-autoloader --no-dev
- 配置环境变量:
cp .env.example .env
编辑.env文件,设置APP_ENV=production、APP_DEBUG=false、APP_URL、数据库连接信息。
- 生成应用密钥:
php artisan key:generate --force
- 编译前端资源:
npm install npm run build
- 设置目录权限:
chown -R myuser:www-data storage bootstrap/cache chmod -R 775 storage bootstrap/cache
- 运行数据库迁移(如有需要):
php artisan migrate --force
CloudPanel后台配置
- 进入站点管理,将Document Root设置为
public(Laravel的Web入口目录) - 启用HTTPS,申请Let's Encrypt证书
- 开启OPcache优化PHP性能
可选优化
队列处理器
若项目使用Laravel队列,创建systemd服务确保后台运行:
# /etc/systemd/system/laravel-queue.service [Unit] Description=Laravel Queue Worker After=network.target [Service] User=myuser Group=www-data Restart=always ExecStart=/usr/bin/php /home/myuser/htdocs/your-domain.com/artisan queue:work --daemon --tries=3 [Install] WantedBy=multi-user.target
然后启动服务:
systemctl daemon-reload systemctl enable laravel-queue systemctl start laravel-queue
定时任务
配置Laravel Scheduler:
crontab -e
添加:
* * * * * cd /home/myuser/htdocs/your-domain.com && php artisan schedule:run >> /dev/null 2>&1
内容的提问来源于stack exchange,提问作者luisfer
相关产品推荐
相关产品推荐

