当サイトは、アフィリエイト・アドセンス広告を掲載しています。広告があるサイトであると知っていただければ幸いです。

消費者庁が、2023年10月1日から施行予定である景品表示法の規制対象(通称:ステマ規制)にならないよう、配慮して記事を作成しています。もし問題のある表現がありましたら、問い合わせよりご連絡ください。

参考:令和5年10月1日からステルスマーケティングは景品表示法違反となります。 | 消費者庁

Apacheを一般ユーザで起動する設定

Apacheを一般ユーザで起動する設定

外部公開しない(開発用など)サーバなどで一般ユーザでApacheを立ち上げたい要求はありませんか?

しかし、デフォルト設定のまま一般ユーザでApacheを起動しようとすると、Permissionがなくてエラーになり、起動できません。

ServerRootの場所を一般ユーザの場所にすれば良さそうですが、apacheのmoduleが格納されていたりする関係でServerRootは変更したくないという場合も多いと思います。

この記事では、ServerRootは変更せずに、Apacheを一般ユーザで起動できるようにするための設定をまとめました。

実行環境

一般ユーザでApacheを起動できない理由

4つありますので、それぞれに変更内容を記述します。
ServerRoot(本記事の場合/etc/httpd)配下のconf/httpd.confを編集します。

1.PIDファイル保存場所にroot権限が必要

  • エラーログ
$ httpd -k start
(13)Permission denied: AH00058: Error retrieving pid file /run/httpd/httpd.pid
AH00059: Remove it before continuing if it is corrupted.
  • デフォルト設定

root権限が必要な下記の場所へ保存する設定になっています。

/run/httpd/httpd.pid
  • 変更内容
PidFile (一般ユーザにpermissionがある保存場所)

2.ログファイル保存場所にroot権限が必要

  • エラーログ
$ httpd -k start
(13)Permission denied: AH00091: httpd: could not open error log file /etc/httpd/logs/error_log.
AH00015: Unable to open logs
  • デフォルト設定
    root権限が必要な下記の場所へ保存する設定になっています。
    相対パスなので、ServerRootの配下です。
ErrorLog "logs/error_log"
CustomLog "logs/access_log" common
CustomLog "logs/access_log" combined
  • 変更内容
ErrorLog (一般ユーザにpermissionがある保存場所)
CustomLog (一般ユーザにpermissionがある保存場所) common
CustomLog (一般ユーザにpermissionがある保存場所) combined

3.ListenするポートがWell-Knownポート

  • エラーログ
$ httpd -k start
(13)Permission denied: AH00072: make_sock: could not bind to address [::]:80
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
  • デフォルト設定
Listen 80

Well-Knownポートである80をListenする設定になっています。

  • 変更内容
## Well-Knownポート以外の番号
Listen 10080

なお、HTTPの標準ポートと変わるため、サーバアクセス時にURLのポート番号を省略できず、記入必須です。

  • 80ポートの場合
    • http://ServerName
  • 80ポートから変更する場合
    • http://ServerName:PortNumber

4.共有メモリ用ファイル保存場所にroot権限が必要(Apache v2.4以上の場合)

  • エラーログ
[auth_digest:error] [pid 4989] (13)Permission denied: AH01762: Failed to create shared memory segment on file /run/httpd/authdigest_shm.4989
[auth_digest:error] [pid 4989] (13)Permission denied: AH01760: failed to initialize shm - all nonce-count checking, one-time nonces, and MD5-sess algorithm disabled
  • デフォルト設定

ダイジェスト認証用のモジュールmod_auth_digestを組み込んでいる場合、Apache立ち上げ時に共有メモリ用ファイルを作成しにいきます。
この保存先もroot権限が必要な下記の場所へ保存する設定になっています。

/run/httpd/authdigest_shm.(PID)
  • 変更内容
DefaultRuntimeDir (一般ユーザにpermissionがある保存場所)
  • 参考情報

API Changes in Apache HTTP Server 2.4 since 2.2

“If your module interfaces with this feature…”という欄にDefaultRuntimeDirについての説明の記述あり(Apache v2.4.2以上で有効)

(おまけ)起動時のServerNameのエラーログが出ないようにする

この項目の変更をしなくてもApache自体は起動しますが、エラーメッセージを出すのを抑えたい場合は変更してください。

  • エラーログ
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message
  • デフォルト設定
    設定なし
  • 変更内容
ServerName localhost

ServerNameは正しければ何でもよいのですが、とりあえずlocalhostにしました。

confファイルの変更内容まとめ

まず、ログファイルなどの保存用にホームにhttpd用のディレクトリを作成し、保存場所はそこへ指定していますが、一般ユーザのPermissionがあれば、どこでも構いません。

$ mkdir ~/httpd

これまでの変更内容をまとめたconfファイルです。
オリジナルとのdiffを取っています。

@@ -39,7 +39,12 @@
 # prevent Apache from glomming onto all bound IP addresses.
 #
 #Listen 12.34.56.78:80
-Listen 80
+Listen 10080
+
+PidFile /home/UserName/httpd/httpd.pid
+
+DefaultRuntimeDir /home/UserName/httpd

 #
 # Dynamic Shared Object (DSO) Support
@@ -93,6 +98,7 @@
 # If your host doesn't have a registered DNS name, enter its IP address here.
 #
 #ServerName www.example.com:80
+ServerName localhost

 #
 # Deny access to the entirety of your server's filesystem. You must
@@ -179,7 +185,8 @@
 # logged here.  If you *do* define an error logfile for a <VirtualHost>
 # container, that host's errors will be logged there and not here.
 #
-ErrorLog "logs/error_log"
+ErrorLog /home/UserName/httpd/error_log

 #
 # LogLevel: Control the number of messages logged to the error_log.
@@ -209,12 +216,14 @@
     # logged therein and *not* in this file.
     #
     #CustomLog "logs/access_log" common
+    CustomLog /home/UserName/httpd/access_log common

     #
     # If you prefer a logfile with access, agent, and referer information
     # (Combined Logfile Format) you can use the following directive.
     #
-    CustomLog "logs/access_log" combined
+    CustomLog /home/UserName/httpd/access_log combined
 </IfModule>

 <IfModule alias_module>

動作確認

$ httpd -k start
$ ps aux | grep httpd
UserName  10161  0.0  0.0 289828  5128 ?        Ss   17:16   0:00 httpd -k start
UserName  10165  0.0  0.0 503028  5648 ?        Sl   17:16   0:00 httpd -k start
UserName  10166  0.0  0.0 306356  3364 ?        Sl   17:16   0:00 httpd -k start
UserName  10169  0.0  0.0 306356  3360 ?        Sl   17:16   0:00 httpd -k start
UserName  10182  0.0  0.0 306356  3364 ?        Sl   17:16   0:00 httpd -k start
UserName  10187  0.0  0.0 306356  3364 ?        Sl   17:16   0:00 httpd -k start

このようにプロセスが起動していることが分かると思います。

http://(IPアドレス):10080

上記のようにApacheを起動しているマシンのIPアドレスとポート番号を指定してブラウザでアクセスし、テストページが表示されていたら無事に起動しています。

私の場合はFedraCore25をベースにしたRPMのApacheのため、FedraのApacheテストページが表示されましたが、Apacheのテストページが表示されれば起動できていると思います。

4094b48a c903 1d41 9d19 6990d3f87b42 - Apacheを一般ユーザで起動する設定

おわりに

本記事のようにconfファイルを変更すれば一般ユーザでもApacheの起動/終了が可能になります。

ポート番号を指定しなければいけない点が、面倒ではありますが、もしよければご利用ください。

技術apache

Posted by tomluck