在Mac OS下搭建LNMP開發(fā)環(huán)境的步驟詳解
一、概述
大家應該都知道LNMP代表的就是:Linux系統(tǒng)下Nginx+MySQL+PHP這種網站服務器架構。Linux是一類Unix計算機操作系統(tǒng)的統(tǒng)稱,是目前最流行的免費操作系統(tǒng)。代表版本有:debian、centos、ubuntu、fedora、gentoo等。Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。Mysql是一個小型關系型數據庫管理系統(tǒng)。PHP是一種在服務器端執(zhí)行的嵌入HTML文檔的腳本語言。這四種軟件均為免費開源軟件,組合到一起,成為一個免費、高效、擴展性強的網站服務系統(tǒng)。下面來看看本文的詳細內容吧。
二、安裝Homebrew
使用Mac的程序員必不可少的一步便是安裝Homebrew,他就像是centOS的yum
命令和ubuntu的apt-get
命令一樣,通過brew
命令,我們可以快速的安裝一些軟件包。
使用命令行安裝Homebrew的命令如下:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
使用brew doctor
檢查是否存在沖突,然后使用brew update && brew upgrade
對brew進行升級。
三、安裝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是否安裝成功,因為默認配置文件監(jiān)聽的是8080端口,所以先對8080端口發(fā)起請求:
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 //重新加載|重啟|退出
四、安裝php-fpm
因為brew并沒有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
程序會自動安裝,等待幾分鐘后完成安裝。
安裝完成后,還需要將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/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
使用以下命令監(jiān)測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)
五、安裝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退出數據庫
六、配置nginx
首先,為我們的配置文件創(chuàng)建一些文件夾,這些是仿照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 /var/www sudo chown :staff /var/www sudo chmod 775 /var/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/*; }
然后創(chuàng)建php-fpm配置文件:
vim /usr/local/ect/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 /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下寫一個測試文件,進行測試即可
總結
以上就是在Mac OS上搭建LNMP開發(fā)環(huán)境的全部步驟了,盡情的享受在Mac OS開發(fā)PHP的快感吧!希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
PHP過濾器 filter_has_var() 函數用法實例分析
這篇文章主要介紹了PHP過濾器 filter_has_var() 函數用法,結合實例形式分析了PHP過濾器 filter_has_var() 函數基本功能、原理、用法及操作注意事項,需要的朋友可以參考下2020-04-04php使用ZipArchive函數實現(xiàn)文件的壓縮與解壓縮
這篇文章主要介紹了php使用ZipArchive函數實現(xiàn)文件的壓縮與解壓縮,需要的朋友可以參考下2015-10-10