Vagrant 入門及基於 Vagrant 的 LAMP 開發環境搭建

Vagrant

1. Vagrant 介紹

相關資源php

下面是節選自官方對 Vagrant 的說明:html

WHY VAGRANT?mysql

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.nginx

To achieve its magic, Vagrant stands on the shoulders of giants. Machines are provisioned on top of VirtualBox, VMware, AWS, or any other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can be used to automatically install and configure software on the machine.git

其實,說白了 Vagrant 就是一個普普統統的裝了 Linux 的 VirtualBox 虛擬機,配以 Vagrant 團隊爲之開發的一系列套件,輔助完成諸如安裝初始化、文件同步、ssh、部署環境升級、功能插件安裝等等一些列問題的開發環境部署套件。github

參見知乎話題:Vagrant 和 Docker的使用場景和區別?sql

解決的痛點:shell

  • 開發環境快速部署
  • 開發環境更迭

2. 安裝 Vagrant 和 VirtualBox

官方說明中,Vagrant 是支持 VirtualBox/VMware/AWS 等虛擬軟件的,選擇 VirtualBox 主要是由於開源、免費、輕便。apache

具體的安裝過程沒有什麼特殊設置,一路下一步便可。ubuntu

2.1. 安裝 VirtualBox(支持 Windows/macOS/Linux)

2.2. 安裝 Vagrant(支持 Windows/macOS/Debian/CentOS)

3. 配置、啓動 Vagrant

3.1. 增長一個 box

# 方式一:使用 box 的絕對地址
$ vagrant box add ubuntu1404 https://github.com/kraksoft/vagrant-box-ubuntu/releases/download/14.04/ubuntu-14.04-amd64.box

# 方式二:使用下載好的本地 box 文件( → 推薦此種方式,可從第三方倉庫下載)
$ vagrant box add ubuntu1404 ./ubuntu-14.04-amd64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'ubuntu1404' (v0) for provider:
    box: Unpacking necessary files from: file:///Users/用戶名/vagrant/boxs/ubuntu-14.04-amd64.box
==> box: Successfully added box 'ubuntu1404' (v0) for 'virtualbox'!

# 方式三:使用 Vagrant 官方倉庫中對應的 box 名稱
# 此種方式沒法修改 box 名稱,而且某些網絡下訪問緩慢
$ vagrant box add ubuntu/trusty64
# 查看已經添加的 box
$ vagrant box list
ubuntu1404 (virtualbox, 0)

3.2. 初始化、啓動

# 建立一個工做目錄
$ mkdir -p ~/vagrant/lamp
$ cd ~/vagrant/lamp

# 以名字爲 ubuntu1404 的 box 初始化 Vagrant
$ vagrant init ubuntu1404

A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

$ ls -la
total 8
drwxr-xr-x  3 用戶名  staff   102  3  2 21:13 .
drwxr-xr-x  5 用戶名  staff   170  3  2 18:47 ..
-rw-r--r--  1 用戶名  staff  3011  3  2 21:13 Vagrantfile

# 啓動 Vagrant
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu1404'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: lamp_default_1488461535325_84495
==> default: Fixed port collision for 22 => 2222. Now on port 2201.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2201 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2201
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Remote connection disconnect. Retrying...
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> 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.
==> default: Mounting shared folders...
    default: /vagrant => /Users/用戶名/myvagrant/lamp
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 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'


# 查看當前目錄下(Vagrant配置文件)對應的虛擬機的運行狀態
$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

完成上面的步驟,其實一個由 Vagrant 管理的虛擬機已經啓動起來了。

此時,若是咱們打開 VirtualBox 軟件,在左側的列表咱們能夠看到一個被新添加、而且正在運行狀態的虛擬機。因此,並不須要打開 VirtualBox 軟件,所有由 Vagrant 在命令行進行管理會更方便,參見附錄部分:5.1. 經常使用命令

注意:雖然 Vagrant 已經啓動運行了,可是在啓動過程報錯:mount: unknown filesystem type 'vboxsf' 這主要是下載的 box 裏面 VirtualBox 擴展有問題,須要從新處理一下,詳見附錄部分:5.3. 解決 mount: unknown filesystem type 'vboxsf'

3.3. ssh 到虛擬機

$ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
vagrant@vagrant-ubuntu-trusty:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

4. 基於 Vagrant 的 LAMP 開發環境搭建

上一步已經啓動了一個 Linux 的虛擬機,以後的環境搭建,咱們就按照正常服務器搭建的流程來操做便可,這裏主要介紹兩種搭建 LAMP 環境的方式:

  • 方式一:使用一鍵LAMP&LNMP編譯安裝包點擊這裏下載

  • 方式二:使用 ubuntu 的 apt-get 直接安裝二進制包,以下:

    # 安裝 MySQL
    	$ sudo apt-get install mysql-server mysql-client
    
    	# 安裝 Apache
    	$ sudo apt-get install apache2
    
    	# 安裝 PHP
    	$ sudo add-apt-repository ppa:ondrej/php
    	$ sudo apt-get update
    	$ sudo apt-get install php5.6
    
    	# 安裝 PHP 擴展
    	$ sudo apt-get install libapache2-mod-php5.6 php5.6-mysql php5.6-gd php5.6-curl php5.6-dev php5.6-xml php5.6-mbstring
    
    	-----------------------------------
    	# 測試
    	$ sudo vim /var/www/html/info.php
    	<?php phpinfo();
    
    	$ curl localhost 或從宿主機訪問

5. 附錄

5.1. 經常使用命令

# 添加 box
$ vagrant box add new-box-name box文件地址(本地、遠程)

# 刪除 box
$ vagrant box remove box-name

# 以指定的 box 初始化
$ vagrant init new-box-name

# 啓動
$ vagrant up

# 關閉
$ vagrant halt

# 重啓,從新加載配置文件
$ vagrant reload

# 掛起
$ vagrant suspend

# 關閉、刪除 Vagrant 建立的虛擬機資源
$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...

# 打包
$ vagrant package --output 自定義的包名.box

# 更多詳細說明
$ vagrant -h
$ vagrant 命令名 -h

5.2. Vagrantfile 經常使用配置

# 虛擬機的 hostname
config.vm.hostname = "ubuntu1404-lamp」

# 網絡設置,通常設置私有(private_network)網絡,並結合端口映射
config.vm.network "private_network", ip: "192.168.47.10"
config.vm.network "forwarded_port", guest: 80, host: 8080

# 共享目錄
# 注意:這裏的 owner 和 group,與你搭建的 LAMP 環境運行用戶一致(在 phpinfo() 頁面中搜索 「user」)
config.vm.synced_folder "/Users/用戶名/www", "/var/www/html", create:true, owner:"www-data", group:"www-data"

# VirtualBox 虛擬機配置(內存、CPU、顯示名稱等)
config.vm.provider "virtualbox" do |vb|
#   # Display the VirtualBox GUI when booting the machine
#   vb.gui = true
#
#   # Customize the amount of memory on the VM:
    vb.memory = "1024"
    vb.name = "ubuntu1404-lamp」
end

5.3. 解決 mount: unknown filesystem type 'vboxsf'

  • 關閉虛擬機 $ vagrant halt

  • 使用 VirtualBox 啓動虛擬機

  • 添加虛擬機加強工具:依次單擊菜單【設備】→【安裝加強功能】

  • 在 VirtualBox 中登陸虛擬機,並執行如下命令掛載、安裝

    $ sudo mount /dev/cdrom /media/cdrom
    $ cd /media/cdrom/
    $ sudo ./VBoxLinuxAddtions.run
  • 安裝成功後,從新啓動 $ vagrant up

5.4. 關閉靜態文件緩存

使用 Apache/Nginx 時會出現諸如圖片修改後但頁面刷新仍然是舊文件的狀況,是因爲靜態文件緩存形成的。須要對虛擬機裏的 Apache/Nginx 配置文件進行修改:

# Apache 配置(httpd.conf 或者 apache.conf)添加:
EnableSendfile off

# Nginx 配置(nginx.conf)添加:
sendfile off;

具體請訪問:https://github.com/whorusq/learning-vagrant

相關文章
相關標籤/搜索