14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Webサーバーへ静的ホームページをgitから直接配置する

14
Posted at

モチベーション

ホームページのソースコードをgitで管理したい。

ソースコードをgitで管理してもFTPを使って手動で配置していると
Webサーバー上のファイルを直接編集して、なぜかgit上のファイルと乖離します。

配置手順を自動化して、gitにあるソースコードとWebサーバー上のソースコードを同じに保ちましょう。

要するに

Webサーバーにsshログインしてgit pullします。

前提条件

Webサーバー

  • sshログインできる
  • gitコマンドがインストールされている

gitサーバー

  • Deploy keysを設定できる
  • githubまたはgitlabを想定

手順

リポジトリを作成

cd homepage
git init
touch index.html
git add .
git commit -m 'Initial Commit.'
git remote add origin git@github.com:oreore/homepage.git
git push -u origin master

WebサーバーのDeploy keysを設定

Webサーバーにsshログインします。

秘密鍵を作成します。

ssh-keygen -t rsa -C "$your_email"

公開鍵を表示します。

cat ~/.ssh/id_rsa.pub

プロジェクトの設定からDeploy keysを選び、表示した公開鍵を登録します。

Webサーバーでgit clone

wwwディレクトリでホームページを公開する例です。

sshログインします。

ssh web@oreno.web.server

git cloneします。

git clone git@github.com:oreore/homepage.git www

Deploy

webユーザーでログインする例です。

ssh web@oreno.web.server "cd www; git pull"

応用編

ロールバックできるようにタグをつける

ssh web@oreno.web.server "cd www; git tag `date +%Y%m%d%H%M%S`; git pull"

間違ってタグをつけた際は、Webサーバーにログインして、手動で直前のタグをgit checkoutすれば元に戻せます。

Webサーバーに秘密鍵でログイン

作業用PCで作成した公開鍵をWebサーバーに送ります。

scp ~/.ssh/id_rsa.pub  web@web@oreno.web.server:/home/web

Webサーバーにsshログインして、公開鍵を登録します。

cat id_rsa.pub >> .ssh/authorized_keys

ログインし直します。

ssh web@oreno.web.server

パスワードを聞かれなければ成功です。

14
10
1

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
14
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?