0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「sudo -u apache」と「sudo」の違い

Posted at

⭐️ 目的

学習用に記録したまとめ

⭐️ 一言で言うと?

sudo と sudo -u apache は 「誰として実行するか」が異なる。

⭐️ 違いの概要

コマンド 実行ユーザー 用途の例
sudo コマンド root(管理者) システム設定やインストールなど、管理者権限が必要な処理
sudo -u apache コマンド apacheユーザー Webアプリが使うユーザーとしてコマンドを実行(特に npm install など)

⭐️ 具体例

✅ sudo

sudo dnf install httpd

→ 管理者ユーザー(root)として dnf install を実行。
→ Apache のインストールなどに使う。

✅ sudo -u apache

sudo -u apache npm install

→ Webサーバーで動く apache ユーザーとして npm install を実行。
→ これは Browsershot や Laravel が Apache ユーザーで動く場合に必須。

⭐️ どちらを使うかの基準

操作内容 使うべきコマンド例
パッケージのインストールやサービスの起動 sudo
Laravelを動かすユーザーでの作業(npm等) sudo -u apache(例:npm install, php artisan)

⭐️ よくあるケース

# Apacheユーザーでnpm installしたい
sudo -u apache HOME=/usr/share/httpd npm install

# rootユーザーで httpd サービスを起動
sudo systemctl start httpd

⭐️ 補足:なぜ Apache ユーザーで動かす?

なぜ Apache ユーザーで動かす?

Laravel + Browsershot + Puppeteer などでは、

▪️ Apache が Puppeteer のキャッシュディレクトリにアクセスする

▪️ npm install 時に作成されたファイルの所有権が Apache でないと、Apache で実行時に失敗する

ため、npm install は Apache ユーザーで行うべき。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?