Created
August 27, 2013 13:02
-
-
Save zetavg/6353231 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Pow(der) + Apache + MySQL + PHP on Mac OS X | |
=========================================== | |
* 參考 [http://bentoncreation.com/post/43279873217/pow-awesomeness-for-php-static-sites](http://bentoncreation.com/post/43279873217/pow-awesomeness-for-php-static-sites)。 | |
## [Pow](http://pow.cx/) | |
``` | |
curl get.pow.cx | sh | |
``` | |
### [Powder](https://github.com/Rodreegez/powder) | |
``` | |
gem install powder | |
``` | |
常用指令: | |
`powder link` `powder unlink` `powder start` `powder stop` `powder restart` | |
## Apache | |
OS X Mountain Lion 內建。 | |
啓動:`sudo apachectl start` | |
關閉:`sudo apachectl stop` | |
重新啓動:`sudo apachectl restart` | |
網站根目錄位置:`/Library/WebServer/Documents/` (localhost/) | |
使用者網站根目錄位置:`~/Sites` (localhost/~User) | |
### 啓動 PHP | |
打開 `/etc/apache2/httpd.conf` | |
找到 | |
``` | |
#LoadModule php5_module libexec/apache2/libphp5.so | |
``` | |
將前面的 `#` 去掉,儲存。 | |
### 監聽 port 8080 | |
因為 port 80 會被 Pow 截走。 | |
打開 `/etc/apache2/httpd.conf` | |
找到 | |
``` | |
Listen 80 | |
``` | |
在下面加上 | |
``` | |
Listen 8080 | |
``` | |
## MySQL | |
``` | |
brew install mysql | |
mysql.server start | |
``` | |
改 root 密碼 | |
``` | |
mysqladmin -u root [-p] password ['******'] | |
``` | |
若先前已經有設定密碼才需要加 `-p`,`password` 後面空白的話可以之後在輸入新密碼。 | |
## 將開發中的網站用 Pow 掛上 | |
首先 `cd` 到專案目錄 | |
``` | |
cd /my/project/ | |
``` | |
將專案目錄連結放到網站根目錄 | |
``` | |
sudo ln -s $(pwd) /Library/WebServer/Documents | |
``` | |
在專案目錄底下建立 `config.ru` | |
``` | |
require "net/http" | |
class ProxyApp | |
def call(env) | |
begin | |
request = Rack::Request.new(env) | |
headers = {} | |
dir = "" | |
env.each do |key, value| | |
if key =~ /^http_(.*)/i | |
headers[$1] = value | |
end | |
if key == "HTTP_HOST" | |
dir = value.chomp(".dev") | |
end | |
end | |
http = Net::HTTP.new("localhost", 8080) | |
http.start do |http| | |
response = http.send_request(request.request_method, "/" + dir + "/" + request.fullpath, request.body.read, headers) | |
[response.code, response.to_hash, [response.body]] | |
end | |
rescue Errno::ECONNREFUSED | |
[500, {}, ["Server is down, try $ sudo apachectl start"]] | |
end | |
end | |
end | |
run ProxyApp.new | |
``` | |
然後 `powder link`,完成! | |
### 移除已經掛上的網站 | |
``` | |
powder unlink | |
rm config.ru | |
sudo rm /Library/WebServer/Documents/$(basename $(pwd)) | |
``` | |
### 嫌太麻煩就寫成 function 吧 | |
``` | |
function phpowder(){ | |
if [[ "$1" == "link" ]]; then | |
sudo ln -s $(pwd) /Library/WebServer/Documents | |
echo 'require "net/http" | |
class ProxyApp | |
def call(env) | |
begin | |
request = Rack::Request.new(env) | |
headers = {} | |
dir = "" | |
env.each do |key, value| | |
if key =~ /^http_(.*)/i | |
headers[$1] = value | |
end | |
if key == "HTTP_HOST" | |
dir = value.chomp(".dev") | |
end | |
end | |
http = Net::HTTP.new("localhost", 8080) | |
http.start do |http| | |
response = http.send_request(request.request_method, "/" + dir + "/" + request.fullpath, request.body.read, headers) | |
[response.code, response.to_hash, [response.body]] | |
end | |
rescue Errno::ECONNREFUSED | |
[500, {}, ["Server is down, try $ sudo apachectl start"]] | |
end | |
end | |
end | |
run ProxyApp.new' > 'config.ru' | |
powder link | |
elif [[ "$1" == "unlink" ]]; then | |
powder unlink | |
rm config.ru | |
sudo rm /Library/WebServer/Documents/$(basename $(pwd)) | |
else | |
echo "Unknown parameters" | |
return -1 | |
fi | |
} | |
``` | |
日後要掛上 php 網站只要用 `phpowder link`,移除用 `phpowder link`,和 Powder 一樣方便! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment