在Mac OS下搭建LNMP開發(fā)環(huán)境的步驟詳解
一、概述
大家應(yīng)該都知道LNMP代表的就是:Linux系統(tǒng)下Nginx+MySQL+PHP這種網(wǎng)站服務(wù)器架構(gòu)。Linux是一類Unix計(jì)算機(jī)操作系統(tǒng)的統(tǒng)稱,是目前最流行的免費(fèi)操作系統(tǒng)。代表版本有:debian、centos、ubuntu、fedora、gentoo等。Nginx是一個(gè)高性能的HTTP和反向代理服務(wù)器,也是一個(gè)IMAP/POP3/SMTP代理服務(wù)器。Mysql是一個(gè)小型關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)。PHP是一種在服務(wù)器端執(zhí)行的嵌入HTML文檔的腳本語(yǔ)言。這四種軟件均為免費(fèi)開源軟件,組合到一起,成為一個(gè)免費(fèi)、高效、擴(kuò)展性強(qiáng)的網(wǎng)站服務(wù)系統(tǒng)。下面來(lái)看看本文的詳細(xì)內(nèi)容吧。
二、安裝Homebrew
使用Mac的程序員必不可少的一步便是安裝Homebrew,他就像是centOS的yum
命令和ubuntu的apt-get
命令一樣,通過(guò)brew
命令,我們可以快速的安裝一些軟件包。
使用命令行安裝Homebrew的命令如下:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
使用brew doctor
檢查是否存在沖突,然后使用brew update && brew upgrade
對(duì)brew進(jìn)行升級(jí)。
三、安裝nginx
nginx在Mac OS中可以直接使用brew命令進(jìn)行安裝:
brew install nginx
如果需要使用80端口的話,需要將nginx加入root組當(dāng)中:
sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
然后使用命令啟動(dòng)nginx服務(wù):
sudo nginx
測(cè)試nginx是否安裝成功,因?yàn)槟J(rèn)配置文件監(jiān)聽的是8080端口,所以先對(duì)8080端口發(fā)起請(qǐng)求:
curl -IL http://127.0.0.1:8080
結(jié)果應(yīng)該類似于下:
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的相關(guān)操作如下:
sudo nginx //啟動(dòng)nginx sudo nginx -s reload|reopen|quit //重新加載|重啟|退出
四、安裝php-fpm
因?yàn)閎rew并沒有php-fpm的源,所以首先要添加源:
brew tap homebrew/dupes brew tap homebrew/php
然后安裝php-fpm,輸入命令:
brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm
程序會(huì)自動(dòng)安裝,等待幾分鐘后完成安裝。
安裝完成后,還需要將php加入$PATH
當(dāng)中:
# 如果使用bash的話 vim ~/.bash_profile export PATH="/usr/local/sbin:$PATH" source ~/.bash_profile # 如果使用ZSH的話 vim ~/.zshrc export PATH="/usr/local/sbin:$PATH" source ~/.zshrc
然后可以設(shè)置php-fpm的開機(jī)自啟動(dòng):
mkdir -p ~/Library/LaunchAgents ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
使用以下命令監(jiān)測(cè)php-fpm是否啟動(dòng)成功:
lsof -Pni4 | grep LISTEN | grep php
如果啟動(dòng)成功應(yīng)當(dāng)有以下類似輸出:
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)
五、安裝MySQL
MySQL也可以使用brew命令直接進(jìn)行安裝:
brew install mysql
同樣,可以設(shè)置MySQL的開機(jī)自啟動(dòng):
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
然后進(jìn)行MySQL的安全安裝,使用以下命令,可以更改root密碼、刪除匿名用戶、關(guān)閉遠(yuǎn)程連接等:
mysql_secure_installation
然后會(huì)輸出以下內(nèi)容:
> Enter current password for root (enter for none): //默認(rèn)沒有密碼,直接回車即可 > Change the root password? [Y/n] //是否更改root密碼,選擇是,然后輸入并確認(rèn)密碼 > Remove anonymous users? [Y/n] //是否刪除匿名用戶,選擇是 > Disallow root login remotely? [Y/n] //是否禁止遠(yuǎn)程登錄,選擇是 > Remove test database and access to it? [Y/n] //是否刪除test數(shù)據(jù)庫(kù),選擇是 > Reload privilege tables now? [Y/n] //是否重載表格數(shù)據(jù),選擇是
測(cè)試數(shù)據(jù)庫(kù)是否安裝成功:
mysql -u root -p
然后輸入剛才設(shè)置的root密碼,將會(huì)輸出以下內(nèi)容:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> exit //輸入exit退出數(shù)據(jù)庫(kù)
六、配置nginx
首先,為我們的配置文件創(chuàng)建一些文件夾,這些是仿照ubuntu的nginx結(jié)構(gòu)進(jìn)行建立的目錄:
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 /var/www sudo chown :staff /var/www sudo chmod 775 /var/www
然后修改nginx配置文件:
vim /usr/local/etc/nginx/nginx.conf
將內(nèi)容替換為:
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/*; }
然后創(chuàng)建php-fpm配置文件:
vim /usr/local/ect/nginx/conf.d/php-fpm
輸入以下內(nèi)容:
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; }
然后加入站點(diǎn)配置文件:
vim /usr/local/ect/nginx/sites-enabled/default
輸入以下內(nèi)容:
server { listen 80; server_name localhost; root /var/www/; access_log /usr/local/etc/nginx/logs/default.access.log main; location / { include /usr/local/etc/nginx/conf.d/php-fpm; } location = /info { allow 127.0.0.1; deny all; rewrite (.*) /.info.php; } error_page 404 /404.html; error_page 403 /403.html; }
重啟nginx,至此,配置完成,在www下寫一個(gè)測(cè)試文件,進(jìn)行測(cè)試即可
總結(jié)
以上就是在Mac OS上搭建LNMP開發(fā)環(huán)境的全部步驟了,盡情的享受在Mac OS開發(fā)PHP的快感吧!希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
php中用date函數(shù)獲取當(dāng)前時(shí)間有誤的解決辦法
以下是對(duì)php中用date函數(shù)獲取當(dāng)前時(shí)間有誤的解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08php實(shí)現(xiàn)比較兩個(gè)字符串日期大小的方法
這篇文章主要介紹了php實(shí)現(xiàn)比較兩個(gè)字符串日期大小的方法,涉及php日期操作的相關(guān)技巧,需要的朋友可以參考下2015-05-05PHP過(guò)濾器 filter_has_var() 函數(shù)用法實(shí)例分析
這篇文章主要介紹了PHP過(guò)濾器 filter_has_var() 函數(shù)用法,結(jié)合實(shí)例形式分析了PHP過(guò)濾器 filter_has_var() 函數(shù)基本功能、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04php使用ZipArchive函數(shù)實(shí)現(xiàn)文件的壓縮與解壓縮
這篇文章主要介紹了php使用ZipArchive函數(shù)實(shí)現(xiàn)文件的壓縮與解壓縮,需要的朋友可以參考下2015-10-10