1
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?

【本番サーバー構築メモ】ApacheユーザーでGitHubのプライベートリポジトリをcloneする完全手順

Last updated at Posted at 2025-07-23

⭐️ はじめに

Laravelなどのプロジェクトを**本番サーバー(例:ConoHa VPS)**にデプロイする際、GitHubのプライベートリポジトリをApacheユーザーでgit cloneしたいケースがあります。

この記事では、AlmaLinux系のサーバーにおいて、ApacheユーザーのSSH鍵を使って安全にcloneする方法を、ステップバイステップで解説します。

⭐️ 前提

▪️ GitHubのアカウントを持っている

▪️ プライベートリポジトリが既に存在している

▪️ サーバーに root または sudo 権限でSSHログインできる

▪️ Apacheユーザーのホームディレクトリが /usr/share/httpd(AlmaLinux標準)

⭐️ 手順

✅ 0:SSH接続

ssh root@your-server-ip

✅ 1:Apacheユーザー用 .ssh フォルダの準備

githubに SSH key登録済みの場合は1~4を飛ばして5からでOK
↓ 以下クリックで5まで飛びます。


Apacheユーザーは通常ログインしないため、ホームディレクトリに .ssh フォルダが存在しません。以下で安全に作成します。

sudo mkdir -p /usr/share/httpd/.ssh
# apache:apache: 所有者もグループも apache に設定
# Apache ユーザー(apache)が /usr/share/httpd/.ssh ディレクトリ内のファイル(特に id_rsa などのSSH鍵)を読み書きできるようにするため。
sudo chown apache:apache /usr/share/httpd/.ssh
# chmod: パーミッション(アクセス権)を変更するコマンド → 700: 所有者のみ読み・書き・実行可能
sudo chmod 700 /usr/share/httpd/.ssh

✅ 2:ApacheユーザーでSSH鍵を生成

sudo -u apache ssh-keygen -t ed25519 -f /usr/share/httpd/.ssh/id_ed25519 -C "apache@yourserver"

▪️ your_email@example.com はGitHubに登録しているメールアドレスでOK

▪️ 質問されたらすべてEnterで進めて大丈夫です

🔑 鍵は以下の場所に生成されます:

# 秘密鍵:
/usr/share/httpd/.ssh/id_ed25519

# 公開鍵:
/usr/share/httpd/.ssh/id_ed25519.pub

✅ 3:公開鍵をGitHubに登録

以下のコマンドで公開鍵の内容を表示します:

cat /usr/share/httpd/.ssh/id_ed25519.pub

コピーしたら、GitHubで次の操作をします。

🔹 GitHub側の手順:

🔸 3-1:GitHubにログイン

🔸 3-2:右上アイコン → Settings

スクリーンショット 2025-07-23 16.27.16.png
スクリーンショット 2025-07-23 16.28.23.png

🔸 3-3:左メニューから SSH and GPG keys をクリック

スクリーンショット 2025-07-23 16.31.44.png

🔸 3-4:New SSH key ボタンをクリック

スクリーンショット 2025-07-23 16.33.23.png

🔸 3-5:任意のタイトルを入力(例: honaki-server)

スクリーンショット 2025-07-23 16.34.49.png

🔸 3-6:公開鍵を貼り付けて Add SSH key

スクリーンショット 2025-07-23 16.35.34.png

✅ 4:パーミッション再確認(念のため)

sudo chown -R apache:apache /usr/share/httpd/.ssh
sudo chmod 700 /usr/share/httpd/.ssh
sudo chmod 600 /usr/share/httpd/.ssh/id_ed25519
sudo chmod 644 /usr/share/httpd/.ssh/id_ed25519.pub

✅ 5:接続確認(GitHubへSSHできるかチェック)

sudo -u apache ssh -T git@github.com

初回は「yes」を入力。
成功すれば以下のようなメッセージが出ます:

Hi your-github-username! You've successfully authenticated, but GitHub does not provide shell access.

✅ 6:一時ディレクトリにclone → /var/www に移動(安全な方法)

Apacheは /var/www に直接書き込めないため、/tmp にcloneしてから移動します。

sudo -u apache git clone git@github.com:yourname/your-private-repo.git /tmp/your-app
sudo mv /tmp/your-app /var/www/
sudo chown -R apache:apache /var/www/your-app
1
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
1
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?