みそしりんぐ

現在進行形みそしる

Ubuntu16.04(Vagrant) x Python3.5 x Django1.9

Vagrant上のUbuntu 16.04にPython 3.5 + Django 1.9の環境を作ります.

目次

  1. Ubuntu
  2. Python
    • インストール
    • virtualenv
  3. Django
    • インストール
    • アプリを作る
  4. 参考

Ubuntu

Vagrant上でUbuntu 16.04を用います.
boxはbento/ubuntu-16.04を使用.

# Vagrantfile

Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/ubuntu-16.04" # ←変更

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  config.vm.network "forwarded_port", guest: 8000, host: 8080 # ←変更(Djangoのrunserverができるように)
......
$ cat /etc/issue
# Ubuntu 16.04 LTS \n \l

Python

インストール

$ sudo apt-get install python3
$ sudo apt-get install python3-pip

virtualenv

インストール
$ sudo pip3 install virtualenv virtualenvwrapper
# ~/.bashrc

### Virtualenvwrapper
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/virtualenvwrapper.sh
fi
環境設定
$ mkvirtualenv mien
# myenvという環境を作る

$ mkvirtualenv myenv --python=/usr/bin/python2.6
# Pythonのバージョンを指定したい場合

$ workon myenv
# myenvという環境ににうつる
$(myenv) python --version
Python 3.5.1+
# こうなったら成功

$(myenv) deactivate
# 環境を抜ける

$ workon
# 作成した環境の一覧

$ rmvirtualenv myenv
# 作成した環境を削除する

Django

インストール

$(myenv) pip install django umemcache 
$(myenv) python -c "import django; print(django.get_version())"
# 1.9.7

アプリを作る

$(myenv) django-admin startproject project-name
$(myenv) cd project-name
$(myenv) python manage.py runserver 0.0.0.0:8000

ブラウザからlocalhost:8080にアクセスすると,下の画像のようなページにアクセスできるはず!
f:id:mssr_nm:20160607114559p:plain


LAMP環境(CentOS 7, Apache, MariaDB, PHP 7)を構築する

参考サイトの内容を自分のやりたいことにあわせて詰め込んだまとめ.
やってることは参考元に詳しいです.

目次

  1. Summary
    • ポイント
    • OS
    • 結果
  2. インストール
  3. phpMyAdmin
    • 準備
    • インストール
    • アクセス制限
  4. DB
    • ログイン
    • phpMyAdmin
    • アプリケーション
  5. 蛇足
  6. 参考

Summary

ポイント

  • Cent OSの公式リポジトリではPHP 5.4が提供されている
    • remiリポジトリから最新バージョンをインストールしましょう
  • DB接続はPDO
  • phpMyAdminも導入
  • DBのセッティング,Composerの導入など,実用で使う直前まで

OS

Vagrant上でCent OSを動かしました.
boxはbento/centos-7.2を使用.

# Vagrantfile

Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/centos-7.2" # ←変更
......
$ cat /etc/redhat-release
# CentOS Linux release 7.2.1511 (Core) 

結果

$ httpd -v
# Server version: Apache/2.4.6 (CentOS)
# Server built:   Nov 19 2015 21:43:13

$ mysql --version
# mysql  Ver 15.1 Distrib 5.5.44-MariaDB, for Linux (x86_64) using readline 5.1

$ php -v
# PHP 7.0.4 (cli) (built: Mar  2 2016 18:03:26) ( NTS )
# Copyright (c) 1997-2016 The PHP Group
# Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

インストール

とりあえずすべてインストールし,起動や設定を軽く行います.

準備

$ sudo yum upgrade

Apache

$ sudo yum install httpd
$ sudo systemctl start httpd
ファイアーウォール

httpdを許可しましょう.

$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --reload

……と参考元にあったのですが,私は

# FirewallD is not running

と言われてしまいました.boxでもともと設定してあるのかな.

設定

.htaccessを許可

# /etc/httpd/conf/httpd.conf

DocumentRoot "/var/www/html"
......
<Directory "/var/www/html">
    AllowOverride FileInfo Options
    #  AllowOverride All でもOK
......

ドキュメントルート変更

# /etc/httpd/conf/httpd.conf

# DocumentRoot "/var/www/html"
DocumentRoot "/var/www/test/public_html"
......
<Directory "/var/www/test/public_html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
......

設定が終わったら再起動しましょう.

$ sudo systemctl restart httpd

MySQLMariaDB

$ sudo yum install mariadb mariadb-server mariadb-devel
設定

文字コード

# /etc/my.cnf

[mysqld]
character-set-server=utf8

[client]
default-character-set=utf8

起動 & 初期設定

$ sudo systemctl start mariadb
$ mysql_secure_installation

コマンドを打った後は,指示通りにすすめればOKです.
わたしはすべてyesで設定しました.

PHP

MariaDB + PDO使用なので,php-mysqlndを追加.また,一緒にComposerもインストール.

$ sudo rpm --import https://raw.githubusercontent.com/remicollet/remirepo/master/remi-release/RPM-GPG-KEY-remi
$ sudo yum install yum-utils http://remi.kazukioishi.net/enterprise/remi-release-7.rpm
$ sudo yum-config-manager --enable remi-php70
$ sudo yum install php php-mbstring php-intl php-gmp php-mysqlnd composer
設定
# /etc/php.ini

[PHP]
error_reporting = E_ALL | E_STRICT
display_errors on

[Date]
date.timezone="Asia/Tokyo"

[mbstring]
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = auto
mbstring.http_output = UTF-8
mbstring.encoding_translation = On
mbstring.detect_order = auto

phpMyAdmin

準備

$ sudo yum install yum-plugin-priorities
# /etc/yum.repos.d/remi.repo

[remi]
priority=100

[remi-test]
priority=100

インストール

$ sudo yum-config-manager --enable remi remi-test
$ sudo yum install phpMyAdmin

アクセス制限

ローカル以外のブラウザからもアクセス可能にする.

# /etc/httpd/conf.d/phpMyAdmin.conf

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require all granted # ← 変更
   </IfModule>
......

DB

今後つくるであろうアプリケーションやphpMyAdminのため,データベースを作ります.
また,ユーザの追加や権限の変更なども行っていきます.

ログイン

MariaDB [(none)]>から始まるコマンドは,以下のようにMariaDBにログインして実行してください.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> ......

phpMyAdmin

アカウント名 pma
パスワード pma_pass

で設定していきます.お好みで変更してください.

phpMyAdmin用データベース・ユーザの作成
-- /usr/share/phpMyAdmin/sql/create_tables.sql

-- --------------------------------------------------------

--
-- Privileges
--
-- (activate this statement if necessary)
GRANT SELECT, INSERT, DELETE, UPDATE, ALTER ON `phpmyadmin`.* TO
  'pma'@localhost;
-- ↑コメントアウトを外す --

-- --------------------------------------------------------
MariaDB [(none)]> source /usr/share/phpMyAdmin/sql/create_tables.sql
MariaDB [(none)]> set password for pma@localhost = password('pma_pass');
MariaDB [(none)]> flush privileges;
設定
<!-- /etc/phpMyAdmin/config.inc.php -->

<?php
......
/**
 * phpMyAdmin configuration storage settings.
 */

/* User used to manipulate with storage */
// $cfg['Servers'][$i]['controlhost'] = '';
// $cfg['Servers'][$i]['controlport'] = '';
$cfg['Servers'][$i]['controluser'] = 'pma';
$cfg['Servers'][$i]['controlpass'] = 'pma_pass';

/* Storage database and tables */
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';
$cfg['Servers'][$i]['favorite'] = 'pma__favorite';
$cfg['Servers'][$i]['users'] = 'pma__users';
$cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
$cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
$cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
$cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings';
$cfg['Servers'][$i]['export_templates'] = 'pma__export_templates';

アプリケーション

データベース名 test
アカウント名 test_user
パスワード test_pass

で設定していきます.お好みで変更してください.

データベース
-- データベース一覧
MariaDB [(none)]> show databases;

-- 作成
MariaDB [(none)]> create database test;
ユーザ
-- ユーザ一覧
MariaDB [(none)]> select user,host,user from mysql.user;

-- 作成
MariaDB [(none)]> create user 'test_user'@'localhost' identified by 'test_pass';

-- 権限変更
MariaDB [(none)]> grant select,index on test.* to 'test_user'@'localhost';

-- phpMyAdminのアカウントにも権限を与えておく
MariaDB [(none)]> grant all on test.* to 'pma'@'localhost';

権限は以下のページなどを参考にどうぞ.
www.dbonline.jp


蛇足

文字化けするのでロケールを変えてみたものの,治らず諦めました.
LANG=C付きで実行すればとりあえずはいけます!
以下ロケール変更方法.

$ sudo localectl set-locale LANG=en_US.utf8

zero-config.com


Gnuplot導入

学校の課題がGnuplot指定だったので,HomebrewでGnuplot入れました.
以下Sublime Text 3で実行できるようにするまで.

目次

  1. Gnuplot
  2. Sublime Text
  3. 参考

Gnuplot

AquaTerm

sourceforge.net

PDFlib
$ brew install pdflib-lite
本体
$ brew install gnuplot --pdf --qt --with-x --without-emacs

Sublime Text

パッケージ

Syntax highlightingとビルドのためにパッケージ(Gnuplot)を導入

環境変数追加

Preferences.sublime-settings ( Preferences -> Settings - User )

"build_env":
{
	"PATH": "/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
}

プログラミングに使える英単語

各単語はweblioへリンクしています.ただ適当にURL自動生成させたのでミスがあるかもしれません.
分類,意味におかしなところがある,綴りが違う,この単語追加して!などなにかあればぜひお知らせ下さい.

なんとなく分類してるのでなんともいえませんが,だいたい

  • 変数 : 名詞
  • 関数 : 動詞
  • 状態 : 形容詞
  • 接頭辞 : 前置詞+α

という感じになっているはずです

目次

  1. 変数
    • コンピュータ
    • 数学
    • 汎用
  2. 関数
    • グループ
      • 得る
      • 送る
      • 管理する
      • 探す
      • 出力する
      • 作る
      • 割り当てる
      • 始める
      • 終わる
  3. 状態
  4. 接頭辞
  5. 参考

変数

コンピュータ

数学

浮動小数点表示
  • 1.6e−19 のように表わしたとき
演算

汎用

  • bucket : バケツ・容器
  • buffer(buf) : 緩衝物(速度差を調整するための一時的な記憶域)
  • temporary(tmp) : 一時的な
  • return value(retval) : 戻り値

その他

関数

  • allow : 許可する
  • partial : 部分的な
  • count : 数える
  • encrypt : 暗号化する
  • exit : 抜け出る,出て行く,出口
  • force : 強制的に動作させる
  • intercept : 傍受する,妨害する,遮る
  • rotate : 回転する
  • lookup データのテーブルから特定のデータをさがし出す
  • parse : 構文解析する
  • run : 実行する
  • save : 保存する
  • separate : 分ける(結びついていたものを区切って別々にする)
  • snap : かちっと(〜の状態に)する
  • throttle : 減速する,抑える
  • wrap : 包む,くるむ

グループ

得る
  • get : 取る,得る
  • download : データを取り込む
  • fetch : 〜を(行って)取って来る
送る
  • send : 送る(行かせる)
  • announce : 告知する,声明を送る
  • deliver : (手紙などを)配達する,明け渡す
  • dispatch : 処理時間を割り当てる(配布,派遣)
  • distribute : 配る,分配する
  • post : 電子メールを出す,レコードへの入力・更新
  • route : 経路を決定する,(〜の経路で)発送する
  • submit : 提出する(他のものに制御や処理を任せる)
管理する
探す
出力する
作る
  • make : 作る(曖昧)
  • create : (ファイル,ディレクトリなどを新たに)作る
  • generate : 生成する(コンパイルと似ているが,特定のタイプのプログラムしか作ることが出来ない)
  • add : 加える,付け加える
  • build : 構築する,ビルドする(コンパイルもリンクも済んだ実行ファイルを作る)
  • compose : 組み立てる,構成する
  • new : 新しい,新規
  • set up : 準備をする,設定する
割り当てる
始める
終わる

状態

接頭辞

  • can : できる
  • has : 持っている
  • is : 確認する
  • to : 変換する
  • inout(中 / 外)
  • updown(上がる / 下がる)

アップデート祭り

たくさん久しぶりなことがあります.
まず休日です.久しぶりの休日です.学校がありません.実に1ヶ月以上ぶりな気がします.
久しぶりの休日で,久しぶりに風邪を引きました.久しぶりの休日ですから仕方ありません.
風邪も引いたので,パソコンで遊んでいました.久しぶりのアップデート祭りです.エラー祭りになりました.
これが今回の主題です.アップデート祭りがエラー祭りになった話.

ところで久しぶりすぎてはてな記法をわすれました.

目次

  1. Ruby
  2. Homebrew
    • Update & Upgrade
    • Doctor
      • 問題発生
      • 解決法

Ruby

RubyGems

$ sudo update_rubygems

Gem

問題発生
$ sudo gem update
ERROR:  Loading command: update (LoadError)
	dlopen(/Users/mssr/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin13.0.2/openssl.bundle, 9): Symbol not found: _SSLv2_client_method
  Referenced from: /Users/mssr/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin13.0.2/openssl.bundle
  Expected in: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
 in /Users/mssr/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin13.0.2/openssl.bundle - /Users/mssr/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/x86_64-darwin13.0.2/openssl.bundle
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass
解決法

ぐぐった結果,Rubyのバージョンを上げると治りそうだということが判明しました.

$ rbenv install 2.1.2
$ rbenv global 2.1.2
$ gem update

Homebrew

Update & Upgrade

$ brew update
$ brew upgrade

Doctor

問題発生
$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!

Warning: Some directories in /usr/local/share/locale aren't writable.
This can happen if you "sudo make install" software that isn't managed
by Homebrew. If a brew tries to add locale information to one of these
directories, then the install will fail during the link step.
You should probably `chown` them:

    /usr/local/share/locale/da
    /usr/local/share/locale/da/LC_MESSAGES
    /usr/local/share/locale/fi
    /usr/local/share/locale/fi/LC_MESSAGES
    /usr/local/share/locale/ga
    /usr/local/share/locale/ga/LC_MESSAGES
    /usr/local/share/locale/id
    /usr/local/share/locale/id/LC_MESSAGES
    /usr/local/share/locale/nl
    /usr/local/share/locale/nl/LC_MESSAGES
    /usr/local/share/locale/pt_BR
    /usr/local/share/locale/pt_BR/LC_MESSAGES
    /usr/local/share/locale/ro
    /usr/local/share/locale/ro/LC_MESSAGES
    /usr/local/share/locale/rw
    /usr/local/share/locale/rw/LC_MESSAGES
    /usr/local/share/locale/sv
    /usr/local/share/locale/sv/LC_MESSAGES
    /usr/local/share/locale/tr
    /usr/local/share/locale/tr/LC_MESSAGES

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
    /usr/local/include/ansidecl.h
    /usr/local/include/bfd.h
    /usr/local/include/bfdlink.h
    /usr/local/include/dis-asm.h
    /usr/local/include/gdb/jit-reader.h
    /usr/local/include/symcat.h

Warning: Unbrewed .la files were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .la files:
    /usr/local/lib/libbfd.la
    /usr/local/lib/libopcodes.la

Warning: Unbrewed static libraries were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected static libraries:
    /usr/local/lib/libbfd.a
    /usr/local/lib/libopcodes.a

Warning: Some installed formula are missing dependencies.
You should `brew install` the missing dependencies:

    brew install cairo glib gobject-introspection harfbuzz icu4c libffi libtool pango pixman

Run `brew missing` for more details.
解決法

アクセス権修復

$ sudo chown -R $USER:admin /usr/local/share

依存ツールファイルをインストール

$ brew install cairo glib gobject-introspection harfbuzz icu4c libffi libtool pango pixman

あとのはファイルを消せって書いてある気もしますが,よくわからないのでとりあえず放置で.

Vagrant × Ruby

いろいろできずに困っていたものが,理由もわからずできるようになったのでまとめ.
Vagrantなんかは相当前に入れたのでどうやって自分が導入したのかいまいち記憶にありませんがそこはいろいろで補いつつ.

Vagrant

Vagrant is a tool for building complete development environments. With an easy-to-use workflow and focus on automation, Vagrant lowers development environment setup time, increases development/production parity, and makes the "works on my machine" excuse a relic of the past.
Vagrant

目次

  1. Vagrant
    • 導入
    • Vagrantのコマンドたち
  2. Ruby + Ruby on Rails
  3. 参考

Vagrant

導入

とりあえず上の2つをインストール.
boxはvagrantbox.esから.私が入れたのは,CentOS 6.5 x86_64.
list出力してきちんと出てくれば完了です.
(私はコピペした際にうっかりcentos65-x86_64-20140116のままにしちゃいましたが短くしといたほうがあとで便利な気がします)

$ vagrant box add centos65-x86_64-20140116 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
$ vagrant box list

作業ディレクトリを作成し,そこに移動後以下を行います.

$ vagrant init centos65-x86_64-20140116
$ vagrant up
$ vagrant ssh

こんな感じで起動,SSH接続でシェルにアクセスできました.

Vagrantのコマンドたち
$ vagrant -h

$ vagrant status // ステータス表示,以下は起動しているとき
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

$ vagrant ssh // マシンにSSH接続
$ vagrant suspend // 保留
$ vagrant resume // 再開
$ vagrant up // 起動
$ vagrant halt // シャットダウン
$ vagrant reload // 再起動
$ vagrant destroy // 停止・削除

Ruby + Ruby on Rails

準備
$ sudo yum -y install git
$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

Git,ればにらを入れます.
rbenvをればにらなんて呼ぶのは私だけだと思いますが見えてしまったものはしょうがない.

パス通し..bash_profileをみれば書き込まれたのがわかると思います.

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l

ruby-build

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Ruby
$ yum install gcc make openssl-devel
$ rbenv install 2.0.0p481
$ rbenv global 2.0.0p481

$ ruby -v
ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux]
Ruby on Rails
$ gem update --system
$ gem install rails --no-ri --no-rdoc -V
$ rbenv rehash

$ rails -v
Rails 4.1.1