资讯专栏INFORMATION COLUMN

如何在 Windows10 环境下部署配置一键启动的 Homestead + Laravel +vu

Lavender / 2021人阅读

摘要:如何在环境下部署配置一键启动的项目我在内要启动写的项目,我使用的是去运行和管理项目。所以运行会报没有权限的错误。不过有问题每次启动的时候都要到虚拟机里面启动还是很麻烦还好有相关的解决方案,在目录下有个的脚本就是用来启动后执行相关操作的。

如何在 windows10 环境下部署配置一键启动的 Homestead + Laravel +vue + PM2 项目

我在homestead 内要启动node写的项目,我使用的是pm2 去运行和管理node 项目。使用pm2 的好处是方便调试和部署项目。不过homestead 里面并没有内置 pm2 功能,我只能自己安装.

进入homestead 虚拟机(用 vagrant ssh 或者 使用shell工具 我推荐使用 finalshell 是一个很强大的免费工具)

安装pm2

npm install pm2@latest -g

这个时候会有个报错

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/pm2
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/pm2/node_modules
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules/pm2
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access "/usr/local/lib/node_modules/pm2"
npm ERR!  { [Error: EACCES: permission denied, access "/usr/local/lib/node_modules/pm2"]
npm ERR!   stack:
npm ERR!    "Error: EACCES: permission denied, access "/usr/local/lib/node_modules/pm2"",
npm ERR!   errno: -13,
npm ERR!   code: "EACCES",
npm ERR!   syscall: "access",
npm ERR!   path: "/usr/local/lib/node_modules/pm2" }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/vagrant/.npm/_logs/2018-08-30T01_48_01_688Z-debug.log

因为homestead 默认登陆的是 vagrant 用户 。所以运行npm install pm2@latest -g 会报没有权限的错误。

要切换到 root 用户 或者 使用

sudo npm install pm2@latest

安装。

我采用的是切换root 用户,但是homestead没有给root设置密码,所以我们要先设置下root 用户的密码

sudo passwd root 

然后用新设置的密码登陆 root 用户,安装PM2.

安装完成后, 初始化pm2

pm2 init 

会生成配置文件 ecosystem.config.js ,设置好项目配置文件.

启动项目

pm2 start ecosystem.config.js

到这时候就可以访问node项目了。

不过有问题每次启动homestead的时候都要到虚拟机里面启动pm2 还是很麻烦还好 homestead 有相关的解决方案,在 homestead 目录下有个 after.sh 的 shell 脚本就是用来启动 homestead 后执行相关 shell 操作的。

我在里面加内容

#!/usr/bin/env bash
# If you would like to do some extra provisioning you may
# add any commands you wish to this file and they will
# be run after the Homestead machine is provisioned.

# 切换到root 用户
su root -c "root"

# 启动微课堂后台前端项目
npm install pm2@latest -g

# 启动微课堂后台前端项目
pm2 start ~/onlineducation.config.js

重启 homestead 会报错

homestead: su: must be run from a terminal
    homestead: npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/pm2
    homestead: npm WARN
    homestead: checkPermissions Missing write access to /usr/local/lib/node_modules/pm2/node_modules
    homestead: npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
    homestead: npm ERR! path /usr/local/lib/node_modules/pm2
    homestead: npm ERR! code EACCES
    homestead: npm ERR! errno
    homestead:  -13
    homestead: npm
    homestead: ERR!
    homestead:  syscall
    homestead:  access
    homestead: npm ERR! Error: EACCES: permission denied, access "/usr/local/lib/node_modules/pm2"
    homestead: npm ERR!
    homestead:   { [Error: EACCES: permission denied, access "/usr/local/lib/node_modules/pm2"]
    homestead: npm ERR!
    homestead:    stack:
    homestead: npm
    homestead: ERR!    "Error: EACCES: permission denied, access "/usr/local/lib/node_modules/pm2"",
    homestead: npm
    homestead:  ERR!
    homestead:    errno: -13,
    homestead: npm ERR!
    homestead:    code: "EACCES",
    homestead: npm ERR!   syscall: "access",
    homestead: npm ERR!   path: "/usr/local/lib/node_modules/pm2" }
    homestead: npm
    homestead: ERR!
    homestead:
    homestead: npm ERR! The operation was rejected by your operating system.
    homestead: npm ERR! It is likely you do not have the permissions to access this file as the current user
    homestead: npm ERR!
    homestead: npm ERR!
    homestead:  If you believe this might be a permissions issue, please double-check the
    homestead: npm ERR! permissions of the file and its containing directories, or try running
    homestead: npm
    homestead: ERR!
    homestead:  the command again as root/Administrator (though this is not recommended).
    homestead: npm
    homestead: ERR! A complete log of this run can be found in:
    homestead: npm ERR!     /home/vagrant/.npm/_logs/2018-08-30T01_35_38_735Z-debug.log
    homestead: [PM2] Spawning PM2 daemon with pm2_home=/home/vagrant/.pm2

su 命令只能在终端运行,看来只能用其他方法切换用户了。

修改 after.sh 脚本 使用 expect改成可远程执行的脚本

#!/usr/bin/env bash
# If you would like to do some extra provisioning you may
# add any commands you wish to this file and they will
# be run after the Homestead machine is provisioned.

# 安装 expect
sudo snap install expect

# 安装 pm2
sudo npm install pm2@latest -g

# 切换到root 用户
expect -c "
spawn su - root
expect ":"
send "root
"
expect ":"
send "cd /home/vagrant
"
expect ":"
send "pm2 start onlineducation.config.js
"
expect ":"
interact
"

如果 expect 不能安装,可以进入虚拟机先安装好,再重启 homestead 就可以了。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29345.html

相关文章

  • 如何在 Windows10 环境下部署配置一键启动的 Homestead + Laravel +vu

    摘要:如何在环境下部署配置一键启动的项目我在内要启动写的项目,我使用的是去运行和管理项目。所以运行会报没有权限的错误。不过有问题每次启动的时候都要到虚拟机里面启动还是很麻烦还好有相关的解决方案,在目录下有个的脚本就是用来启动后执行相关操作的。 如何在 windows10 环境下部署配置一键启动的 Homestead + Laravel +vue + PM2 项目 我在homestead 内要...

    graf 评论0 收藏0
  • 如何将FPGA资源平民化?阿里工程师有了新突破

    摘要:阿里云服务器平台在云端提供统一硬件平台与中间件,可大大降低加速器的开发与部署成本。我们相信,通过即开即用的硬件资源统一的软硬件逻辑开发接口和市场,阿里云能够真正兑现计算资源平民化的承诺。 阿里云ECS的异构计算团队和高性能计算团队一直致力于将计算资源平民化;高性能计算团队在做的E-HPC就是要让所有云上用户都能够瞬间拥有一个小型的超算集群,使得超算不再仅仅是一些超算中心和高校的特权;而...

    liujs 评论0 收藏0
  • Vue.js学习

    摘要:一基础学习模式下图不仅概括了模式,还描述了在中是如何和以及进行交互的。关于这一点我们将在后续反应系统中讨论。父组件通过向下传递数据给子组件,子组件通过给父组件发送消息。这个对象必须是普通对象原生对象,及原型属性会被忽略。 Vue.js 是用于构建交互式的 Web 界面的库。Vue.js 提供了 MVVM 数据绑定和一个可组合的组件系统,具有简单、灵活的 API。 其实和Jquery一样...

    TIGERB 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<