背景
最近在學習php,由于不想在環境搭建上花費太多功夫,再加上之前在linux和window上使用過phpstudy,這次在mac系統上也想使用phpstudy,可是上網查了一下,發現mac上并沒有相關的phpstudy安裝包,那可怎么辦,剛好之前用過vagrant,virtualbox配合linux版本的phpstudy即可。有了思路,接下來看看我們需要準備什么東西來完成我們上面的想法
前置條件
1. mac os 系統
2. vagrant
立即學習“PHP免費學習筆記(深入)”;
3. virtualbox
4. git
5. phpstudy
6. 離線版box
開始處理
首先安裝vagrant和virtualbox
下載vagrant mac版本安裝包,安裝直接拖到Application中即可,安裝virtualbox同樣的方式,
安裝完之后,由于網絡環境不是很好,這里就不直接使用vagrant 自己的box商店了,用自己離線下載的Centos 7 box 首先添加到 vagrant 中,命令如下
添加離線box到vagrant中
vagrant?box?add?centos/7?/Users/ylf/Desktop/centos-7.0-x86_64.box
添加之后可以可以用以下命令查看是否正確
vagrant?box?list
創建Vagrantfile配置文件并運行虛擬機
新建一個目錄,在目錄中創建Vagrantfile 文件的內容如下
#?-*-?mode:?ruby?-*- #?vi:?set?ft=ruby?: #?All?Vagrant?configuration?is?done?below.?The?"2"?in?Vagrant.configure #?configures?the?configuration?version?(we?support?older?styles?for #?backwards?compatibility).?Please?don't?change?it?unless?you?know?what #?you're?doing. Vagrant.configure("2")?do?|config| ??#?The?most?common?configuration?options?are?documented?and?commented?below. ??#?For?a?complete?reference,?please?see?the?online?documentation?at ??#?https://docs.vagrantup.com. ??#?Every?Vagrant?development?environment?requires?a?box.?You?can?search?for ??#?boxes?at?https://vagrantcloud.com/search. ??config.vm.box?=?"centos/7" #??config.vm.box_version?=?"1801.02" ??#?Disable?automatic?box?update?checking.?If?you?disable?this,?then ??#?boxes?will?only?be?checked?for?updates?when?the?user?runs ??#?`vagrant?box?outdated`.?This?is?not?recommended. ??#?config.vm.box_check_update?=?false ??#?Create?a?forwarded?port?mapping?which?allows?access?to?a?specific?port ??#?within?the?machine?from?a?port?on?the?host?machine.?In?the?example?below, ??#?accessing?"localhost:8080"?will?access?port?80?on?the?guest?machine. ??#?NOTE:?This?will?enable?public?access?to?the?opened?port ??#?config.vm.network?"forwarded_port",?guest:?80,?host:?8080 ??#?Create?a?forwarded?port?mapping?which?allows?access?to?a?specific?port ??#?within?the?machine?from?a?port?on?the?host?machine?and?only?allow?access ??#?via?127.0.0.1?to?disable?public?access ??#?config.vm.network?"forwarded_port",?guest:?80,?host:?8080,?host_ip:?"127.0.0.1" ??config.ssh.username='root' ??config.ssh.password='vagrant' ??config.ssh.insert_key='true' ??#?Create?a?private?network,?which?allows?host-only?access?to?the?machine ??#?using?a?specific?IP. ??#?config.vm.network?"private_network",?ip:?"192.168.33.10" ??#?Create?a?public?network,?which?generally?matched?to?bridged?network. ??#?Bridged?networks?make?the?machine?appear?as?another?physical?device?on ??#?your?network. ??#?config.vm.network?"public_network" ???config.vm.network?"public_network"?,?ip:?"192.168.3.233"?,bridge:?"en1:?Wi-Fi?(AirPort)" ??#?Share?an?additional?folder?to?the?guest?VM.?The?first?argument?is ??#?the?path?on?the?host?to?the?actual?folder.?The?second?argument?is ??#?the?path?on?the?guest?to?mount?the?folder.?And?the?optional?third ??#?argument?is?a?set?of?non-required?options. ??#?config.vm.synced_folder?"../data",?"/vagrant_data" ??#?Provider-specific?configuration?so?you?can?fine-tune?various ??#?backing?providers?for?Vagrant.?These?expose?provider-specific?options. ??#?Example?for?VirtualBox: ??# ??#?config.vm.provider?"virtualbox"?do?|vb| ??#???#?Display?the?VirtualBox?GUI?when?booting?the?machine ??#???vb.gui?=?true ??# ??#???#?Customize?the?amount?of?memory?on?the?VM: ??#???vb.memory?=?"1024" ??#?end ??# ??#?View?the?documentation?for?the?provider?you?are?using?for?more ??#?information?on?available?options. ??#?Enable?provisioning?with?a?shell?script.?Additional?provisioners?such?as ??#?Puppet,?Chef,?Ansible,?Salt,?and?Docker?are?also?available.?Please?see?the ??#?documentation?for?more?information?about?their?specific?syntax?and?use. ??#?config.vm.provision?"shell",?inline:?<p>然后再對應的目錄中打開終端,在終端中輸入如下命令</p><pre class="brush:php;toolbar:false">vagrant?up?&&?vagrant?ssh
稍等片刻,虛擬機應該已經創建好了,這時候系統會讓輸入密碼,vagrant創建的虛擬機默認密碼是vagrant 在終端中輸入vagrant ,這些終端輸入密碼時不會顯示輸入的字符,輸入完之后,直接敲回車即可。到這里linux環境已經配置好了。
簡單的解釋一下上面的配置項是什么意思
設置使用的box為centos/7
?config.vm.box?=?"centos/7"
設置默認用戶為root,不然默認的用戶為vagrant
config.ssh.username='root' config.ssh.password='vagrant' config.ssh.insert_key='true'
由于與學習用的機子,這里把ip設置為靜態ip 這樣虛擬機跟自己電腦就是同意局域網,可以直接互通了。
提示: 這里的ip地址大家根據自己的電腦實際的ip來設置,ifconfig 查到具體的ip ,這里的ip設置為與其類似的,bridge 是橋接網卡的,我這里用的無線網卡,如果是用的有線連接,請根據ifconfig查出來的值具體設置
config.vm.network?"public_network"?,?ip:?"192.168.3.233"?,bridge:?"en1:?Wi-Fi?(AirPort)"
到這里應該虛擬機已經設置完畢
安裝phpstudy
把下載好的phpstudy-all.bin 放到Vagrantfile同級目錄中,然后復制phpstudy-all.bin安裝包到~目錄
cp?/vagrant/phpstudy-all.bin?~/
然后執行授權,安裝
chmod?+x?~/phpstudy-all.bin ~/phpstudy-all.bin
等待安裝完畢,根據實際情況每個人的機子安裝的時間不太一樣,幾分鐘到幾十分鐘都有,跟網速,還有磁盤有關系,這時候phpstudy就安裝完畢,安裝完之后測試一下phpstudy啟動是否方便正常
phpstudy?restart
這時候可能會出現下面錯誤,這個原因是因為沒有安裝psmisc,安裝即可
?line?82:?killall:?command?not?found
安裝psmisc
yum?install?psmisc
到這里phpstudy已經安裝配置完畢,但是如果我們用來開發的話,還是需要設置一些其他的東西,我們需要設置一下mysql可以遠程訪問
mysql 遠程訪問
還是在那個虛擬機中運行下面的命令登陸mysql
/phpstudy/mysql/bin/mysql?-u?root?-proot
登陸mysql,調整當前的數據庫
use?mysql;
給root用戶遠程訪問權限
grant?all?privileges?on?*.*?to?'root'@'%'?identified?by?'root'; flush?privileges;
關閉防火墻
systemctl?stop?firewalld
禁止防火墻開機啟動
systemctl?disabled?firewalld
到這里mysql遠程連接已經開啟,在mac上安裝phpstudy到這里已經差不多到此結束,介于篇幅,后面我們會具體講一下如何利用phpstorm配合phpstudy遠程調試,遠程部署,自動上傳
推薦教程:phpStudy極速入門視頻教程