macOS MojaveにHomebrewでApacheをインストールする方法

- 使っているOS : macOS Mojave バージョン10.14.1

# (しなくてもいいけど)事前準備をする

```bash
# アップデートしておく
$ brew update
Already up-to-date.
$ brew -v
Homebrew 1.8.5
Homebrew/homebrew-core (git revision 0b33e; last commit 2018-12-13)
Homebrew/homebrew-cask (git revision bd7c4; last commit 2018-12-14)

# Homebrewが元気かお医者さんに聞いておく
$ brew doctor
Your system is ready to brew.

# インストールしちゃってないか確認する
$ brew list | grep httpd
$
```

# (決まっていればしなくてもいいけど)インストールするものを選ぶ

```bash
# 探す
$ brew search httpd
==> Formulae
darkhttpd                    httpd                        httpdiff                     libmicrohttpd                lighttpd                     mighttpd2

# いっぱいあるから一応見てみる
# Webサーバだけど違う
$ brew info darkhttpd
darkhttpd: stable 1.12 (bottled)
Small static webserver without CGI
https://unix4lyfe.org/darkhttpd/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/darkhttpd.rb
==> Analytics
install: 11 (30 days), 41 (90 days), 171 (365 days)
install_on_request: 11 (30 days), 40 (90 days), 166 (365 days)
build_error: 0 (30 days)

# いつものやつ
$ brew info httpd
httpd: stable 2.4.37 (bottled)
Apache HTTP server
https://httpd.apache.org/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpd.rb
==> Dependencies
Required: apr ✘, apr-util ✔, brotli ✘, nghttp2 ✘, openssl ✘, pcre ✘
==> Caveats
DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
  brew services start httpd
Or, if you don`t want/need a background service you can just run:
  apachectl start
==> Analytics
install: 15,299 (30 days), 53,854 (90 days), 181,402 (365 days)
install_on_request: 11,811 (30 days), 40,493 (90 days), 126,112 (365 days)
build_error: 0 (30 days)

# なんか違う
$ brew info httpdiff
httpdiff: stable 1.0.0 (bottled), HEAD
Compare two HTTP(S) responses
https://github.com/jgrahamc/httpdiff
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/httpdiff.rb
==> Dependencies
Build: go ✘
==> Options
--HEAD
	Install HEAD version
==> Analytics
install: 3 (30 days), 14 (90 days), 73 (365 days)
install_on_request: 3 (30 days), 14 (90 days), 73 (365 days)
build_error: 0 (30 days)

# ライブラリだった
$ brew info libmicrohttpd
libmicrohttpd: stable 0.9.59 (bottled)
Light HTTP/1.1 server library
https://www.gnu.org/software/libmicrohttpd/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/libmicrohttpd.rb
==> Options
--with-ssl
	Enable SSL support
==> Analytics
install: 873 (30 days), 3,174 (90 days), 31,195 (365 days)
install_on_request: 603 (30 days), 2,287 (90 days), 17,251 (365 days)
build_error: 0 (30 days)
```

# Apacheをインストールする
```bash
$ brew install httpd
==> Installing dependencies for httpd: apr, openssl, brotli, c-ares, jansson, nghttp2 and pcre
==> Installing httpd dependency: apr
==> Downloading https://homebrew.bintray.com/bottles/apr-1.6.5.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring apr-1.6.5.mojave.bottle.tar.gz
==> Caveats
apr is keg-only, which means it was not symlinked into /usr/local,
because Apple`s CLT package contains apr.

If you need to have apr first in your PATH run:
  echo 'export PATH="/usr/local/opt/apr/bin:$PATH"' >> ~/.bash_profile

==> Summary
🍺  /usr/local/Cellar/apr/1.6.5: 60 files, 1.3MB
==> Installing httpd dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2q.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2q.mojave.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2q: 1,794 files, 12.1MB
==> Installing httpd dependency: brotli
==> Downloading https://homebrew.bintray.com/bottles/brotli-1.0.7.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring brotli-1.0.7.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/brotli/1.0.7: 25 files, 2.2MB
==> Installing httpd dependency: c-ares
==> Downloading https://homebrew.bintray.com/bottles/c-ares-1.15.0.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring c-ares-1.15.0.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/c-ares/1.15.0: 72 files, 482.6KB
==> Installing httpd dependency: jansson
==> Downloading https://homebrew.bintray.com/bottles/jansson-2.12.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring jansson-2.12.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/jansson/2.12: 11 files, 157.5KB
==> Installing httpd dependency: nghttp2
==> Downloading https://homebrew.bintray.com/bottles/nghttp2-1.35.1.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring nghttp2-1.35.1.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/nghttp2/1.35.1: 26 files, 2.7MB
==> Installing httpd dependency: pcre
==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring pcre-8.42.mojave.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.42: 204 files, 5.5MB
==> Installing httpd
==> Downloading https://homebrew.bintray.com/bottles/httpd-2.4.37_1.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring httpd-2.4.37_1.mojave.bottle.tar.gz
==> Caveats
DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
  brew services start httpd
Or, if you don`t want/need a background service you can just run:
  apachectl start
==> Summary
🍺  /usr/local/Cellar/httpd/2.4.37_1: 1,648 files, 26.9MB
==> Caveats
==> apr
apr is keg-only, which means it was not symlinked into /usr/local,
because Apple`s CLT package contains apr.

If you need to have apr first in your PATH run:
  echo 'export PATH="/usr/local/opt/apr/bin:$PATH"' >> ~/.bash_profile

==> openssl
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

openssl is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have openssl first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find openssl you may need to set:
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

==> httpd
DocumentRoot is /usr/local/var/www.

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.

To have launchd start httpd now and restart at login:
  brew services start httpd
Or, if you don`t want/need a background service you can just run:
  apachectl start
```

# インストール時の説明に沿ってPATHを設定する
[keg-only について - 理系学生日記](https://kiririmode.hatenablog.jp/entry/20141210/p1)

## apr(Apache Portable Runtime)
[Apache Portable Runtime - Wikipedia](https://ja.wikipedia.org/wiki/Apache_Portable_Runtime)

```bash
# apr is keg-only, which means it was not symlinked into /usr/local,
# because Apple`s CLT package contains apr.
# If you need to have apr first in your PATH run:
echo 'export PATH="/usr/local/opt/apr/bin:$PATH"' >> ~/.bash_profile
```

## openssl

```bash
# openssl is keg-only, which means it was not symlinked into /usr/local,
# because Apple has deprecated(非推奨の) use of OpenSSL in favor(～の方を好む) of its own TLS and crypto libraries.
# If you need to have openssl first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
# For compilers to find openssl you may need to set:
echo 'export LDFLAGS="-L/usr/local/opt/openssl/lib"' >> ~/.bash_profile
echo 'export CPPFLAGS="-I/usr/local/opt/openssl/include"' >> ~/.bash_profile
```

# インストールされたことを確認する
```bash
$ which httpd
/usr/local/bin/httpd

$ httpd -v
Server version: Apache/2.4.37 (Unix)
Server built:   Nov  8 2018 08:14:31
```

# 何も設定しないでApacheを起動すると普通にエラーになるので設定してから起動する
- [Apacheを起動しようとしたらPermission denied: AH00091: httpd: could not open error log fileとなったときの対応方法 - Qiita](https://qiita.com/ponsuke0531/items/40d7c4a14f7946e9b2e7)
- [Apacheを起動しようとしたらAddress already in use: AH00072: make_sock: could not bind to addressとなったときの対応方法](https://qiita.com/ponsuke0531/items/d3ed0b7c9713e42a54e4)
- [Apacheを起動しようとしたらCould not reliably determine the server's fully qualified domain name, using IPアドレス. Set the 'ServerName' directive globally to suppress this messageとなったときの対応方法](https://qiita.com/ponsuke0531/items/d87aa4d514bd646b6f28)

## (必要な場合は)PHPを設定する
[Apacheのhttpd.confにPHPを設定する方法 - Qiita](https://qiita.com/ponsuke0531/items/e27c3dafcc9741ef3e73)