Skip to content

Instantly share code, notes, and snippets.

@jkintscher
Last active February 2, 2021 12:04
Show Gist options
  • Save jkintscher/5617738 to your computer and use it in GitHub Desktop.
Save jkintscher/5617738 to your computer and use it in GitHub Desktop.
Apache Virtual Host Setup on OSX
#!/bin/bash
vhost=$1
NO_COLOR=$'\033[0;39m'
YELLOW=$'\033[1;33m'
while [ -z "$vhost" ]; do
read -p "Name of new vHost in $YELLOW$PWD$NO_COLOR: " vhost
done
htdocs="$HOME/Sites"
cat <<vHostTemplate >> ~/Sites/httpd-vhosts.conf
<VirtualHost *:80>
ServerName $vhost.local
CustomLog "$htdocs/logs/$vhost-access_log" combined
ErrorLog "$htdocs/logs/$vhost-error_log"
DocumentRoot "$PWD"
</VirtualHost>
vHostTemplate
sudo sh -c "echo '127.0.0.1 $vhost.local' >> /etc/hosts"
sudo sh -c "apachectl restart"

Apache

$ [ ! -d ~/Sites ] && mkdir ~/Sites
$ touch ~/Sites/httpd-vhosts.conf
$ sudo mv /etc/apache2/extra/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf~previous
$ sudo ln -s ~/Sites/httpd-vhosts.conf /etc/apache2/extra
$ mkdir ~/Sites/logs
$ chmod 0777 ~/Sites/logs
$ sh -c "cat >> ~/Sites/httpd-vhosts.conf <<'EOF'
# 
# Use name-based virtual hosting.
# 
NameVirtualHost *:80

# 
# Set up permissions for VirtualHosts in ~/Sites
# 
<Directory "/Users/`whoami`/Sites">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

# For http://localhost in the OS X default location
<VirtualHost _default_:80>
    ServerName localhost
    DocumentRoot /Users/`whoami`/Sites
</VirtualHost>

#
# VirtualHosts below
#

## Template
#<VirtualHost *:80>
#    ServerName [DOMAIN].local
#    CustomLog "/Users/`whoami`/Sites/logs/[DOMAIN].local-access_log" combined
#    ErrorLog "/Users/`whoami`/Sites/logs/[DOMAIN].local-error_log"
#    DocumentRoot "/Users/`whoami`/Sites/[DOMAIN].local"
#</VirtualHost>
EOF"

Restart Apache with sudo apachectl restart.

How to create a new Virtual Host

To add a site, duplicate the <VirtualHost> section under the Template, remove the comments, and edit appropriately.

Edit /etc/hosts (or use Gas Mask) and add an entry for 127.0.0.1 for your project.local like

127.0.0.1 project.local

to the very top top of your hosts file.

Note: Because of a bug in Mac OS X Lion and later that causes long loading times after the httpd process was idle for a few minutes, I recommend to have all VHs in a single line instead of a separate line for each.

PHP

Enable mod_php for Apache, and also set up an /etc/php.ini file based on the default one shipped with Lion with some development-friendly changes. Change your timezone as needed.

$ sudo sh -c "grep php /etc/apache2/httpd.conf|grep LoadModule|cut -d'#' -f2 > /etc/apache2/other/php5-loadmodule.conf"
$ sudo cp -a /etc/php.ini.default /etc/php.ini
$ sudo chmod ug+w /etc/php.ini
$ sudo chgrp admin /etc/php.ini
$ sudo sh -c "cat >> /etc/php.ini <<'EOF'


;;
;; User customizations below
;;

; Original - memory_limit = 128M
memory_limit = 196M
; Original - post_max_size = 8M
post_max_size = 200M
; Original - upload_max_filesize = 2M
upload_max_filesize = 100M
; Original - default_socket_timeout = 60
default_socket_timeout = 600
; Original - max_execution_time = 30
max_execution_time = 300
; Original - max_input_time = 60
max_input_time = 600
; Original - display_errors = Off
display_errors = On
; Original - html_errors = Off
html_errors = On
; Original - error_reporting = E_ALL & ~E_NOTICE
error_reporting =  E_ALL | E_STRICT
; Original - short_open_tag = Off
short_open_tag = On
; Original - display_startup_errors = Off
display_startup_errors = on
; Original - ;date.timezone =
date.timezone = 'America/New_York'

[xdebug]
xdebug.var_display_max_children = 999
xdebug.var_display_max_data = 99999
xdebug.var_display_max_depth = 100
EOF"

Open the php.ini, search and change the following settings:

extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626"
zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"

Restart Apache with sudo apachectl restart

MySQL

Install mysql formula with Homebrew. Follow instructions at the end of the installation. Then run the following:

$ cp $(brew --prefix mysql)/support-files/my-small.cnf /usr/local/var/mysql/my.cnf
$ sed -i "" 's/max_allowed_packet = 1.*M/max_allowed_packet = 2G/g' /usr/local/var/mysql/my.cnf
$ [ ! -d ~/Library/LaunchAgents ] && mkdir ~/Library/LaunchAgents
$ sudo sed -i "" 's/\/var\/mysql\/mysql\.sock/\/tmp\/mysql\.sock/g' /etc/php.ini

In case MySQL can’t connect to your local DB server and fails with something like

ErrorException [ Warning ]: mysql_connect(): No such file or directory

Follow this SO answer.

Credit

Mostly inspired by

http://akrabat.com/php/setting-up-php-mysql-on-os-x-10-7-lion/

and

http://echodittolabs.org/blog/2011/09/os-x-107-lion-development-native-apache-php-homebrew-mysql-or-mariadb

and a good portion of local testing and trial-and-error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment