天天看點

mac vagrant 建立 lnmp 開發環境

centos7 官方鏡像 https://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1707_01.VirtualBox.box

建議使用迅雷下載下傳,浏覽器下載下傳可能太慢。

實操筆記

mac homebrew 安裝 vagrant

$ brew cask install vagrant
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 4189, done.
remote: Counting objects: 100% (4189/4189), done.
remote: Compressing objects: 100% (4167/4167), done.
remote: Total 4189 (delta 26), reused 654 (delta 19), pack-reused 0
Receiving objects: 100% (4189/4189), 1.33 MiB | 975.00 KiB/s, done.
Resolving deltas: 100% (26/26), done.
Checking connectivity... done.
Tapped 1 command and 4092 casks (4,197 files, 4.3MB).
==> Satisfying dependencies
==> Downloading https://releases.hashicorp.com/vagrant/2.2.2/vagrant_2.2.2_x86_64.dmg
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'vagrant'.
==> Installing Cask vagrant
==> Running installer for vagrant; your password may be necessary.
==> Package installers may write to any location; options such as --appdir are ignored.
Password:
installer: Package name is Vagrant
installer: Installing at base path /
installer: The install was successful.
vagrant was successfully installed!           

把下載下傳好的box鏡像安裝到本地

$ vagrant box add lnmp ~/Downloads/CentOS-7-x86_64-Vagrant-1707_01.VirtualBox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'lnmp' (v0) for provider:
    box: Unpacking necessary files from: file:///Users/zhoumengkang/Downloads/CentOS-7-x86_64-Vagrant-1707_01.VirtualBox.box
==> box: Successfully added box 'lnmp' (v0) for 'virtualbox'!           

檢視已有的 vagrant box 清單

$ vagrant box list
lnmp (virtualbox, 0)           

初始化該 box

$ vagrant init lnmp
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.           

我們執行

vagrant init <本地 box 名稱>

會在目前目錄生成 Vagrantfile 檔案

mac vagrant 建立 lnmp 開發環境

啟動虛拟機

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'lnmp'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant-lnmp_default_1546650046283_10632
==> default: Vagrant has detected a configuration issue which exposes a
==> default: vulnerability with the installed version of VirtualBox. The
==> default: current guest is configured to use an E1000 NIC type for a
==> default: network adapter which is vulnerable in this version of VirtualBox.
==> default: Ensure the guest is trusted to use this configuration or update
==> default: the NIC type using one of the methods below:
==> default:
==> default:   https://www.vagrantup.com/docs/virtualbox/configuration.html#default-nic-type
==> default:   https://www.vagrantup.com/docs/virtualbox/networking.html#virtualbox-nic-type
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (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:2222
    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: Rsyncing folder: /Users/zhoumengkang/vagrant-lnmp/ => /vagrant           

登入虛拟機

$ vagrant ssh
Last login: Sat Jan  5 01:03:42 2019 from 10.0.2.2
[vagrant@bogon ~]$           

修改虛拟機配置 Vagrantfile

我們修改兩項即可,端口映射和目錄同步,友善開發。

config.vm.network "forwarded_port", guest: 8080, host: 8080           
config.vm.synced_folder  "/Users/zhoumengkang/PhpstormProjects/yq-lib", "/home/vagrant/yq-lib"           

這樣主控端的

/Users/zhoumengkang/PhpstormProjects/yq-lib

就和虛拟機的

/home/vagrant/yq-lib

可以同步了。

目錄同步可能遇到的問題,解決辦法參考 https://mengkang.net/1315.html
mac vagrant 建立 lnmp 開發環境

虛拟機重新開機後

vagrant halt && vagrant up           

檢視目錄同步是否正常

$ vagrant ssh
Last login: Sat Jan  5 02:33:05 2019 from 10.0.2.2
[vagrant@localhost ~]$ ls
yq-lib
[vagrant@localhost ~]$ cd yq-lib/
[vagrant@localhost yq-lib]$ ls
boot.php  composer.json  composer.lock  public  vendor  Yq           

安裝 lnmp 環境

我比較習慣lnmp.org一鍵安裝包,是以參考官網的做法

yum install wget -y
wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5
sudo ./install.sh lnmp           

問題1 虛拟機記憶體不夠

You will Install MySQL 5.6.40
Memory less than 1GB, can't install MySQL 5.6+ or MairaDB 10+!           

修改 Vagrantfile 調整記憶體

config.vm.provider "virtualbox" do |vb|
  vb.memory = 2048
end           

問題2 目錄沒有權限 nginx 403

用lnmp工具設定web 目錄為

/home/vagrant/yq-lib/public

,把

vagrant

設定為755

打包分享

進入我們生成

Vagrantfile

目錄裡,執行

vagrant package

打包

mac vagrant 建立 lnmp 開發環境

不過最後生成的 box 檔案怎麼這麼大 3.8G