1. 程式人生 > >vagrant+虛擬機器搭建開發環境

vagrant+虛擬機器搭建開發環境

環境準備

1.安裝VirtualBox

2.安裝Vagrant

brew install vagrant

使用步驟

假設我們拿到的 box 存放路徑是 ~/box/package.box,在終端裡輸入:

$ vagrant box add lexing ~/box/package.box  # 新增 package.box 映象並命名為 lexing
$ mkdir ~/my_vagrant  && cd ~/my_vagrant  # 切換到專案目錄
$ vagrant init lexing  # 用 lexing 映象初始化。
$ vagrant ssh(登入到虛擬機器,賬號密碼都是vagrant)

預設情況下Vagrant會把你的專案目錄(儲存Vagrantfile的那個)與虛擬機器中的/vagrant進行同步

config.vm.network "private_network", ip: "192.168.33.10"

在Vagrantfile 中將上面這條註釋去掉,192.168.33.10就是虛擬機器的ip

下面舉例php專案搭建

gitlab拉專案,例如lx_pay_platform

cd /vagrant && mkdir code && cd code
git clone [email protected].lexing
360.com:lxpay/lx_pay_platform.git(對應目錄在宿主機器的~/my_vagrant/code/)

配nginx
在/etc/nginx/conf.d/ 新增lx_pay_platform 檔案,內容如下:

server {
    listen 80;
    server_name pay.lexing360.com.test;
    charset utf-8;

    root /vagrant/code/lx_pay_platform/public/;
    location / {
        try_files $uri $uri/ /index.php?$args;
        index index.html
index.htm index.php; } location ~ \.php$ { fastcgi_pass unix:/run/php/php7.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /vagrant/code/lx_pay_platform/public/$fastcgi_script_name; include fastcgi_params; try_files $uri =404; } }

重啟nginx: sudo service nginx reload
在宿主機器新增hosts地址

sudo vim /etc/hosts 
192.168.33.10 pay.lexing360.com.test

下載phpstorm,擼吧~