團隊一致性的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

查看經常使用服務

打包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快速部署分佈式虛擬機

這個不是本教程的目的,具體更多你能夠翻閱官方或百度搜索相關教程自行學習

原文地址:https://segmentfault.com/a/1190000015984473php

相關文章
相關標籤/搜索