Windows下搭建Linux Web开发环境

简介

Windows下开发如Rails等Web开发环境,主要是方便像前端开发等使用Windows系统的开发
人员可以在Windows参与项目开发,主要实现以下目标:

  • 可以在Windows使用如Sublime Text等工具修改代码
  • 使用Git等工具提交代码
  • 在Linux运行项目代码,在Windows下看实际效果
  • 两个系统跑起来不卡:)

所需软件

安装PowerShell, cmder, choco, babun, VirtualBox, Vagrant

  • PowerShell: Windows自带的cmd命令行工具是比较弱的,微软推出的PowerShell比cmd要强大得多,Win7自带1.0,推荐升级到4.0,安装Windows Remote Management Framework 4.0即可。
  • cmder: Windows下优秀的终端模拟器,功能强大,足够美观,主要是方便复制和粘贴(Shift+Insert)
  • chocolatey: Windows下的包管理工具,类似Linux下的apt-getyum和Mac下的brew。例如可以通过choco install virtualbox这个命令直接安装VirtualBox。这里涉及的软件都可以用chocolatey安装,只不过它会从外国网站直接下载,可能有些慢。
  • babun: 提供类似Linux环境,装完基本的Linux命令都有了,可以在里面跑shell脚本,其本质还是用了cygwin,不过没有前者那么臃肿,而且加了pact命令方便安装额外的包,比如通过pact install tig来安装tig
  • Vagrant: 主要用来管理虚拟机,可以把虚拟机装好打包成box,统一团队里所有开发环境。

安装步骤

PowerShell

从微软官网下载Windows Management Framework 4.0安装,装完重启即可。服务器安装这个特别有用,它有个PowerShell Remoting可以支持远程登录管理服务器,类似Linux下的ssh,但是两者机制不一样。可以实现Windows服务器集群的自动化管理。

可以通过在run里输入powershell或在附件里找到来运行。

Chocolatey

启动PowerShell,然后在里面输入:

Set-ExecutionPolicy AllSigned

iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))  

之后可以输入choco -h来确认是否安装成功。

国内使用会比较慢,推荐设置代理来加快下载速度

cmder

通过choco install cmder或在官网下载安装,推荐完全安装包。

之后在settings -> Startup -> Environment里最下面加入以下设置来开户中文支持:

set LANG=zh_CN.UTF-8  

这个比自带的cmd要好得多,只不过Windows里的路径在命令里有点反人类,熟悉了Linux命令行的人用不习惯。所以安装babun来解决这个问题。

babun

通过chocolatey安装: choco install babun,比较大,最好用迅雷下载安装。

之后就可以

用cmder启动babun

settings -> Startup -> Task里新增一个babun

Task parameters里填入:

/icon "%userprofile%\.babun\cygwin\bin\mintty.exe" /dir "%userprofile%"

Start console里填入:

%userprofile%\.babun\cygwin\bin\mintty.exe -o Transparency=0 /bin/env CHERE_INVOKING=1 /bin/zsh.exe

可以把cmder启动的任务默认设置为刚刚设置的babun

VirtualBox

通过choco install virtualbox安装或官网下载

重启后试试环境变量起使用没有,如何没有的话可以手动添加一下:

 vboxmanage -h

如果没有的话可以把它的安装目录加入~/.zshrc定义的PATH里,把export PATH部分改成类似以下:

export PATH=/cygdrive/d/Program\ Files/Oracle/VirtualBox:$HOME/bin:/usr/local/bin:$PATH  

再通过vboxmanage -h验证一下。

Vagrant

通过choco install vagrant或官网下载安装,重启生效。

添加Box

Box就是人家已经打包好的虚拟机,官网列表上有很多现成的,同样,国外下载的比较慢,最好是同事打包好给你。

# 添加一个基础的开发用的Box,假设已经下载好放在D盘,把它命名为`ubuntu_dev`
vagrant box add ubuntu_dev file:///d:/ubuntu.box  

用Box来初始化工作环境

一个Box可以用在不同的环境下,这里用Rails开发为例

# 新建文件夹
mkdir -p /cygdrive/d/workspace/ubuntu

cd /cygdrive/d/workspace/ubuntu

# 用`ubuntu_dev`这个Box来初始环境,会在这个目录下生成一个`Vagrantfile`,也就是这个虚拟机的配置文件, 其实是ruby文件
vagrant init ubuntu_dev  

修改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 = "ubuntu16"

  # 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.
  # 在本机通过`localhost:8080`来访问虚拟机的`localhost:3000`端口
  config.vm.network "forwarded_port", guest: 3000, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # 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"

  # 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 = tru
     vb.name = "ubuntu_dev"
     # CPU数量
     vb.cpus = 2

     # 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

启动和使用

# 启动虚拟机,必须在`Vagrantfile`所在的文件夹,可以过`vagrant halt`在停止虚拟机
vagrant up

# 1. 这里有可能出现`Authentication Failure`,在`Vagrantfile`里加入以下设置, 然后用密码登录,密码为`vagrant`
config.ssh.insert_key = false

# 2. 进去虚拟机后,运行
echo "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key" > .ssh/authorized_keys

# 3. 现在可以把`config.ssh.insert_key = false`从`Vagrantfile`里去掉,即可正常无密码登录

# 登录虚拟机
vagrant ssh

# 在默认情况下,虚拟机里的`/vagrant`文件夹指向本机的`D:\workspace\ubuntu`,也就是`Vagrantfile`所在的文件夹
# 所以可以在Windows下用你习惯的方式修改`D:\workspace\ubuntu`里的代码,然后在虚拟机里,执行`/vagrant`里的代码

cd /vagrant/demo  
bundle install  
bundle exec rails s  
# 然后在Windows里的浏览器打开`http://localhost:8080`即可看到相应的页面

Guimin Lin

Read more posts by this author.