Skip to content

Instantly share code, notes, and snippets.

@tysian
Last active September 4, 2022 16:03
Show Gist options
  • Save tysian/182c7d9a6790f9f61530f1cb54cefcbc to your computer and use it in GitHub Desktop.
Save tysian/182c7d9a6790f9f61530f1cb54cefcbc to your computer and use it in GitHub Desktop.
Mac Setup

Originally written by Andy Miller @ getgrav.com

This setup was used on fresh Monterey version

Apache + PHP + MySQL + phpMyAdmin + xDebug

Requirements

  1. Homebrew installed
  2. VSCode installed

Pre-setup

XCode Command Line Tools

It's good to have them

xcode-select --install

Run this to make sure that you installed everything correctly:

brew doctor

If you need something, you will get an instruction how to update/fix it.

Monterey Required Libraries

Some libraries might also need this:

brew install openssl

Apache installation

MacOS 12.0 Monterey comes with Apache 2.4 preinstalled, but it is better to use Homebrew version (with all those scripts we will (or won't) need).

Firstly we need to stop current apache server and disable it from auto-start when system starts.

As folks from grav said:

It really doesn't hurt to just run all these commands in order - even if it's a fresh installation

sudo apachectl stop
sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null

Now we will install Brew version of Apache

brew install httpd

So when everything is installed, you should see something like this:

🍺  /opt/homebrew/Cellar/httpd/2.4.51: 1,660 files, 32.0MB

You can now run Homebrew's Apache:

brew services start httpd

Let's test if everything works correctly, go to your browser and open http://localhost:8080. You should see a text "It works!".

it-works

Source: tutorial from getgrav.org

Troubleshooting Apache

If there is a problem with starting Apache server - make sure you've done everything correctly. If yes, check if Apache server is running:

ps -aef | grep httpd

Try to restart it using Brew services:

brew services restart httpd

Your error logs can be found here:

tail -f /opt/homebrew/var/log/httpd/error_log

If there is a problem with permissions (as non-sudo user), try to run apache directly

/opt/homebrew/bin/httpd -k start

This bypasses the brew services command and often prints out specific issues. If you have issues reported about not being able to write to log files, try removing all the current log httpd log files:

rm -rf /opt/homebrew/var/log/httpd/*

And try to run command again. If you're getting an error similar to this:

Address already in use: AH00072: make_sock: could not bind to address

Try changing Listen option in httpd.conf (check the instruction below) to this:

Listen 0.0.0.0:80

Apache configuration

Now we have to configure our Apache server. Make sure you can open files in VS Code via terminal, then open Apache configuration file httpd.conf in the IDE

code /opt/homebrew/etc/httpd/httpd.conf

Port

Search for a line:

Listen 8080

and change port to 80:

Listen 80

Document root folder

Default document root directory is /opt/homebrew/var/www, but can change it, for example to your home directory Search for DocumentRoot and you should see

DocumentRoot "/opt/homebrew/var/www"

You can change it now to the directory of your choice:

DocumentRoot "/Users/<your_username>/dev"

You also need to change <Directory> tag right below the DocumentRoot line. This should point to your new directory as well:

<Directory "/Users/<your_username>/dev">

htaccess files

In the <Directory> tag you have AllowOverride option, change it to All

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

Another important thing is to enable mod_rewrite. To do it, simply search for mod_rewrite.so and make sure is uncommented:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

If you want to use Apache Virtual Hosts, make sure to also uncomment module for that:

LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so

and

# Virtual hosts
Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

Now you can add your vhosts in httpd-vhosts.conf

code /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf

like this (replace <your_username> with your actual username 😉):

<VirtualHost *:80>
    DocumentRoot "/Users/<your_username>/dev"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/<your_username>/dev/your-awesome-website"
    ServerName your-awesome-website.com
</VirtualHost>

User & Group

To make sure that all permissions are correct, we have to change a User and Group in config to the current username. By default both are daemon or _www. Simply just change it to:

User <your_username>
Group staff

Servername

Apache likes to have a server name in the configuration, but this is disabled by default, so search for example.com, find this line:

#ServerName www.example.com:8080

and replace it with:

ServerName localhost

After changing anything in config file, you have to restart Apache server:

brew services restart httpd

⚠ If you receive an error upon restarting Apache, try removing the quotes around the DocumentRoot and Directory designations we set up earlier.

Let's test if you did everything correctly, create an index.html in your DocumentRoot directory with some dummy data inside:

mkdir ~/dev
echo "<h1>My User Web Root</h1>" > ~/dev/index.html

Now go to your browser and open http://localhost url.

🛑 Make sure you removed :8080 from the URL. You might also clear browser cache using shortcut⇪⌘R .

You should see the My User Web Root text. If not, check Troubleshooting section above.

sites-webroot

Source: tutorial from getgrav.org

PHP

The best option currently is to use shivammathur's homebrew tap for many php versions. Feel free to install as many as you need:

brew install shivammathur/php/php@7.0
brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
brew install shivammathur/php/php@7.4
brew install shivammathur/php/php@8.0

php.ini config files are located in selected version directory (replace <version> with selected version, for example: 8.0):

/opt/homebrew/etc/php/<version>/php.ini

Now we have to link one of these versions, for example for version 7.4:

brew unlink php && brew link --overwrite --force php@7.4

You can use this command to switch between php versions.

For now, to check if everything works, run:

php -v
PHP 7.4.3 (cli) (built: Jul  5 2021 15:13:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

Apache configuration

You have to open Apache config file:

code /opt/homebrew/etc/httpd/httpd.conf

And find search mod_rewrite module:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

Add your libphp modules right under below it:

#LoadModule php7_module /opt/homebrew/opt/php@7.0/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.1/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.2/lib/httpd/modules/libphp7.so
#LoadModule php7_module /opt/homebrew/opt/php@7.3/lib/httpd/modules/libphp7.so
LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so
#LoadModule php_module /opt/homebrew/opt/php@8.0/lib/httpd/modules/libphp.so

And uncomment the version you're currently using.

Now you have to add index.php to DirectoryIndex, so replace this:

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

with this

<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
</FilesMatch>

Make sure you saved this file, then restart Apache server:

brew services restart httpd

Let's test if everything works correctly. Create info.php with php informations inside your DocumentRoot directory:

echo "<?php phpinfo();" > ~/Sites/info.php

Then open http://localhost/info.php in your browser.

php-running-74

Source: tutorial from getgrav.org

You can also use a php version switcher script by Andy MIller (rhukster):

curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /opt/homebrew/bin/sphp
chmod +x /opt/homebrew/bin/sphp

Then switch to selected version:

sphp 8.0

And output should look like this

Switching to php@8.0
Switching your shell
Unlinking /opt/homebrew/Cellar/php@7.3/7.3.32... 25 symlinks removed.
Unlinking /opt/homebrew/Cellar/php@7.4/7.4.25... 0 symlinks removed.
Unlinking /opt/homebrew/Cellar/php/8.0.12... 0 symlinks removed.
Linking /opt/homebrew/Cellar/php/8.0.12... 24 symlinks created.
Switching your apache conf
Restarting apache
Stopping `httpd`... (might take a while)
==> Successfully stopped `httpd` (label: homebrew.mxcl.httpd)
==> Successfully started `httpd` (label: homebrew.mxcl.httpd)

PHP 8.0.12 (cli) (built: Oct 21 2021 00:21:19) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.12, Copyright (c), by Zend Technologies

All done!

MySQL

Let's use MariaDB as our mysql database

brew update
brew install mariadb

then run mysql server:

brew services start mariadb

It's good idea to run mysql setup:

sudo /opt/homebrew/bin/mysql_secure_installation

Xdebug

Xdebug is pretty cool tool which colors var_dumps, error stacks and more. To install it make sure you choose compatible version for your php version:

PHP Version Compatible Xdebug version
PHP 7.0 Xdebug 2.7
PHP 7.1 Xdebug 2.9
PHP 7.2+ Xdebug 3.0

For PHP 7.0

sphp 7.0
pecl uninstall -r xdebug
pecl install xdebug-2.7.2

For PHP 7.1

sphp 7.1
pecl uninstall -r xdebug
pecl install xdebug-2.9.8

For PHP 7.2+

ℹ Note: change sphp 7.2 to the version you want to install xdebug for (from 7.2 to 8.1)

sphp 7.2 # or any other version you need, eg. 7.4, 8.0, etc.
pecl uninstall -r xdebug
pecl install xdebug

Now open php.ini and remove zend_extension="xdebug.so" from the top of the file.

code /opt/homebrew/etc/php/<version>/php.ini

After that, simply create new file and update it:

code /opt/homebrew/etc/php/7.0/conf.d/ext-xdebug.ini

Before Xdebug 3.0 you can use this config:

[xdebug]
zend_extension="xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

In Xdebug version 3.0+ you can use simplified config:

[xdebug]
zend_extension="xdebug.so"
xdebug.mode=debug

You can add multiple modes at the same time to the xdebug.mode with comma-separated values, such as

xdebug.mode=debug,develop

More xdebug info here

Now restart Apache server:

brew services restart httpd

and check your info page: http://localhost/info.php

xdebug

Source: tutorial from getgrav.org

To get more details and features, feel free to check full grav tutorial here.

phpMyAdmin

Install phpmyadmin using brew:

brew install phpmyadmin

Then you'll get an instruction how to connect it to the Apache server.

Simply open httpd.conf

code /opt/homebrew/etc/httpd/httpd.conf

add following code (feel free to copy code provided by phpMyAdmin installation) at the end of httpd.conf configuration file.

Alias /phpmyadmin /usr/local/share/phpmyadmin
<Directory /usr/local/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

Restart Apache

brew services restart httpd

Then open http://localhost/phpmyadmin

# Aliases by Coding Garden (w3cj) - https://github.com/w3cj/dotfiles/blob/master/.bash_profile
# -------
# Aliases
# -------
alias cpv='rsync -ah --info=progress2'
alias ns='npm start'
alias start='npm start'
alias nr='npm run'
alias run='npm run'
alias nis='npm i -S'
alias nid='npm i -D'
alias l="ls" # List files in current directory
alias ll="ls -al" # List all files in current directory in long list format
alias o="open ." # Open the current directory in Finder
# ----------------------
# Git Aliases
# ----------------------
alias ga='git add'
alias gaa='git add .'
alias gpsh='git push'
alias gl='git log'
alias gs='echo ""; echo "*********************************************"; echo -e " DO NOT FORGET TO PULL BEFORE COMMITTING"; echo "*********************************************"; echo ""; git status'
alias gss='git status -s'
alias gd='git diff'
alias gdc='git diff --cached'
alias gcm='git commit -m'
alias gca='git commit -am'
alias gb='git branch'
alias gc='git checkout'
alias gra='git remote add'
alias grs='git reset'
alias grr='git remote rm'
alias gpu='git pull'
alias gcl='git clone'
alias gi='git init'
# ----------------------
# Functions
# ----------------------
function mkcd --description "Create directory and enter it"
mkdir $argv && cd $argv
end
function ctrl__apache
switch $argv[1]
case "start"
brew services start httpd
case "restart"
brew services restart httpd
case "stop"
brew services stop httpd
case "status"
brew services info httpd
case "rootdir"
open /opt/homebrew/etc/httpd
case "vhost"
code /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf /etc/hosts
case "*"
echo "Wrong argument, please use: start, restart, stop, status, vhost, rootdir"
end
end
function ctrl__mysql
switch $argv[1]
case "start"
brew services start mariadb
case "restart"
brew services restart mariadb
case "stop"
brew services stop mariadb
case "status"
brew services info mariadb
case "*"
echo "Wrong argument, please use: start, restart, stop, status"
end
end
function ctrl__php
switch $argv[1]
case "version"
php -v
case "rootdir"
open /opt/homebrew/etc/php
case "switch"
sphp $argv[2]
case "*"
echo "Wrong argument, please use: version, switch <version>, rootdir"
end
end
function ctrl --description "Controll stuff provided in arguments"
switch $argv[1
case "apache"
ctrl__apache $argv[2..-1]
case "mysql"
ctrl__mysql $argv[2..-1]
case "php"
ctrl__php $argv[2..-1]
case "*"
echo "Wrong argument provided, plase use: apache, mysql, php"
end
end
xcode-select --install
# Install brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update
# Install iTerm2
brew install --cask iterm2
# iTerm settings:
# profiles->themes
# profiles->keys->presets->natural text editing
# profiles->general->working directory->reuse previous session dir || set directory
# install fish and set default shell
brew install fish
# install oh-my-fish
curl https://raw.githubusercontent.com/oh-my-fish/oh-my-fish/master/bin/install | fish
# setup fish
# open .profile->add fish at the end of file
# update config.fish
# add functions/ctrl.fish
brew install git
brew install nvm
mkdir ~/.nvm
omf install nvm
nvm install stable
brew install --cask alfred
# Preferences->Keyboard->shortcuts->spotlight->show spotlight search [off] -> alfred preferences->change keybindings
# Install firefox
brew tap homebrew/cask-version
brew install --cask firefox-developer-edition
# Install alternative alt-tab version
brew install --cask alt-tab
# Install app for handling apps windows
brew install --cask rectangle
# Instapp app to add stats to the top bar
brew install --cask stats
# Install app to configure battery stuff
brew install --cask aldente
# Install app to use mouse & touchbar at the same time
# Add it manually to the autostart
brew install --cask unnaturalscrollwheels
# Git manager
brew install --cask fork
# Time guard, disable postpone for long breaks
brew install --cask stretchly
# Slack for work
brew install --cask slack
# Cool IDE
brew install --cask visual-studio-code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment