macOSで、Apache Webサーバーをソースからビルドして運用する

主題
macOS Big Surにおいて、事実上無用の長物と化した、OSビルトインのApache Webサーバーの代わりに、別のApache Webサーバーを導入する手順を述べる。(最終更新日:2024.07.04)

背景
macOS Catalinaでは、ビルトインのApache Webサーバーの設定ファイルを変更しても、問題なく使えた。(デスクトップ上に場所が変更された項目はできたが、退避されるのは、変更前のファイルだった)
ところが、macOS Big Surでは、場所が変更された項目には、変更したファイルが退避されるようになった。システム保護の考え方からは、Big Surの動作は正しい。が、長年親しんできた、パーソナルWeb共有は、事実上できなくなったと言っていい。
本稿の目的は、融通の効かなくなったOSビルトインのApache Webサーバーの代わりに、別のApache Webサーバーを導入することで、パーソナルWeb共有を維持すること、及び、その作業記録を残すことにある。

手順

  1. OSビルトインApache Webサーバーの停止

    macOS 12.6.6以降では、このプロパティリストは、Disabledになっているため、この作業は不要である。

    sudo apachectl stop
    sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
  2. OpenSSLのコンパイルとインストール(更新)

    curl -LO https://www.openssl.org/source/openssl-3.3.1.tar.gz
    tar xf openssl-3.3.1.tar.gz
    cd openssl-3.3.1
    ./config --openssldir=/usr/local/ssl
    make && sudo make install
  3. Apache Webサーバーのコンパイルとインストール

    1. APR(Apache Portable Runtime)のコンパイルとインストール

      curl -LO https://dlcdn.apache.org//apr/apr-1.7.4.tar.bz2
      tar xf apr-1.7.4.tar.bz2
      cd apr-1.7.4
      ./configure
      make && sudo make install
    2. Expat XML Parserのコンパイルとインストール

      curl -LO https://github.com/libexpat/libexpat/releases/download/R_2_6_2/expat-2.6.2.tar.xz
      tar xf expat-2.6.2.tar.xz
      cd expat-2.6.2
      ./configure
      make && sudo make install
    3. APR-Utilのコンパイルとインストール

      curl -LO https://dlcdn.apache.org//apr/apr-util-1.6.3.tar.bz2
      tar xf apr-util-1.6.3.tar.bz2
      cd apr-util-1.6.3
      ./configure \
      --prefix=/usr/local/apr-util \
      --with-apr=/usr/local/apr \
      --with-openssl=/usr/local/ssl
      make && sudo make install
    4. PCRE(Perl Compatible Regular Expressions)2のコンパイルとインストール(更新)

      curl -LO https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.44/pcre2-10.44.tar.bz2
      tar xf pcre2-10.44.tar.bz2
      cd pcre2-10.44
      ./configure
      make && sudo make install
    5. zlibのコンパイルとインストール

      curl -LO https://www.zlib.net/zlib-1.3.1.tar.xz
      tar xf zlib-1.3.1.tar.xz
      cd zlib-1.3.1
      ./configure
      make test
      sudo make install
    6. Apacheのコンパイルとインストール(更新)

      curl -LO https://dlcdn.apache.org/httpd/httpd-2.4.61.tar.bz2
      tar xf httpd-2.4.61.tar.bz2
      cd httpd-2.4.61
      ./configure \
      --with-apr=/usr/local/apr \
      --with-apr-util=/usr/local/apr-util \
      --with-pcre=/usr/local/bin/pcre2-config \
      --enable-mods-shared=reallyall \
      --enable-ssl \
      --with-ssl=/usr/local/ssl \
      --enable-proxy \
      --enable-proxy-ajp \
      --enable-dav \
      --enable-dav-fs \
      --enable-headers \
      --enable-rewrite=shared \
      --enable-deflate
      make && sudo make install
    7. apachectlのパスを通す
      Apacheは/usr/local/apache2にインストールされる。Apacheのコントロールコマンドapachectlを、OSビルトインのものから置き換える必要がある。

      export PATH=/usr/local/apache2/bin:${PATH}

      筆者は、上記を.bash_profileに書き込んでいる。

    8. テスト
      パスの確認

      which apachectl
      /usr/local/apache2/bin/apachectl

      Apacheの起動

      sudo apachectl start


      ウェブブラウザで、http://localhost/を表示させる。It works!と表示されたら、インストールは成功である。

  4. Apacheの設定変更
    インストールが終わったら、Apacheの設定変更である。これによりホームディレクトリに置いたサイトディレクトリ内のコンテンツを表示できるようにする。

    1. httpd.confの編集
      Apacheの設定ファイルhttpd.confは、/usr/local/apache2/confにある。変更をdiff形式で示す。

      diff -ru --exclude=.DS_Store a/httpd.conf b/httpd.conf
      --- a/httpd.conf	2020-11-14 02:07:21.000000000 +0900
      +++ b/httpd.conf	2020-11-14 23:37:10.000000000 +0900
      @@ -160,7 +160,7 @@
       LoadModule dir_module modules/mod_dir.so
       #LoadModule actions_module modules/mod_actions.so
       #LoadModule speling_module modules/mod_speling.so
      -#LoadModule userdir_module modules/mod_userdir.so
      +LoadModule userdir_module modules/mod_userdir.so
       LoadModule alias_module modules/mod_alias.so
       #LoadModule rewrite_module modules/mod_rewrite.so
      
      @@ -195,7 +195,7 @@
       # e-mailed.  This address appears on some server-generated pages, such
       # as error documents.  e.g. admin@your-domain.com
       #
      -ServerAdmin you@example.com
      +ServerAdmin your_email@address.com
      
       #
       # ServerName gives the name and port that the server uses to identify itself.
      @@ -204,7 +204,7 @@
       #
       # If your host doesn't have a registered DNS name, enter its IP address here.
       #
      -#ServerName www.example.com:80
      +ServerName localhost:80
      
       #
       # Deny access to the entirety of your server's filesystem. You must
      @@ -481,7 +481,7 @@
       #Include conf/extra/httpd-languages.conf
      
       # User home directories
      -#Include conf/extra/httpd-userdir.conf
      +Include conf/extra/httpd-userdir.conf
      
       # Real-time info on requests and configuration
       #Include conf/extra/httpd-info.conf
    2. httpd-userdir.confの編集
      httpd-userdir.confは、/usr/local/apache2/conf/extraにある。変更をdiff形式で示す。

      diff -ru --exclude=.DS_Store a/extra/httpd-userdir.conf b/extra/httpd-userdir.conf
      --- a/extra/httpd-userdir.conf	2020-11-14 02:07:21.000000000 +0900
      +++ b/extra/httpd-userdir.conf	2020-11-17 01:06:27.000000000 +0900
      @@ -7,15 +7,13 @@
       # directory if a ~user request is received.  Note that you must also set
       # the default access control for these directories, as in the example below.
       #
      -UserDir public_html
      +UserDir Sites
      
       #
       # Control access to UserDir directories.  The following is an example
       # for a site where these directories are restricted to read-only.
       #
      -<Directory "/home/*/public_html">
      -    AllowOverride FileInfo AuthConfig Limit Indexes
      -    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
      -    Require method GET POST OPTIONS
      -</Directory>
      -
      +Include /usr/local/apache2/conf/users/*.conf
      +<IfModule bonjour_module>
      +       RegisterUserSite customized-users
      +</IfModule>
    3. ユーザー設定ファイルの準備
      次のようなユーザー設定ファイルyourLoginName.confを用意し、/usr/local/apache2/conf/usersに配置する。

      <Directory "/Users/yourLoginName/Sites/">
          AllowOverride All
          Options Indexes FollowSymLinks Multiviews
          Require all granted
      </Directory>
    4. Apacheの再起動
      設定変更を反映させるため、Apacheを再起動する。

      sudo apachectl restart
    5. サイトディレクトリ内のコンテンツ表示確認
      各自がサイトディレクトリに置いたコンテンツを表示させる。

      例:http://localhost/~yourLoginName/

  5. Apacheの自動起動
    設定が終わったApacheをシステム起動時に自動起動させる手順は別の投稿で説明しているので、参照されたい。

参考サイト

以上。

この投稿へのコメント

コメントはありません。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

この投稿へのトラックバック

  1. […] 参考ブログ:macOSで、Apache Webサーバーをソースからビルドして運用する – 桃源老師のつぶやき […]

トラックバック URL