flask + apache2

아파치2, wsgi 설치

sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi
sudo apt-get install libapache2-mod-wsgi-py3


apache2 site 정보 작성

/etc/apache2/sites-available/flask_wsgi.conf

<VirtualHost *>
ServerName {앱이름}
WSGIDaemonProcess {앱이름} user={사용자이름} threads=5
WSGIScriptAlias / {앱경로}/app.wsgi
<Directory {앱경로}>
WSGIProcessGroup {앱이름}
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName example.jhin.kr
WSGIDaemonProcess hello_world user=philomoral threads=5 #실행할 앱 이름 helloworld, user는 philomoral
WSGIScriptAlias / /home/philomoral/web_test/flask_test/app.wsgi
<Directory /home/philomoral/web_test/flask_test>
WSGIProcessGroup hello_world #앱 이름 helloworld
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>

사이트 비활성화/활성화

sudo a2dissite 000-default
sudo a2ensite flask_wsgi

ssl

SSL 페이지 정리 참고하여 작성한 후 사이트 설정을 다음과 같이 바꾼다.

일반 사이트 설정과 비슷하나 주요 차이점으로는 다음과 같다.

  • Virtualhost의 포트를 443으로 설정하는 점
  • SSL관련 설정 내용이 추가되는 점
  • IfModule 영역으로 감싸는 점
  • ServerName, WSGIDaemonProcess 이름을 일반 사이트 설정과 중복되지 않게 설정해야하는 점
<IfModule mod_ssl.c>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
ServerName tero.example.com:443
WSGIDaemonProcess hello_world_ssl user=philomoral threads=5
WSGIScriptAlias / /home/philomoral/web_test/flask_test/app.wsgi
<Directory /home/philomoral/web_test/flask_test>
WSGIProcessGroup hello_world_ssl
WSGIApplicationGroup %{GLOBAL}
WSGIScriptReloading On
Require all granted
</Directory>
</VirtualHost>
</IfModule>

flask 앱 작성

앱생성경로: /home/philomoral/web_test/flask_test

Virtualenv 경로: /home/philomoral/web_test/venv

/home/philomoral/web_test/app.wsgi

#가상환경 활성화
activate_this = '/home/philomoral/web_test/venv/bin/activate_this.py'
with open(activate_this) as file_:
exec(file_.read(), dict(file=activate_this))
#가상환경 활성화 완료
import sys
sys.path.insert(0,"/home/philomoral/web_test/flask_test") #앱 경로
from hello_world import app as application #hello_world.py 앱 실행

가상환경에서는 psycopg2 라이브러리가 잘 작동되지 않는 문제가 있음. 로컬환경에서는 적용 가능

hello_world.py

from flask import Flask
app = Flask(__name__)
@app.route('/')def home():    return 'Hello World!'
if __name__ == '__main__':    app.run(host="0.0.0.0",port=5050)


업로드파일 파일이름 한글 저장 (인코딩)

/etc/apache2/envvars 파일의 해당부분을 다음과 같이 변경한다.

	#export LANG=C
	export LANG=ko_KR.utf8

Anaconda

아나콘다 3 설치

홈페이지에서 받아서 설치

.Do you wish the installer to initialize Anaconda3 in your /home/#####/.bashrc ? [yes|no] [no] >>> yes

yes로 대답한다.

bunzip2 명령어를 못 찾는 경우,

sudo apt-get install bzip2 

명령어 활성. ~/.bashrc  최하단에 다음 내용 추가

source anaconda3/etc/profile.d/conda.sh


환경생성

conda create -n [환경이름]

환경 활성/비활성화

conda activate [환경이름]
conda deactivate [환경이름]

환경 목록

conda info --envs

환경 제거

conda remove -n [환경이름] --all

환경 복사본

conda create --name [새로운환경이름] --clone [복사대상환경이름]

USB장치의 포트넘버 고정

별첨2. 포트넘버 고정하기 


 

usb 포트 꽂는 위치에 따라서 포트 넘버를 지정할 수 있다. 


 

별첨2.1. 특정 usb 장치가 꽂혀있는 위치 검색하기. 


 

udevadm info -a -n /dev/ttyUSBx | grep devpath 


 

위의 명령어로 특정 usb 디바이스가 꽂혀있는 위치를 알아낸다. 여기서 /dev/ttyUSBx는 특정 usb 장치의 포트이다. 해당 명령어를 실행하면 다음과 같은 결과가 나올 수 있다. 

ATTRS{devpath}==”1.3.2″ 


 

별첨2.2. usb 포트 규칙 생성하기 

/etc/udev/rules.d 하위에 규칙 파일을 만들어서 usb 포트의 심볼릭링크를 만들 수 있다. 예를들어 FILENAME.rules 파일을 만들고, 해당 파일의 내용을 다음과 같이 지정할 수 있다. 


 

ATTRS{devpath}==”1.3.2″, SYMLINK=”SFS_test” 


 

여기서 ATTRS{devpath}는 별첨 2.1 에서 검색된 내용을 입력하면 된다. 규칙파일을 생성하였으면 재부팅한다 


 

별첨2.3. 테스트하기 


 

이렇게하면 /dev/SFS_test 심볼릭 링크가 생성되며, /dev/ttyUSBx에 연결되어 있다. 연결된 포트 정보는 다음 명령어로 확인할 수 있다. 

ls -l /dev/SFS_test 

 아래 명령으로 룰 적용을 새로고침 할 수 있다.

sudo udevadm control --reload-rules && sudo udevadm trigger

아이피 고정하기

/etc/network/interfaces  

파일의 내용을 수정한다.

 

Include files~ 부분은 주석처리 한다 

 

auto eth0 #eth0 –> connection name(ifconfig 시) 

iface eth0 inet static 

address  192.168.0.193 

netmask 255.255.255.0 

broadcast 192.168.0.255 

gateway 192.168.0.1 

dns-nameservers 192.168.0.1 8.8.8.8 

 

 

서비스 재시작 

sudo /etc/init.d/networking restart 

sudo service networking restart

 

DNS 서버 설정이 안된다면 resolvconf 설치하자 

sudo apt-get install resolvconf