vagrant+虚拟机搭建开发环境

来源:互联网 发布:mac 恢复删除文件 编辑:程序博客网 时间:2024/05/19 13:08

环境准备

1.安装VirtualBox

https://www.virtualbox.org/wiki/Downloads

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 codegit clone git@git.lexing360.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,撸吧~

Vagrant介绍
使用 Vagrant 打造跨平台开发环境
laravel homestead