mac mojave下安装php+apache遇到的问题 #16

mac mojave系统默认安装的php版本是7.1+, 目前php开发环境有一大部分人php版本还是在5.6+左右,如果使用mac默认的apache+php5就存在兼容的问题。所以要重新安装apache替换掉系统默认的版本。

1、安装php(已经安装请忽略) 跳转至:https://php-osx.liip.ch 选择需要安装的版本 这里介绍的apache+php5

curl -s https://php-osx.liip.ch/install.sh | bash -s 5.6

安装好php5版本后,我们先使用系统默认的apache看看是否正常工作,配置apache支持php

php安装之后但是系统默认的还是7.1+。

php -v 或者 which php 查看命令

首先做php版本切换,打开

sudo vim ~/.bash_profile //php bin 路径 export PATH=/usr/local/php5/bin:$PATH

保存 重载环境变量

source ~/.bash_profile

再次查看是否已经切换到php5.6了

php -v 或者 which php 查看命令

cd /etc/apache2
sudo vim  ./httpd.conf

找到

#LoadModule php7_module libexec/apache2/libphp7.so
LoadModule php5_module /usr/local/php5/libphp5.so
#LoadModule php5_module libexec/apache2/libphp5.so
#LoadModule perl_module libexec/apache2/mod_perl.so
LoadModule hfs_apple_module libexec/apache2/mod_hfs_apple.so

这个片段 把php7注释 添加 php5 module支持 libphp5.so 在php安装目录下填写完成的路径 我这里是

/usr/local/php5/libphp5.so

另外记得修改访问配置

//注意1
<Directory />
Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
    Order deny,allow
    Allow from all
</Directory>

//注意2
DocumentRoot "工程目录"
<Directory "工程目录">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    #Options Indexes FollowSymLinks
    Options FollowSymLinks Multiviews
    MultiviewsMatch Any

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride All
    Order deny,allow
    Allow from all 
    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

不然会出现403错误。

现在可以重启 apache

sudo apachectl -k restart  

会出现如下错误:

httpd: Syntax error on line 177 of /private/etc/apache2/httpd.conf: Cannot load /usr/local/php5/libphp5.so into server: dlopen(/usr/local/php5/libphp5.so, 10): no suitable image found.  Did find:\n\t/usr/local/php5/libphp5.so: code signature in (/usr/local/php5/libphp5.so) not valid for use in process using Library Validation: mapped file has no cdhash, completely unsigned? Code has to be at least ad-hoc signed.\n\t/usr/local/php5/libphp5.so: stat() failed with errno=22

需要签名之类的错误。

这种情况我们解决办法之一就是重新安装apache

命令如下:

macOS comes with pre-installed apache, following are the commands to stop it and prevent from autostart on boot:
$ sudo apachectl stop
$ sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Install apache through homebrew
$ brew install httpd

Check apache installation through the following command and the output should be /usr/local/bin/apachectl
$ which apachectl

Start apache through the following command
$ sudo apachectl -k start

Set apache to startup on bootload
$ sudo brew services start httpd

上段命令参考:https://github.com/liip/php-osx/issues/249#issuecomment-424586068

brew 命令安装详见:https://brew.sh/

我的httpd安装目录/usr/local/etc/httpd

配置apache的httpd.conf 前面开始已经详细说了apache配置这里不再赘述。

再次启动apache php已经加载了php5,错误搞定。