Jekyll2018-02-20T08:38:45+00:00http://blog.atwata.com/atwata developer blogITエンジニアの技術メモ置き場
2018-02-20T08:38:45+00:002018-02-20T08:38:45+00:00http://blog.atwata.com/2018/02/20/2017-09-06-rbenv-error<p>rbenvを使って、ruby 2.4.1をインストールしようとしたが下記のエラー</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ rbenv install 2.4.1
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...
BUILD FAILED (CentOS Linux 7 using ruby-build 20170726-9-g86909bf)
Inspect or clean up the working tree at /tmp/ruby-build.20170905165809.22615
Results logged to /tmp/ruby-build.20170905165809.22615.log
Last 10 log lines:
checking for ruby... false
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/ruby-build.20170905165809.22615/ruby-2.4.1':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
</code></pre></div></div>
<p><code class="highlighter-rouge">configure: error: no acceptable C compiler found in $PATH</code></p>
<p>とのことなのでCコンパイラ(gcc)をインストール</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo yum install gcc
</code></pre></div></div>
<p>再度試してみるが、今度は違うエラーが発生。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ rbenv install 2.4.1
Downloading ruby-2.4.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.bz2
Installing ruby-2.4.1...
BUILD FAILED (CentOS Linux 7 using ruby-build 20170726-9-g86909bf)
Inspect or clean up the working tree at /tmp/ruby-build.20170905170227.22952
Results logged to /tmp/ruby-build.20170905170227.22952.log
Last 10 log lines:
The Ruby openssl extension was not compiled.
The Ruby readline extension was not compiled.
The Ruby zlib extension was not compiled.
ERROR: Ruby install aborted due to missing extensions
Try running `yum install -y openssl-devel readline-devel zlib-devel` to fetch missing dependencies.
Configure options used:
--prefix=/home/vagrant/.rbenv/versions/2.4.1
LDFLAGS=-L/home/vagrant/.rbenv/versions/2.4.1/lib
CPPFLAGS=-I/home/vagrant/.rbenv/versions/2.4.1/include
</code></pre></div></div>
<p>ライブラリが足りないとコマンドまで教えてくれるので、それに従ってインストール</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo yum install -y openssl-devel readline-devel zlib-devel
</code></pre></div></div>
<p>するとruby2.4.1をインストールできました!</p>go言語のインストールからhello worldまで2018-01-30T00:00:00+00:002018-01-30T00:00:00+00:00http://blog.atwata.com/app/2018/01/30/golang-hello-world<h2 id="インストール">インストール</h2>
<p>centos7環境でgo言語はyumでインストールできる。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ sudo yum install golang
</code></pre></div></div>
<p>インストールできたのでバージョンを確認</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ go version
go version go1.8.3 linux/amd64
</code></pre></div></div>
<h2 id="hello-worldの確認">Hello Worldの確認</h2>
<p><code class="highlighter-rouge">hello.go</code> ファイルを作成する</p>
<p>日本語の確認もしたかったので、公式ドキュメントにあるソースをコピペ。</p>
<p>公式→ <a href="http://golang-jp.org/">http://golang-jp.org/</a></p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">package</span> <span class="n">main</span>
<span class="n">import</span> <span class="s2">"fmt"</span>
<span class="n">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
<span class="n">fmt</span><span class="p">.</span><span class="n">Println</span><span class="p">(</span><span class="s2">"Hello, 世界"</span><span class="p">)</span>
<span class="p">}</span>
<span class="err">実行する。</span>
<span class="p">$</span> <span class="n">go</span> <span class="n">hello</span><span class="p">.</span><span class="n">go</span>
<span class="n">go</span><span class="p">:</span> <span class="n">unknown</span> <span class="n">subcommand</span> <span class="s2">"hello.go"</span>
</code></pre></div></div>
<p><code class="highlighter-rouge">go ファイル名</code> ではなく <code class="highlighter-rouge">go run ファイル名</code> らしい</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ go run hello.go
Hello, 世界
</code></pre></div></div>
<p>実行ファイルを作ることもできる(スクリプト言語と異なり、golangがない環境でも生成されたバイナリを実行可能)</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ go build hello.go
$ ls
hello hello.go
$ ./hello
Hello, 世界
</code></pre></div></div>
<p>Linux上でWindows向けにコンパイルすることもできる</p>
<p>※-oで出力ファイル名を指定</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ GOOS=windows GOARCH=amd64 go build -o ./hello.exe
$ ls
hello hello.exe hello.go
</code></pre></div></div>
<p>このhello.exeとhelloをWindows環境に持っていって実行してみる</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>c:\tmp>dir
2018/01/30 18:38 <DIR> .
2018/01/30 18:38 <DIR> ..
2018/01/30 17:56 1,551,539 hello
2018/01/30 18:37 1,627,136 hello.exe
c:\tmp>hello
Hello, 世界
c:\tmp>hello.exe
Hello, 世界
</code></pre></div></div>
<p>あれ?helloもhello.exeも両方実行できる。なんでだろう。
helloのほうはエラーになることを期待してたのに</p>インストールrubyのrmagickで画像を結合・リサイズ・合成する2018-01-05T00:00:00+00:002018-01-05T00:00:00+00:00http://blog.atwata.com/app/2018/01/05/rmagick-sample<h2 id="インストール">インストール</h2>
<p>まずはLinux上にImageMagickをインストールする(CentOS)</p>
<p>言語に依存しない画像処理ソフトウェアであるImageMagick(イメージマジック)をインストールする。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo yum install ImageMagick ImageMagick-devel
</code></pre></div></div>
<p>rubyで扱えるようにするためのgemを入れる</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gem install rmagick
</code></pre></div></div>
<h2 id="使い方">使い方</h2>
<h3 id="画像をファイルから読み込んで2倍の大きさにしてファイルに書き出すサンプル">画像をファイルから読み込んで2倍の大きさにしてファイルに書き出すサンプル</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>require 'rmagick'
image = Magick::ImageList.new("from.jpg")
image = image.scale(2.0)
image.write("to.jpg")
</code></pre></div></div>
<h3 id="2つの画像を結合するサンプル">2つの画像を結合するサンプル</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>require 'rmagick'
image = Magick::ImageList.new("from1.jpg", "from2.jpg")
image = image.append(false) #falseだと左右につなげる。trueだと上下につなげる
image.write("to.jpg")
</code></pre></div></div>
<h3 id="画像をリサイズする">画像をリサイズする</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>require 'rmagick'
imageList = Magick::ImageList.new("from.jpg")
imageList = imageList.resize(200, 200)
imageList.write("to.jpg")
</code></pre></div></div>
<h3 id="二つの画像を合成する">二つの画像を合成する</h3>
<p>透過pngなど透明部分がある画像ならそこは抜いてくっつける。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>imageListFrom = Magick::ImageList.new("from.jpg")
imageListFrame = Magick::ImageList.new("frame.png")
imageList = imageListFrom.composite(imageListFrame, 0, 0, Magick::OverCompositeOp)
imageList.write("to.png")
</code></pre></div></div>インストールcentos7へのpostgresqlのインストール・初期設定2017-12-30T00:00:00+00:002017-12-30T00:00:00+00:00http://blog.atwata.com/middle/2017/12/30/postgresql-install-and-setup<h2 id="インストール">インストール</h2>
<p>yumでインストールする。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>yum install postgresql-server
</code></pre></div></div>
<p>yumでインストールすると</p>
<ul>
<li>postgresql-server</li>
<li>postgresql</li>
<li>postgresql-lib</li>
</ul>
<p>がまとめてインストールされる</p>
<h2 id="データベース初期化">データベース初期化</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>postgresql-setup initdb
</code></pre></div></div>
<p>/var/lib/pgsql/dataに設定ファイルが作成される。</p>
<h2 id="自動起動設定">自動起動設定</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>systemctl enable postgresql.service
</code></pre></div></div>
<h2 id="postgresql起動">postgresql起動</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>systemctl start postgresql.service
</code></pre></div></div>
<h2 id="postgresqlにログイン">postgresqlにログイン</h2>
<p>postgresqlのログイン方法はいろいろあるが、ここではOSユーザpostgresqlでログインしてみる。postgresユーザならpostgresqlに対するパスワードの認証なしでDBにアクセスすることができる。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>su - postgres
psql
</code></pre></div></div>インストールローカル環境のみでgitリポジトリを作り、履歴管理やブランチ操作を行いたい2017-10-10T00:00:00+00:002017-10-10T00:00:00+00:00http://blog.atwata.com/tool/2017/10/10/init-local-bare-git-repository<h2 id="ローカル環境にgitリモートブランチを作る">ローカル環境にgitリモートブランチを作る</h2>
<p>個人開発のちょっとしたツールやプログラムのソース管理について。</p>
<ul>
<li>githubを使ってもよいのだが、公開したくないタイプのソースである</li>
<li>gitlabをわざわざ立ち上げるのも面倒</li>
<li>有料のgithubプライベート版を使うまでもない</li>
<li>そもそもちょっとした履歴管理とローカルディレクトリ間でのpush、pullができればよい</li>
<li>具体的には開発環境で動かして試したソースをpusuして、本番環境でpullしたい</li>
<li>ちなみに開発も本番も同じサーバ</li>
</ul>
<p>コマンドのみなら、すべてローカル環境で管理できる。</p>
<p>以下手順。</p>
<h3 id="共有リポジトリを作成する">共有リポジトリを作成する</h3>
<p>ローカルサーバにあるがリモートリポジトリとして扱われる</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ cd /path/to
$ mkdir myapp.git
$ cd myapp.git
</code></pre></div></div>
<p>リモートリポジトリのディレクトリは.gitをつけるのが慣例らしい</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ git --bare init --shared
</code></pre></div></div>
<p>リモートリポジトリとして初期化するコマンド</p>
<p>–bareは作業ファイルをもたないpushされるための専用のリポジトリという意味</p>
<p>–shareはこのリポジトリを共有可能にするためのオプション。これがないとpushしてもファイルを作成できない。</p>
<h3 id="ファイルが作成されていることを確認">ファイルが作成されていることを確認</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ ls
branches config description HEAD hooks info objects refs
</code></pre></div></div>
<h3 id="別の場所にいってcloneする">別の場所にいってcloneする</h3>
<p>ちなみにローカルからのcloneとはネットワーク経由での取得なので、
<code class="highlighter-rouge">ssh localhost</code> で接続できるようになっている必要がある。</p>
<p>公開鍵認証でログインできるようになっていればSSHエージェント転送を有効にするのがラク</p>
<p>ためしにtmpディレクトリに移動しclone</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ cd /tmp/
$ pwd
/tmp
$ git clone localhost:/path/to/myapp.git
Cloning into 'myapp'...
warning: You appear to have cloned an empty repository.
$ cd myapp/
$ ls
</code></pre></div></div>
<p>これであとは通常のgithubに対してgit操作するようにソースの管理を行える</p>
<h2 id="既存のローカルディレクトリを管理対象にする">既存のローカルディレクトリを管理対象にする</h2>
<p>ついでにこのリポジトリに対してすでに存在するディレクトリをgit管理対象とした上で、pushする方法</p>
<h3 id="既存のファイルがあるディレクトリに移動して初期化リモートリポジトリ追加">既存のファイルがあるディレクトリに移動して初期化、リモートリポジトリ追加</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd /path/to/hoge/
git init
git remote add origin localhost:/path/to/myapp.git
</code></pre></div></div>
<p>git fetchでエラーが出なければ正しくリモートリポジトリが設定されている</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git fetch
</code></pre></div></div>
<h3 id="既存のファイルを追加してコミットプッシュ">既存のファイルを追加してコミット、プッシュ</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>git add .
git commit
git push origin master
</code></pre></div></div>ローカル環境にgitリモートブランチを作るRails5でnginx+pumaでproduction環境を構築する2017-09-30T00:00:00+00:002017-09-30T00:00:00+00:00http://blog.atwata.com/middle/2017/09/30/rails5-nginx-puma<h2 id="nginxのインストール">nginxのインストール</h2>
<p>過去に書いたのこちらを参考</p>
<p>http://blog.atwata.com/middle/2017/04/21/centos-nginx.html</p>
<h2 id="rails側puma設定">rails側puma設定</h2>
<p>rails5ならデフォルトで入っている</p>
<p>rails serverで起動しているのもpuma(webrickではない)</p>
<p>とりあえずpumaをproduction設定で起動</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bundle exec puma -t 5:5 -e production -C config/puma.rb
</code></pre></div></div>
<h3 id="secret_key_base未設定エラー">secret_key_base未設定エラー</h3>
<p>すると、こういうエラーがでる</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>RuntimeError: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml
</code></pre></div></div>
<p>secret_key_baseがセットされていないというエラー</p>
<p>対応は下記のようにsecretを生成して、環境変数にセットする</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bundle exec rake secret
</code></pre></div></div>
<p>を実行すると長いランダム文字列が生成させる</p>
<p>これはconfig/secret.ymlで管理しているのだが、productionは下記のようになっている</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
</code></pre></div></div>
<p>cookieの生成に使うらしいので、セキュリティ上ファイル化は好ましくないらしい。</p>
<p>都度生成し、環境変数として渡す仕組みになっている</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>export SECRET_KEY_BASE=hogehoge
</code></pre></div></div>
<p>のようにする</p>
<p>改めて起動</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bundle exec puma -t 5:5 -e production -C config/puma.rb
</code></pre></div></div>
<h3 id="assetsprecompile未実施エラー">assets:precompile未実施エラー</h3>
<p>起動ログにエラーは出ていないが画面にアクセスするとエラー。
productionはデフォルトでは起動したコンソールにエラーは出さず、ログファイルに出力される。</p>
<p><code class="highlighter-rouge">log/production.log</code> を確認すると下記の表示。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ActionView::Template::Error (The asset "application.css" is not present in the asset pipeline.):
</code></pre></div></div>
<p>確認するとproduction環境では自動でcssやjsをコンパイルしてくれないので(デフォルト設定がそうなっているので)
自分でプリコンパイルする必要がある。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>bundle exec rake assets:precompile RAILS_ENV=production
</code></pre></div></div>
<p>しかし</p>
<h3 id="yarnがインストールされていないエラー">yarnがインストールされていないエラー</h3>
<p>下記のエラーが発生</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
Download Yarn at https://yarnpkg.com/en/docs/install
</code></pre></div></div>
<p>yarnがないというエラー。rails5では使うらしい。</p>
<p>yarnとはjavascriptのパッケージマネージャ。npmの後継として利用され始めている。</p>
<p>下記のようにインストール。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>npm install yarn -g
</code></pre></div></div>
<p>起動して画面が表示されるようになった、</p>
<h3 id="静的ファイルpublicにアクセスできない">静的ファイル(public)にアクセスできない</h3>
<p>しかしcssが効かない。画像も表示されない。</p>
<p>確認すると</p>
<p><code class="highlighter-rouge">config/environments/production.rb</code> が下記のような設定になっているために読み込みができなくなっていた</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code># config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.public_file_server.enabled = true
</code></pre></div></div>
<p>上記のように強制的にtrueにする。</p>
<p>ついにproduction環境のpumaでrailsアプリが起動するようになった。</p>
<h2 id="次はnginxとのつなぎ込み">次はnginxとのつなぎ込み</h2>
<p>例として下記のように設定ファイルを記述する。</p>
<p>/etc/nginx/conf.d/myapp.conf</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>upstream puma_myapp {
server unix:/path/to/myapp/tmp/puma_myapp.sock;
}
server {
listen 80;
server_name [ホスト名];
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /path/to/myapp/;
client_max_body_size 100m;
error_page 404 /404.html;
error_page 500 502 503 504 /500.html;
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
</code></pre></div></div>
<p>nginx再起動</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo systemctl restart nginx
</code></pre></div></div>
<p>これで無事に80番ポートでnginx経由でrailsアプリにアクセスできるようになりました。</p>nginxのインストールrailsでpostgresを使ってテーブルを先に作ってそれを画面に表示2017-09-27T00:00:00+00:002017-09-27T00:00:00+00:00http://blog.atwata.com/app/2017/09/27/rails-postgres-model<h2 id="ざっくりやること">ざっくりやること</h2>
<ul>
<li>database.ymlでのdb設定</li>
<li>テーブルと対になったModel作成</li>
<li>Modelを呼び出すコントローラと表示するビュー作成</li>
</ul>
<h2 id="db設定をpostgresとしてアプリ作成">db設定をpostgresとしてアプリ作成</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rails new myapp -d postgresql
</code></pre></div></div>
<h2 id="databaseyml編集">database.yml編集</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>config/database.yml
default:
adapter: postgresql
encoding: unicode
pool: 5
# dbにアクセスするユーザ名/パスワード/ホストを指定
username: postgres
password: postgres
host: 127.0.0.1
# developmentのところにdatabase名を指定
development:
<<: *default
# database: myapp_development
database: postgres
</code></pre></div></div>
<h2 id="空のモデル作成">空のモデル作成</h2>
<ul>
<li>テーブル名はsampleとする</li>
</ul>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rails generate model sample
</code></pre></div></div>
<h2 id="テーブル名とpkのカラムを指定">テーブル名とPKのカラムを指定</h2>
<p>sample.rbが作成されるのでテーブル名を指定</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>app/model/sample.rb
class Sample < ActiveRecord::Base
self.table_name = 'sample'
end
</code></pre></div></div>
<h2 id="マイグレーション">マイグレーション</h2>
<p>これをしないとPengindMigrationErrorが発生する</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rake db:migrate
</code></pre></div></div>
<h2 id="コントローラ作成">コントローラ作成</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rails generate controller samplelist index
</code></pre></div></div>
<h2 id="コントローラ編集">コントローラ編集</h2>
<ul>
<li>app/controllers/samplelist_controller.rb</li>
</ul>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">SamplelistController</span> <span class="o"><</span> <span class="no">ApplicationController</span>
<span class="k">def</span> <span class="nf">index</span>
<span class="vi">@title</span> <span class="o">=</span> <span class="s1">'Model Sample'</span>
<span class="vi">@datas</span> <span class="o">=</span> <span class="no">Sample</span><span class="p">.</span><span class="nf">all</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>
<h2 id="ビュー編集">ビュー編集</h2>
<ul>
<li>app/views/samplelist/index.html.erb</li>
</ul>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o"><</span><span class="n">h2</span><span class="o">><</span><span class="sx">%= @title %></h2>
<table>
<% @datas.each do |data| %>
<tr>
<td><%=</span> <span class="n">data</span><span class="p">.</span><span class="nf">column1</span> <span class="sx">%></td></span>
<span class="o"><</span><span class="n">td</span><span class="o">><</span><span class="sx">%= data.column2 %></td>
<td><%=</span> <span class="n">data</span><span class="p">.</span><span class="nf">column3</span> <span class="sx">%></td></span>
<span class="o"><</span><span class="n">td</span><span class="o">><</span><span class="sx">%= data.column4 %></td>
</tr>
<% end %>
</table>
</span></code></pre></div></div>
<h2 id="ブラウザで画面確認">ブラウザで画面確認</h2>
<ul>
<li>下記にアクセス</li>
</ul>
<p>http://localhost:3000/samplelist/index</p>ざっくりやることrailsをとりあえず動かしてみる2017-09-24T00:00:00+00:002017-09-24T00:00:00+00:00http://blog.atwata.com/app/2017/09/24/rails-try-to-run-first<h2 id="まずはrailsをインストール">まずはrailsをインストール</h2>
<ul>
<li>rubyとgemがインストールされていることが前提</li>
</ul>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gem install rails
</code></pre></div></div>
<ul>
<li>インストール完了後、バージョン確認</li>
</ul>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rails -v
</code></pre></div></div>
<h2 id="ひな形の作成">ひな形の作成</h2>
<ul>
<li>sampleという名前で作ってみる</li>
</ul>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rails new sample
</code></pre></div></div>
<h2 id="試しに起動してみる">試しに起動してみる</h2>
<ul>
<li>localhost以外からの接続も受け付ける場合は <code class="highlighter-rouge">-b 0.0.0.0</code> をつける</li>
</ul>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cd sample
rails server -b 0.0.0.0
</code></pre></div></div>
<p>下記にアクセスしてようこそ画面がでてくればまずはOK</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>http://動かしているIP:3000/
</code></pre></div></div>
<ul>
<li>起動するポートを変更したい場合</li>
</ul>
<p>Address already in useエラーが発生して、起動できない場合がある。
その場合は指定するポート番号を変更する。</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rails server -b 0.0.0.0 -p 3001
</code></pre></div></div>まずはrailsをインストールcentos7でcronを時間通り動かす2017-09-13T00:00:00+00:002017-09-13T00:00:00+00:00http://blog.atwata.com/os/2017/09/13/centos7-cron<h2 id="anacron">anacron</h2>
<p>centos7デフォルトだとanacronが動くため設定した時間通りにcronが実行されない</p>
<h2 id="通常のcronを入れなおす">通常のcronを入れなおす</h2>
<p>noanacronをインストールし、anacronをインストールする(この順番で入れる必要がある)</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo yum -y install cronie-noanacron
sudo yum -y remove cronie-anacron
</code></pre></div></div>
<p>インストール後は再起動が必要</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo systemctl restart crond
</code></pre></div></div>
<p>下記設定ファイルに実行したジョブを記載</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo vi /etc/cron.d/dailyjobs
# Run the daily, weekly, and monthly jobs if cronie-anacron is not installed
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# run-parts
2 4 * * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.daily
22 4 * * 0 root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.weekly
42 4 1 * * root [ ! -f /etc/cron.hourly/0anacron ] && run-parts /etc/cron.monthly
</code></pre></div></div>anacronpostgresqlにログインできない(Ident authentication failedエラー)2017-09-07T00:00:00+00:002017-09-07T00:00:00+00:00http://blog.atwata.com/db/2017/09/07/postgresql-login<h2 id="下記のエラーが出てログインできない場合の対処法">下記のエラーが出てログインできない場合の対処法</h2>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>psql: FATAL: Ident authentication failed for user "postgres";
</code></pre></div></div>
<p>通常イメージするユーザ名とパスワードでログインできるようにする。</p>
<h3 id="パスワードを設定する">パスワードを設定する</h3>
<p>まずはOSのpostgresユーザにスイッチし、postgres上のパスワードを設定する</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ su - postgres
</code></pre></div></div>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ psql
</code></pre></div></div>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>postgres=# alter role postgres with password 'postgres';
</code></pre></div></div>
<h3 id="ログイン周りの設定をきちんとする">ログイン周りの設定をきちんとする</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>sudo vi /var/lib/pgsql/data/pg_hba.conf
</code></pre></div></div>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code># TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
#local all all peer
local all all md5
# IPv4 local connections:
#host all all 127.0.0.1/32 ident
host all all 127.0.0.1/32 md5
</code></pre></div></div>
<p>上記のMETHODを適宜変更する</p>
<ul>
<li>md5だと通常イメージするパスワード認証</li>
<li>trustだとOS認証(OSユーザで認証済みならパスワードなしでログインできる)</li>
</ul>
<h3 id="typeがlocalのところはこうやってログインするときの設定">TYPEがlocalのところはこうやってログインするときの設定</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>psql -U postgres
</code></pre></div></div>
<h3 id="typeがhostのところはこうやってログインするときの設定">TYPEがhostのところはこうやってログインするときの設定</h3>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>psql -U postgres -h 127.0.0.1
</code></pre></div></div>
<h3 id="postgresql再起動">postgresql再起動</h3>
<p>設定ファイルを変更したらpostgresqlを再起動しないと反映されない</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>$ sudo service postgresql restart
</code></pre></div></div>下記のエラーが出てログインできない場合の対処法