Virtualbox+Vagrant搭建linux虚拟机并搭建easySwoole框架
Virtualbox+Vagrant搭建linux虚拟机并搭建easySwoole框架
首先是官网下载
VirtualBox
Downloads – Oracle VM VirtualBox
Vagrant
Downloads | Vagrant by HashiCorp
一路next安装
可以在VirtualBox中的管理-》全局设定-》常规中,为虚拟机设置默认存储路径,这样c盘不会爆了
再下载linux系统的镜像,这个网址下载挺快的,可以自行选择对应的版本,我下载的是CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box
Index of /centos-cloud/centos/7/vagrant/x86_64/images/ (ustc.edu.cn)http://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/下载完后,将box文件放到一个自定义文件夹,这边放到了D盘的boxes文件夹中。
然后再新建一个文件夹作为vagrant初始化这个镜像的文件夹,这边新建的是D盘的runbox文件夹。
进行初始化操作时,先通过win+R+cmd进入命令控制行,再cd到runbox文件夹
运行下面的命令将镜像添加到vagrant环境,CentOS7是我修改后的CentOS-7-x86_64-Vagrant-1905_01.VirtualBox.box的名字,centos7是我创建的vagrant环境的命名
vagrant box add centos7 D:/boxes/CentOS7.box
再初始化
vagrant init centos7
如果无法初始化则先打开VirtualBox,再运行下面的命令
vagrant up
运行三个命令的截图
到这里,VirtualBox中就自动创建好了一个虚拟机:
虚拟机运行好后,再打开一个控制命令行,通过ipconfig查看虚拟机运行的网络ip地址:192.168.56.1
在刚刚初始化vagrant的D盘的runbox文件夹中可以看到出现了一个vagrantfile配置文件,可以修改这里的网络配置,使得本机可以与新建的虚拟机通信
具体就是找到config.vm.network "private_network",在下面添加一行:ip就是上面的地址后加个0
config.vm.network "private_network", ip: "192.168.56.10"
这里的vb.cpus和vb.memory是配置虚拟机的cpu内存和存储内存的
设置共享目录
在vagrantfile添加文件目录的挂载,这里将D盘的/work/easySwoole挂载到虚拟机的work文件夹
config.vm.synced_folder "D:/work/easySwoole", "/work"
然后运行reload命令重新加载配置
vagrant reload
本机文件夹与虚拟机的文件夹挂载时,如果重新加载配置出现下面的信息说明挂载失败了:
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:
mount -t vboxsf -o uid=1000,gid=1000,_netdev work /work
The error output from the command was:
mount: unknown filesystem type 'vboxsf'
大致意思是说VirtualBox没有文件挂载的插件需要下载vbguest插件:
vagrant plugin install vagrant-vbguest
vagrant vbguest
如果安装好插件后,还是无法挂载,打印出下面的内容:
==> default: You assigned a static IP ending in ".1" to this machine.
==> default: This is very often used by the router and can cause the
==> default: network to not work properly. If the network doesn't work
==> default: properly, try changing this IP.
==> default: Attempting graceful shutdown of VM...
==> default: You assigned a static IP ending in ".1" to this machine.
==> default: This is very often used by the router and can cause the
==> default: network to not work properly. If the network doesn't work
==> default: properly, try changing this IP.
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
[default] No Virtualbox Guest Additions installation found.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.huaweicloud.com
Package centos-release-7-9.2009.1.el7.centos.x86_64 already installed and latest version
Nothing to do
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bfsu.edu.cn
* extras: mirrors.bfsu.edu.cn
* updates: mirrors.huaweicloud.com
No package kernel-devel-3.10.0-1127.el7.x86_64 available.
Error: Nothing to do
Unmounting Virtualbox Guest Additions ISO from: /mnt
umount: /mnt: not mounted
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
umount /mnt
Stdout from the command:
Stderr from the command:
umount: /mnt: not mounted
可以参考下面的文章,安装低版本的vbguest插件:
插件安装好后再执行vagrant reload命令加载配置文件,然后vagrant up重新启动虚拟机的时候需要等几分钟更新插件
通过宝塔快速搭建php的easySwoole环境
进入宝塔官网复制linux面板的安装脚本
再通过命令ssh进入虚拟机
vagrant ssh
再运行命令sudo su -进入到虚拟机的root,执行刚刚复制的宝塔的安装脚本
虚拟机的宝塔安装好后,在主机中连接虚拟机的宝塔控制面板,地址是上面vagrantfile中配置的虚拟机ip地址192.168.56.10,再加上宝塔给的端口和后缀:8888/af954a35/,用户名和密码是宝塔安装好后会给出username和password
登录
登录后会提示注册,然后再进行LNMP环境的搭建,可以自行选择版本(注:easySwoole版本3.5.x要求php版本>=7.3),然后一键安装即可,这边没有选择ftp暂时用不到
安装swoole
官网有个坑,选择easySwoole版本3.5.x的时候,注意swoole的版本要选择大于等于4.4.7的,不然会报错
tips:宝塔安装的php的位置可以在软件商店中的已安装软件中查看
安装composer
composer默认会通过宝塔安装
更新命令
composer self-update
配置国内镜像
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
安装composer时遇到的问题和解决
(87条消息) putenv() has been disabled for security reasons_loveyoulouyou的博客-CSDN博客
可以参照官网的命令进行easySwoole的框架安装,记得在宝塔中将php禁止的函数删除掉,如readlink(),proc_open() ,symlink() ,putenv() ,否则安装easyswoole会报错
安装成功后启动easyswoole服务
虚拟机启动的easyswoole服务,可以在主机通过虚拟机的ip地址和9501端口访问到,如果访问不到的话,需要在宝塔中对9501的端口进行放行设置
php easyswoole server start
因为这边取消了index.php的路由,在主机访问easyswoole后出现的是这样的界面
可以重新执行安装命令 php easyswoole install
,并且输入 Y
、Y
,再次执行 php easyswoole server start
启动服务,就可以正常访问欢迎页。
到这里在虚拟机中将easySwoole框架算是搭建好了。
做easySwoole的基础基础开发时遇到的报错
1.
PHP Fatal error: Namespace declaration statement has to be the very first statement or after any declare call in the script in xxx/
vendor/bin/easyswoole line 13
修改项目根目录下的 vendor
目录下的 bin
目录下的 easyswoole
文件 (即 vendor/bin/easyswoole
),删除该文件的第一行 #!/usr/bin/env php
),然后重新执行 php easyswoole server start
启动服务即可
2.
[trigger][error]:[Uncaught EasySwoole\Component\Process\Exception: EasySwoole\Bridge\BridgeProcess bind xxx/Temp/bridge.sock fail case Operation not permitted in xxx/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php:32
Stack trace:
#0 xxx/vendor/easyswoole/bridge/src/BridgeProcess.php(24): EasySwoole\Component\Process\Socket\AbstractUnixProcess->run()
#1 xxx/vendor/easyswoole/component/src/Process/AbstractProcess.php(150): EasySwoole\Bridge\BridgeProcess->run()
#2 {main}
thrown at file:xxx/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php line:32]
修改easyswoole框架配置文件中的最后一一个配置项为 'TEMP_DIR' => '/tmp'
3.框架启动的时候的报错
[2022-06-29 15:17:26][trigger][warning]:[unlink(/tmp/EasySwoole.TaskWorker.2.sock): Operation not permitted at file:/work/Test/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php line:27]
[2022-06-29 15:17:26][trigger][error]:[EasySwoole\Task\Worker bind /tmp/EasySwoole.TaskWorker.1.sock fail case Address already in use at file:/work/Test/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php line:32]
PHP Fatal error: Uncaught EasySwoole\Component\Process\Exception: EasySwoole\Bridge\BridgeProcess bind /tmp/bridge.sock fail case Address already in use in /work/Test/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php:32
Stack trace:
#0 /work/Test/vendor/easyswoole/bridge/src/BridgeProcess.php(24): EasySwoole\Component\Process\Socket\AbstractUnixProcess->run()
#1 /work/Test/vendor/easyswoole/component/src/Process/AbstractProcess.php(150): EasySwoole\Bridge\BridgeProcess->run()
#2 {main}
thrown in /work/Test/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php on line 32
Fatal error: Uncaught EasySwoole\Component\Process\Exception: EasySwoole\Bridge\BridgeProcess bind /tmp/bridge.sock fail case Address already in use in /work/Test/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php:32
Stack trace:
#0 /work/Test/vendor/easyswoole/bridge/src/BridgeProcess.php(24): EasySwoole\Component\Process\Socket\AbstractUnixProcess->run()
#1 /work/Test/vendor/easyswoole/component/src/Process/AbstractProcess.php(150): EasySwoole\Bridge\BridgeProcess->run()
#2 {main}
thrown in /work/Test/vendor/easyswoole/component/src/Process/Socket/AbstractUnixProcess.php on line 32
解决方法:需要赋予root权限再启动框架(进入sudo)
更多推荐
所有评论(0)