Linux + Nginx + Mysql + PHP 本地環境搭建配置

來源:互聯網
上載者:User

1、首先我們需要下載Homebrew,Homebrew簡稱brew,是Mac OSX上的軟體包管理工具,能在Mac中方便的安裝軟體或者卸載軟體,可以說Homebrew相當於Linux下的apt-get、yum,它的官方網址是 http://brew.sh ,開啟終端輸入:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Homebrew使用

搜尋軟體:brew search 軟體名,如 brew search wget

安裝軟體:brew install 軟體名,如 brew install wget

卸載軟體:brew remove 軟體名,如 brew remove wget

2、安裝好後我們下載並安裝Nginx,開啟終端輸入:

brew install nginx
安裝好了後我們可以看到:

 

ok,我們現在直接啟動nginx:

$ sudo nginx
輸入:ps aux | grep nginx ,可以看到nginx進程已啟動:

 

現在我們再開啟瀏覽器訪問127.0.0.1(或localhost)可以看到下面的效果:

 

這時候我們想讓這個頁面顯示成我們想要顯示的頁面,怎麼辦呢?

開啟nginx.conf檔案,終端輸入:

vi /usr/local/etc/nginx/nginx.conf
找到以下代碼並修改成如下代碼:


server {
        listen       8080;
        server_name  localhost;

        charset koi8-r;

        access_log  /usr/local/etc/nginx/logs/host.access.log  main;

        location / {
            root   /Users/Evai/web; #你存放的PHP頁面路徑
            index  index.php;
        }
}

繼續往下找並修改,這段代碼是解析php檔案的:


location ~ \.php$ {
            root           /Users/Evai/web; #你存放的php頁面路徑
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
}

儲存好檔案後,重新載入配置,輸入:

nginx -s reload
 

nginx 常用命令:

#啟動 nginx
sudo nginx
#   重新載入配置 | 重啟 | 停止 |退出
nginx -s reload|reopen|stop|quit
#測試組態是否有語法錯誤
nginx -t
3、接下來是安裝PHP,PHP在mac下預設安裝了,但是不好控製版本,所以我們用brew可以再mac下安裝最新版本,甚至是多個版本,我裝了php5.6,完整安裝路徑如下:


brew update
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew install php56 --with-fpm --without-apache --without-snmp --without-apache --with-debug --with-fpm --with-intl --with-homebrew-curl --with-homebrew-libxslt --with-homebrew-openssl --with-imap --with-mysql --with-tidy

接下來在 ~/.bashrc 檔案裡加入以下環境變數:

export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
同時將php-fpm加入開機啟動:

ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
運行 php -v 查看:

 

4、最後是安裝Mysql,一樣的方法:

brew install mysql
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
sudo chown -R 777 /usr/local/var/mysql/
第一句是安裝,後面的是確保正常使用。可以通過以下命令啟動或停止:

mysql.server start
mysql.server stop
啟動後預設應為空白密碼,可以通過mysqladmin設定一個密碼:

mysqladmin -uroot password "你的密碼"
最後將Mysql加入開機啟動:

cp /usr/local/Cellar/mysql/5.6.22/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
結束,環境配置完成!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.