apostrophe で動いていた WEB アプリ2つの wplj への引っ越しが終わったので、apostrophe の方も Ubuntu 14.04 LTS から 16.04 LTS にアップグレードした。というか、これまでは Ubuntu と Windows 7 のデュアルブート体制だったのを、Windows はさっぱりと消して Ubuntu をクリーンインストールした。
詳細は wplj へのインストールの時と同じなので割愛。あとは少しずつ環境を整えていこう。
カテゴリー: Ubuntu
Railsアプリの引っ越し(4)デーモンとして動かす編
起動スクリプトを、これも apostrophe からコピー。
tonzlr@wplj:/etc/init.d$ cd tonzlr@wplj:~$ cd /etc/init.d tonzlr@wplj:/etc/init.d$ sudo scp tonzlr@apostrophe:/etc/init.d/tonzlr . sudo tonzlr のパスワード: tonzlr@apostrophe's password: tonzlr 100% 455 0.4KB/s 00:00
これも変更しないでいいんじゃないかな。
#!/bin/bash PATH=/usr/local/bin:/usr/bin:/bin:/sbin:PATH TONZLR_ROOT=/home/tonzlr/tonzlr case "$1" in start) echo "Starting Tonzlr" cd ${TONZLR_ROOT} export SECRET_KEY_BASE=`rake secret` bundle exec unicorn_rails -c unicorn.conf -E production -D ;; stop) echo "Stoping Tonzlr" PID=`cat ${TONZLR_ROOT}/unicorn.pid` kill -QUIT ${PID} ;; *) echo "Usage: tonzlr {start|stop}" >&2 exit 1 ;; esac exit 0
起動確認。
tonzlr@wplj:~/tonzlr$ sudo /etc/init.d/tonzlr start Starting Tonzlr
OK。
Unit ファイルを書く。
[Unit] Description=Tonzlr service After=network.target [Service] ExecStart=/etc/init.d/tonzlr start ExecStop=/etc/init.d/tonzlr stop Restart=always Type=forking PIDFile=/home/tonzlr/tonzlr/unicorn.pid [Install] WantedBy=multi-user.target
そして、/etc/systemd/system/tonzlr.service にリンクを張る。
tonzlr@wplj:~/tonzlr$ sudo ln -s /lib/systemd/system/tonzlr.service /etc/systemd/system/tonzlr.service
起動確認。
tonzlr@wplj:~/tonzlr$ sudo systemctl start tonzlr
OK。start したり stop したりして確認できた。
Ubuntu の起動時に自動起動するようにする。
tonzlr@wplj:~/tonzlr$ sudo systemctl enable tonzlr Synchronizing state of tonzlr.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable tonzlr insserv: warning: script 'K01bruschetta' missing LSB tags and overrides insserv: warning: script 'tonzlr' missing LSB tags and overrides insserv: warning: script 'bruschetta' missing LSB tags and overrides update-rc.d: error: tonzlr Default-Start contains no runlevels, aborting.
むぅ、またエラーが出てる。だけど、この間の Flask アプリではこれでもちゃんと自動起動できるようになっていたので、今回もそれを期待してリブートしてみる。
tonzlr@wplj:~/tonzlr$ sudo reboot
……はたしてリブートした結果……おお、ちゃんと自動起動されている!
よし、最後に、Nginx のヴァーチャルホスト tonzlr を設定する。
upstream unicorn-tonzlr { server 127.0.0.1:9009; } server { # port listen 80; # server name server_name tonzlr; # document root root /home/tonzlr/tonzlr; # index #index index.php index.html index.htm; # log files access_log /var/log/nginx/tonzlr/access.log combined; error_log /var/log/nginx/tonzlr/error.log warn; keepalive_timeout 60; proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60; location / { #root /home/tonzlr/tonzlr; 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; proxy_pass http://unicorn-tonzlr; } }
sites-enabled/tonzlr にリンクを張って:
tonzlr@wplj:/etc/nginx$ sudo ln -s /etc/nginx/sites-available/tonzlr /etc/nginx/sites-enabled/tonzlr
ログファイル用のディレクトリを作る。
tonzlr@wplj:/etc/nginx$ sudo mkdir /var/log/nginx/tonzlr
Nginx をリロード。
tonzlr@wplj:/etc/nginx$ sudo systemctl reload nginx
hosts ファイルに tonzlr を追加して、http://tonzlr/ にブラウザでアクセスしてみる。
OK!!!!!!!!!
あとは、他のマシンの hosts ファイルを書き換えるだけだ。
Railsアプリの引っ越し(3)Unicornで動かす編
Unicorn のインストール。
tonzlr@wplj:~/tonzlr$ sudo apt install unicorn [sudo] tonzlr のパスワード: パッケージリストを読み込んでいます... 完了 依存関係ツリーを作成しています 状態情報を読み取っています... 完了 以下のパッケージが自動でインストールされましたが、もう必要とされていません: libpango1.0-0 libpangox-1.0-0 ubuntu-core-launcher これを削除するには 'sudo apt autoremove' を利用してください。 以下の追加パッケージがインストールされます: ruby-kgio ruby-rack ruby-raindrops 以下のパッケージが新たにインストールされます: ruby-kgio ruby-rack ruby-raindrops unicorn アップグレード: 0 個、新規インストール: 4 個、削除: 0 個、保留: 8 個。 269 kB のアーカイブを取得する必要があります。 この操作後に追加で 898 kB のディスク容量が消費されます。 続行しますか? [Y/n] Y 取得:1 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 ruby-kgio amd64 2.10.0-1build1 [27.7 kB] 取得:2 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 ruby-rack all 1.6.4-3 [81.3 kB] 取得:3 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 ruby-raindrops amd64 0.16.0-1build1 [33.0 kB] 取得:4 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 unicorn amd64 4.9.0-2build2 [127 kB] 269 kB を 0秒 で取得しました (1,720 kB/s) 以前に未選択のパッケージ ruby-kgio を選択しています。 (データベースを読み込んでいます ... 現在 216599 個のファイルとディレクトリがインストールされています。) .../ruby-kgio_2.10.0-1build1_amd64.deb を展開する準備をしています ... ruby-kgio (2.10.0-1build1) を展開しています... 以前に未選択のパッケージ ruby-rack を選択しています。 .../ruby-rack_1.6.4-3_all.deb を展開する準備をしています ... ruby-rack (1.6.4-3) を展開しています... 以前に未選択のパッケージ ruby-raindrops を選択しています。 .../ruby-raindrops_0.16.0-1build1_amd64.deb を展開する準備をしています ... ruby-raindrops (0.16.0-1build1) を展開しています... 以前に未選択のパッケージ unicorn を選択しています。 .../unicorn_4.9.0-2build2_amd64.deb を展開する準備をしています ... unicorn (4.9.0-2build2) を展開しています... man-db (2.7.5-1) のトリガを処理しています ... systemd (229-4ubuntu16) のトリガを処理しています ... ureadahead (0.100.0-19) のトリガを処理しています ... ureadahead will be reprofiled on next reboot ruby-kgio (2.10.0-1build1) を設定しています ... ruby-rack (1.6.4-3) を設定しています ... ruby-raindrops (0.16.0-1build1) を設定しています ... unicorn (4.9.0-2build2) を設定しています ... insserv: warning: script 'K01bruschetta' missing LSB tags and overrides insserv: warning: script 'bruschetta' missing LSB tags and overrides insserv: warning: script 'K01bruschetta' missing LSB tags and overrides insserv: warning: current start runlevel(s) (empty) of script `unicorn' overrides LSB defaults (2 3 4 5). insserv: warning: current stop runlevel(s) (0 1 2 3 4 5 6) of script `unicorn' overrides LSB defaults (0 1 6). insserv: warning: script 'bruschetta' missing LSB tags and overrides systemd (229-4ubuntu16) のトリガを処理しています ... ureadahead (0.100.0-19) のトリガを処理しています ...
apostrophe から設定ファイル unicorn.conf をコピー。
tonzlr@wplj:~/tonzlr$ scp tonzlr@apostrophe:tonzlr/unicorn.conf . tonzlr@apostrophe's password: unicorn.conf 100% 166 0.2KB/s 00:00
たぶん、変更しないでいいはず。
tonzlr@wplj:~/tonzlr$ cat unicorn.conf
listen "9009"
worker_processes 1
working_directory "/home/tonzlr/tonzlr"
pid "./unicorn.pid"
stdout_path "./unicorn.log"
stderr_path "./unicorn.log"
preload_app true
tonzlr@wplj:~/tonzlr$ bundle exec unicorn_rails -c unicorn.conf -E production
OK。ブラウザで http://localhost:9009/ にアクセスして確認できた。
Railsアプリの引っ越し(2)とりあえず動いた編
前回のエントリのあとごちょごちょやってたらとりあえず動くようになったので、書いておく。
まず、依存しているらしいパッケージを apt install
でインストール。
- libgmp3-dev
- libsqlite3-dev
専用ユーザの作りなおし。
takatoh@wplj $ sudo adduser tonzlr takatoh@wplj $ sudo gpasswd -a tonzlr sudo
作ったユーザでログインして、GitHub からクローン。
tonzlr@wplj:~$ git clone https://github.com/takatoh/Tonzlr.git tonzlr
bundle install
。
tonzlr@wplj:~$ cd tonzlr tonzlr@wplj:~/tonzlr$ bundle install --path vendor/bundle Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Installing rake 10.4.2 Installing i18n 0.7.0 Installing json 1.8.1 with native extensions Installing minitest 5.5.0 Installing thread_safe 0.3.4 Installing builder 3.2.2 Installing erubis 2.7.0 Installing rack 1.5.2 Installing mime-types 1.25.1 Installing polyglot 0.3.5 Installing arel 5.0.1.20140414130214 Installing coffee-script-source 1.8.0 Installing execjs 2.2.2 Installing thor 0.19.1 Installing hike 1.2.3 Installing multi_json 1.10.1 Installing libv8 3.16.14.3 (x86_64-linux) Using bundler 1.14.6 Installing tilt 1.4.1 Installing ref 1.0.5 Installing sass 3.2.19 Installing spring 1.2.0 Installing sqlite3 1.3.10 with native extensions Installing will_paginate 3.0.7 Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator /usr/bin/ruby2.3 -r ./siteconf20170304-6634-1swo6wh.rb extconf.rb creating Makefile current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" clean current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" compiling generator.c In file included from generator.c:1:0: ../fbuffer/fbuffer.h: In function ‘fbuffer_to_s’: ../fbuffer/fbuffer.h:175:47: error: macro "rb_str_new" requires 2 arguments, but only 1 given VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ ../fbuffer/fbuffer.h:175:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion] VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ Makefile:239: ターゲット 'generator.o' のレシピで失敗しました make: *** [generator.o] エラー 1 make failed, exit code 2 Gem files will remain installed in /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1 for inspection. Results logged to /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/json-1.8.1/gem_make.out An error occurred while installing json (1.8.1), and Bundler cannot continue. Make sure that `gem install json -v '1.8.1'` succeeds before bundling.
やっぱりエラーになったけど、めげずに bundle update
。
tonzlr@wplj:~/tonzlr$ bundle update Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Resolving dependencies... Installing rake 12.0.0 (was 10.4.2) Installing i18n 0.8.1 (was 0.7.0) Installing json 1.8.6 (was 1.8.1) with native extensions Installing minitest 5.10.1 (was 5.5.0) Installing thread_safe 0.3.6 (was 0.3.4) Installing builder 3.2.3 (was 3.2.2) Using erubis 2.7.0 Installing rack 1.5.5 (was 1.5.2) Using mime-types 1.25.1 Using polyglot 0.3.5 Using arel 5.0.1.20140414130214 Using bundler 1.14.6 Installing coffee-script-source 1.12.2 (was 1.8.0) Installing execjs 2.7.0 (was 2.2.2) Installing thor 0.19.4 (was 0.19.1) Using hike 1.2.3 Installing multi_json 1.12.1 (was 1.10.1) Installing libv8 3.16.14.17 (x86_64-linux) (was 3.16.14.3) Using tilt 1.4.1 Installing rdoc 4.3.0 (was 4.2.0) Installing ref 2.0.0 (was 1.0.5) Using sass 3.2.19 Installing spring 1.7.2 (was 1.2.0) Installing sqlite3 1.3.13 (was 1.3.10) with native extensions Installing turbolinks-source 5.0.0 Installing will_paginate 3.1.5 (was 3.0.7) Installing tzinfo 1.2.2 Installing rack-test 0.6.3 (was 0.6.2) Installing treetop 1.4.15 Installing coffee-script 2.4.1 (was 2.3.0) Installing uglifier 3.1.3 (was 2.6.0) Installing sprockets 2.12.4 (was 2.12.3) Installing sdoc 0.4.2 (was 0.4.1) Installing therubyracer 0.12.3 (was 0.12.1) with native extensions Installing turbolinks 5.0.1 (was 2.5.3) Installing activesupport 4.1.4 Installing mail 2.5.4 Installing actionview 4.1.4 Installing activemodel 4.1.4 Installing jbuilder 2.6.3 (was 2.2.6) Installing actionpack 4.1.4 Installing activerecord 4.1.4 Installing actionmailer 4.1.4 Installing railties 4.1.4 Installing sprockets-rails 2.3.3 (was 2.2.2) Installing coffee-rails 4.0.1 Installing jquery-rails 3.1.4 (was 3.1.2) Installing rails 4.1.4 Installing sass-rails 4.0.5 Bundle updated!
今度は成功。
設定ファイルの編集。
tonzlr@wplj:~/tonzlr$ cd config tonzlr@wplj:~/tonzlr/config$ cp site_config.yml.example site_config.yml tonzlr@wplj:~/tonzlr/config$ vi site_config.yml
--- development: storage_dir: ./storage-devel test: storage_dir: ./storage-test production: storage_dir: /media/aysheaia/wallpapers
データベースのマイグレーション。
tonzlr@wplj:~/tonzlr$ rake db:migrate RAILS_ENV=production == 20141229035713 CreateWallpapers: migrating ================================= -- create_table(:wallpapers) -> 0.0021s == 20141229035713 CreateWallpapers: migrated (0.0023s) ======================== == 20141229040752 CreateImages: migrating ===================================== -- create_table(:images) -> 0.0009s == 20141229040752 CreateImages: migrated (0.0010s) ============================ == 20141230093231 AddImagesCountToWallpapers: migrating ======================= -- add_column(:wallpapers, :images_count, :integer, {:default=>0}) -> 0.0038s == 20141230093231 AddImagesCountToWallpapers: migrated (0.0039s) ============== == 20141230233607 CreateTags: migrating ======================================= -- create_table(:tags) -> 0.0010s == 20141230233607 CreateTags: migrated (0.0011s) ============================== == 20141230234524 CreateTaggings: migrating =================================== -- create_table(:taggings) -> 0.0010s -- add_index(:taggings, :tag_id) -> 0.0007s -- add_index(:taggings, [:taggable_id, :taggable_type]) -> 0.0012s == 20141230234524 CreateTaggings: migrated (0.0031s) ========================== == 20141231051856 CreatePools: migrating ====================================== -- create_table(:pools) -> 0.0011s == 20141231051856 CreatePools: migrated (0.0012s) ============================= == 20141231052318 CreatePoolings: migrating =================================== -- create_table(:poolings) -> 0.0009s == 20141231052318 CreatePoolings: migrated (0.0010s) ==========================
apostrophe からデータベースを持ってきて上書き。
tonzlr@wplj:~/tonzlr$ cd db tonzlr@wplj:~/tonzlr/db$ scp tonzlr@apostrophe:tonzlr/db/production.sqlite3 . tonzlr@apostrophe's password: production.sqlite3 100% 6721KB 6.6MB/s 00:00 tonzlr@wplj:~/tonzlr/db$ cd ..
assets のプリコンパイル。
tonzlr@wplj:~/tonzlr$ rake assets:precompile I, [2017-03-04T14:51:43.129624 #7460] INFO -- : Writing /home/tonzlr/tonzlr/public/assets/application-542d4bf5c7f8bc802982fa7761156fb8.js I, [2017-03-04T14:51:43.184646 #7460] INFO -- : Writing /home/tonzlr/tonzlr/public/assets/application-9084123bb8f8533cdd8e6f85fc1b536c.css
Rails 自身で配信するように、config/environments/production.rb を編集。
# Disable Rails's static asset server (Apache or nginx will already do this). config.serve_static_assets = true
そして起動。SECRET_KEY_BASE 環境変数を設定するのを忘れないように。
tonzlr@wplj:~/tonzlr$ export SECRET_KEY_BASE=`rake secret` tonzlr@wplj:~/tonzlr$ bundle exec rails s -e production => Booting WEBrick => Rails 4.1.4 application starting in production on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option) => Ctrl-C to shutdown server [2017-03-04 14:55:12] INFO WEBrick 1.3.1 [2017-03-04 14:55:12] INFO ruby 2.3.1 (2016-04-26) [x86_64-linux-gnu] [2017-03-04 14:55:12] INFO WEBrick::HTTPServer#start: pid=7479 port=3000
これで、ブラウザでアクセスすると、動いているのが確認できた。
Railsアプリの引っ越し
Flask アプリの引っ越しが完了したので、apostrophe (Ubuntu 14.04 LTS) で動いているもうひとつの WEB アプリ(Rails 製)を wplj (Ubuntu 16.04 LTS) に引っ越す。
下準備
このアプリはファイルを貯めこむタイプなので、事前に tar
コマンドと scp
コマンドを使って /media/aysheaia/wallpapers 以下にファイルを配置しておいた。
専用ユーザの作成
takatoh@wplj $ sudo adduser tonzlr [sudo] takatoh のパスワード: ユーザー `tonzlr' を追加しています... 新しいグループ `tonzlr' (1002) を追加しています... 新しいユーザー `tonzlr' (1002) をグループ `tonzlr' に追加しています... ホームディレクトリ `/home/tonzlr' を作成しています... `/etc/skel' からファイルをコピーしています... 新しい UNIX パスワードを入力してください: 新しい UNIX パスワードを再入力してください: passwd: パスワードは正しく更新されました tonzlr のユーザ情報を変更中 新しい値を入力してください。標準設定値を使うならリターンを押してください フルネーム []: 部屋番号 []: 職場電話番号 []: 自宅電話番号 []: その他 []: 以上で正しいですか? [Y/n] Y takatoh@wplj $ sudo gpasswd -a tonzlr sudo ユーザ tonzlr をグループ sudo に追加
Railsアプリの配置
ここからは上で作ったユーザで作業。
GitHub からクローンする。
tonzlr@wplj:~$ git clone https://github.com/takatoh/Tonzlr.git tonzlr
ディレクトリを移動して、依存パッケージをインストール。
tonzlr@wplj:~$ cd tonzlr tonzlr@wplj:~/tonzlr$ bundle install プログラム 'bundle' はまだインストールされていません。 'bundle' を利用するために、コンピュータの管理者に 'ruby-bundler' をインストールすることを相談してください
bundler がインストールされてなかった。インストール。
tonzlr@wplj:~/tonzlr$ sudo gem install bundler [sudo] tonzlr のパスワード: tonzlr は sudoers ファイル内にありません。この事象は記録・報告されます。
あれ?なんでだ?
仕方がないので、takatoh ユーザに戻ってインストール。
takatoh@wplj $ sudo gem install bundler
もう一度 tonzlr ユーザで。
tonzlr@wplj:~/tonzlr$ bundle install --path vendor/bundle Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Installing rake 10.4.2 Installing i18n 0.7.0 Installing json 1.8.1 with native extensions Installing minitest 5.5.0 Installing thread_safe 0.3.4 Installing builder 3.2.2 Installing erubis 2.7.0 Installing rack 1.5.2 Installing mime-types 1.25.1 Installing polyglot 0.3.5 Installing arel 5.0.1.20140414130214 Installing coffee-script-source 1.8.0 Installing execjs 2.2.2 Installing thor 0.19.1 Installing hike 1.2.3 Installing multi_json 1.10.1 Installing libv8 3.16.14.3 (x86_64-linux) Using bundler 1.14.6 Installing tilt 1.4.1 Installing ref 1.0.5 Installing sass 3.2.19 Installing spring 1.2.0 Installing sqlite3 1.3.10 with native extensions Installing will_paginate 3.0.7 Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator /usr/bin/ruby2.3 -r ./siteconf20170304-4815-1cena5t.rb extconf.rb mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h extconf failed, exit code 1 Gem files will remain installed in /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1 for inspection. Results logged to /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/json-1.8.1/gem_make.out An error occurred while installing json (1.8.1), and Bundler cannot continue. Make sure that `gem install json -v '1.8.1'` succeeds before bundling.
む、なんかエラーが。ヘッダファイル ruby.h が見つからないのか?
再び takatho ユーザで。ああもう、いちいちめんどくさいな。
takatoh@wplj $ sudo apt install ruby-dev
tonzlr に戻ってもう一度。今度はどうだ。
tonzlr@wplj:~/tonzlr$ bundle install --path vendor/bundle Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Using rake 10.4.2 Using i18n 0.7.0 Installing json 1.8.1 with native extensions Using minitest 5.5.0 Using thread_safe 0.3.4 Using builder 3.2.2 Using erubis 2.7.0 Using rack 1.5.2 Using mime-types 1.25.1 Using polyglot 0.3.5 Using arel 5.0.1.20140414130214 Using coffee-script-source 1.8.0 Using execjs 2.2.2 Using thor 0.19.1 Using hike 1.2.3 Using multi_json 1.10.1 Using libv8 3.16.14.3 (x86_64-linux) Using bundler 1.14.6 Using tilt 1.4.1 Using ref 1.0.5 Using sass 3.2.19 Using spring 1.2.0 Installing sqlite3 1.3.10 with native extensions Using will_paginate 3.0.7 Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator /usr/bin/ruby2.3 -r ./siteconf20170304-5824-26t94o.rb extconf.rb creating Makefile current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" clean current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" compiling generator.c In file included from generator.c:1:0: ../fbuffer/fbuffer.h: In function ‘fbuffer_to_s’: ../fbuffer/fbuffer.h:175:47: error: macro "rb_str_new" requires 2 arguments, but only 1 given VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ ../fbuffer/fbuffer.h:175:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion] VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ Makefile:239: ターゲット 'generator.o' のレシピで失敗しました make: *** [generator.o] エラー 1 make failed, exit code 2 Gem files will remain installed in /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1 for inspection. Results logged to /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/json-1.8.1/gem_make.out An error occurred while installing json (1.8.1), and Bundler cannot continue. Make sure that `gem install json -v '1.8.1'` succeeds before bundling.
まだエラーが!
ググってみるとこのページに
cf. https://github.com/flori/json/issues/253
libgmp3-dev をインストールすればいい、みたいに書いてある。
takatoh@wplj $ sudo apt install libgmp3-dev
tonzlr@wplj:~/tonzlr$ bundle install --path vendor/bundle Fetching gem metadata from https://rubygems.org/.......... Fetching version metadata from https://rubygems.org/.. Fetching dependency metadata from https://rubygems.org/. Using rake 10.4.2 Using i18n 0.7.0 Installing json 1.8.1 with native extensions Using minitest 5.5.0 Using thread_safe 0.3.4 Using builder 3.2.2 Using erubis 2.7.0 Using rack 1.5.2 Using mime-types 1.25.1 Using polyglot 0.3.5 Using arel 5.0.1.20140414130214 Using coffee-script-source 1.8.0 Using execjs 2.2.2 Using thor 0.19.1 Using hike 1.2.3 Using multi_json 1.10.1 Using libv8 3.16.14.3 (x86_64-linux) Using bundler 1.14.6 Using tilt 1.4.1 Using ref 1.0.5 Using sass 3.2.19 Using spring 1.2.0 Installing sqlite3 1.3.10 with native extensions Using will_paginate 3.0.7 Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator /usr/bin/ruby2.3 -r ./siteconf20170304-7573-1jyzrko.rb extconf.rb creating Makefile current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" clean current directory: /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" compiling generator.c In file included from generator.c:1:0: ../fbuffer/fbuffer.h: In function ‘fbuffer_to_s’: ../fbuffer/fbuffer.h:175:47: error: macro "rb_str_new" requires 2 arguments, but only 1 given VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ ../fbuffer/fbuffer.h:175:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion] VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ Makefile:239: ターゲット 'generator.o' のレシピで失敗しました make: *** [generator.o] エラー 1 make failed, exit code 2 Gem files will remain installed in /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/gems/json-1.8.1 for inspection. Results logged to /home/tonzlr/tonzlr/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0/json-1.8.1/gem_make.out An error occurred while installing json (1.8.1), and Bundler cannot continue. Make sure that `gem install json -v '1.8.1'` succeeds before bundling.
まだダメだ。最後のメッセージのように json をインストールしてみる。
takatoh@wplj $ sudo gem install json -v '1.8.1' Fetching: json-1.8.1.gem (100%) Building native extensions. This could take a while... ERROR: Error installing json: ERROR: Failed to build gem native extension. current directory: /var/lib/gems/2.3.0/gems/json-1.8.1/ext/json/ext/generator /usr/bin/ruby2.3 -r ./siteconf20170304-7998-r1om93.rb extconf.rb creating Makefile current directory: /var/lib/gems/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" clean current directory: /var/lib/gems/2.3.0/gems/json-1.8.1/ext/json/ext/generator make "DESTDIR=" compiling generator.c In file included from generator.c:1:0: ../fbuffer/fbuffer.h: In function ‘fbuffer_to_s’: ../fbuffer/fbuffer.h:175:47: error: macro "rb_str_new" requires 2 arguments, but only 1 given VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ ../fbuffer/fbuffer.h:175:20: warning: initialization makes integer from pointer without a cast [-Wint-conversion] VALUE result = rb_str_new(FBUFFER_PAIR(fb)); ^ Makefile:239: ターゲット 'generator.o' のレシピで失敗しました make: *** [generator.o] エラー 1 make failed, exit code 2 Gem files will remain installed in /var/lib/gems/2.3.0/gems/json-1.8.1 for inspection. Results logged to /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/json-1.8.1/gem_make.out
ダメだな。詰んだか?
……というところで時間切れ。今日はここまで。
Flaskアプリを引っ越す(5)解決編
データの更新ができない問題が解決した。
アプリのせいじゃなくて、パーミッションの問題だった。次のようにして解決。
bruschetta@wplj:~/bruschetta$ chmod 666 bruschetta/bruschetta.db
bruschetta@wplj:~/bruschetta$ chmod 777 bruschetta
最初、データベースファイルのパーミッションだけ変えてみたけどダメで、ディレクトリのパーミッションも変えたら OK になった。
Flaskアプリを引っ越す(4)
前回でやっと終わったと思った Flask アプリの引っ越し、終わってなかった。
動作確認で、表示や検索の確認はできたのでそれで OK と思っていた。ところが、今日データを更新しようとしたらエラーになって更新できないことが判明。どうやらデータベース周りのエラーらしい。
そういえば、データベースを初期化した時にメッセージが出てた。Warning だったのでとりあえずそのままにしたんだけど、あれがダメだったのか?
ともかく新しいデータを登録できない状況で、アプリを改修しなきゃいけなさそう。そのためには、apostrophe で動いているもうひとつの Web アプリ(こっちは Rails 製)の引っ越しをして、apostrophe を 16.04 LTS にアップグレードしなきゃならない。そうでないと開発環境が整わないから。
ああ、道は遠いな。
Flaskアプリを引っ越す(3)
昨日は、Ubuntu 起動時にアプリを自動起動する systemctl enable
コマンドでエラーが出る、というところで終わった。
動いている
エラーが出ているのでてっきり自動起動に失敗しているものとばかり思っていたけど、ちゃんと enabled になっていた。
bruschetta@wplj:~$ systemctl list-unit-files | grep bruschetta bruschetta.service enabled
bruschetta@wplj:~$ systemctl status bruschetta ● bruschetta.service - Bruschetta service Loaded: loaded (/lib/systemd/system/bruschetta.service; enabled; vendor prese Active: active (running) since 火 2017-02-28 22:42:15 JST; 6h ago Process: 1105 ExecStart=/etc/init.d/bruschetta start (code=exited, status=0/SU Main PID: 1138 (uwsgi) CGroup: /system.slice/bruschetta.service ├─1138 uwsgi bruschetta.ini ├─1569 uwsgi bruschetta.ini └─1570 uwsgi bruschetta.ini
試しにリブートしてみても、ちゃんと自動起動していることを確認した。
うーん、ますます訳がわかんなくなってきた。が、まあいい、この件は後でゆっくり調べることにして、先に進めよう。
データベースファイルをコピー
apostrophe からデータベースファイルをコピーする。Sqlite3 なので、単にファイルを上書きコピーすればいいだけだ。
bruschetta@wplj:~/bruschetta/bruschetta$ scp bruschetta@apostrophe:bruschetta/bruschetta/bruschetta.db . bruschetta@apostrophe's password: bruschetta.db 100% 876KB 876.0KB/s 00:00
そんで、アプリを再起動。
bruschetta@wplj:~/bruschetta/bruschetta$ sudo systemctl stop bruschetta [sudo] bruschetta のパスワード: bruschetta@wplj:~/bruschetta/bruschetta$ sudo systemctl start bruschetta
service
コマンドじゃなくて systemctl
コマンドを使ってみた。ブラウザで確認してみると、無事データが移行されていた。
Nginxのヴァーチャルホスト
ヴァーチャルホスト bruschetta でアプリにアクセスできるようにする。これも設定ファイルを apostrophe からコピーしてくる。
bruschetta@wplj:~/bruschetta/bruschetta$ cd /etc/nginx/sites-available bruschetta@wplj:/etc/nginx/sites-available$ sudo scp bruschetta@apostrophe:/etc/nginx/sites-available/bruschetta . bruschetta@apostrophe's password: bruschetta 100% 905 0.9KB/s 00:00
コピーした設定ファイルがこれ。
upstream uwsgi-bruschetta { server 127.0.0.1:9090; } server { # port listen 80; # server name server_name bruschetta; # document root root /home/bruschetta/bruschetta; # index #index index.php index.html index.htm; # log files access_log /var/log/nginx/bruschetta/access.log combined; error_log /var/log/nginx/bruschetta/error.log warn; keepalive_timeout 60; proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60; #client_max_body_size 20M; location / { #root /home/bruschetta/bruschetta; 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; proxy_pass http://uwsgi-bruschetta; } }
sites-enabled/bruschetta にリンクを張る。
bruschetta@wplj:/etc/nginx/sites-available$ sudo ln -s /etc/nginx/sites-available/bruschetta /etc/nginx/sites-enabled/bruschetta bruschetta@wplj:/etc/nginx/sites-available$ cd ../sites-enabled bruschetta@wplj:/etc/nginx/sites-enabled$ ls bruschetta default
ログファイル用のディレクトリを作る。
bruschetta@wplj:/etc/nginx/sites-enabled$ sudo mkdir /var/log/nginx/bruschetta
/etc/hosts ファイルを編集して1行追加。
192.168.1.6 bruschetta
そして Nginx を再起動。
bruschetta@wplj:/etc/nginx/sites-enabled$ sudo systemctl restart nginx
OK。あとは他のマシンの hosts ファイルも編集して bruschetta にアクセスできるようにするだけだ。はー、長かった。
Flaskアプリを引っ越す(2)
前のエントリは、/lib/systemd/system/bruschetta.service ファイルの書き方がわからない、というところで終わった。
Unit設定ファイル
その後、なんとなくではあるけど書けたので、とりあえずは動くようになった。参考になったページはここ。
cf. 第4回 Unit設定ファイルの記述方法 – ITPro
Unit設定ファイルというらしい。ともかく、このページを参考にして書いたのがこれ。
[Unit] Description=Bruschetta service After=network.target [Service] ExecStart=/etc/init.d/bruschetta start ExecStop=/etc/init.d/bruschetta stop Restart=always Type=forking PIDFile=/var/run/bruschetta.pid
このファイルから、/etc/systemd/system/bruschetta.service にリンクを張る。
bruschetta@wplj:~$ sudo ln -s /lib/systemd/system/bruschetta.service /etc/systemd/system/bruschetta.service
起動確認
で、起動。
bruschetta@wplj:~$ sudo service bruschetta start
やった!動いた!start
と stop
を繰り返してみると、ちゃんと起動、停止する。今度は大丈夫そうだ。
ところが
Ubuntu 自体を再起動すると、サービスが動いていない。またもググッて調べた結果、systemctl enable
コマンドを使えばいいらしい。
cf. Systemdを使ってさくっと自作コマンドをサービス化してみる – Qiita
bruschetta@wplj:~$ sudo systemctl enable bruschetta [sudo] bruschetta のパスワード: Synchronizing state of bruschetta.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable bruschetta insserv: warning: script 'bruschetta' missing LSB tags and overrides update-rc.d: error: bruschetta Default-Start contains no runlevels, aborting.
あれぇ?
更にググるとこのページを見つけた。
cf. Systemd入門(4) – serviceタイプUnitの設定ファイル – めもめも
/lib/systemd/system/bruschetta.service ファイルに [Install] セクションを付け足してみる。
[Unit] Description=Bruschetta service After=network.target [Service] ExecStart=/etc/init.d/bruschetta start ExecStop=/etc/init.d/bruschetta stop Restart=always Type=forking PIDFile=/var/run/bruschetta.pid [Install] WantedBy=multi-user.target
今度はどうだ。
bruschetta@wplj:~$ sudo systemctl enable bruschetta Synchronizing state of bruschetta.service with SysV init with /lib/systemd/systemd-sysv-install... Executing /lib/systemd/systemd-sysv-install enable bruschetta insserv: warning: script 'K01bruschetta' missing LSB tags and overrides insserv: warning: script 'bruschetta' missing LSB tags and overrides update-rc.d: error: bruschetta Default-Start contains no runlevels, aborting.
ああダメだ。……というところで今日は時間切れ。
Flaskアプリを引っ越す
apostrophe で動いている Flask アプリを wplj に引っ越す。
virtualenvとuwsgiのインストール
takatoh@wplj $ pip install virtualenv プログラム 'pip' はまだインストールされていません。 次のように入力することでインストールできます: sudo apt install python-pip
あれ、pip
がインストールされてなかった。
takatoh@wplj $ sudo apt install python-pip
では、あらためて。
takatoh@wplj $ sudo pip install virtualenv takatoh@wplj $ sudo pip install uwsgi
以前のエントリを見たら sudo
をつけていたので、今回もつけて実行した。
takatoh@wplj $ pip list adium-theme-ubuntu (0.3.4) dnspython (1.12.0) pip (8.1.1) pycrypto (2.6.1) setuptools (20.7.0) unity-lens-photos (1.0) uWSGI (2.0.14) virtualenv (15.1.0) wheel (0.29.0) You are using pip version 8.1.1, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
virtualenv と uWSGI がちゃんとインストールされてる。pip をアップグレードしろみたいなメッセージが出てるけど、これはあとでいいや。
ユーザを作る
Flask アプリを動かす専用のユーザを作る。
takatoh@wplj $ sudo adduser bruschetta ユーザー `bruschetta' を追加しています... 新しいグループ `bruschetta' (1001) を追加しています... 新しいユーザー `bruschetta' (1001) をグループ `bruschetta' に追加しています... ホームディレクトリ `/home/bruschetta' を作成しています... `/etc/skel' からファイルをコピーしています... 新しい UNIX パスワードを入力してください: 新しい UNIX パスワードを再入力してください: passwd: パスワードは正しく更新されました bruschetta のユーザ情報を変更中 新しい値を入力してください。標準設定値を使うならリターンを押してください フルネーム []: 部屋番号 []: 職場電話番号 []: 自宅電話番号 []: その他 []: 以上で正しいですか? [Y/n] y
sudo
する権利をつける。
takatoh@wplj $ sudo gpasswd -a bruschetta sudo ユーザ bruschetta をグループ sudo に追加
ここで今作ったユーザでログインしなおす。
Flaskアプリの配置
GitHub からクローン。
bruschetta@wplj:~$ git clone https://github.com/takatoh/Bruschetta.git bruschetta Cloning into 'bruschetta'... remote: Counting objects: 358, done. remote: Total 358 (delta 0), reused 0 (delta 0), pack-reused 358 Receiving objects: 100% (358/358), 31.96 KiB | 0 bytes/s, done. Resolving deltas: 100% (241/241), done. Checking connectivity... done.
virtualenv を作って必要なライブラリをインストール。
bruschetta@wplj:~/bruschetta$ virtualenv env New python executable in /home/bruschetta/bruschetta/env/bin/python Installing setuptools, pip, wheel...done. bruschetta@wplj:~/bruschetta$ source env/bin/activate (env) bruschetta@wplj:~/bruschetta$ pip install -r requirements.txt Collecting Flask (from -r requirements.txt (line 1)) Downloading Flask-0.12-py2.py3-none-any.whl (82kB) 100% |████████████████████████████████| 92kB 3.0MB/s Collecting Flask-SQLAlchemy (from -r requirements.txt (line 2)) Downloading Flask_SQLAlchemy-2.2-py2.py3-none-any.whl Collecting Flask-Script (from -r requirements.txt (line 3)) Downloading Flask-Script-2.0.5.tar.gz (42kB) 100% |████████████████████████████████| 51kB 4.0MB/s Collecting itsdangerous>=0.21 (from Flask->-r requirements.txt (line 1)) Downloading itsdangerous-0.24.tar.gz (46kB) 100% |████████████████████████████████| 51kB 4.6MB/s Collecting click>=2.0 (from Flask->-r requirements.txt (line 1)) Downloading click-6.7-py2.py3-none-any.whl (71kB) 100% |████████████████████████████████| 71kB 4.4MB/s Collecting Werkzeug>=0.7 (from Flask->-r requirements.txt (line 1)) Downloading Werkzeug-0.11.15-py2.py3-none-any.whl (307kB) 100% |████████████████████████████████| 317kB 3.0MB/s Collecting Jinja2>=2.4 (from Flask->-r requirements.txt (line 1)) Downloading Jinja2-2.9.5-py2.py3-none-any.whl (340kB) 100% |████████████████████████████████| 348kB 4.0MB/s Collecting SQLAlchemy>=0.8.0 (from Flask-SQLAlchemy->-r requirements.txt (line 2)) Downloading SQLAlchemy-1.1.5.tar.gz (5.1MB) 100% |████████████████████████████████| 5.1MB 237kB/s Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask->-r requirements.txt (line 1)) Downloading MarkupSafe-0.23.tar.gz Building wheels for collected packages: Flask-Script, itsdangerous, SQLAlchemy, MarkupSafe Running setup.py bdist_wheel for Flask-Script ... done Stored in directory: /home/bruschetta/.cache/pip/wheels/e2/ea/d8/8d114e46cef819f7d9879504a7f9cb2a88a479af2858223d9f Running setup.py bdist_wheel for itsdangerous ... done Stored in directory: /home/bruschetta/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a Running setup.py bdist_wheel for SQLAlchemy ... done Stored in directory: /home/bruschetta/.cache/pip/wheels/8d/0a/3b/2109101a4052e58ad64f83661383a6b483a1c383eb09aad6d6 Running setup.py bdist_wheel for MarkupSafe ... done Stored in directory: /home/bruschetta/.cache/pip/wheels/a3/fa/dc/0198eed9ad95489b8a4f45d14dd5d2aee3f8984e46862c5748 Successfully built Flask-Script itsdangerous SQLAlchemy MarkupSafe Installing collected packages: itsdangerous, click, Werkzeug, MarkupSafe, Jinja2, Flask, SQLAlchemy, Flask-SQLAlchemy, Flask-Script Successfully installed Flask-0.12 Flask-SQLAlchemy-2.2 Flask-Script-2.0.5 Jinja2-2.9.5 MarkupSafe-0.23 SQLAlchemy-1.1.5 Werkzeug-0.11.15 click-6.7 itsdangerous-0.24
データベースの初期化。
(env) bruschetta@wplj:~/bruschetta$ python manage.py init_db manage.py:2: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead. from flask.ext.script import Manager /home/bruschetta/bruschetta/bruschetta/__init__.py:2: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead. from flask.ext.sqlalchemy import SQLAlchemy
なんか SQLAlchemy がらみでメッセージが出てるな。まあいい、先に進めよう。
起動確認。
(env) bruschetta@wplj:~/bruschetta$ python manage.py runserver manage.py:2: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead. from flask.ext.script import Manager /home/bruschetta/bruschetta/bruschetta/__init__.py:2: ExtDeprecationWarning: Importing flask.ext.sqlalchemy is deprecated, use flask_sqlalchemy instead. from flask.ext.sqlalchemy import SQLAlchemy * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [28/Feb/2017 20:14:23] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [28/Feb/2017 20:14:23] "GET /static/style.css HTTP/1.1" 200 - 127.0.0.1 - - [28/Feb/2017 20:14:23] "GET /favicon.ico HTTP/1.1" 404 -
OK。ブラウザでも確認できた。
ここで virtualenv から抜ける。
(env) bruschetta@wplj:~/bruschetta$ deactivate
uWSGIで起動
uWSGI の設定ファイルを apostrophe からコピーしてくる。
bruschetta@wplj:~/bruschetta$ scp bruschetta@apostrophe:bruschetta/bruschetta.ini . bruschetta@apostrophe's password: bruschetta.ini 100% 350 0.3KB/s 00:00
内容はこんな感じ。
[uwsgi] uid = www-data gid = www-data http = :9090 venv = /home/bruschetta/bruschetta/env python-path = /home/bruschetta/bruschetta wsgi-file = /home/bruschetta/bruschetta/manage.py callable = app master=true pidfile=/var/run/bruschetta.pid daemonize=/home/bruschetta/bruschetta/bruschetta.log
ディレクトリ構成は一緒だから、このまま行けるはずだ。
bruschetta@wplj:~/bruschetta$ sudo uwsgi bruschetta.ini sudo bruschetta のパスワード: uWSGI getting INI configuration from bruschetta.ini
OK。今度もブラウザで確認できた。
起動スクリプト
これも以前のスクリプトと同じ。
#!/bin/sh PATH=/usr/local/bin:/usr/bin:/bin:/sbin APP_ROOT=/home/bruschetta/bruschetta case "$1" in start) cd ${APP_ROOT} uwsgi bruschetta.ini ;; stop) PID_FILE=/var/run/bruschetta.pid PID=`cat ${PID_FILE}` kill -INT ${PID} rm ${PID_FILE} ;; *) echo "Usage: bruschetta {start|stop}" >&2 exit 1 ;; esac exit 0
起動確認。
bruschetta@wplj:~/bruschetta$ sudo service bruschetta start Failed to start bruschetta.service: Unit bruschetta.service not found.
あれ、ダメだ。bruschetta.service ってなんだ?
ググってみると、/lib/systemd/system/bruschetta.service ファイルを作ってやればいいようだけど、書き方がよくわからない。どうしたもんか……