uWSGIを使っていこう

uWSGI(Web Server Gateway Interface)とはPythonのアプリとWebサーバのプロトコルのこと。
Django, Flask, BottleはこのWSGIの規約にそっている

uWSGIはアプリケーションサーバの一種
通信は
– UNIXドメインソケット(高速)
– HTTP

$ pip3 install uWSGI

一緒にflaskもインストールします。
$ pip3 install flask

run.py

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/')
def api_sample():
	result = {"code": "001", "name": "apple"}
	return jsonify(ResultSet=result)

if __name__ == '__main__':
	app.run(host="0.0.0.0")

ん、どこにuWSGIが使われているんだ??

続いてuWSGIコマンドを実行する
$ uwsgi –http=0.0.0.0:8080 –wsgi-file=run.py –callable=app

なんだこれ? サーバ機能やな

### 設定ファイルから実行する
uwsgi.ini

[uwsgi]
wsgi-file=run.py
callable=app

http=0.0.0.0:8080

$ uwsgi uwsgi.ini

### iniファイルの設定項目

daemonize = /var/log/uwsgi.log
log-reopen = true
log-maxsize = 8000000
logfile-chown = on
logfile-chmod = 644

current_release = /opt/apps/current/sample_api
chdir = %(current_release)
wsgi-file = %(current_release)/run.py

processes = 4
threads = 2
thunder-lock = true
max-requests = 3000
max-requests-delta = 300
master = True

なるほど、簡易webサーバを作ってるみたいやな

[aws ec2]ubuntu20.04にningxを入れて起動させたい

1. SSHログイン
ssh ubuntu@* -i ~/.ssh/*.pem

2. apt update
$ sudo apt update
$ sudo apt upgrade

3. mysql
$ sudo apt install mysql-client-core-8.0
$ sudo apt-get update
$ sudo apt install mysql-server
$ mysqld –version
$ sudo service mysql start
$ sudo mysql_secure_installation
$ sudo mysql -u root -p
mysql> create database hanbai;

4. Nginx install
$ sudo apt install nginx

5. 仮想環境作成
$ sudo apt install python3-venv python3-pip python3-dev
$ sudo chown ubuntu /home
$ git clone https://github.com/*/*.git
$ cd hanbai
$ ls
README.md db.sqlite3 hanbai manage.py sales
$ python3 -m venv vdjango

$ . vdjango/bin/activate
(vdjango)$ pip3 install Django==3.0.4
(vdjango)$ pip3 install uwsgi

$ sudo ufw allow 80

6. Nginx設定
$ cd /etc/nginx/conf.d
$ sudo vi project.conf

server{
    listen 80;
    server_name ${publicIp};

    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

7. iniファイル
$ ls
README.md db.sqlite3 hanbai manage.py sales vdjango
$ vim django.ini

[uwsgi]
module          =  project.wsgi:application
master          =  true
pidfile         =  django.uwsgi.pid
enable-threads  = true
http            =  127.0.0.1:8000
processes       =  5
harakiri        =  50
max-requests    =  5000
vacuum          =  true
home            =  vdjango
daemonize       =  django.uwsgi.log

8. library install & migration
// 省略

9. Nginxとuwsgi起動
$ sudo service nginx start
$ sudo apt-get install -y uwsgi
$ sudo apt install uwsgi-plugin-python3

うーん、
$ sudo python3 manage.py runserver 0.0.0.0:8000 で8000ポート開けても動くんだけど、なんか違うんだよな。。

[aws ec2]Apache2を入れるが、Internal Server Error

### apache2 インストール
$ sudo apt update
$ sudo apt install apache2
$ sudo ufw app list
$ sudo ufw allow ‘Apache Full’
$ sudo ufw status
$ sudo systemctl status apache2
$ hostname -I

### mod_wsgi
$ apt-get install apache2-dev
$ pip3 install mod_wsgi

### settings.py

ALLOWED_HOSTS = ['*'] 

$ mod_wsgi-express module-config
LoadModule wsgi_module “/home/ubuntu/.local/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so”
WSGIPythonHome “/usr”

### migrate
$ python3 manage.py makemigrations sales
$ python3 manage.py migrate

### apache設定
sudo vi /etc/apache2/sites-available/000-default.conf

LoadModule wsgi_module "/usr/lib/apache2/modules/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so"
WSGIScriptAlias / /var/www/hanbai/hanbai/wsgi.py
WSGIPythonHome "/usr"
WSGIPythonPath "/var/www/hanbai"

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        # DocumentRoot /var/www/hanbai

        # WSGIScriptAlias / /var/www/hanbai/hanbai/wsgi.py
        # WSGIPythonPath /var/www/hanbai/        
        <Directory /var/www/hanbai/hanbai/>
            <Files wsgi.py>
                Order deny,allow
                AllowOverride None
                require all granted
            </Files>
        </Directory>
</VirtualHost>

$ sudo /etc/init.d/apache2 restart

$ /var/log/apache2/error.log
[client 59.126.236.35:46554] from django.core.wsgi import get_wsgi_application
[Wed Oct 28 23:46:17.546723 2020] [wsgi:error] [pid 10444:tid 139970643724032] [client 59.126.236.35:46554] ModuleNotFoundError: No module named ‘django’

$ pip3 freeze | grep wsgi
mod-wsgi==4.7.1

WSGIPythonPath /var/www/hanbai:/home/ubuntu/.local/lib/python3.8/site-packages

何故だ。。Djangoをインストールした場所が悪かった?
もう一回やるか。。

[aws ec2]ubuntuにsshする際にPermission denied (publickey).

$ ssh -i ~/.ssh/*.pem ec2-user@${public ip} -vvv
ec2-user@${public ip}: Permission denied (publickey).

セキュリティグループのinbound rulesでsshを許可しているのに何故だろうと調べていたところ、sshでログインする際のubuntuのuser nameはec2-userではなく、ubuntuらしい。

[公式]Linux インスタンス用ユーザーガイド

$ ssh ubuntu@${public ip} -i ~/.ssh/*.pem
Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-1024-aws x86_64)

なるほど、わかりませんね。

[Django3.0] MySQL8にrootでmigrationしようとしてAccess deniedとなる時

bionicからfocal(Ubuntu20.04)で作り直している手順で、mysql8にmigrationしようとすると、Access denied for user ‘root’@’localhost'”となった。

### focalにMySQL 8.20 インストール
$ sudo apt install -y mysql-server
$ mysqld –version
$ sudo service mysql start
$ sudo mysql_secure_installation
$ sudo mysql -u root -p

### Django
$ pip3 install Django==3.0.4

### library インストール
// 省略

### git clone
$ git clone https://github.com/*/${appName}.git
$ cd ${appName}
$ git branch dev
$ git checkout dev

### init.py追記

import pymysql
pymysql.install_as_MySQLdb()

$ pip3 install PyMySQL

### migration
$ python3 manage.py makemigrations sales

->django.db.utils.OperationalError: (1698, “Access denied for user ‘root’@’localhost'”)

なんですと???
MySQL8はrootユーザで外部からアクセスできないらしい。
という事で、Django用のユーザを作ってあげる必要がある。

$ sudo mysql –u root -p
mysql> SHOW VARIABLES LIKE ‘validate_password%’;
mysql> set global validate_password.length=6;
mysql> set global validate_password.policy=LOW;
mysql> CREATE USER ‘django’@’%’ IDENTIFIED BY ‘hogehoge’;
mysql> GRANT ALL PRIVILEGES ON . TO ‘django’@’%’ WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

settings.pyを修正して再度migration
$ python3 manage.py makemigrations sales
$ python3 manage.py migrate
$ python3 manage.py createsuperuser
$ python3 manage.py runserver 192.168.33.10:8000
動作確認

ヒョーーーーー、とりあえず20.04、centos8を普段使いの環境にしたから一安心^^

[Ubuntu20.04] Focal Fossaを始めよう

awsでもUbuntu20.04がbionicよりも上に来てるので、これからはbionicではなく、focalを使っていきたいと思う

$ mkdir focal
$ cd focal
$ vagrant init ubuntu/focal64

vagrantfile35行目のポートフォワーディングコメントアウト削除

config.vm.network "private_network", ip: "192.168.33.10"

if Vagrant.has_plugin?("vagrant-vbguest")
   config.vbguest.auto_update = false  
end

$ vagrant up
$ vagrant ssh
$ python3 -V
Python 3.8.5

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install python3-pip
$ pip3 -V
$ git version

うーん、なんか良さそう

[Django3.0] tesseractで画像の文字を解析するページを実装したい

1) コーディングする前に、まず画面設計から作ります。
– イメージとしてはポップアップ

2) 続いて、Webpack&SASS環境でドラッグ&ドロップのFrontEndを書いていく
– 画像ファイルの制御は file.type.match(‘image.*’)とする。
– 例えばjpegなら、file typeは’image/jpeg’となる。

var imageType = 'image.*';

				if(! file.type.match(imageType)){
					alert('画像を選択してください');
					$('#input_file').val('');
					$('#drop_area').css('border', '1px dashed #aaa');
					return;
				}


良い感じ。これをDjangoに実装する

3) Django
### template
– aタグのリンクをwindow.openで設定する

<a href="javascript:window.open('/read', null, 'top=0,left=0,width=500px,height=650');">画像から商品名を読み取る</a>

– radioのvalueはlangの値を入れます。

<div class="row col-md-12 mt-10">
	  			<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="jpn" id="lang" checked>
				  <label class="form-check-label" for="lang">日本語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="eng" id="lang">
				  <label class="form-check-label" for="lang">英語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="chi_sim" id="lang">
				  <label class="form-check-label" for="lang">簡体字中国語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="chi_tra" id="lang">
				  <label class="form-check-label" for="lang">繁体字中国語</label>
				</div>
				<div class="form-check col-md-2">
				  <input class="form-check-input" type="radio" name="lang" value="spa" id="lang">
				  <label class="form-check-label" for="lang">スペイン語</label>
				</div>
			</div>

views.py
– Postされた画像は request.FILES[‘*’]で受け取る。
– 条件分岐で、request.FILES[‘read_img’]とすると、MultiValueDictKeyErrorになるので、request.FILES.get(‘read_img’, False)とする。

from PIL import Image
import sys
import pyocr
def img_read(request):
	if(request.method == 'POST' and request.FILES.get('read_img', False)):
		tools = pyocr.get_available_tools()
		img = request.FILES['read_img']
		langs = request.POST['lang']
		img = Image.open(img)
		txt = tools[0].image_to_string(img,lang=langs,builder=pyocr.builders.TextBuilder(tesseract_layout=6))
		params = {
			'txt': txt,
		}
		return render(request, 'sales/img_read.html', params)
	else:
		return render(request, 'sales/img_read.html')

出来たーーーーーーーーーーーーーー^^
きゃっ♩ きゃっ🎵 きゃっ🎶
早速git pushしよー

[tesseract4.1.1] Ubuntuにtesseract(Eng、日本語、簡体字中国語、繁体字中国語、スペイン語)を入れよう

1. PPA(パーソナル・パッケージ・アーカイブ)を追加
$ sudo add-apt-repository ppa:alex-p/tesseract-ocr
$ sudo apt-get update

2. Tesseractインストール
$ sudo apt install tesseract-ocr
$ sudo apt install libtesseract-dev

$ tesseract -v
tesseract 4.1.1-rc2-25-g9707
$ tesseract –list-langs
List of available languages (2):
eng
osd

3. 日本語、簡体字中国語、繁体字中国語、スペイン語をインストール
$ sudo apt install tesseract-ocr-jpn tesseract-ocr-chi-sim tesseract-ocr-chi-tra tesseract-ocr-spa
$ tesseract –list-langs
List of available languages (6):
chi_sim
chi_tra
eng
jpn
osd
spa

4. pyocr install
$ pip install pyocr

5. 中国語の画像を読み取る

$ tesseract chinabank.jpeg output -l chi_sim
中国平安
PINGAN

保险’ 银行. 投资

6. pythonで書く

from PIL import Image
import sys
import pyocr

tools = pyocr.get_available_tools()
langs = "chi_sim"

img = Image.open('images.jpeg')
txt = tools[0].image_to_string(
		img,
		lang=langs,
		builder=pyocr.builders.TextBuilder(tesseract_layout=6)
	)
print(txt)

$ python app.py
中国平安
PINGAN
保险; 银行.投资

さて、これをDjangoで実装したい。
思ったより考えるの難しいな。

[RaspberryPi 4] cronでカメラモジュールの画像をS3にアップロード

1. shellでカメラモジュールを実行する。
app.sh

#!/bin/bash
raspistill -w 800 -h 600 -o /home/pi/image.jpg

$ chmod +x app.sh
// テスト
$ sudo sh ./app.sh

2. raspi4のcronを有効にする
$ sudo vim /etc/rsyslog.conf
-> コメントアウト

cron.*                          /var/log/cron.log

$ sudo /etc/init.d/rsyslog restart
$ sudo /etc/init.d/cron status

3. カメラモジュールを起動するシェルを実行するcronを書く
テストの為、1分おきの実行にする。
$ crontab -e

*/1 * * * * sudo sh /home/pi/app.sh

$ /etc/init.d/cron restart
$ sudo journalctl -f -u cron
// 写真がとれてることを確認

4. raspi4にboto3をインストール
$ python3 -V
$ pip3 -V
$ pip3 install boto3

5. S3にアップロードするpythonスクリプトを書く
// 省略

6. S3にアップロードするcronを追加してテスト
$ which python3

*/1 * * * *  /usr/bin/python3 /home/pi/upload.py

$ /etc/init.d/cron restart
$ sudo journalctl -f -u cron

7. S3のバケットでraspi4で撮った写真がアップロードされているか確認

OK。数日かかると思ってたけど意外と早くできた。
ドキュメント修正して、次はDjangoにtesseract4->cv2の実装やな

ubuntu bionicでpython3のcronを設定する

まず実行したいpyファイルを用意します。
/home/vagrant/local/cron/app.py

$ crontab -l
no crontab for vagrant
$ crontab -e

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny
  4. /bin/ed

Choose 1-4 [1]: 2

$ which python
/usr/bin/python

$ crontab -e

*/5 * * * * /usr/bin/python /home/vagrant/local/cron/app.py

// cronリスタート
$ /etc/init.d/cron restart
// 実行ログの確認
$ sudo journalctl -f -u cron

実行テストは、5分おき(*/5 * * * *)ではなく、1分おきの方が良いですね。
続いて、raspberry pi4でcronを設定します。