べすとえふぉーと

プログラミング等のノート 

公開鍵認証の設定

Ubuntuサーバー側

sudo apt-get install openssh-server

ローカル(Mac)で鍵を作成して転送  名称入力時にvps2を入力

ssh-keygen 
scp -P $PORT vps2.pub $USER@$IP:/

Ubuntuサーバー側で鍵設定

chmod 700 .ssh/
cat vps2.pub >> .ssh/authorized_keys
chmod 600 .ssh/authorized_key
vi /etc/ssh/sshd_config 

以下のように変更

PasswordAuthentication no
RSAAuthentication yes 
PubkeyAuthentication yes 


Ubuntu側で再起動

service ssh restart

ローカル(Mac)からログイン

openssl rsa -in vps2 -outform pem > vps2.pem
chmod 600 vps2.pem 
ssh -p $PORT -i vps2.pem $USER@$IP

Ubuntu 14.04でvnc環境構築

インストール

# sudo apt-get install xorg gnome-core gnome-system-tools gnome-app-install
# sudo apt-get install tightvncserver

パスワードの設定

# vncpasswd

xstartupの設定 vi ~/.vnc/xstartup

#!/bin/sh
unset DBUS_SESSION_BUS_ADDRESS
export GTK_IM_MODULE=ibus
export XMODIFIERS="@im=ibus"
export QT_IM_MODULE=ibus
/usr/bin/ibus-daemon -dxr
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
exec gnome-session &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus -n &
gnome-terminal &

起動と確認

# vncserver :1
# netstat -an | grep 5901

クライアント側から接続

vnc://x.x.x.x:5901

終了する場合

# vncserver -kill :1

日本語設定

# sudo apt-get install language-pack-ja-base language-pack-ja ibus-mozc
# sudo update-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"
# sudo dpkg-reconfigure locales
# sudo apt-get install unifont
# reboot

bashrcには以下を設定

export LC_MESSAGES=ja_JP.UTF-8
export LC_IDENTIFICATION=ja_JP.UTF-8
export LC_COLLATE=ja_JP.UTF-8
export LANG=ja_JP.UTF-8
export LC_MEASUREMENT=ja_JP.UTF-8
export LC_CTYPE=ja_JP.UTF-8
export LC_TIME=ja_JP.UTF-8
export LC_NAME=ja_JP.UTF-8

Let's encryptをUbuntu14.04に設定

環境:
Apache 2.4.7
Ubuntu 14.04

git clone https://github.com/letsencrypt/letsencrypt
cd letsencrypt
./letsencrypt-auto certonly --webroot -w /var/www/html -d $domain

メールを入力する画面が表示されるので入力してからApacheに設定

./certbot-auto --apache -d $domain

Apache再起動

/etc/init.d/apache2 restart

bottleのアプリをherokuで公開する

ディレクトリ作成

# mkdir bottletestapp
# cd bottletestapp

app.pyの中身

import os
from bottle import route,run

@route("/")
def hello_world():
    return "hello"

run(host="0.0.0.0",port=int(os.environ.get("PORT",5000)))

requirements.txt の中身

bottle==0.4.3

Procfile作成

#echo web: python app.py > Procfile

git

#git init
#git add .
#git commit -m "initial commit"

アプリを作成してpush

#heroku apps:create bottletestapp
#git push heroku master

アクセス
http://bottletestapp.herokuapp.com/

アプリを削除

#heroku apps:destroy --app bottletestapp --confirm bottletestapp

Go 使い方メモ ゴルーチン チャネル

package main

import "fmt"

func main() {
    //make channel
    c := make(chan int)
    //send function

    go func(send chan<- int) {
        for i := 0; i < 8; i++ {
            send <- i
        }
        close(send)
    }(c)

    //receive roop
    for {
        value,ok := <-c
        if !ok {
            break
        }
        //receive
        fmt.Println(value)
    }

}
package main

import (
    "fmt"
)

func dofunc(intval int,s chan<- int) {
    for i := 0; i < 100; i++ {
        if i+1 == intval && intval % 2 == 0 {
            s <- intval
            close(s)
        }
    }
}

func main() {
    c := make(chan int)
    go dofunc(30,c)
    go dofunc(3,c)
    go dofunc(4,c)
    for {
        value,ok := <- c
        // return false when channel closed
        if value > 0 {
            fmt.Println("ret:", value)
            break
        }
        if !ok {
            break
        }
    }
}

Go 使い方メモ ゴルーチン

元がゴルーチンのtest()実行後3秒で終わるので、test()は2まで表示して終了する

package main

import(
    "fmt"
    "time"
)

func main() {
    fmt.Println("Start")

    fmt.Println("normal test")
    test()

    fmt.Println("go routine test")
    go test()

    time.Sleep(3 * time.Second)

    fmt.Println("End")

}

func test() {
    for i := 0; i < 5; i++ {
        fmt.Println(i)
        time.Sleep(1 * time.Second)
    }
}