标签归档:vagrant

Vagrant box国内镜像及本地安装教程

Vagrant是非常好的本地开发环境搭建工具。

通常使用官方下载都会比较慢,而国内box下载地址较少,所以我特别下载了几个传到百度网盘。

(目前官方已支持CDN加速,可能不太需要了)

国内镜像下载

官方直接下载

官方已经采用cdn加速

vagrant box add ubuntu/trusty64
vagrant box add ubuntu/trusty32
vagrant box add generic/ubuntu1804
vagrant box add generic/ubuntu1604

vagrant box add generic/centos7
vagrant box add generic/centos8
vagrant box add generic/centos8

vagrant box add generic/debian8
vagrant box add generic/debian10

下载后的使用方法

添加vagrant box到box list

vagrant box add centos7 CentOS-7.box

初始化一个虚拟机使用刚才添加的vagrant box

vagrant init centos7

启动vagrant box虚拟机

vagrant up

使用Vagrant+CentOS 7搭建PHP7开发环境(含centos7.box直接下载地址)

Vagrant是一款基于命令行的虚拟机管理软件,可以用来快速部署统一的开发环境。

下载Vagrant

https://www.vagrantup.com/downloads.html

下载CentOS 7 Box

官方box下载地址

https://app.vagrantup.com/boxes/search

第三方box下载地址

http://www.vagrantbox.es/

使用原生下载

https://app.vagrantup.com/centos/boxes/7

vagrant init centos/7
vagrant up

vagrant 配置

  config.vm.network "public_network", ip: "192.168.31.245"
  config.vm.synced_folder "d:/data", "/data"
  config.vm.synced_folder "d:/phpcode", "/phpcode"

通过下载工具下载centos 7 box

官方box文件下载地址:http://cloud.centos.org/centos/7/vagrant/x86_64/images/

百度网盘box文件下载地址

  • CentOS 7: https://pan.baidu.com/s/1kVlAz59

添加并运行box

vagrant box add centos7 CentOS-7.box
vagrant init centos7
vagrant up

基础系统安装

基本系统安装

vagrant ssh
sudo passwd vagrant
sudo yum groupinstall "Development tools" -y
sudo yum install vim gcc kernel-devel kenel-devel-`uname -r`

禁止selinux

sudo setenforce 0 

sudo vi /etc/selinux/config

SELINUX=disabled

停止防火墙

sudo systemctl disable firewalld
sudo systemctl stop firewalld

更新系统

sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum install yum-utils
sudo yum-config-manager --enable remi-php72
sudo yum update
sudo yum install php-gd php-pdo php-opcache php-fpm php-pecl-redis php-pecl-mysql php-pecl-mysql php-mbstring php-intl php-cli php-xml
sudo yum install nginx -y
sudo yum install mariadb mariadb-server -y

修改nginx配置

mkdir /data/log/nginx /data/run/nginx -p
sudo service nginx stop 
sudo vim /etc/nginx/nginx.conf

nginx.conf配置修改如下:

user vagrant;
error_log /data/log/nginx/error.log;
pid /data/run/nginx/nginx.pid;

access_log  /data/log/nginx/access.log  main;

include /data/phpcode/projectname/vagrant/nginx/app.conf;

测试nginx配置

sudo nginx -t

修改nginx service配置:

sudo vim /usr/lib/systemd/system/nginx.service

nginx.service修改内容如下:

[Service]
PIDFile=/data/run/nginx/nginx.pid

重新加载service

sudo systemctl daemon-reload
sudo systemctl start nginx

修改PHP配置

mkdir  /data/run/php-fpm/session /data/run/php-fpm/wsdlcache /data/run/php-fpm/opcache /data/log/php-fpm/ -p
sudo service php-fpm stop
sudo vim /etc/php-fpm.d/www.conf

配置内容

user = vagrant
group = vagrant
php_value[session.save_path]    = /data/run/php-fpm/session
php_value[soap.wsdl_cache_dir]  = /data/run/php-fpm/wsdlcache
php_value[opcache.file_cache]  = /data/run/php-fpm/opcache
php_admin_value[error_log] = /data/log/php-fpm/www-error.log
slowlog = /data/log/php-fpm/www-slow.log
request_slowlog_timeout = 1

重启

sudo service php-fpm stop

配置Mysql

mkdir /data/mysql /data/run/mariadb /data/log/mariadb -p
sudo service mariadb stop
sudo vim /etc/my.cnf

mysqld配置

[mysqld]
datadir=/data/mysql
socket=/usr/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/data/log/mariadb/mariadb.log
pid-file=/data/run/mariadb/mariadb.pid

mysql client 配置

[client]

初始化数据库

sudo /usr/libexec/mariadb-prepare-db-dir mariadb.service

修改systemd配置

sudo vim /usr/lib/systemd/system/mariadb.service

配置内容

User=vagrant
Group=vagrant

重载systemd

sudo systemctl daemon-reload
sudo systemctl start mariadb

修改mysql账号密码

'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h localhost.localdomain password 'new-password'

# Alternatively you can run:
'/usr/bin/mysql_secure_installation'

配置composer

下载安装文件

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

安装

php composer-setup.php

删除安装文件

php -r "unlink('composer-setup.php');"

设置全局路径(windows请按参考文档设置)

sudo mv composer.phar /usr/bin/composer

配置使用国内镜像

composer config -g repo.packagist composer https://packagist.phpcomposer.com

vagrant多虚拟机配置

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

   config.vm.provider "virtualbox" do |vb|
     vb.gui = false
     vb.customize ["modifyvm", :id, "--memory", "512"]
   end

  config.vm.provision "shell", inline: "echo Hello"

  config.vm.define "dev", primary: true do |dev|
    dev.vm.box = "centos-aliyun-6.5"
    dev.vm.network :private_network, ip: "192.168.1.100"
    dev.vm.provision "shell", inline: "echo dev"
    dev.vm.synced_folder "d:/project", "/var/www"
  end

  config.vm.define "db1", autostart: false do |db1|
    db1.vm.box = "centos-aliyun-6.5"
    db1.vm.network :private_network, ip: "192.168.1.101"
    db1.vm.provision "shell", inline: "echo db1"
  end

  config.vm.define "db2", autostart: false do |db2|
    db2.vm.box = "centos-aliyun-6.5"
    db2.vm.network :private_network, ip: "192.168.1.102"
    db2.vm.provision "shell", inline: "echo db2"
  end

  config.vm.define "db3", autostart: false do |db3|
    db3.vm.box = "centos-aliyun-6.5"
    db3.vm.network :private_network, ip: "192.168.1.103"
    db3.vm.provision "shell", inline: "echo db2"
  end

end

使用vagrant快速搭建MariaDB Galera Cluster集群测试环境

参考

下载并安装ubuntu 12.04的32位box 初始化vagrant box

wget http://files.vagrantup.com/precise32.box
vagrant box add base Downloads/precise32.box
vagrant init

设置最大内存512

vim ~/VagrantFile

config.vm.provider :virtualbox do |vb|
    # Don't boot with headless mode
    vb.gui = false

    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "512"]
end

配置虚拟IP

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

启动虚拟机修改为163源

启动虚拟机,并通过ssh登录到客户机

vagrant up
vagrant ssh

采用163.com的源

sudo vim /etc/apt/sources.list

deb http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ precise-backports main restricted universe multiverse

deb http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-proposed main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-security main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://archive.ubuntu.com/ubuntu/ precise-backports main restricted universe multiverse

更新源

sudo aptitude update

添加mariadb源,并安装集群版

大部分linux的二进制发行包都可以在这里找到:https://downloads.mariadb.org/mariadb/repositories/

apt-get install python-software-properties
sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xcbcb082a1bb943db
sudo add-apt-repository 'deb http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/5.5/ubuntu precise main'
sudo aptitude update
sudo aptitude install mariadb-galera-server mariadb-client

打包虚拟机,以便再次安装 退出客户机,回到主机界面

vargrant package --output galera-db.box
vargant box add db galera-db.box

配置虚拟机

vim ~/Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo Hello"
  config.vm.define "db1" do |db1|
    db1.vm.box = "db"
    db1.vm.network :private_network, ip: "10.0.1.100"
  end

  config.vm.define "db2" do |db2|
    db2.vm.box = "db"
    db2.vm.network :private_network, ip: "10.0.1.101"
  end
end

重新加载虚拟机配置

vagrant reload

配置第一台db节点

vagrant ssh db1
sudo service mysql start
sudo mysql_secure_installation mysql -uroot -p

mysql> GRANT USAGE ON *.* to sst@'%' IDENTIFIED BY 'sstpass123';
mysql> GRANT ALL PRIVILEGES on *.* to sst@'%';
mysql> FLUSH PRIVILEGES;
mysql> quit

sudo cp /usr/share/mysql/wsrep.cnf /etc/mysql/conf.d/ sudo vim /etc/mysql/conf.d/wsrep.cnf

wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://"
wsrep_sst_auth=sst:sstpass123
wsrep_sst_method=rsync
wsrep_node_name=db1
wsrep_node_address=10.0.1.100

sudo service mysql restart

安装第二个节点

vagrant ssh db2

除了wsrep.cnf有少许不同外,其他同第一节点

wsrep_cluster_address="gcomm://10.0.1.100"
wsrep_node_name=db2
wsrep_node_address=10.0.1.101

sudo service mysql restart

测试节点同步

mysql -usst -psstpass123 -h10.0.1.100 show status like 'wsrep%'; 可以看到wsrep_connected都为ON

create database testdb;
use testdb;
create table t(id int primary key auto_increment);
insert into t values(null);
insert into t values(null);
select * from t;
exit;

mysql -usst -psstpass123 -h10.0.1.101

use testdb;
select * from t;
insert into t values(null);
insert into t values(null);
select * from t;

参考:

使用vagrant快速搭建phalcon开发环境

vagrant是一款开发环境快速搭建的工具,phalcon是一个c语言的php框架。

参考:

http://www.vagrantup.com/

http://phalconphp.com/en/download/vagrant

下载并安装vagrant

http://www.vagrantup.com/downloads.html

支持windows,linux,mac

下载并启动phalcon镜像

vagrant init phalconbox https://s3-eu-west-1.amazonaws.com/phalcon/phalcon125-apache2-php54-mysql55.box
vagrant up