GitHub 地址 <——— 所有文件都在这里

前提条件

安装 Vagrant,VirtualBox。

设置

下载软件并放入 soft 目录

MySQL:mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar 文件的 下载地址

安装 vagrant-vbguest 插件

如果需要使用同步目录 synced_folder,就需要安装这个插件:

vagrant plugin install vbguest

使用 VirtualBox 安装虚拟机时,需要虚拟机支持 vboxsf 文件系统。如果不支持,则在启动时执行到 Vagrantfile 中的同步目录(synced folders)这一行时会报错如下:

mount: unknown filesystem type 'vboxsf'

在 Vagrant 中安装 vagrant-vbguest 插件后,Vagrant 会自动在虚拟机中安装 vbguest,从而解决这个问题:

D:\workspace\vagrant-lnmp>vagrant plugin install vagrant-vbguest
Installing the 'vagrant-vbguest' plugin. This can take a few minutes...
Fetching: micromachine-2.0.0.gem (100%)
Fetching: vagrant-vbguest-0.15.2.gem (100%)
Installed the plugin 'vagrant-vbguest (0.15.2)'!

启动

在当前目录中执行 vagrant up,启动完成后就可以访问虚拟机了。

修改 ssh 登录的默认用户为 root

Vagrant 默认的登录用户是 vagrant,密码也是 vagrant。可以配置为 root 用户(默认密码也是 vagrant):

  • 登录虚拟机,复制 ssh key:
vagrant ssh
# 把 vagrant 用户的 ssh key 拷贝到 root 用户下
sudo cp -r ./.ssh/ /root/
sudo chown -R root.root /root/.ssh
exit
  • 修改 Vagrantfile 中的登录用户名:
config.ssh.username = 'root'
  • 重新 reload:
vagrant reload
  • 登录后默认用户为 root:
vagrant ssh

使用同步目录将宿主机文件映射到虚拟机

Vagrant 默认会将 Vagrantfile 所在的目录中的所有文件和目录复制到虚拟机的 /vagrant 目录,注意这并不是同步目录(可以修改文件试试)。如果需要创建同步目录,则需要在 virtualbox 虚拟机中安装增强插件 VirtualBox Guest Additions。

通过 Vagrant 操作

直接安装 vagrant-vbguest 插件即可自动帮我们完成所有的配置。

注意,这里在安装 kernelkernel-devel 两个组件时经常发生问题,因为在安装这两个软件时只要 uname -r 指定了精确的内核版本,而内核版本包含一个比较大的数字,不一定能精确匹配到:

通过 VirtualBox 操作的步骤如下:

  • 启动 virtualbox 客户端
  • 关闭当前虚拟机
  • 在设置-存储新增加一个空的虚拟光驱
  • 启动虚拟机在菜单页面载入VirtualBox Guest Additions
  • 登录虚拟机用户名/密码为vagrant/vagrant,安装 VirtualBox Guest Additions
# 安装kernel、gcc、make等包
sudo yum -y update
sudo yum -y install kernel-devel kernel-headers gcc make perl
sudo reboot

# mount镜像
sudo mkdir -p /mnt/cdrom
sudo mount /dev/cdrom /mnt/cdrom
cd /mnt/cdrom
sudo ./VBoxLinuxAdditions.run

修改 Vagrantfile 中映射目录

# 设定当前项目目录映射到虚拟机中的目录
config.vm.synced_folder ".", "/opt/gocode/src/..."

# 重新reload
vagrant reload

常见问题

创建同步目录失败

CentOS7 默认用的 3.10 内核,跟升级内核之后的内核编号有所不同:

[root@VM_139_74_centos ~]# uname -r
4.15.6-1.el7.elrepo.x86_64
[root@VM_120_242_centos ~]# uname -r
3.10.0-327.36.3.el7.x86_64

Vagrant 安装过程的信息:

...
Please install the Linux kernel "header" files matching the current kernel
for adding new hardware support to the system.
The distribution packages containing the headers are probably:
    kernel-devel kernel-devel-3.10.0-693.21.1.el7.x86_64

...
   default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Rsyncing folder: /cygdrive/d/workspace/vagrant-lnmp/ => /vagrant
==> default: Mounting shared folders...
    default: /home/vagrantdemo => D:/workspace/vagrantdemo
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=1000,gid=1000 home_vagrantdemo /home/vagrantdemo

The error output from the command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

这个报错也好解决,直接通过 vagrant ssh 登录虚拟机后,执行:

sudo yum install kernel kernel-devel -y

安装即可,可能内核的具体版本会有所差异,但一般不影响使用。

安装完成后,退出虚拟机,在宿主机执行:

vagrant reload

配置文件无法写入,报错 bash: /etc/yum.repos.d/nginx.repo: Permission denied

bash-4.2$ sudo echo '[nginx]
> name=nginx repo
> baseurl=http://nginx.org/packages/centos/7/$basearch/
> gpgcheck=0
> enabled=1' > /etc/yum.repos.d/nginx.repo
bash: /etc/yum.repos.d/nginx.repo: Permission denied

上面这种写法虽然使用了 sudo,但是 sudo 仅能作用于 echo,无法影响到输出重定向。此时需要使用这种写法:

bash-4.2$ sudo bash -c "echo '[nginx]
> name=nginx repo
> baseurl=http://nginx.org/packages/centos/7/$basearch/
> gpgcheck=0
> enabled=1' > /etc/yum.repos.d/nginx.repo"
bash: /etc/yum.repos.d/nginx.repo: Permission denied

epel-release >= 7 is needed by webtatic-release-7-3.noarch

yum install -y kernel-devel kernel-devel-uname -r

默认安装 kernel-devel 插件时会使用 uname -r 检查系统内核版本。但是有可能找不到这个内核版本对应的插件。如果插件安装失败,会导致同步目录无法使用。

...
   default: SSH auth method: private key
==> default: Machine booted and ready!
[default] No installation found.
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
There are no enabled repos.
 Run "yum repolist all" to see the repos you have.
 To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
 To enable custom repositories:
     yum-config-manager --enable <repo>
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

yum install -y kernel-devel kernel-devel-`uname -r` gcc binutils make perl bzip2

Stdout from the command:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile


Stderr from the command:

There are no enabled repos.
 Run "yum repolist all" to see the repos you have.
 To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
 To enable custom repositories:
     yum-config-manager --enable <repo>
PS D:\workspace\vagrant-lnmp-bento74-shellinline> vagrant reload
==> default: Attempting graceful shutdown of VM...

vagrant@127.0.0.1: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

出现这个问题,多半是关掉了密码登录,只能用证书登录。Vagrant 默认只支持证书登录。可以用 Xshell 或 PuTTY 通过 127.0.0.1 加指定的端口号登录试试,如果无法输入密码,只能通过证书登录,则确认没有开启密码登录。虚拟机具体的主机名和端口号可以用 vagrant ssh-config 查看:

PS D:\workspace\vagrant-lnmp> vagrant ssh
vagrant@127.0.0.1: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

PS D:\workspace\vagrant-lnmp> vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2200
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile D:/workspace/vagrant-lnmp/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

其中 IdentityFile 指明了用户私钥位置,将其导入 Xshell 后,再使用默认的用户 vagrant 访问 HostName 和 Port 指定的主机和端口,一般就可以连上了。

当然,也可以在 Vagrantfile 文件中开启密码登录,同时还可以指定默认登录的用户为 root 用户:

config.ssh.username = "root"
config.ssh.password = "vagrant"

或者通过证书登录后,编辑配置文件:/etc/ssh/sshd_config 添加一行配置:

PasswordAuthentication yes

然后,重启 sshd:

sudo systemctl reload sshd

$'\r': command not found

Windows 文件的换行符为:[CR][LF],即回车换行 \r\n
Linux 和 Unix 文件的换行符为:[LF],只有换行 \n

解决方案:
用 Sublime 打开文件,在菜单中选择 view -> Line Endings -> Unix,然后保存即可。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐