我在homestead 内要启动node写的项目,我使用的是pm2 去运行和管理node 项目。使用pm2 的好处是方便调试和部署项目。不过homestead 里面并无内置 pm2 功能,我只能本身安装.前端
进入homestead 虚拟机(用 vagrant ssh 或者 使用shell工具 我推荐使用 finalshell 是一个很强大的免费工具)vue
安装pm2node
npm install pm2@latest -g
这个时候会有个报错shell
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 会报没有权限的错误。npm
要切换到 root 用户 或者 使用windows
sudo npm install pm2@latest
安装。bash
我采用的是切换root 用户,可是homestead没有给root设置密码,因此咱们要先设置下root 用户的密码ssh
sudo passwd root
而后用新设置的密码登录 root 用户,安装PM2.工具
安装完成后, 初始化pm2this
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\r\" expect \":\" send \"cd \/home\/vagrant\r\" expect \":\" send \"pm2 start onlineducation.config.js\r\" expect \":\" interact "
若是 expect 不能安装,能够进入虚拟机先安装好,再重启 homestead 就能够了。