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

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

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

curl -s | bash -s 5.6



php -v 或者 which php 查看命令


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

保存 重载环境变量

source ~/.bash_profile


php -v 或者 which php 查看命令

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


#LoadModule php7_module libexec/apache2/
LoadModule php5_module /usr/local/php5/
#LoadModule php5_module libexec/apache2/
#LoadModule perl_module libexec/apache2/
LoadModule hfs_apple_module libexec/apache2/

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



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

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
    # 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


现在可以重启 apache

sudo apachectl -k restart  


httpd: Syntax error on line 177 of /private/etc/apache2/httpd.conf: Cannot load /usr/local/php5/ into server: dlopen(/usr/local/php5/, 10): no suitable image found.  Did find:\n\t/usr/local/php5/ code signature in (/usr/local/php5/ 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/ stat() failed with errno=22




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


brew 命令安装详见:


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

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