Mac OS上搭建LNMP開發環境
1. 概述
LNMP代表的就是:Linux系統下Nginx+MySQL+PHP這種網站伺服器架構。Linux是一類Unix計算機作業系統的統稱,是目前最流行的免費作業系統。代表版本有:debian、centos、ubuntu、fedora、gentoo等。Nginx是一個高效能的HTTP和反向代理伺服器,也是一個IMAP/POP3/SMTP代理伺服器。Mysql是一個小型關係型資料庫管理系統。PHP是一種在伺服器端執行的嵌入HTML文件的指令碼語言。這四種軟體均為免費開源軟體,組合到一起,成為一個免費、高效、擴充套件性強的網站服務系統。
2. 安裝Homebrew
使用Mac的程式設計師必不可少的一步便是安裝Homebrew,他就像是centOS的yum
apt-get
命令一樣,通過brew
命令,我們可以快速的安裝一些軟體包。 使用命令列安裝Homebrew的命令如下:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
使用brew doctor
檢查是否存在衝突,然後使用brew update && brew upgrade
對brew進行升級。
3. 安裝nginx
nginx在Mac OS中可以直接使用brew命令進行安裝:
brew install nginx
如果需要使用80埠的話,需要將nginx加入root組當中:
sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/
sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
然後使用命令啟動nginx服務:
sudo nginx
測試nginx是否安裝成功,因為預設配置檔案監聽的是8080埠,所以先對8080埠發起請求:
curl -IL http://127.0.0.1:8080
結果應該類似於下:
HTTP/1.1 200 OK
Server: nginx/1.9.1
Date: Fri, 29 May 2015 14:50:47 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 29 May 2015 14:40:47 GMT
Connection: keep-alive
ETag: “5444dea7-264”
Accept-Ranges: bytes
nginx的相關操作如下:
sudo nginx //啟動nginx
sudo nginx -s reload|reopen|quit //重新載入|重啟|退出
4. 安裝php-fpm
因為brew並沒有php-fpm的源,所以首先要新增源:
brew tap homebrew/dupes
brew tap homebrew/php
然後安裝php-fpm,輸入命令:
# 在此我們安裝php7.1 如果想安裝php5.6等只需要將php71改為php56等
# 安裝之前可以檢視安裝選項 brew options homebrew/php/php71
brew install php71 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm
程式會自動安裝,等待幾分鐘後完成安裝。
安裝完成後,還需要將php加入$PATH
當中:
# 如果使用bash的話
vim ~/.bash_profile
export PATH="/usr/local/sbin:$PATH"
source ~/.bash_profile
# 如果使用ZSH的話
vim ~/.zshrc
export PATH="/usr/local/sbin:$PATH"
source ~/.zshrc
然後可以設定php-fpm的開機自啟動:
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist
使用以下命令監測php-fpm是否啟動成功:
lsof -Pni4 | grep LISTEN | grep php
如果啟動成功應當有以下類似輸出:
php-fpm 27578 wenzhiquan 9u IPv4 0xf29f8b26c08fc27 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 27628 wenzhiquan 0u IPv4 0xf29f8b26c08fc27 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 27629 wenzhiquan 0u IPv4 0xf29f8b26c08fc27 0t0 TCP 127.0.0.1:9000 (LISTEN)
php-fpm 27630 wenzhiquan 0u IPv4 0xf29f8b26c08fc27 0t0 TCP 127.0.0.1:9000 (LISTEN)
5. 安裝MySQL
MySQL也可以使用brew命令直接進行安裝:
brew install mysql
同樣,可以設定MySQL的開機自啟動:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
然後進行MySQL的安全安裝,使用以下命令,可以更改root密碼、刪除匿名使用者、關閉遠端連線等:
mysql_secure_installation
然後會輸出以下內容:
Enter current password for root (enter for none): //預設沒有密碼,直接回車即可
Change the root password? [Y/n] //是否更改root密碼,選擇是,然後輸入並確認密碼
Remove anonymous users? [Y/n] //是否刪除匿名使用者,選擇是
Disallow root login remotely? [Y/n] //是否禁止遠端登入,選擇是
Remove test database and access to it? [Y/n] //是否刪除test資料庫,選擇是
Reload privilege tables now? [Y/n] //是否過載表格資料,選擇是
測試資料庫是否安裝成功:
mysql -u root -p
然後輸入剛才設定的root密碼,將會輸出以下內容:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> exit //輸入exit退出資料庫
6. 配置nginx
首先,為我們的配置檔案建立一些資料夾,這些是仿照ubuntu的nginx結構進行建立的目錄:
mkdir -p /usr/local/etc/nginx/logs
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
sudo mkdir -p /Users/zhangweipeng/www
sudo chown :staff /Users/zhangweipeng/www
sudo chmod 775 /Users/zhangweipeng/www
然後修改nginx配置檔案:
vim /usr/local/etc/nginx/nginx.conf
將內容替換為:
worker_processes 1;
error_log /usr/local/etc/nginx/logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /usr/local/etc/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 65;
index index.html index.php;
include /usr/local/etc/nginx/sites-enabled/*;
}
然後建立php-fpm配置檔案:
vim /usr/local/etc/nginx/conf.d/php-fpm
輸入以下內容:
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
然後加入站點配置檔案:
vim /usr/local/ect/nginx/sites-enabled/default
輸入以下內容:
server {
listen 80;
server_name localhost;
root /Users/zhangweipeng/www;
access_log /usr/local/etc/nginx/logs/default.access.log main;
location / {
include /usr/local/etc/nginx/conf.d/php-fpm;
#如果想檢視www目錄下的檔案列表而不是自動載入index檔案可使用:
#autoindex on;
}
location = /info {
allow 127.0.0.1;
deny all;
rewrite (.*) /.info.php;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
重啟nginx,至此,配置完成,在/Users/zhangweipeng/www下寫一個測試檔案,進行測試即可
#重啟nginx指令
sudo nginx -s reload
盡情的享受在Mac OS開發PHP的快感吧!
補充內容
安裝擴充套件
使用brew search php72-
命令,可以檢視還有哪些擴充套件可以安裝,然後執行brew install php72-XXX
就可以了
- #### 安裝redis擴充套件
brew install php71-redis --build-from-source
brew services start redis
ps aux | grep php-fpm
sudo killall php-fpm
sudo php71-fpm start