后端的同窗用 Vagrant 快速的、可重复的建立各类不一样环境的虚拟机,来测试部署各后端程序。对于前端同窗来讲,想向全栈发展,服务器相关的一些操做是绕不开的一道槛。这里先前端为视角,以 Vagrant 来引导你们逐步进入服务器技术相关的领域。
安装 Vagrant 很是简单,能够在Downloads 页面选择最新的版本安装。Vagrant 支持 Windows、Linux、Mac 等平台。html
同时电脑中还须要再安装virtualbox。前端
建立一个目录用于存放 Vagrantfile 以及 Vagrant 在工做中的数据nginx
» mkdir myvagrant » cd myvagrant
接下来前往 vagrant 官网寻找一个合适的 Box,这里我选择了 Virtualbox 版本的Centos7后端
» vagrant init centos/7
运行这个命令后会在当前目录下新建一个 Vagrantfile 的配置文件。centos
若是本地没有 Centos7 这个镜像,那么接下来将会是一个漫长的下载过程。在这若是实在等不下去,能够先经过别的各类途径将 Box 下载到本地。而后再命令行中输入以下代码:浏览器
» vagrant box add --name centos/7 /local_download_path/virtualbox.box
经过 vagrant box list
来检查 box 是否添加成功。出现以下提示表明安装成功。服务器
centos/7 (virtualbox, 0)
再次执行初始化命令,并经过up
命令来启动app
» vagrant init centos/7 » vagrant up
出现以下提示表明启动成功dom
Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'centos/7'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: myvagrant_default_1528726843301_65709 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> 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. ==> default: Rsyncing folder: /work/training/vagrant/myvagrant/ => /vagrant
输入 vagrant ssh
登陆ssh
此时 vagrant 将使用默认的用户 vagrant 以及预设的 SSH 公钥密钥键值对直接登陆虚拟机。
[vagrant@localhost ~]$
切换用户到 root,默认密码是 vagrant
密码在 Linux 中是看不见的,但确实已经输入了
[vagrant@localhost ~]$ su Password: [root@localhost vagrant]#
[root@localhost vagrant]# exit exit [vagrant@localhost ~]$ exit logout Connection to 127.0.0.1 closed.
Vagrant 提供了好几种方法来关闭虚拟机,你能够根据不一样的状况选择不一样的方式。
vagrant suspend
将虚拟机置于休眠状态。这时候主机会保存虚拟机的当前状态。再用vagrant up
启动虚拟机时可以返回以前工做的状态。这种方式优势是休眠和启动速度都很快,只有几秒钟。缺点是须要额外的磁盘空间来存储当前状态。vagrant halt
则是关机。若是想再次启动仍是使用vagrant up
命令,不过须要多花些时间。vagrant destroy
则会将虚拟机从磁盘中删除。若是想从新建立仍是使用vagrant up
命令。Nginx 是一个十分轻量级的 HTTP 服务器
接上篇登陆到虚拟机,并切换到 root 用户。
[root@localhost vagrant]# yum install -y epel-release [root@localhost vagrant]# yum install -y nginx
[root@localhost vagrant]# systemctl start nginx
检查 nginx 状态
[root@localhost vagrant]# systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2018-06-11 15:02:45 UTC; 57s ago Process: 2910 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 2908 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 2907 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 2912 (nginx) CGroup: /system.slice/nginx.service ├─2912 nginx: master process /usr/sbin/nginx └─2913 nginx: worker process Jun 11 15:02:45 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server... Jun 11 15:02:45 localhost.localdomain nginx[2908]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok Jun 11 15:02:45 localhost.localdomain nginx[2908]: nginx: configuration file /etc/nginx/nginx.conf test is successful Jun 11 15:02:45 localhost.localdomain systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument Jun 11 15:02:45 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
出现如上所示表明 nginx 启动成功。
如今 nginx 是启动到了 vagrant 中,如何在咱们的电脑中直接访问呢 ? 这里须要对 vagrant 作一下端口转发的配置。
首先退出虚拟机。编辑上一篇初始化后生成的配置文件 Vagrantfile,找到
# config.vm.network "forwarded_port", guest: 80, host: 8080
将最前边的#
号删除并保存。
运行 vagrant reload
来应用修改后的配置文件。
登陆虚拟机,切换到 root 用户,启动 nginx
这时打开电脑浏览器输入 http://localhost:8080/
就能看到 nginx 的欢迎页面了。