Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

ロリポップサーバにSSH接続、gitで更新する構築方法!

こんにちは、長谷川です。
私は、数年前からWebサイトやサービスをリリースしています。
FTPを利用してファイルをアップロードしていましたが、
あまりにも面倒でgitを使って管理することにしました。

最近だと、gitで管理するのは主流で簡単にgit環境が構築できる
SSH接続が対応しているレンタルサーバも増えています。

ロリポップもSSH接続ができて
大手なサーバで10日間無料なので
ぜひチャレンジしたい方は登録してみてください。



では、本題のgitで更新する方法を紹介します。

ロリポップのサーバ管理でSSHを有効にする

ロリポップのアカウント作成を済ませ(スタンダードプラン以上)
SSHを有効にすると、
SSHのアカウントが生成されます。

以降のサーバ、アカウント名、パスワードは
ここに載っているものを参照

SSH接続をする

ssh アカウント名@サーバ名 -p 2222

パスワードを問われるので
SSHアカウントのパスワード入力

ベアリポジトリとノンベアリポジトリを作成

SSH接続をしてサーバ上に
ベアリポジトリ(ローカルとの中継で公開する必要のないフォルダ)と

ノンベアリポジトリ(公開されるフォルダ)を作成します。

1)ベアリポジトリを作成

フォルダ名を◯◯.git(.gitは必須)作成

フォルダ直下で

git init --bare

を入力

2)ノンベアリポジトリを作成

ロリポップの公開フォルダ(web)以下の階層に
フォルダを作成(フォルダ名は自由)
3)2で作成したフォルダに入り

git init

git remote add origin ~/◯◯.git

(ベアリポジトリのパス)

4)ベアリポジトリのpost-receiveを作成

post-receive.sampleファイルのあるフォルダまでコマンドで移動します。
(〇〇.git / hooks /)

post-receive編集する際の例「

mv post-receive.sample post-receive

vi post-receive

insertモードで以下の三行を追加します。
本番用のパスは、home/users~と言ったパスを入力します。

分からなかったら

pwd

と入力しましょう。

#!bin/sh
cd /home/users/0/(本番用のフォルダパス)
git --git-dir=.git pull origin master

exitで抜けてローカルにクローンを作成

exitでSSH接続を切断して
ローカルリポジトリを作成します。

git clone ssh:アカウント名@サーバ名:2222 ベアリポジトリのパス(~/home/users•••/◯◯.git

これでローカルリポジトリが
生成されました。

これでローカルリポジトリを変更して

git add .

git commit -m 'test'

git push origin master

とでも入力してみてください。

ロリポップサーバのリモートリポジトリが
反映されているはずです。

反映されていな場合

post-receiveの中身の
ノンベアリポジトリのパスが
きちんと通っているかを確認

push時に警告は出ていないかを確認する

スポンサーリンク

おわりに

今回は、ロリポップサーバにgitから更新する方法をご紹介しました。

私自身、この方法が成功するまで結構時間が掛かっており、
フォルダ名を〇〇.gitにしていなかった点や、
ノンベアリポジトリ 、ペアリポジトリの基礎知識が皆無というところで
かなり苦労しました。

まだまだ勉強不足で自分の満足のいく方法ではないので
さらに良いやり方をご存知の方はご教授ください。

例えば、
git hubとロリポップサーバを同時に更新する方法など。

なるべくシンプルに書いたので
参考にしてみてください。

最後まで読んでいただき、ありがとうございました。

追記

ローカルとリモートのリポジトリ名(origin等)や、
標準ブランチ(masterなりmain)、
なるべく合わせると変なところでつまずに済みそう。
(ちゃんと理解するのがベストだけど。)

コメント

  1. 匿名 より:

    共有ありがとうございます,おかげでやることがすごくよくわかりました。
    やってみての情報共有として,
    私の環境では post-receive.sample は既定では存在せず,自分で作成しました。
    あとローカルレポジトリを作成するところ

    > git clone ssh:アカウント名@サーバ名:2222ノンベアリポジトリのパス(~/home/users•••/◯◯.git

    のところは,ノンベアじゃなくてベアリポジトリじゃないかなと思いました。
    お礼がてら情報共有させてください!

スポンサーリンク
スポンサーリンク
タイトルとURLをコピーしました