vagrant + virtualbox 打造跨平台开发环境

  • baagee 发布于 2017-02-13 21:13:11
  • 分类:Linux
  • 1367 人围观
  • 5 人喜欢

vagrant是构建在虚拟化技术之上的虚拟机运行环境管理工具,它的运行需要依赖虚拟化技术,比如virtualbox,wmware

它能建立和删除虚拟机

配置虚拟机运行参数

管理虚拟机运行状态,自动化配置和安装开发环境

打包和分发虚拟机运行环境

vagrant常用命令:

  1. vagrant box list 查看已有的虚拟机box
  2. vagrant box add <虚拟机名> <虚拟机文件路径> 新增一个虚拟机box
  3. vagrant box remove <虚拟机名> 删除指定的box
  4. vagrant init <虚拟机名>初始化vagrantfile
  5. vagrant up 启动虚拟机
  6. vagrant ssh ssh登录虚拟机
  7. vagrant suspend 挂起虚拟机
  8. vagrant reload重启虚拟机
  9. vagrant halt 关闭虚拟机
  10. vagrant status 查看虚拟机状态
  11. vagrant destory 删除虚拟机

vagrant对于个人可以具有跨平台,可移动,自动化部署无需人工参与的优点,对于公司可以减少人力培训成本,统一开发环境

软件安装

1,VirtualBox 5.1.8

下载地址 https://www.virtualbox.org/wiki/Download_Old_Builds_5_1

2,Vagrant 1.8.6

下载地址:https://releases.hashicorp.com/vagrant/1.8.6/

3,安装Xshell命令行工具

box下载

链接:http://pan.baidu.com/s/1boBe1m3 密码:242r

4,接下来就是安装了

使用vagrant box add php_dev ubuntu1404.box 新增一个虚拟机box

建立一个文件夹比我我的php_dev

进入php_dev文件夹

然后初始化 vagrant init php_dev

会发现多了个文件Vagrantfile

然后就可以vagrant ssh登录虚拟机了

安装php,apache,nginx,mysql

首先为了尽快安装,替换源

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份

sudo vim /etc/apt/sources.list #修改源
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

将上面的全部替换到源文件里,sudo apt-get update #更新列表

Ngnix的安装:

sudo apt-get install nginx

nginx -v

nginx version: nginx/1.4.6 (Ubuntu)

测试Nginx:

curl -I 'http://127.0.0.1'
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Sat, 07 Jan 2017 07:49:41 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
Connection: keep-alive
ETag: "5315bd25-264"
Accept-Ranges: bytes

Apache的安装:

sudo apt-get install apache2
apache2 -v

Server version: Apache/2.4.7 (Ubuntu)
Server built: Jul 15 2016 15:34:04

测试访问,此时不能访问,因为80端口被nginx占用了,可以先把nginx停掉

sudo /etc/init.d/nginx stop

curl -I 'http://127.0.0.1'

 HTTP/1.1 200 OK
 Date: Sat, 07 Jan 2017 08:15:26 GMT
 Server: Apache/2.4.7 (Ubuntu)
 Last-Modified: Sat, 07 Jan 2017 07:17:45 GMT
 ETag: "2cf6-5457bee51dd0d"
 Accept-Ranges: bytes
 Content-Length: 11510
 Vary: Accept-Encoding
 Content-Type: text/html`

更改端口,将端口设置为8888

修改 ports.conf 文件

cd /etc/apache2/

sudo vim ports.conf 

修改后重启apache

sudo /etc/init.d/apache2 restart

curl -I 'http://127.0.0.1:8888'
 HTTP/1.1 200 OK
 Date: Sat, 07 Jan 2017 08:24:15 GMT
 Server: Apache/2.4.7 (Ubuntu)
 Last-Modified: Sat, 07 Jan 2017 07:17:45 GMT
 ETag: "2cf6-5457bee51dd0d"
 Accept-Ranges: bytes
 Content-Length: 11510
 Vary: Accept-Encoding
 Content-Type: text/html`

开启nginx

sudo /etc/init.d/nginx start

vagrant@dev:/etc/apache2$ ps -ef | grep nginx
root      9824     1  0 08:53 ?        00:00:00 nginx: master process /usr/sbin/nginx
www-data  9825  9824  0 08:53 ?        00:00:00 nginx: worker process
www-data  9826  9824  0 08:53 ?        00:00:00 nginx: worker process
www-data  9827  9824  0 08:53 ?        00:00:00 nginx: worker process
www-data  9828  9824  0 08:53 ?        00:00:00 nginx: worker process
vagrant   9830  1845  0 08:54 pts/0    00:00:00 grep --color=auto nginx
vagrant@dev:/etc/apache2$ ps -ef | grep apache
root      9800     1  0 08:53 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  9804  9800  0 08:53 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  9805  9800  0 08:53 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  9806  9800  0 08:53 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  9807  9800  0 08:53 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  9808  9800  0 08:53 ?        00:00:00 /usr/sbin/apache2 -k start
vagrant   9832  1845  0 08:54 pts/0    00:00:00 grep --color=auto apache
vagrant@dev:/etc/apache2$ curl -I 'http://127.0.0.1'
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 26 Feb 2017 08:54:40 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
Connection: keep-alive
ETag: "5315bd25-264"
Accept-Ranges: bytes

vagrant@dev:/etc/apache2$ curl -I 'http://127.0.0.1:80'
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Sun, 26 Feb 2017 08:54:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Mar 2014 11:46:45 GMT
Connection: keep-alive
ETag: "5315bd25-264"
Accept-Ranges: bytes

vagrant@dev:/etc/apache2$ curl -I 'http://127.0.0.1:8888'
HTTP/1.1 200 OK
Date: Sun, 26 Feb 2017 08:54:50 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Type: text/html;charset=UTF-8

以上可以看到通过80端口访问nginx,通过8888访问apache,没问题

Mysql的安装

sudo apt-get install mysql-server #服务器端

#安装期间会提示输入为mysql设置root密码

sudo apt-get install mysql-client #客户端

mysql -uroot -p #测试连接库

php的安装:

sudo apt-get install php5-cli

php -v

  PHP 5.5.9-1ubuntu4.20 (cli) (built: Oct  3 2016 13:00:37)
  Copyright (c) 1997-2014 The PHP Group
  Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
  with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

安装PHP扩展

php5-mcrypt

php5-mysql

php5-gd

#支持apache2的php模块

sudo apt-get install libapache2-mod-php5

#开启rewrite功能

sudo a2enmod rewrite

#支持nginx fastcgi

sudo apt-get install php5-cgi php5-fpm


评论

点击图片切换
还没有评论,快来抢沙发吧!