Many of our projects, especially CMS projects projects, are based on PHP or require a traditional server. We tend to prefer Apache in those cases, because it's prevalent and widely used.
This guide walks you through setting up Apache natively on your MacOS with the ability to support multiple versions of PHP using PHP-FPM (FastCGI Process Manager). It is based on the fantastic instructions provided by MacOS Sierra Apache Multiple PHP Versions and Apache httpd 2.4 and PHP 7 in macOS
After running this setup guide, you will have 2 types of services running:
httpdApache accepting incoming http(s) requests and matching them up with configured VirtualHost entries.php-fpmPHP FastCGI Process Manager receiving requests from Apache to process a PHP file.
PHP runs as a service to allow us to have multiple versions of PHP active for Apache at any time. Without FPM, Apache must be configured for a specific version of PHP. Switching for a project requires reconfiguring and restarting Apache.
- Prerequisites
- Installing Xcode Libraries
- Installing Apache
- Setting up Apache
- Installing PHP
- Setting up a new PHP site in Apache
- Installing Composer
- Common Issues
- MacOS Sierra 10.12.6 or greater
- Successfully run the Sparkbox laptop script to install Homebrew, PHP-FPM, and MySQL.
- Reader must be comfortable issuing commands in the terminal
xcode-select --install
Disable MacOS Apache
which apachectl
sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
Configure Apache (httpd), which was already installed via the laptop script, to auto start using Homebrew services.
brew services start httpd
Useful commands to know:
sudo apachectl start
sudo apachectl stop
sudo apachectl -k restart
Important file locations:
/usr/local/etc/httpd/httpd.conf
Edit your Apache Configuration File (/usr/local/etc/httpd/httpd.conf)
to enable:
Change the Listen setting from 8080 to 80.
Listen 80
Uncomment out the following lines (they will not be together):
LoadModule xml2enc_module lib/httpd/modules/mod_xml2enc.so
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
LoadModule proxy_module lib/httpd/modules/mod_proxy.so
LoadModule proxy_fcgi_module lib/httpd/modules/mod_proxy_fcgi.so
📝 Note: mod_proxy will support using PHP-FPM for executing multiple
concurrently installed versions of PHP.
Each vhost (site) will be configured in its own file in the
/usr/local/etc/httpd/vhosts directory. Allow this to happen by:
- Add the following line to the end of your
httpd.conf
Include /usr/local/etc/httpd/vhosts/*.conf
mkdir -p /usr/local/etc/httpd/vhosts
Multiple versions of PHP were installed and configured to run as [brew services] by the Sparkbox laptop script. You can see each version running with
brew services list | grep php
Important file locations:
/usr/local/etc/php/5.6/php.ini
/usr/local/etc/php/7.0/php.ini
Each site will have its own vhost file in /usr/local/etc/httpd/vhosts.
<project_name>: Domain safe name such asseesparkbox<php_port>: Port to the PHP-FPM instance, such as9071for 7.1<project_root>: Web root such as/projects/seesparkbox.com/ee
~/Documents. Apache
requires both write and execute permissions on all paths in the site directory,
which is not advised. Instead, we recommend ~/projects as the parent for all
Apache projects.
Copy, paste, & edit the following vhost configuration inside the
file /usr/local/etc/httpd/vhosts/<project_name>.conf:
<VirtualHost *:80>
ServerName <project_name>.local
DocumentRoot "<project_root>"
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:<php_port><project_root>/$1
ErrorLog "/usr/local/var/log/httpd/<project_name>-error_log"
CustomLog "/usr/local/var/log/httpd/<project_name>-access_log" common
DirectoryIndex index.html index.php
<Directory "<project_root>">
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>
</VirtualHost>
Save this file.
- Make sure httpd is configured to start on reboot:
brew services restart httpd - Restart Apache with the changes made:
sudo apachectl -k restart
With sudo open /etc/hosts and add the following to the end of the file:
127.0.0.1 <project_name>.local
Installing composer globally.
brew install composer
In PHP versions similar to 7.0.27, and httpd versions similar to 2.4.29, you might run into errors like this within your log file:
[Thu Feb 15 09:16:51.849759 2018] [core:notice] [pid 3230:tid 140735914537792] AH00052: child pid 3254 exit signal Segmentation fault (11)
To fix this, you must enable the mpm_prefork_module by adding this line to your httpd.conf file:
LoadModule mpm_prefork_module lib/httpd/modules/mod_mpm_prefork.so
mpm_event_module in order to get this to load.