Vagrant 入門指北

Vagrant 入門指北

WX20180613-224646@2x

後端的同窗用 Vagrant 快速的、可重複的建立各類不一樣環境的虛擬機,來測試部署各後端程序。對於前端同窗來講,想向全棧發展,服務器相關的一些操做是繞不開的一道檻。這裏先前端爲視角,以 Vagrant 來引導你們逐步進入服務器技術相關的領域。

快速入門

安裝

安裝 Vagrant 很是簡單,能夠在Downloads 頁面選擇最新的版本安裝。Vagrant 支持 Windows、Linux、Mac 等平臺。html

同時電腦中還須要再安裝virtualbox前端

建立一個目錄用於存放 Vagrantfile 以及 Vagrant 在工做中的數據nginx

» mkdir myvagrant
» cd myvagrant

接下來前往 vagrant 官網尋找一個合適的 Box,這裏我選擇了 Virtualbox 版本的Centos7後端

以 Centos7 初始化 Vagrant 工程

» 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

Nginx 是一個十分輕量級的 HTTP 服務器

接上篇登陸到虛擬機,並切換到 root 用戶。

[root@localhost vagrant]# yum install -y epel-release
[root@localhost vagrant]# yum install -y nginx

啓動 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 的歡迎頁面了。

相關文章
相關標籤/搜索