over 3 years ago

系統:

Mac OS X Yosemite 10.10

安裝版本

Apache 2.4.10
PHP 5.6.2
MySQL 5.6.21

安裝步驟如下

1. 安裝 Mac 套件管理 - Homebrew

如果系統還沒安裝 Homebrew,可以依照下面指令安裝。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew doctor
brew update

2. 使用 Homebrew 安裝 git 版本控制

透過 Homebrew 可以輕鬆安裝想要的套件,例如 git

brew install -v git

3. 確保 Homebrew 安裝套件的優先性

系統本身會內建一些套件,我們必須執行以下指令來確保我們使用 Homebrew 安裝更新版套件的優先使用性。

echo "export PATH=\$(echo \$PATH | sed 's|/usr/local/bin||; s|/usr/local/sbin||; s|::|:|; s|^:||; s|\(.*\)|/usr/local/bin:/usr/local/sbin:\1|')" >> ~/.bash_profile && source ~/.bash_profile

4. 安裝 Command Lind Developer Tools

後續都必須用到的工具,請務必安裝,避免會發生安裝上的錯誤。

xcode-select --install

5. 安裝 MySQL

使用以下指令安裝最新版 MySQL

brew install -v mysql

備註: 指定安裝版本可參考指令

brew install mysql55
設置 MySQL 環境

新增設置需要的 MySQL 設定檔 - my.cnf

cp -v $(brew --prefix mysql)/support-files/my-default.cnf $(brew --prefix mysql)/my.cnf
cat >> $(brew --prefix mysql)/my.cnf <<'EOF'
# Echo & Co. changes
max_allowed_packet = 2G
innodb_file_per_table = 1
EOF
sed -i '' 's/^# \(innodb_buffer_pool_size\)/\1/' $(brew --prefix mysql)/my.cnf

使用 OS X 的 launchd,於系統啟動時執行 MySQL

[ ! -d ~/Library/LaunchAgents ] && mkdir -v ~/Library/LaunchAgents
[ -f $(brew --prefix mysql)/homebrew.mxcl.mysql.plist ] && ln -sfv $(brew --prefix mysql)/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
[ -e ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist ] && launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

在預設上, MySQL 的 root 使用者帳號是沒有密碼的,你可以使用以下指令來設置想要的密碼。

$(brew --prefix mysql)/bin/mysql_secure_installation

6. 安裝 Apache

一開始必須停止內建的 Apache,如果之前已經執行,可使用以下指令來避免其於系統啟動時執行。

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Apache 並不是在 Homebrew formulas 預設的 repository 中,我們必須透過 brew tap 指令去新增其他的 formula repositories。

brew tap homebrew/dupes
brew tap homebrew/apache

我們將安裝 Apache 2.4 ( with OpenSSL form Homebrew ),來取代系統內建的 Apache。

brew install -v httpd24 --with-brewed-openssl

在這裡我們將使用 ~/Sites 目錄來管理有關 Apache 的設定檔以及 Log 記錄檔,使先使用 ~/Sites/httpd-vhosts.conf 來配置我們的虛擬主機。

[ ! -d ~/Sites ] && mkdir -pv ~/Sites
touch ~/Sites/httpd-vhosts.conf
USERHOME=$(dscl . -read /Users/`whoami` NFSHomeDirectory | awk -F": " '{print $2}') cat >> $(brew --prefix)/etc/apache2/2.4/httpd.conf <<EOF
# Include our VirtualHosts
Include ${USERHOME}/Sites/httpd-vhosts.conf
EOF

新增資料夾管理 Log 記錄檔。

[ ! -d ~/Sites/logs ] && mkdir -pv ~/Sites/logs
設定 httpd.conf

新增 symbol link 至 ~/Sites

ln -s $(brew --prefix)/etc/apache2/2.4/httpd.conf ~/Sites/

unmark 所需的 modules

LoadModule vhost_alias_module libexec/mod_vhost_alias.so
LoadModule rewrite_module libexec/mod_rewrite.so

更改一些設定

Options Indexes FollowSymLinks Includes ExecCGI
<IfModule dir_module>
    DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
                   default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
                   home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>

現在,來設定先前我們讓 httpd.conf 載入的 ~/Sites/httpd-vhosts.conf 內容

USERHOME=$(dscl . -read /Users/`whoami` NFSHomeDirectory | awk -F": " '{print $2}') cat > ~/Sites/httpd-vhosts.conf <<EOF
#
# Set up permissions for VirtualHosts in ~/Sites/htdocs
#
<Directory "${USERHOME}/Sites/htdocs">
  Options Indexes FollowSymLinks Includes ExecCGI
  AllowOverride All
  Require all granted
</Directory>

# For http://localhost in the users' Sites/htdocs folder

# This log format will display the per-virtual-host as the first field followed by a typical log line
LogFormat "%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedmassvhost

<VirtualHost *:8080>
  ServerName dev
  ServerAlias *.dev

  CustomLog "${USERHOME}/Sites/logs/dev-access_log" combinedmassvhost
  ErrorLog "${USERHOME}/Sites/logs/dev-error_log"

  VirtualDocumentRoot ${USERHOME}/Sites/htdocs/%-2+
</VirtualHost>
EOF

7. RUN WITH PORT 80

新增 Anchor file

sudo nano /etc/pf.anchors/apache

加入下面文字

rdr pass on lo0 inet proto tcp from any to 127.0.0.1 port = 80 -> 127.0.0.1 port 8080

設定 pf.conf,設定完成時會如下所示

scrub-anchor "com.apple/*"
nat-anchor "com.apple/*"
rdr-anchor "com.apple/*"
rdr-anchor "apache"  # Apache port forwarding
dummynet-anchor "com.apple/*"
anchor "com.apple/*"
load anchor "com.apple" from "/etc/pf.anchors/com.apple"
load anchor "apache" from "/etc/pf.anchors/apache"  # Apache port forwarding

啟動 pf

sudo pfctl -ef /etc/pf.conf

為了讓 pf 於每次系統啟動時執行,我們必須做以下步驟

sudo nano /System/Library/LaunchDaemons/com.apple.pfctl.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <!--<key>Disabled</key>
    <false/>-->
    <key>Label</key>
    <string>com.apple.pfctl</string>
    <key>WorkingDirectory</key>
    <string>/var/run</string>
    <key>Program</key>
    <string>/sbin/pfctl</string>
    <key>ProgramArguments</key>
    <array>
        <string>pfctl</string>
        <string>-e</string>
        <string>-f</string>
        <string>/etc/pf.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

8. 安裝 PHP

我們必須先取得 PHP 的 formula repository

brew tap homebrew/php
brew tap homebrew/versions
brew update && brew upgrade

進行安裝 PHP,下面指令可以自動在 httpd.conf 中新增載入 PHP module

brew install -v php56 --homebrew-apxs --with-apache

新增 PHP 配置參數至 Apache httpd.conf 檔中

cat >> $(brew --prefix)/etc/apache2/2.4/httpd.conf <<EOF

# Send PHP extensions to mod_php
AddHandler php5-script .php
AddType text/html .php
EOF

設置 timezone 和變更其他 PHP 設定

sed -i '-default' "s|^;\(date\.timezone[[:space:]]*=\).*|\1 \"$(sudo systemsetup -gettimezone|awk -F": " '{print $2}')\"|; s|^\(memory_limit[[:space:]]*=\).*|\1 256M|; s|^\(post_max_size[[:space:]]*=\).*|\1 200M|; s|^\(upload_max_filesize[[:space:]]*=\).*|\1 100M|; s|^\(default_socket_timeout[[:space:]]*=\).*|\1 600|; s|^\(max_execution_time[[:space:]]*=\).*|\1 300|; s|^\(max_input_time[[:space:]]*=\).*|\1 600|;" $(brew --prefix)/etc/php/5.6/php.ini

新增 PHP error log

USERHOME=$(dscl . -read /Users/`whoami` NFSHomeDirectory | awk -F": " '{print $2}') cat >> $(brew --prefix)/etc/php/5.6/php.ini <<EOF

; PHP Error log
error_log = ${USERHOME}/Sites/logs/php-error_log
EOF

This weird little "hack" is needed to fix a permissions problem with using pear or pecl.

touch $(brew --prefix php56)/lib/php/.lock && chmod 0644 $(brew --prefix php56)/lib/php/.lock

9. 啟動 Apache

啟動 Homebrew 的 Apache,並且設定系統啟動時執行

ln -sfv $(brew --prefix httpd24)/homebrew.mxcl.httpd24.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.httpd24.plist

10. DNSMasq

以下設定可以讓任何以 .dev 結尾的 DNS request,指向 IP 位址 127.0.0.1

brew install -v dnsmasq
echo 'address=/.dev/127.0.0.1' > $(brew --prefix)/etc/dnsmasq.conf
echo 'listen-address=127.0.0.1' >> $(brew --prefix)/etc/dnsmasq.conf

Because DNS services run on a lower port, we need to have this run out of /Library/LaunchDaemons, so we do need to use sudo for the initial setup.

sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons 
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist

With DNSMasq running, configure OS X to use your local host for DNS queries ending in .dev

sudo mkdir -v /etc/resolver 
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/dev'
OS X EI Capitan 10.11.1 安裝設定 RVM + Ruby on Rails →
 
comments powered by Disqus