MACOS下安装PHP运行环境

MACOS自带了Apache和PHP

使用Apache

启动、关闭、重启Apache命令如下:
开启: sudo apachectl start
停止: sudo apachectl stop
重启: sudo apachectl restart

查看Apache版本
httpd -v 或 sudo apachectl -v
查找Apache执行文件
which apachectl

使用PHP

查找PHP执行文件 which php
如果不存在就创建符号链接
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

脚本会在执行前暂停,并说明将它将做什么,效果如上图,点击enter继续。
安装完成之后执行: brew update

常用的brew命令如下:

brew搜索软件 brew search
brew安装软件 brew install
brew卸载软件 brew uninstall
brew软件信息 brew info
其他命令请使用查看 brew help

2.安装 Nginx

执行两条命令:
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

执行两条命令:
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

执行两条命令:
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执行文件 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;#监听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,请参照以下步骤,不要盲目复制:
``# 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``


**如有错误 ,请指正,谢谢~~~**
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容