CakePHP ローカル環境からVPSにpush
ローカルCentOSからVPSCentOSへpushできるようにする - americandog1993の日記
基本の流れは昨日の記事と同じ。
今日はindex.htmlでなくCakePHPでチャレンジしてみる。
VPS側操作
前記事と同様にtest.gitとtestを準備する。
ローカル側操作
/var/www/html下にCakePHPプロジェクトを作る。
VirtualBoxのCentOS7にCakePHPを導入する - Qiita
ここを参考に。
$ cd /var/www/html $ php /usr/local/bin/composer create-project --prefer-dist cakephp/app test
少し待つ。
プロジェクトができたら以下のgit操作を行う。
$ cd test $ git init $ git remote add origin ssh://americandog@xxx.xxx.xx.xx:22/var/www/git/test.git $ git add . $ git commit -m "test commit" $ git push origin master
ローカルCentOSからVPSCentOSへpushできるようにする
すごくハマって日曜日の夜潰したのでメモ。
動機
VPSに直接SFTPアクセスしてファイル操作するのダサいからやめたい。
ローカル開発環境で動き検証してからpushしたい。
git初期設定
ローカル側、VPS側とも最低限の初期設定を済ませておく。
git config --global user.name "americandog" git config --global user.email "e@mail.com"
下準備
ローカル、VPS共にまず最もシンプルな構成でHelloWorldを組む。
さくらVPS(CentOS7)をWebサーバ化してHelloWorldする - americandog1993の日記
VPS側操作
以下のディレクトリを作成。
$ sudo mkdir /var/www/git
今回の作業ディレクトリを外部から書き込み可能にしておく。
$ sudo chmod -R 777 /var/www/html $ sudo chmod -R 777 /var/www/git
今回は777としたが、たぶん777より安全な権限指定方法はあるので、各自調べてほしい。
gitディレクトリ下に好きな名前でリポジトリを作成。
$ sudo mkdir /var/www/git/test.git
test.gitでgit initする。
共有リポジトリには--bareと--shareコマンドをつける、らしい。
$ cd /var/www/git/test.git $ git --bare init --shared
htmlディレクトリにtest.gitをcloneする。
$ cd /var/www/html $ git clone /var/www/git/test.git
これでVPSのhtml直下にtestリポジトリができる。
最後にhookを作る。
hookを作っておかないとgit/test.gitへpushされた内容がhtml/testに反映されない。
$ vi /var/www/git/test.git/hooks/post-receive
作ったhookに実行権限を与える。
$ sudo chmod +x /var/www/git/test.git/hooks/post-receive
ここらの操作は完全に
自前のサーバーにGitリポジトリを作成してプッシュでサイト更新する - Qiita
の受け売り。
ローカル側操作
まずVPS側と同じように作業ディレクトリをsshログインユーザの所有とする。
これをやり忘れるとハマるので注意。
$ sudo chown -R americandog /var/www/html $ sudo chown -R americandog /var/www/git
htmlディレクトリにVPSのtest.gitをcloneしてくる。
git clone対象urlは ssh://ユーザ名@ホスト名(IPアドレス):ポート番号/var/www/git/test.git のように記述する。
$ cd /var/www/html $ git clone ssh://americandog@xxx.xxx.xx.xx:22/var/www/git/test.git
ローカルのhtml直下にもtestリポジトリができる。
html直下に作ってあったindex.htmlをtestに入れる。
$ cd /var/www/html $ mkdir test $ mv index.html test/index.html
$ git add . $ git commit -m "first commit" $ git push
ハマった箇所
とにかく権限設定でハマった。
権限をまともに設定していないと作業ユーザでgit cloneができない。
かといってsudo git cloneするとrootでcloneしたことになり、実行者がsshユーザでなくなってしまい、permission denied(public key)エラー等を引き起こす。
権限の理解がまだ疎かなのでかなり試行錯誤するハメになってしまった。
また、ローカルのrsa_id、VPSのauthorized_keysの権限設定も注意したい。
sshで公開鍵認証を使ってアクセスする - Qiita
鍵は600、鍵が入っている.sshは700に権限設定しないと色々問題が起こるようだ。
参考
GitHub - GitHubからクローンできない。(38609)|teratail
【bitbucket】git cloneで`Permission denied (publickey).`や`Bad owner or permissions`と言われたときの対処法 - tweeeetyのぶろぐ的めも
自前のサーバーにGitリポジトリを作成してプッシュでサイト更新する - Qiita
Gitで”Permission denied (publickey).” が出たときのメモ - Qiita
Gitリモートリポジトリ構築 CapmNetwork
Gitのフックの説明と挙動の検証 - Qiita
gitでsshのポート番号を指定する | 黒川仁の文具堂ブログ三昧
CentOSにSSH鍵認証で接続するGitリポジトリを作成
git のリポジトリに ssh を利用してアクセスできるようにする. - It_lives_vainlyの日記
No such remote 'origin' - Qiita
gitのoriginのurlを変更する。 - 僕の今さら日記
共用サーバーにGitリモートリポジトリを設置、pushで公開できるようにする | Tips Note by TAM
今回はめちゃくちゃ調べたが、半分わかって半分わかってない感じ。
とりあえず動かせたのでよしとする。
VPSでCakePHP③ MySQL設定
CakePHP導入段階ではDBとの接続が確立されていない。
ということでMySQL設定を進めていく。
MySQLにログイン
まずは起動。
$ sudo systemctl start mysqld
初期パスワードが生成されているので確認。
$ sudo cat /var/log/mysqld.log | grep 'password is generated' 2017-07-29T03:12:19.639837Z 1 [Note] A temporary password is generated for root@localhost: [パスワード]
パスワードを確認できたらログインする。
$ mysql -u root -p
パスワードを求められるので入力。
うまく入れた場合はまずパスワードを変更する(ここでトラブった場合は後述)。
mysql> set password for root@localhost=password('Cake@1234');
ある程度強固なパスワードを設定する必要があり、大文字英字、小文字英字、数字、記号をすべて使わなければ通らないので注意。
databaseを作成する。
mysql> create database cake_db; Query OK, 1 row affected (0.00 sec)
ここまで終えたらMySQLを出る。
mysql> \q Bye
CakePHPに接続
$ cd [自分のプロジェクトディレクトリ] $ sudo vi config/app.php
app.phpの'Datasources'以下を修正。
// config/app.php 'username' => 'root' 'password' => 'Cake@1234' 'database' => 'cake_db'
これでdatabaseが緑になるはず。
なぜかMySQLに入れない時
MySQL初ログイン時、どう考えても正しいパスワードを打ってるのに入れないことがある(筆者はこのトラブルに遭遇した)。
その場合は下記のteratailの質問を参考にセーフモードでパスワードの再設定を行う。
MySQL - CentOS7.2でのMySQLインストールでの問題(53026)|teratail
以下、回答からの引用。
CentOS7ならこのコマンドを順に叩いていけば解決するはず。
systemctl stop mysqld systemctl set-environment MYSQLD_OPTS="--skip-grant-tables" systemctl start mysqld mysql -u root mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPassword') -> WHERE User = 'root' AND Host = 'localhost'; mysql> FLUSH PRIVILEGES; mysql> quit systemctl stop mysqld systemctl unset-environment MYSQLD_OPTS systemctl start mysqld
VPSでCakePHP② CakePHP環境構築
VirtualBoxのCentOS7にCakePHPを導入する - Qiita
ほとんどこの通りに進めればいけた。
Apache, git, unzipを入れる
$ sudo yum -y install git unzip httpd
httpdはApacheWebサーバ。
gitはそのままgit。
unzipはなんだろう。解凍かなんかするためのツール?(たぶん)
Apacheを起動しておく。
$ sudo systemctl start httpd
firewall設定
Apacheを入れたのでアクセスできるようにしておく。
$ sudo firewall-cmd --zone=public --add-service=http --permanent $ sudo firewall-cmd --add-port=80/tcp --zone=public --permanent $ sudo firewall-cmd --reload
MySQLを入れる
$ sudo yum -y install http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm $ sudo yum -y install mysql-community-server
なぜかmysql-community-serverのインストールがうまくいかないことがあるので注意。
その場合は諦めずもう一度同じコマンド実行したらインストールできる(はず)。
PHP7を入れる
$ sudo yum -y install epel-release $ rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm $ yum -y install --enablerepo=remi,remi-php70 php php-devel php-mbstring php-intl php-mysql
Composerを入れる
ComposerはRailsでいうbundler的なツールらしい。
色々必要なアレコレをうまいことやってくれる便利なやつ。
$ curl -sS https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer
CakePHPプロジェクト作成
$ cd /var/www/html $ sudo /usr/local/bin/composer create-project --prefer-dist cakephp/app cakephp_app
プロジェクト名は任意に。
続いてCakePHPのページがルートに表示されるようにする。
$ sudo vi /etc/httpd/conf/httpd.conf
# /etc/httpd/conf/httpd.conf DocumentRoot "/var/www/html/cakephp_app/webroot"
Apacheを再起動。
$ sudo systemctl restart httpd
ブラウザでアクセスしてCakePHPの画面は表示されればOK。
Databaseのところが赤くなってると思うが後で設定していく。
VPSでCakePHP① CentOSセットアップ
備忘録。
環境
さくらのVPS
CentOS7
アップデート
とりあえず全体アップデートをかける。
# yum update
ユーザ作成
平常作業時はrootを使わないのが基本、らしい。
# useradd americandog # passwd americandog
二回パスワードを打つ。
これで作成は完了。
それから作成ユーザにsudo権限を持たせておく。
# usermod -G wheel americandog
以後作成ユーザで操作していく。
# su americandog
セキュリティ
サーバーをセキュアにするために以下の設定を行う。
最初からrootログインできないように設定。
sshログインに使うポートを変更(22はあまりに有名なため攻撃を受けやすい)。
パスワードログインを不可にして鍵ログインするように設定。
鍵生成、公開鍵のサーバーへの受け渡しはここを参考に。
ssh公開鍵認証設定まとめ - Qiita
$ sudo vi /etc/ssh/sshd_config
# /etc/ssh/sshd_config - #Port 22 + Port 22222 - #PermitRootLogin yes + PermitRootLogin no - #PasswordAuthentication yes + PasswordAuthentication no
再起動して設定反映。
$ sudo systemctl restart sshd
ファイアウォールのポートも合わせる。
<!-- /user/lib/firewalld/services/ssh.xml --> - <port protocol="tcp" port="22"/> + <port protocol="tcp" port="22222"/>
再起動で設定反映。
$ sudo firewall-cmd --reload
Railsで本番環境だけpostgresql使いたい設定したら怒られた
ここ数日、自分で環境作ってのRailsアプリ開発に挑戦している。
予想はしてたが、ハマりまくる。
Cloud9って楽だったんだなあ。
Gemfile
source 'https://rubygems.org' gem 'rails', '5.0.3' gem 'puma', '3.9.1' gem 'bootstrap-sass', '3.3.6' gem 'sass-rails', '5.0.6' gem 'uglifier', '3.2.0' gem 'coffee-rails', '4.2.2' gem 'jquery-rails', '4.3.1' gem 'turbolinks', '5.0.1' gem 'jbuilder', '2.6.1' gem 'therubyracer' group :development, :test do gem 'sqlite3', '1.3.13' gem 'byebug', '9.0.6', platform: :mri end group :development do gem 'web-console', '3.5.1' gem 'listen', '3.0.8' end group :test do gem 'rails-controller-testing', '1.0.2' gem 'minitest-reporters', '1.1.14' end group :production do gem 'pg', '0.18.4' end
開発環境ではsqliteで、って設定。
これでbundle installしたらpgないぞコラって怒られた。
対処
色々試したのでどれが効いたか正直あやふやなのだけど、
まずおとなしくpostgresqlを入れてみた。
# wget yum.postgresql.org/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm # rpm -ivh pgdg-centos95-9.5-2.noarch.rpm # yum -y install postgresql95-server postgresql95-devel postgresql95-contrib
が、だめ。
次に--without productionを試した。
$ bundle install --without production
bundle updateしろと出てきたらその通り従う。
これでいけた。
rails gしたら`method_missing': undefined method `load_defaults' ってエラー出た
Gemfile
source 'https://rubygems.org' gem 'rails', '5.0.3' gem 'puma', '3.9.1' gem 'bootstrap-sass', '3.3.6' gem 'sass-rails', '5.0.6' gem 'uglifier', '3.2.0' gem 'coffee-rails', '4.2.2' gem 'jquery-rails', '4.3.1' gem 'turbolinks', '5.0.1' gem 'jbuilder', '2.6.1' gem 'therubyracer' group :development, :test do gem 'sqlite3', '1.3.13' gem 'byebug', '9.0.6', platform: :mri end group :development do gem 'web-console', '3.5.1' gem 'listen', '3.0.8' end group :test do gem 'rails-controller-testing', '1.0.2' gem 'minitest-reporters', '1.1.14' end group :production do gem 'pg', '0.18.4' end
CentOS環境にはRails 5.1〜, GemfileにはRails 5.0〜を指定してbundle update, bundle installした。
その結果rails gした際に以下のエラー発生。
# webyaji=筆者の開発アプリ [vagrant@localhost webyaji]$ rails g model test /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/railtie/configuration.rb:95:in `method_missing': undefined method `load_defaults' for #<Rails::Application::Configuration:0x007fa562795108> (NoMethodError) from /home/vagrant/app/webyaji/config/application.rb:12:in `<class:Application>' from /home/vagrant/app/webyaji/config/application.rb:10:in `<module:Webyaji>' from /home/vagrant/app/webyaji/config/application.rb:9:in `<top (required)>' from /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:156:in `require' from /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:156:in `require_application_and_environment!' from /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:143:in `generate_or_destroy' from /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:60:in `generate' from /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands/commands_tasks.rb:49:in `run_command!' from /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.3/lib/rails/commands.rb:18:in `<top (required)>' from bin/rails:9:in `require' from bin/rails:9:in `<main>'
たぶんload_defaultsとかいうメソッドしらねーよって怒られてる。
一時間ほどハマったが調べていくとload_defaultsがRails5.1系から使えるようになったメソッドと判明。
# config/application.rb require_relative 'boot' require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module Webyaji class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 5.1 + # config.load_defaults 5.1 # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. end end
コメントアウト後、再度rails gして大丈夫になったことを確認。
[vagrant@localhost webyaji]$ rails g model test Expected string default value for '--jbuilder'; got true (boolean) invoke active_record create db/migrate/20170725131230_create_tests.rb create app/models/test.rb invoke test_unit create test/models/test_test.rb create test/fixtures/tests.yml