MACOS下安裝PHP運行環(huán)境

MACOS自帶了Apache和PHP

使用Apache

啟動、關(guān)閉、重啟Apache命令如下:
開啟: sudo apachectl start
停止: sudo apachectl stop
重啟: sudo apachectl restart

查看Apache版本
httpd -v 或 sudo apachectl -v
查找Apache執(zhí)行文件
which apachectl

使用PHP

查找PHP執(zhí)行文件 which php
如果不存在就創(chuàng)建符號鏈接
ln -s /xxx/xxx/php /usr/local/bin/php 注意一定要寫絕對路徑
查看PHP版本
php -v 或 php -verseion
PHP的配置非常簡單,進到/etc/apache2/目錄,編輯httpd.conf,找到LoadModule php5_module libexec/apache2/libphp5.so放開注釋即可。

上面介紹了MACOS自帶的Apache和PHP,這里樓主將介紹如何使用brew安裝及管理PHP\NGINX\MYSQL等軟件。

1.安裝 Homebrew

將以上命令粘貼至終端
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Paste_Image.png

腳本會在執(zhí)行前暫停,并說明將它將做什么,效果如上圖,點擊enter繼續(xù)。
安裝完成之后執(zhí)行: brew update

常用的brew命令如下:

brew搜索軟件 brew search
brew安裝軟件 brew install
brew卸載軟件 brew uninstall
brew軟件信息 brew info
其他命令請使用查看 brew help

2.安裝 Nginx

執(zhí)行兩條命令:
brew search nginx
brew install nginx
如下圖,因為樓主是安裝過,所以nginx后面就多了一個?

Paste_Image.png
查看Nginx信息,如下圖:
brew info nginx
Paste_Image.png

使用Nginx

啟動nginx:brew services start nginx 或 sudo nginx
停止nginx:brew services stop nginx
重啟nginx:brew services restart nginx
查看nginx配置文件:sudo nginx -t

3.安裝PHP

執(zhí)行兩條命令:
brew search php56
brew install php56
如下圖,因為樓主是安裝過,所以php56后面就多了一個?

Paste_Image.png
查看php56信息,同Nginx:
brew info php56

使用PHP

啟動php: /usr/local/opt/php54/sbin/php56-fpm start
停止php: /usr/local/opt/php54/sbin/php56-fpm stop
重啟php: /usr/local/opt/php54/sbin/php56-fpm restart
查看php版本: php56 -v 或 php56 --version
查看php信息: php56 -i
查看php配置: php56 -i | grep php.ini
更多php信息: php56 -h

4.安裝MYSQL

執(zhí)行兩條命令:
brew search mysql
brew install mysql
如下圖,因為樓主是安裝過,所以mysql后面就多了一個?

Paste_Image.png

查看mysql信息,同Nginx:
brew info mysql

使用MYSQL

啟動mysql: /brew services start mysql 或 mysql.server start
停止mysql: mysql.server stop
重啟mysql: mysql.server restart
查看mysql進程: ps -ef | grep mysql
連接mysql: mysql 或 mysql -u root -h 127.0.0.1 -p

Paste_Image.png
如果mysql命令找不到,請使用 ln -s /xxx/xxx/mysql /usr/local/bin/mysql
查找mysql執(zhí)行文件 which mysql

5.配置NGINX

使用sudo nginx -t查找nginx.conf
打開nginx.conf ,在http塊添加一行include servers/*.conf;
增加default.conf文件,添加server塊,如下:

        listen       80;#端口號
        server_name  127.0.0.1;
        root /Users/xxx/project;#項目目錄
        if ( $host ~* (.*)\.(.*)\.(.*)){
            set $domain $1;
        }
        location / {
            index  index.php index.html index.htm;
        }
        location ~ .+\.php($|/) {
            include        fastcgi_params;
            set $real_script_name $uri;
            set $path_info "/";
            if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
                set $real_script_name $1;
                set $path_info $2;
            }
            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
            fastcgi_pass   127.0.0.1:9000;#監(jiān)聽9000端口
            fastcgi_index  index.php;
        }
  }```

重啟NGINX: ``sudo brew services restart nginx``
最后,打開瀏覽器訪問:http://127.0.0.1/info.php

![Paste_Image.png](http://upload-images.jianshu.io/upload_images/5983766-7722625bf5462eeb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

使用 `` brew list ``查看已安裝的軟件
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/5983766-a917f4d214a40c67.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
#####至此,brew安裝PHP\NGINX\MYSQL成功。



如要編譯安裝PHP,請參照以下步驟,不要盲目復(fù)制:
``# wget http://cn2.php.net/get/php-7.0.0.tar.gz/from/this/mirror ``
``# wget http://cn2.php.net/get/php-5.3.29.tar.gz/from/this/mirror ``
``# wget http://cn2.php.net/get/php-5.6.19.tar.gz/from/this/mirror ``
``# wget http://cn2.php.net/get/php-5.6.29.tar.gz/from/this/mirror ``
``# tar -zxvf mirror ``
``# cd php-5.6.29/``
```# ./configure --prefix=/usr/local/php56 --sysconfdir=/etc/php56 --with-config-file-path=/etc/php56 --enable-fpm --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mhash --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib --enable-mbstring --with-mcrypt --enable-sockets --with-iconv-dir --with-xsl --enable-zip --with-pcre-dir --with-pear --enable-session  --enable-gd-native-ttf --enable-xml --with-freetype-dir --enable-gd-jis-conv --enable-inline-optimization --enable-shared --enable-bcmath --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mbregex --enable-pcntl --with-xmlrpc --with-gettext --enable-exif --with-readline --with-recode --with-tidy  ```
``# make && make install``
``# cp php.ini-development /usr/local/php56/etc/php.ini``
``# cp php-fpm.conf.default /usr/local/php56/etc/php-fpm.conf``
``# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm``
``# chmod +x /etc/init.d/php-fpm``
``# service php-fpm start``


**如有錯誤 ,請指正,謝謝~~~**
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容