You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CloudPanel中安装npm及部署Laravel 10应用?

在CloudPanel部署Laravel 10应用的完整指南

解决npm安装权限问题

CloudPanel创建的普通用户默认没有sudo执行权限,必须先切换到root用户操作:

su - root

输入服务器root密码(可通过DigitalOcean控制台重置)。

安装Node.js与npm

推荐用nvm安装(支持多版本切换,适配Laravel版本需求):

  1. 安装nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  1. 加载bash配置:
source ~/.bashrc
  1. 安装LTS版本Node.js(Laravel 10推荐18.x/20.x):
nvm install --lts
  1. 验证安装:
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

项目配置步骤

  1. 安装PHP依赖:
composer install --optimize-autoloader --no-dev
  1. 配置环境变量:
cp .env.example .env

编辑.env文件,设置APP_ENV=production、APP_DEBUG=false、APP_URL、数据库连接信息。

  1. 生成应用密钥:
php artisan key:generate --force
  1. 编译前端资源:
npm install
npm run build
  1. 设置目录权限:
chown -R myuser:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache
  1. 运行数据库迁移(如有需要):
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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:37:28