Vagrant進階:搭建簡易Python,PHP開發環境

來源:互聯網
上載者:User
至於怎麼安裝Vagrant,Python,PHP,本文就不講解了。

本文主要講解以本地作為開發環境,寫完代碼能直接自動同步到虛擬機器(測試環境),提高測試開發效率。


其中為了測試方便,Python和PHP都用內建的Web Server,而不搭建Nginx或者Apache。


用Python和PHP的內建Web Server的做法只適合開發測試環境,不適合生產環境。


1.首先需要對Vagrant環境進行配置。編輯Vagrant檔案,如下,去掉注釋。

config.vm.network :forwarded_port, guest: 3000, host: 8080


這樣就可以通過宿主機8080連接埠訪問到虛擬機器上得3000連接埠。這步解決了在本地進行訪問測試。而不需要跑到虛擬機器裡測試。


2.本地代碼怎麼自動同步到測試環境,同樣修改Vagrant檔案。如下。

config.vm.synced_folder "./data", "/vagrant"


其中第一個參數是宿主機檔案夾,存放本地代碼,第二個參數是虛擬機器對應的目錄,當本地data目錄有更新時,vagrant目錄就會同步。是不是很方便啊。


宿主機data目錄結構如下:

localhost:data angel$ ll -R

total 0

drwxr-xr-x 3 angel staff 102 4 28 18:51 phpTest

drwxr-xr-x 3 angel staff 102 4 28 19:52 pythonTest


./phpTest:

total 8

-rw-r--r-- 1 angel staff 192 4 28 18:51 router.php


./pythonTest:

total 8

-rw-r--r-- 1 angel staff 88 4 28 19:36 index.html



虛擬機器上vagrnt上的目錄結構如下:

[vagrant@localhost vagrant]$ ll -R

.:

total 0

drwxr-xr-x 1 vagrant vagrant 102 Apr 28 11:51 phpTest

drwxr-xr-x 1 vagrant vagrant 102 Apr 28 12:52 pythonTest


./phpTest:

total 4

-rw-r--r-- 1 vagrant vagrant 192 Apr 28 11:51 router.php


./pythonTest:

total 4

-rw-r--r-- 1 vagrant vagrant 88 Apr 28 12:36 index.html


發現沒有,是不是一模一樣啊,全自動的哦, 是不是很方便啊。


3.啟動Python內建Web Server

在虛擬機器上的pythonTest目錄啟動如下命令:

[vagrant@localhost pythonTest]$ python -m SimpleHTTPServer 3000

Serving HTTP on 0.0.0.0 port 3000 ...


在宿主機瀏覽器上輸入http://localhost:8080,就可以看到,

Welcome to Python 


這樣簡易的python環境就搭建好了。


4.啟動php內建的web Server

在虛擬機器上得phpTest目錄下啟動如下命令

[vagrant@localhost phpTest]$ php -S 0.0.0.0:3000 router.php

PHP 5.5.11 Development Server started at Mon Apr 28 13:37:19 2014

Listening on http://0.0.0.0:3000

Document root is /vagrant/phpTest


在宿主機瀏覽器上輸入http://localhost:8080,就可以看到,


Welcome to PHP

這樣簡易的PHP環境就搭建好了。

看官注意了,php啟動內建Web Server,想要遠端存取,host必須是 0.0.0.0, 本人就因為開始用localhost而報錯,調試了一下午.

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.