标题 2019-07-01
简介
Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。
它的主要意义是让所有开发人员都使用和线上服务器一样的环境,本质上和你新建一个虚拟机
安装
常用命令
vagrant init # 初始化,生成Vagrantfile
vagrant up # 启动虚拟机
vagrant halt # 关闭虚拟机
vagrant reload # 重启虚拟机
vagrant ssh # SSH 至虚拟机
vagrant suspend # 挂起虚拟机
vagrant resume # 唤醒虚拟机
vagrant status # 查看虚拟机运行状态
vagrant destroy # 销毁当前虚拟机
vagrant box list # 查看本地box列表
vagrant box add # 添加box到列表
vagrant box remove # 从box列表移除
vagrant provision
vagrant reload --provision
homestead
站在巨人的肩膀上,快速部署PHP开发环境
homestead是laravel官方的一个php开发环境 (本教程采用homestead box6.1.0)
内置:
Ubuntu 18.04
Git
PHP 7.2
PHP 7.1
PHP 7.0
PHP 5.6
Nginx
Apache (可选。本教程不安装)
MySQL
MariaDB (可选。本教程不安装)
Sqlite3
PostgreSQL
Composer
Node (With Yarn, Bower, Grunt, and Gulp)
Redis
Memcached
Beanstalkd
Mailhog
Neo4j (可选。本教程不安装)
MongoDB (可选。本教程不安装)
Elasticsearch (可选。本教程不安装,以免过大)
ngrok
wp-cli
Zend Z-Ray
Go
Minio
下载box(homestead)
box镜像
{
"name": "laravel/homestead", "versions": [ { "version": "6.1.0", "providers": [ { "name": "virtualbox", "url": "X:/path/homestead-6.1.0.box" } ] } ]
}
vagrant add box metadata.json
or
vagrant add box laravel/homestead X:/path/homestead-6.1.0.box --box-version=6.1.0
vagrant box list
vagrant init laravel/homestead --box-version=6.1.0
调整后的Vagrantfile配置
ENV['VAGRANT_DEFAULT_PROVIDER'] = "virtualbox"
Vagrant.configure("2") do |config|
# 虚拟机名
# config.vm.define = "sxx_v1"
# 镜像名
config.vm.box = "laravel/homestead"
# 镜像版本要求
config.vm.box_version = "6.1.0"
# config.vm.box_version = ">= 6.1.0"
# 服务名 这里注意不能用下划线"_"
config.vm.hostname = "sxx-dev-server"
# 虚拟机的私有网络ip
config.vm.network "private_network", ip: "192.168.10.10"
# 共享同步的数据目录
config.vm.synced_folder "D:/qkl/sxx/docker", "/home/share", create:false
config.vm.synced_folder "D:/qkl/sxx/sxx_admin3", "/web/www/sxx_admin3", create:false
config.vm.synced_folder "D:/qkl/sxx/vagrant/nginx/conf", "/etc/nginx/sites-custom/", create:false
# 端口转发
# guest->虚拟机
# host->宿主
config.vm.network "forwarded_port", guest: 22, host: 2222
config.vm.network "forwarded_port", guest: 80, host: 8000
# VirtualBox 虚拟机配置:
config.vm.provider "virtualbox" do |vb|
vb.gui = false vb.name = "sxx_v1" vb.memory = "2048" vb.cpus = "2"
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
#config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
#SHELL
end
启动
vagrant up
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'laravel/homestead' is up to date...
==> 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: 80 (guest) => 8000 (host) (adapter 1) 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: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
/sbin/ifdown 'eth1' || true
/sbin/ip addr flush dev 'eth1'
sed -e '/^#VAGRANT-BEGIN/,$ d' /etc/network/interfaces > /tmp/vagrant-network-interfaces.pre
sed -ne '/^#VAGRANT-END/,$ p' /etc/network/interfaces | tac | sed -e '/^#VAGRANT-END/,$ d' | tac > /tmp/vagrant-network-interfaces.post
cat \
/tmp/vagrant-network-interfaces.pre \
/tmp/vagrant-network-entry \
/tmp/vagrant-network-interfaces.post \
rm -f /tmp/vagrant-network-interfaces.pre
rm -f /tmp/vagrant-network-entry
rm -f /tmp/vagrant-network-interfaces.post
/sbin/ifup 'eth1'
Stdout from the command:
Stderr from the command:
bash: line 4: /sbin/ifdown: No such file or directory
bash: line 20: /sbin/ifup: No such file or directory
解决错误
ssh -p 2222 vagrant@ip
or
直接打开virtualbox的虚拟机进入
sudo apt-get install -y ifupdown
vagrant reload
初始化工作
su passwd root
sudo apt-get install -y php7.2-dev
sudo apt-get install -y php7.1-dev
sudo apt-get install -y php7.0-dev
sudo apt-get install -y php5.6-dev
update-alternavtives --config php
安装扩展
redis
mongodb
rdkafka(安装前先安装librdkafka-https://github.com/edenhill/l...)
zookeeper(安装前线安装zookeeker的C库支持)
建议后续安装的扩展
swoole
查看常用服务
clipboard.png
clipboard.png
打包vagrant box
vagrant package --output sxx_v1
分享和使用
分享本教程的vagrantbox和部分配置
链接: https://pan.baidu.com/s/1vkU5... 密码: 6666
链接: https://pan.baidu.com/s/1z7Mf... 密码: 8888
链接: https://pan.baidu.com/s/1qZ29... 密码: 9999
总结
vagrant 本质还是虚拟机,不过是通过vagrant快速部署和管理虚拟机
本教程只是演示了一个配置配置一台虚拟机,vagrant通过vagrantfile快速部署分布式虚拟机
这个不是本教程的目的,具体更多你可以翻阅官方或百度搜索相关教程自行学习