首先,我们为什么要这样做?
我们用vagrant搭建起来了linux环境,也就是在windows下面虚拟出来的linux环境,但是,可以用如下的几种方式进行编辑:
1.如果我们用vim进行编辑,是非常费劲的,不提倡
2.通过编辑器的ftp直连,就像:
这种方式只能用notepad这种,只在连接的时候加载,而不能用phpstorm这种提前加载到本地,因为这种方式,用phpstorm会造成一定的问题,我的开发环境用的是阿里云主机,随便找个电脑安装个notepad就可以干活了,也就说这种方式比较适合远程。
3. 最通用的方式,就是本地window通过vagrant虚拟出来一个linux,然后,通过映射的方式,将windows下的文件夹映射到vagrant的linux中,然后,我们的编辑器(phpstorm)加载window下的这个文件夹,就可以了,当我们修改window下的这个文件夹,因为是挂载到linux的(有点像u盘的感觉),我们把nginx指向该文件夹,就可以进行开发了(上面说的有点啰嗦,不过意思说明白了),这也就是本文要讲述的方式。
另外需要注意的是:
vagrant虚拟的linux的文件是无法映射到windows中的,只能windows的文件映射到linux中,就像window环境中的某个文件夹挂载到vagrant 的 linux中的感觉,但是不能把linux的文件夹挂载到windows,具体操作如下:
打开Vagrantfile,修改配置内容如下(完全修改):
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| # The most common configuration options are documented and commented below. # For a complete reference, please see the online documentation at # https://docs.vagrantup.com. # Every Vagrant development environment requires a box. You can search for # boxes at https://atlas.hashicorp.com/search. config.vm.box = "centos-6.6-x86_64" config.vm.hostname = "dev" config.ssh.username = "root" config.ssh.password = "123456" config.ssh.insert_key = "true" config.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'" config.ssh.forward_agent = true # Disable automatic box update checking. If you disable this, then # boxes will only be checked for updates when the user runs # `vagrant box outdated`. This is not recommended. # config.vm.box_check_update = false # Create a forwarded port mapping which allows access to a specific port # within the machine from a port on the host machine. In the example below, # accessing "localhost:8080" will access port 80 on the guest machine. config.vm.network "forwarded_port", guest: 80, host: 80 # Create a private network, which allows host-only access to the machine # using a specific IP. config.vm.network "private_network", ip: "192.168.10.12" # Create a public network, which generally matched to bridged network. # Bridged networks make the machine appear as another physical device on # your network. config.vm.network "public_network" # Share an additional folder to the guest VM. The first argument is # the path on the host to the actual folder. The second argument is # the path on the guest to mount the folder. And the optional third # argument is a set of non-required options. # config.vm.synced_folder "../data", "/vagrant_data" config.vm.synced_folder "D:\\linux\\fecshop", "/www/web/develop/fecshop" # Provider-specific configuration so you can fine-tune various # backing providers for Vagrant. These expose provider-specific options. # Example for VirtualBox: # config.vm.provider "virtualbox" do |vb| # Display the VirtualBox GUI when booting the machine # vb.gui = true vb.name = "dev" # Customize the amount of memory on the VM: vb.memory = "2048" end # # View the documentation for the provider you are using for more # information on available options. # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies # such as FTP and Heroku are also available. See the documentation at # https://docs.vagrantup.com/v2/push/atlas.html for more information. # config.push.define "atlas" do |push| # push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME" # end # 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 # apt-get update # apt-get install -y apache2 # SHELL end
在上面的配置中可以看到如下:
config.vm.synced_folder “D:\\linux\\fecshop”, “/www/web/develop/fecshop”
第一个路径是window的路径,第二个是vagrant’中linux的路径
启动,如图:
注意:
1.如果 /www/web/develop/fecshop 这个文件夹在linux中存在,那么启动后,原来的文件夹将看不到。
2. 如果linux存在文件夹/www/web/develop/fecshop,您想把这个文件夹的内容复制到映射后的/www/web/develop/fecshop中,那么,您可以先将/www/web/develop/fecshop 改名为 /www/web/develop/fecshop_cp,然后,添加映射配置,重启(vagrant reload)vagrant,然后通过命令复制过去即可
\cp -rf /www/web/develop/fecshop_cp/* /www/web/develop/fecshop/