团队一致性的PHP开发环境之Vagrant

Vagrant

简介

Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。
它的主要意义是让所有开发人员都使用和线上服务器一样的环境,本质上和你新建一个虚拟机

安装

# https://www.vagrantup.com/downloads.html
# vagrant 下载, 自行安装

# https://www.virtualbox.org/wiki/Downloads
# virtualbox 下载, 自行安装

常用命令

vagrant init      # 初始化,生成Vagrantfile

vagrant up        # 启动虚拟机
vagrant halt      # 关闭虚拟机
vagrant reload    # 重启虚拟机
vagrant ssh       # SSH 至虚拟机
vagrant suspend   # 挂起虚拟机
vagrant resume    # 唤醒虚拟机
vagrant status    # 查看虚拟机运行状态
vagrant destroy   # 销毁当前虚拟机


#box管理命令
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)

# wget https://vagrantcloud.com/laravel/boxes/homestead/versions/6.1.0/providers/virtualbox.box
# 网盘:

box镜像

# metadata.json 
{
    "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
# laravel/homestead (virtualbox, 6.1.0)

vagrant init laravel/homestead --box-version=6.1.0
# 生成了Vagrantfile文件

调整后的Vagrantfile配置

# -*- mode: ruby -*-
# vi: set ft=ruby :

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

# output
==> 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'
# Remove any previous network modifications from the interfaces file
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 \
  > /etc/network/interfaces

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

# 这提示出错了,应该是官方的打包没安装ifupdown时候的问题

解决错误

# 这里的ip,我们上面设置为192.168.10.10
ssh -p 2222 vagrant@ip
or
直接打开virtualbox的虚拟机进入
# 输入密码
sudo apt-get install -y ifupdown

# 重新启动
vagrant reload

# output

初始化工作

# 重置/设置root密码
su passwd root
# 输入新密码 这里我是Vagrant123

# 解决homestead6.1.0里没有phpize的问题 安装过程会提示操作,默认选择即可
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

# 如果需要更换linux下默认版本
# ubunut
update-alternavtives --config php
# 选择自己需要的版本前的序列即可
# nginx配置里可以直接指定版本,具体查看分享的nginx配置即可

安装扩展

# 这我就不演示怎么安装了基本都是phpize -> configure -> make && make install
redis
mongodb
rdkafka(安装前先安装librdkafka-https://github.com/edenhill/librdkafka)
zookeeper(安装前线安装zookeeker的C库支持)

建议后续安装的扩展

swoole
# https://wiki.swoole.com/wiki/index/prid-1

查看常用服务

团队一致性的PHP开发环境之Vagrant
团队一致性的PHP开发环境之Vagrant

打包vagrant box

vagrant package --output sxx_v1

# output 项目下生成了sxx_v1

分享和使用

1. 安装vagrant
2. 安装virtualbox
3. copy 分享的box
4. 添加box: vagrant add box qklin/sxx_v1 box_file -box-version=6.1.0 或者上面的metadata.json添加
5. 设置复制一份Vagrantfile,调整成自己所需的共享目录
6. 添加nginx的虚拟主机配置
7. vagrant up
8. 如果nginx有问题,你可以ssh到虚拟机,然后重启下nginx,因为共享目录可能迟于nginx,所以nginx启动除了问题,重启nginx即可(/usr/sbin/nginx -s reload)

分享本教程的vagrantbox和部分配置

# sxx_v1 box 
链接: https://pan.baidu.com/s/1vkU5PTt856Ru7eCiqU3KpQ 密码: 6666
# sxx_v1配置(nginx, vagrantfile等)
链接: https://pan.baidu.com/s/1z7MfT2pIYMGqq_tBwmfDNA 密码: 8888
# homestead 官方的7.14.2配置(仅做参考,可以快速增加其他服务,参考scripts里的脚本)
链接: https://pan.baidu.com/s/1qZ29r3w41PZ2tLDguzULAw 密码: 9999

总结

vagrant 本质还是虚拟机,不过是通过vagrant快速部署和管理虚拟机
本教程只是演示了一个配置配置一台虚拟机,vagrant通过vagrantfile快速部署分布式虚拟机

这个不是本教程的目的,具体更多你可以翻阅官方或百度搜索相关教程自行学习

相关推荐