Skip to content

Instantly share code, notes, and snippets.

@luissquall
Last active November 8, 2017 23:58
Show Gist options
  • Save luissquall/4440662 to your computer and use it in GitHub Desktop.
Save luissquall/4440662 to your computer and use it in GitHub Desktop.
Apache & PHP on OS X

Apache & PHP on OS X

Requirements

  • macOS High Sierra
  • Homebrew (Required by "Homebrew PHP")

Installation

Troubleshooting

# Check the error log
tail -f /private/var/log/apache2/error_log

# Check apache status
sudo apachectl

# Compare config file against a backup
diff -u /private/etc/apache2/httpd.conf /private/etc/apache2/httpd.conf.20151001131408 | colordiff

# httpd-vhosts may have been overwritten
cat /private/etc/apache2/extra/httpd-vhosts.conf

Error: Invalid formula: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-apache/mod_suexec.rb

# `brew tap homebrew/apache` fails
cd /usr/local/Homebrew/Library/Taps/homebrew
git clone https://github.com/Homebrew/homebrew-apache.git
curl https://raw.githubusercontent.com/ferjul17/homebrew-apache/929a52cb2dc235507aa151334d45eb5042e44a35/mod_suexec.rb > homebrew-apache/mod_suexec.rb

References

#!/bin/bash
#######################################
## Native Apache & PHP configuration ##
#######################################
# Start apache
sudo apachectl start
# Create DocumentRoot dir and modify its permissions
document_root=/private/var/www
sudo mkdir -p $document_root
sudo chown -R www:www $document_root
sudo chmod g+sw $document_root
# Add current user (e.g. luissquall) to the www group
sudo dseditgroup -o edit -a `whoami` -t user www
# Copy index to www and create a php file
cp /Library/WebServer/Documents/index.html.en $document_root/index.html
echo "<?php echo phpinfo(); ?>" > $document_root/info.php
# Edit apache main config file
sudo sed -E -i ".`date +%Y%m%d%H%M%S`" \
-e 's/^#(LoadModule +php._module +libexec.*)/\1/' \
-e 's/^#(LoadModule +rewrite_module.*)/\1/' \
-e 's#^(DocumentRoot) .*#\1 "'$document_root'"#' \
-e '/^DocumentRoot/ {n;s#^(<Directory).*#\1 "'$document_root'">#;}' \
-e 's/^#(Include.*httpd-vhosts.conf)/\1/' \
/private/etc/apache2/httpd.conf
# Edit vhosts file. Remove current Virtual Hosts and add the following definition
cat << EOF | sudo tee /private/etc/apache2/extra/httpd-vhosts.conf > /dev/null
<VirtualHost *:80>
`printf '\t'`DocumentRoot "$document_root"
</VirtualHost>
EOF
# Copy and configure php.ini
sudo cp /private/etc/php.ini.default /private/etc/php.ini
sudo chmod u+w /private/etc/php.ini
sudo sed -E -i ".`date +%Y%m%d%H%M%S`" \
-e 's/^(error_reporting) +=.+/\1 = E_ALL \& ~E_DEPRECATED/' \
-e 's#^;?(date.timezone) +=.*#\1 = America/Merida#' \
-e 's#^(post_max_size) +=.*#\1 = 50M#' \
-e 's#^(upload_max_filesize) +=.*#\1 = 50M#' \
/private/etc/php.ini
# Create home shortcuts
cd
ln -s $document_root www
ln -s /etc/hosts hosts
ln -s /private/etc/apache2/extra/httpd-vhosts.conf vhosts.conf
# Restart apache
sudo apachectl restart
# Test everything is fine
curl 127.0.0.1
curl 127.0.0.1/info.php
# open /Applications/Google\ Chrome.app http://127.0.0.1/info.php
#!/bin/bash
##################
## Homebrew PHP ##
##################
# Install/update XCode Command Line Tools
xcode-select --install
# The php.ini file can be found in: /usr/local/etc/php/5.5/php.ini
# More info: brew info php55
# https://github.com/Homebrew/homebrew-php
brew update
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php
# With the release of macOS Sierra the Apache module is now not built by default. If you want to build it
# on your system you have to install php with the --with-httpd24 option.
brew install php56 --with-httpd24
brew install php56-xdebug php56-mcrypt
# Verify php5 module is installed
ls -la /usr/local/opt/php56/libexec/apache2/libphp5.so
# Remove brew's apache
brew rm --ignore-dependencies httpd24
brew services list
# Enable PHP in Apache
sudo sed -E -i ".`date +%Y%m%d%H%M%S`" \
-e 's/^(LoadModule +php._module +libexec.*)/#\1/' \
-e '/LoadModule +php._module/a\
LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so' \
-e 's/(DirectoryIndex index.html)/\1 index.php/' \
/private/etc/apache2/httpd.conf
# Tell Apache to parse certain extensions as PHP
cat << 'EOF' | sudo tee -a /private/etc/apache2/httpd.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
EOF
# Configure PHP
sudo sed -E -i ".`date +%Y%m%d%H%M%S`" \
-e 's/^(error_reporting) +=.+/\1 = E_ALL \& ~E_DEPRECATED/' \
-e 's#^;?(date.timezone) +=.*#\1 = America/Merida#' \
-e 's#^(post_max_size) +=.*#\1 = 50M#' \
-e 's#^(upload_max_filesize) +=.*#\1 = 50M#' \
/usr/local/etc/php/5.6/php.ini
sudo apachectl restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment