Quantcast
Channel: ザリガニが見ていた...。
Viewing all articles
Browse latest Browse all 54

OSX 10.10 YosemiteでWeb共有を有効にする

$
0
0

OSX 10.8以降、システム環境設定からWeb共有のGUI設定がなくなってしまった。しかし、なくなったのはGUI設定だけで、apache2の設定ファイルを修正すれば、以前同様Web共有が使える。そこで、簡単にWeb共有を開始できるように、以前の日記でAppleScriptにまとめておいた。

ところが、時は流れてOSX 10.10 Yosemiteでは、上記AppleScriptではWeb共有が使えなくなっていた...。

apacheのバージョン

  • 調べてみると、OSX 10.10 Yosemiteから、apacheのバージョンが2.4に変更されていた。(以前はバージョン2.2だった)
$ apachectl -v
Server version: Apache/2.2.26 (Unix)
Server built:   Dec 10 2013 22:09:38
    • OSX 10.10 Yosemiteから
$ apachectl -v
Server version: Apache/2.4.9 (Unix)
Server built:   Sep  9 2014 14:48:20
  • apacheの設定ファイルをバージョン2.4に対応したものに修正することで、YosemiteでもWeb共有を使えるようになるのだ。

Web共有を有効にする手順

Webサーバーを起動
  • Webサーバーを起動するだけで、Web共有は始まる。但し...
    • /Library/WebServer/Documents/をルートとするWeb共有のみ有効。
    • ユーザーごとの~/SitesをルートとするWeb共有はまだ無効。
$ sudo apachectl start
  • http://localhost
    • /Library/WebServer/Documents/index.html.enの内容が表示されている。


/etc/apache2/httpd.confを修正
  • ユーザーごとのWeb共有を有効にするため、以下のコメントマークを外して、有効にしておいた。
    • userdir_moduleをロードすることを指定。
    • /etc/apache2/extra/httpd-userdir.confの設定も取り込むことを指定。


LoadModule authz_host_module libexec/apache2/mod_authz_host.so

LoadModule authz_core_module libexec/apache2/mod_authz_core.so

-#LoadModule userdir_module libexec/apache2/mod_userdir.so
+LoadModule userdir_module libexec/apache2/mod_userdir.so

# User home directories
-#Include /private/etc/apache2/extra/httpd-userdir.conf
+Include /private/etc/apache2/extra/httpd-userdir.conf

      • −赤い行= 修正前
      • +緑の行= 修正後
      • 黒い行 = デフォルトの設定状態
  • ついでにPHPも有効にするには、以下のコメントマークも外して、有効にしておく。
    • php5_moduleをロードすることを指定。


-#LoadModule php5_module libexec/apache2/libphp5.so
+LoadModule php5_module libexec/apache2/libphp5.so
/etc/apache2/extra/httpd-userdir.confを修正
  • ユーザーごとのWeb共有を有効にするため、以下のコメントマークを外して、有効にしておいた。
    • 上記/etc/apache2/httpd.confで取り込みを指定された設定ファイルである。
    • ユーザごとの設定ファイル/etc/apache2/users/*.confの設定も取り込むことを指定。


# Settings for user home directories
#
# Required module: mod_authz_core, mod_authz_host, mod_userdir

#
# UserDir: The name of the directory that is appended onto a user's home
# 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 Sites

#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#
-#Include /private/etc/apache2/users/*.conf
+Include /private/etc/apache2/users/*.conf
<IfModule bonjour_module>
RegisterUserSite customized-users
</IfModule>
      • −赤い行= 修正前
      • +緑の行= 修正後
      • 黒い行 = デフォルトの設定状態
~/Sites フォルダを追加
  • ユーザーごとのWeb共有の起点となる ~/Sites を追加した。(以前と同じ)
    • 上記/etc/apache2/extra/httpd-userdir.confの"UserDir Sites"という設定によって、指定されたフォルダである。
$ mkdir -p ~/Sites
~/Sites/index.htmlも追加
  • ユーザーごとのWebページの動作確認用として、「Now Web Sharing!」というテキストファイルも保存しておいた。(以前と同じ)
$ echo Now Web Sharing! > ~/Sites/index.html
/etc/apache2/users/USER_NAME.confを追加
  • ユーザーごとのWeb共有の設定である。
  • 例えば、ログインユーザー名がzariであれば、/etc/apache2/users/zari.confというパスになる。
    • 上記/etc/apache2/extra/httpd-userdir.confの"Include /private/etc/apache2/users/*.conf"という先ほど有効にした設定によって、Includeされる。
$ cat < /etc/apache2/users/zari.confOptions Indexes MultiViewsAllowOverride NoneRequire all grantedEOS
# apache 2.2
Order allow,deny
Allow from all
# apache 2.4
Require all granted
Webサーバーを再起動
  • 以上の設定を反映させるため、Webサーバーを再起動する。
$ sudo apachectl restart


AppleScriptにまとめる

  • 以上の仕組みをAppleScriptにまとめておくのだ。



activate
setrestodisplay dialogweb_sharing_msg() buttons {"キャンセル", "OFF", "ON"} cancel button 1 default buttonweb_sharing_btn() with title"Web共有コントローラー"

ifres'sbutton returned = "ON"then
apachectl_init()apachectl_restart()endif

ifres'sbutton returned = "OFF"then
apachectl_stop()endif




onapachectl_init()activate
do shell script"mkdir -p ~/Sites"
--do shell script "chmod +x ~/Sites" --ホーム直下ではデフォルト権限なので不要
do shell script"[ -f ~/Sites/index.html ] || echo Now Web Sharing! > ~/Sites/index.html"
ifapache_version() starts with"2.2"then
do shell script"f=/etc/apache2/users/`basename $HOME`.conf
cat <<EOS > $f~
<Directory \"$HOME/Sites/\">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
EOS
diff -q $f~ $f || mv $f~ $f
rm -f $f~
"withadministrator privileges
endif
ifapache_version() starts with"2.4"then
do shell script"f=/etc/apache2/users/`basename $HOME`.conf
cat <<EOS > $f~
<Directory \"$HOME/Sites/\">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
EOS
diff -q $f~ $f || mv $f~ $f
rm -f $f~
"withadministrator privileges
endif
do shell script"f=/etc/apache2/httpd.conf
cat $f |
sed -E 's|#(LoadModule[ \\t]+authz_core_module[ \\t]+libexec/apache2/mod_authz_core.so)|\\1|g' |
sed -E 's|#(LoadModule[ \\t]+authz_host_module[ \\t]+libexec/apache2/mod_authz_host.so)|\\1|g' |
sed -E 's|#(LoadModule[ \\t]+userdir_module[ \\t]+libexec/apache2/mod_userdir.so)|\\1|g' |
sed -E 's|#(Include[ \\t]+/private/etc/apache2/extra/httpd-userdir.conf)|\\1|g' |
sed -E 's|#(LoadModule[ \\t]+php5_module[ \\t]+libexec/apache2/libphp5.so)|\\1|g' | # PHP enabled
cat > $f~
diff -q $f~ $f || mv $f~ $f
rm -f $f~
"withadministrator privileges
do shell script"f=/etc/apache2/extra/httpd-userdir.conf
cat $f |
sed -E 's|#(Include /private/etc/apache2/users/\\*.conf)|\\1|g'> $f~
diff -q $f~ $f || mv $f~ $f
rm -f $f~
"withadministrator privileges
endapachectl_init

onapachectl_restart()activate
do shell script"apachectl restart"withadministrator privileges
endapachectl_restart

onapachectl_stop()activate
do shell script"apachectl stop"withadministrator privileges
endapachectl_stop

onweb_sharing_state()try
do shell script"curl -s http://localhost/"
true
onerror
false
endtry
endweb_sharing_state

onweb_sharing_btn()ifweb_sharing_state() then
3
else
2
endif
endweb_sharing_btn

onweb_sharing_msg()ifweb_sharing_state() then
"Web共有の状態 : ON
"& access_url_msg() & "
"& php_enabled_msg()else
"Web共有の状態 : OFF"
endif
endweb_sharing_msg

onhttp_code(url_text)do shell script"curl -LI "& url_text& " -o /dev/null -w %{http_code}"
endhttp_code

onuser_level_root_state()try
http_code("http://localhost/~"& (system info)'s short user name) = "200"
onerror
false
endtry
enduser_level_root_state

onaccess_url_msg()ifuser_level_root_state() then
"
http://"& first_ip_address() & "/
http://"& first_ip_address() & "/~"& (system info)'s short user name& "/
"
else
"
http://"& first_ip_address() & "/
"
endif
endaccess_url_msg

onphp_enabled()try
setstatus_codetodo shell script"grep -E '^LoadModule[ \\t]+php5_module[ \\t]+libexec/apache2/libphp5.so$' /etc/apache2/httpd.conf"
true
onerror
false
endtry
endphp_enabled

onphp_enabled_msg()ifphp_enabled() then
"<?php ... ?> 有効"
else
"<?php ... ?> 無効"
endif
endphp_enabled_msg

onapache_version()do shell script"apachectl -v | awk'/version/{print $3}' | sed -E 's/[^0-9.]+//'"
endapache_version

--優先順位の高いIPアドレスを取得
onfirst_ip_address()do shell script"ipconfig getifaddr `netstat -rn -f inet | awk'/^default/{print $6;exit}'`"
endfirst_ip_address
  • 上記スクリプトを実行すると、以下のように表示され、Web共有のON・OFFと状態の確認ができるのだ。




Viewing all articles
Browse latest Browse all 54

Trending Articles