vagrant 下载部署linux环境

1. 安装 VirtualBox

虚拟机还是得依靠 VirtualBox 来搭建,免费小巧
下载地址:https://www.virtualbox.org/wiki/Downloads

* 虽然 Vagrant 也支持 VMware,不过 VMware 是收费的,对应的 Vagrant 版本也是收费的。

我下载的是:VirtualBox 5.1.6 for Windows hosts  x86/amd64


2. 下载  Vagrant

下载地址:http://downloads.vagrantup.com/

3.下载contos box,可以来这里下载:http://www.vagrantbox.es/

 

经过上面的下载,我们

下载了virtual box   vagrant   centos 6.6 box  三个文件

4.安装 virtualbox ,  vagrant ,这个基本都是下一步,安装完成后要重启

安装上面的两个成功后,重启后。

4.1

window建+r ,打开命令行,

进入命令行模式,输入vagrant,看看是否安装成功

4.2 添加centos box

进入d盘,添加centos box

进入contos box文件所在的文件夹,我的是在d:\vagrant文件路径下,按照上面截图的命令行进入d盘,在通过cd vagrant 进入相应文件夹。

D:\vagrant\centos-6.6-x86_64.box

按照这个命令添加一个box

vagrant box add 名称 路径

譬如:

D:\vagrant\vagrant box add centos-6.6-x86_64   centos-6.6-x86_64.box

这里将  box 名称:centos-6.6-x86_64  对应 D:\vagrant\vagrant\centos-6.6-x86_64.box这个文件,后面可以使用centos-6.6-x86_64,使用后直接对应上面对应的文件。

通过vagrant box list  查看添加的列表

4.3创建虚拟机:

添加了 Box 以后,我们就可以用 Vagrant 基于这个 Box 去创建虚拟机了。先找个地方去创建一个目录,这个目录就是你的项目所在的目录,它会自动跟虚拟机上的某个目录同步,也就是在你电脑上的这个目录里面的文 件,你同样可以在虚拟机里的某个目录里面找到。比如我的目录在d:\myvagrant,我创建这个目录,然后再进入到这个目录,在命令行工具下面执行:

初始化:

启动 vagrant up命令,第一次会慢一些,因为要复制文件。

在上面,发现了拨错:

Timed out while waiting for the machine to boot. This means that  
    Vagrant was unable to communicate with the guest machine within  
    the configured ("config.vm.boot_timeout" value) time period.  
  
    If you look above, you should be able to see the error(s)  that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong.  
  
    If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly,as well.  
  
    If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.

 

打开文件:D:\myvagrant\Vagrantfile

将Vagrantfile配置文件中vb.gui = true的注释去掉,下面将三行的代码的注释去掉了,也就是前面的  # 号,而不是一行,这里要注意,如果仅仅去掉vb.gui = true的注释,会报错。

config.vm.provider "virtualbox" do |vb|  
#   # Don't boot with headless mode  
    vb.gui = true  
#  
#   # Use VBoxManage to customize the VM. For example to change     memory:  
#   vb.customize ["modifyvm", :id, "--memory", "1024"]  
 end

然后关闭  vagrant  halt  ,重启,查看报错。
运行vagrant up 启动 virtualbox 后,GUI会给出提示:

VT-x/AMD-V硬件加速在您的系统中不可用。您的64-位虚拟机将无法检测到 64-位处理器,从而无法启动。  
这是由于在BOIS中没有开启cpu虚拟化支持,重启F2或F10等进入BIOS设置Virtualization为Enable(我的Thinkpad是Security=>Virtualizatio设置为Enable);

电脑重启后,再次vagrant up启动虚拟机还是有一些问题,当时也没有记录下来错误信息,只记得解决方案是使用vagrant destroy将虚拟机从磁盘中删除,然后使用vagrant up命令重新创建。

重启后,换一个文件路径,我原来的是d:\myvagrant,我换到了另外一个路径d:\disk\va1

上面出现waining,是没有问题的,现在我们可以通过ssh直连了。

查看vagrant的状态  vagrant status

关闭VM     vagrant halt

在启动  vagrant up, 第二次启动会比较快,因为不会复制文件,但是还是很慢,哈哈,多等下就好了。

ssh 连接

ip:127.0.0.1

端口:2222

用户名:vagrant

密码:vagrant

root的密码也是vagrant  ,上面连接一定要注意,更改ssh的默认端口22 为 2222

ssh 登录后,我发现语言是德语的,日,使用root账号登录,更改语言

 

vim /etc/sysconfig/i18n

语言改成英语

LANG="en_US.UTF-8"  
SYSFONT="latarcyrheb-sun16"

重启一下linux

查看centos 版本:

[root@localhost ~]# cat /etc/redhat-release  
CentOS release 6.6 (Final)  
[root@localhost ~]#

 

到此为止,初始环境搭配好了,下面需要安装 lnmp环境了。

具体参看下一节

vagrant 配置

Vagrant 初始化成功后,会在初始化的目录里生成一个 Vagrantfile 的配置文件,可以修改配置文件进行个性化的定制。

Vagrant 默认是使用端口映射方式将虚拟机的端口映射本地从而实现类似 http://localhost:80 这种访问方式,这种方式比较麻烦,新开和修改端口的时候都得编辑。

相比较而言,host-only 模式显得方便多了。打开 Vagrantfile,将下面这行的注释去掉(移除 #)并保存:

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

重启虚拟机,这样我们就能用 192.168.33.10 访问这台机器了,你可以把 IP 改成其他地址,只要不产生冲突就行。

注意:对于端口映射一定要配置,譬如:

config.vm.network “forwarded_port”, guest: 80, host: 80

不然,在开启nginx后,通过127.0.0.1是无法访问的,这个一定不要忘记

我暂时采用的是单个匹配的方式:如下:

# -*- 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 = "centos6.6"

  # 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: 8080, host: 8080
  config.vm.network "forwarded_port", guest: 80, host: 80
  config.vm.network "forwarded_port", guest: 2001, host: 2001
  config.vm.network "forwarded_port", guest: 2030, host: 2030
  
  config.vm.network "forwarded_port", guest: 2030, host: 2030
  config.vm.network "forwarded_port", guest: 2020, host: 2020
  config.vm.network "forwarded_port", guest: 2010, host: 2010
  config.vm.network "forwarded_port", guest: 2000, host: 2000
  config.vm.network "forwarded_port", guest: 1000, host: 1000
  config.vm.network "forwarded_port", guest: 1010, host: 1010
  #config.vm.network :private_network, ip: "192.168.111.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 = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # 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

6. 打包分发

当你配置好开发环境后,退出并关闭虚拟机。在终端里对开发环境进行打包:

vagrant package

打包完成后会在当前目录生成一个 package.box 的文件,将这个文件传给其他用户,其他用户只要添加这个 box 并用其初始化自己的开发目录就能得到一个一模一样的开发环境了。

这个box文件,如果给了其他人用,先通过下面的命令行添加

vagrant box add  fecshop  fecshop.box

 

然后,和上面一样,找一个新的文件夹,

vagrant init fecshop

执行成功后,启动

vagrant up

 

另外需要注意的是,需要把 Vagrantfile 这个里面的配置复制到新的地方,配置一致才可以。这样就完成了分发。
7. 常用命令

vagrant init  # 初始化
vagrant up  # 启动虚拟机
vagrant halt  # 关闭虚拟机
vagrant reload  # 重启虚拟机
vagrant ssh  # SSH 至虚拟机
vagrant status  # 查看虚拟机运行状态
vagrant destroy  # 销毁当前虚拟机

更多内容请查阅官方文档 http://docs.vagrantup.com/v2/cli/index.html
8. 注意事项

使用 Apache/Nginx 时会出现诸如图片修改后但页面刷新仍然是旧文件的情况,是由于静态文件缓存造成的。需要对虚拟机里的 Apache/Nginx 配置文件进行修改:

# Apache 配置添加:
EnableSendfile off

# Nginx 配置添加:
sendfile off;

 

其他问题:

问题2: vagrant启动报错The following SSH command responded with a non-zero exit status.

The following SSH command responded with a non-zero exit status.  
Vagrant assumes that this means the command failed!  
  
ARPCHECK=no /sbin/ifup eth1 2> /dev/null  
  
Stdout from the command:  
  
Device eth1 does not seem to be present, delaying initialization.  
  
Stderr from the command:

解决方案
虽然vagrant up启动报错,但是vagrant ssh还是能登陆虚拟机的,进入虚拟机后,执行如下命令

sudo rm -f /etc/udev/rules.d/70-persistent-net.rules

对, 问题就处在在持久网络设备udev规则(persistent network device udev rules)是被原VM设置好的,再用box生成新VM时,这些rules需要被更新。而这和Vagrantfile里对新VM设置private network的指令发生冲突。删除就好了。
vagrant reload 再次启动就OK。

问题3:在配置好vagrant和虚拟机中的nginx后,当你通过宿主机访问虚拟机中的web站点时,出现页面一直在加载,无法正常显示页面
(此块由程序员 Young 614168741 添加)
解决方法:因为centos7.0(含7.0)及以上版本会默认安装firewalld防火墙,firewalld有zone的概念默认在public区,只能指定外部连接进入,首先systemctl status firewalld 查看防火墙状态,active:running表示开启转态,然后我们firewall-cmd –zone=public –add-port=80/tcp –permanent,或者firewall-cmd –zone=public –add-port=8080/tcp –permanent(取决于你的Vagrantfile端口端口那一个是映射到虚拟机的80),执行完,firewall-cmd –zone=public –list-ports检查下public区上的端口,出现刚才添加的端口说明成功,再firewall-cmd –reload重启下firewalld,这个时候通过宿主机通过制定的配置的端口访问虚拟机站点应该就可以了

Linux 作为开发环境的方法分享

1.为什么用linxu作为开发环境?
经常的会遇到开发环境调试好的程序,到了线上不好用,可能是大小写,所以操作系统要一致

2. 线上和开发测试的所有的软件要一致,包括mysql nginx php版本。
php版本不一致导致某些函数废弃等,可能会导致问题

因此,需要所有的环境,开发和线上一致,这样最好,在开发环境调试好的程序到了线上,一般不会出现问题。

但是linux编程比较费劲,用vim 还是不方便,但是咋们可以用远程ftp的方式加载,一般的编辑器都有远程ftp的功能,下面说的是详细。

1. 将linxu的文件挂载到windows中 ,使用samba,linux和window文件共享 安装samba

2. 安装samba的好处就是可以可以在window中打开linux的挂载盘,就像操作本地一样,不过也可以不用samba,用编辑器的ftp功能直接远程连接,我现在的开发环境,用的是阿里云的主机,深圳机房,直接ftp连接编程,就和本地一样,非常方便,只要有一个电脑,安装了notepad++,我就可以写代码了,下面说说我的思路:

1.安装notepad++
安装完了notepad,需要安装一个编辑器的插件,点击菜单 , 插件–>plugin Manager –> show Plugin Manager。点击后,
在弹出的窗口列表中找到NppFtp ,点击安装,完成后重启notepad++。
然后点击菜单 插件–>NppFtp–>show Nppftp Window ,就弹出来窗口了,用鼠标可以拖到左侧。

2.添加ftp ,点击那个灰色的小齿轮,点击profile settings

在弹出的窗口中点击add new 填写ftp信息就可以了,填写完成后,点击close

3.打开目录树:
如图选择后,就可以打开文件目录了

后面就可以自己编写代码,直接保存了

4.对于linux的php mysql nginx的环境的配置,可以查看

一.关于Yii2在Linux下面的配置,您可以查看:YII2开发测试环境安装配置

Linux 安装php 5.4 – yii2 环境 Linux – 安装nginx – 搭配YII2环境
YII2开发环境文件上传 – 安装vsftpd linux 安装mysql5.6
linux设置环境变量 临时设置 和 永久设置 linux和window文件共享 安装samba
Linux php 扩展安装 mongo ,redis ,soap,imap,pdo_mysql,oauth YII2开发环境版本控制 – 安装SVN
yii2 – redis 配置 Yii2 – Mongodb的安装和配置 mongo

Yii2 安装 – composer

yii2的框架的安装,以及扩展的安装,都是用composer,下面是安装composer的具体方法:

  1. 查看php是否存在
    php --version

    如果不存在,将安装的php设置下快捷方式,这样,就可以直接用php执行。

  2. 我当前的php的安装路径为/usr/local/php/bin/php 我设置了下快捷方式:
  3. ln -s /usr/local/php/bin/php  /usr/local/bin/php
  4. 安装composer
    curl -sS https://getcomposer.org/installer | php
    mv composer.phar /usr/local/bin/composer
  5. 到这里就安装完成了,譬如,我可以用下面的方式安装asset
    composer global require "fxp/composer-asset-plugin:~1.1.1"

    上面如果报错:参看:http://stackoverflow.com/questions/36579559/updating-composer-throws-exception-class-fxp-composer-assetplugin-repository-npm

    try to update your fxp/composer-asset-plugin:

    php composer.phar global update fxp/composer-asset-plugin --no-plugins

    PS: If after update it still not working, try also following:

    composer global require fxp/composer-asset-plugin --no-plugins

    如果上面的方法还是有问题,譬如我曾经的报错:

    我采取的办法是清空composer global存放的文件夹。我的composer我使用的是root账户,我清空命令如下:

    rm -rf /root/.composer

    然后重新安装:

    composer global require "fxp/composer-asset-plugin:^1.2.0"

    文章参考:

    https://getcomposer.org/doc/03-cli.md#global

https://getcomposer.org/doc/03-cli.md#composer-home

Yii2 – 安装高级模板 advanced project template

在yii2官网,可以看到有两个版本,一个是base template,一个是advanced tempalte,高级模板更加强大一些,初始化后有一个公用的common,三个控制入口,frontend,backend,console,分别对应前台,后台,控制台,很适合线上有一定规模的网站,譬如电商网站,我们用frontend做前端显示,backend做后台,console做一些命令行的数据处理,cron定时处理脚本等,这里,我所要说的是,如何从零安装yii2的高级模板。

composer堪称神器,类似于yum安装,自己解决包依赖,所以,安装的过程中也是用composer来安装。

1.安装composer

curl -sS https://getcomposer.org/installer | php  
mv composer.phar /usr/local/bin/composer 
composer self-update

第一行为下载composer,具体执行log:

[root@iZ942k2d5ezZ ~]# curl -sS https://getcomposer.org/installer | php 
All settings correct for using Composer
Downloading 1.0.2...

Composer successfully installed to: /root/composer.phar
Use it: php composer.phar

 

2.初始安装:fxp/composer-asset-plugin

composer global require "fxp/composer-asset-plugin:~1.1.1"

3.安装yii2

composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application

安装log如下:

[root@iZ942k2d5ezZ test]# composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application
Running composer as root/super user is highly discouraged as packages, plugins and scripts cannot always be trusted
Installing yiisoft/yii2-app-advanced (2.0.8)
  - Installing yiisoft/yii2-app-advanced (2.0.8)
    Downloading: 100%         

Created project in yii-application
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing yiisoft/yii2-composer (2.0.4)               
    Loading from cache

  - Installing swiftmailer/swiftmailer (v5.4.1)
    Loading from cache

  - Installing bower-asset/jquery (2.2.3)
    Loading from cache

  - Installing bower-asset/yii2-pjax (v2.0.6)
    Loading from cache

  - Installing bower-asset/punycode (v1.3.2)
    Loading from cache

  - Installing bower-asset/jquery.inputmask (3.2.7)
    Loading from cache

  - Installing cebe/markdown (1.1.0)
    Loading from cache

  - Installing ezyang/htmlpurifier (v4.7.0)
    Downloading: 100%         

  - Installing yiisoft/yii2 (2.0.8)
    Downloading: 100%         

  - Installing yiisoft/yii2-swiftmailer (2.0.5)
    Loading from cache

  - Installing yiisoft/yii2-codeception (2.0.5)
    Loading from cache

  - Installing bower-asset/bootstrap (v3.3.5)
    Loading from cache

  - Installing yiisoft/yii2-bootstrap (2.0.6)
    Loading from cache

  - Installing yiisoft/yii2-debug (2.0.6)
    Loading from cache

  - Installing bower-asset/typeahead.js (v0.11.1)
    Loading from cache

  - Installing phpspec/php-diff (v1.1.0)
    Loading from cache

  - Installing yiisoft/yii2-gii (2.0.5)
    Loading from cache

  - Installing fzaninotto/faker (v1.5.0)
    Loading from cache

  - Installing yiisoft/yii2-faker (2.0.3)
    Loading from cache

fzaninotto/faker suggests installing ext-intl (*)
Writing lock file
Generating autoload files

 

执行完成上面的,yii2就下载好了,我们需要初始化

4. yii 高级模板初始化

进入yii-application目录(上面用composer安装的目录),执行:

./init

执行log如下:

[root@iZ942k2d5ezZ yii-application]# ./init
Yii Application Initialization Tool v1.0

Which environment do you want the application to be initialized in?

  [0] Development
  [1] Production

  Your choice [0-1, or "q" to quit] 0

  Initialize the application under 'Development' environment? [yes|no] yes

  Start initialization ...

   generate common/config/main-local.php
   generate common/config/params-local.php
   generate frontend/config/main-local.php
   generate frontend/config/params-local.php
   generate frontend/web/index.php
   generate frontend/web/index-test.php
   generate console/config/main-local.php
   generate console/config/params-local.php
   generate backend/config/main-local.php
   generate backend/config/params-local.php
   generate backend/web/index.php
   generate backend/web/index-test.php
   generate tests/codeception/config/config-local.php
   generate yii
   generate cookie validation key in backend/config/main-local.php
   generate cookie validation key in frontend/config/main-local.php
      chmod 0777 backend/runtime
      chmod 0777 backend/web/assets
      chmod 0777 frontend/runtime
      chmod 0777 frontend/web/assets
      chmod 0755 yii
      chmod 0755 tests/codeception/bin/yii

  ... initialization completed.

[root@iZ942k2d5ezZ yii-application]# 

到这里就安装了yii2高级模板的安装,配置nginx

前台地址:frontend/web

后台地址:backend/web

访问的时候,在linux下面需要将web目录下面的assets文件夹设置成可写。

这样就完成了yii2 advanced的安装了