在MAC系統下進行PHP開發,最簡單的方法是到http://www.apachefriends.org/en/xampp-macosx.html下載個文件,裝上就可直接進行開發了,好處很明顯,簡單易用。
使用MacPorts配置PHP開發環境(PHP54+PHP FPM+NGINX+MYSQL55),首先需要安裝port,訪問:http://www.macports.org/install.php,直接下載源代碼: https://distfiles.macports.org/MacPorts/MacPorts-2.1.3.tar.gz。
安裝port:
1. tar xzvf MacPorts-2.1.3.tar.gz 解壓
2. cd MacPorts-2.1.3
3. ./configure && make && sudo make install
這樣就安裝好port了。
4. vi ~/.bash_profile
按i鍵進入編輯方式,粘下以下文字:
export PATH=/opt/local/bin:$PATH
按ESC鍵退出編輯方式,按:鍵再輸入wq保存退出。
現在我們就可以很方便地使用port了。
安裝php:
1. port info php 列出所支持的php信息,可以根據需要來選對應的版本安裝,我選php54這個版本。
2. sudo port install php54 php54-fpm php54-mysql php54-mbstring php54-mcrypt php54-sockets php54-curl 可以根據需要增加或者減少
3. cd /opt/local/etc/php54/
4. sudo cp ./php-fpm.conf.default ./php-fpm.conf
5. sudo cp ./php.ini-development ./php.ini 可以根據需要換成php.ini-production的
6. vi ~/.bash_profile 加上以下內容:
#php-fpm
alias fpm_start='sudo launchctl load -w /Library/LaunchDaemons/org.macports.php54-fpm.plist'
alias fpm_stop='sudo launchctl unload -w /Library/LaunchDaemons/org.macports.php54-fpm.plist'
alias fpm_restart='fpm_stop; fpm_start'
7. 打開個新的命令窗口,輸入:fpm_start,php-fpm就開始運行了,可以用top命令查看或者用ps -e | grep php-fpm查看。
這樣子php就已經搞定了。
nginx:
1. sudo port install nginx
2. vi ~/.bash_profile 加上以下內容:
# nginx
alias nginx_start='sudo launchctl load -w /Library/LaunchDaemons/org.macports.nginx.plist'
alias nginx_stop='sudo launchctl unload -w /Library/LaunchDaemons/org.macports.nginx.plist'
alias nginx_restart='nginx_stop; nginx_start;'
3. cd /opt/local/etc/nginx/
4. sudo cp fastcgi.conf.example fastcgi.conf 如此類推,把那些以.example為后綴的拷出一份沒有.example為后綴的文件來。
5. sudo vi nginx.conf 找到
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 這段,內容改成這樣子:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /opt/local/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
其實就是去掉前面的#號和改了一下fastcgi_param SCRIPT_FILENAME /opt/local/share/nginx/html$fastcgi_script_name;
6. nginx_start就可以啟動了
7. cd /opt/local/share/nginx/html/
8. vi phpinfo.php 加入以下內容:
<?php phpinfo(); ?>
9. 通過瀏覽器訪問:http://localhost/phpinfo.php 完成。
MYSQL:
1. sudo port install mysql55 再根據安裝完成后的內容設置數據庫
2. vi ~/.bash_profile 加上以下內容:
#mysql
alias mysql_start='sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql55-server.plist'
alias mysql_stop='sudo launchctl unload -w /Library/LaunchDaemons/org.macports.mysql55-server.plist'
alias mysql_restart='mysql_stop; mysql_start'
3. mysql_start
4. sudo vi /opt/local/ect/mysql55/my.cnf 注掉# !include /opt/local/etc/mysql55/macports-default.cnf 這一行,再加上: [mysqld_safe] socket = /tmp/mysql.sock, 否則mysql命令可以連接,工具連不上。