Shell Script 문법 정리
Title: Bash Shell Script 문법 정리 Author: DongDongE Tags: Programming Release: 2021.02.08 [Shell
이번에는 Linux Ubuntu에 Python Flask를 설치해보도록 하겠습니다.
본글은 flask-docs와 tutorials를 참조하여 공부후 작성하였습니다.
띠용!!
본글은 Ubuntu 18.04 Desktop 기반으로 합니다.
Description: Ubuntu 18.04 LTS
Release: 18.04
Codename : Bionic Beaver
- Apache 설치
- Apche Module 설치
- Flask 라이브러리 설치
3-2. Jinja2 설명- Apache 환경 설정
위 순서로 진행하겠습니다.
Flask WSGI (Apache mod_wsgi)와 연동하여 파이썬 기반 웹 애플리케이션을 호스팅하기 위한 인터페이스를 제공합니다. Flask 자체적으로 웹서버를 구동할 수 있으나 Apache의 강력한 기능을 사용하기 위해 웹서버를 Apache로 설정하였습니다.
Apache 패키지를 설치해주십시옹.
dongdonge@dongdonge$ sudo apt-get update && sudo apt-get install apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3
libaprutil1-ldap liblua5.2-0
Suggested packages:
apache2-doc apache2-suexec-pristine | apache2-suexec-custom
The following NEW packages will be installed:
apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1
libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.2-0
0 upgraded, 9 newly installed, 0 to remove and 536 not upgraded.
Need to get 1,713 kB of archives.
After this operation, 6,920 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
WSGI - Web Server Gateway Interface
Python Web Application 표준으로 채택되었으며, Web Server와 Web Application 프로그램 사이 범용 인터페이스 규격입니다.
Graham Dumpleton의 Apache Module로, Apache에서 Python 기반 웹 애플리케이션을 호스팅 하기 위한 WSGI 인터페이스를 제공합니다.
Apache Module (wsgi)와 python-dev를 패키지로 설치해주십시옹.
!!! Python이 3.X일 경우 "apt-get install libapache2-mod-wsgi-py3 python3-dev"로 하시면 됩니다.
dongdonge@dongdonge$ sudo apt-get install libapache2-mod-wsgi python-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libc-dev-bin libc6-dev libexpat1 libexpat1-dev libpython-dev libpython-stdlib
libpython2.7 libpython2.7-dev libpython2.7-minimal libpython2.7-stdlib linux-libc-dev
manpages-dev python python-minimal python2.7 python2.7-dev python2.7-minimal
Suggested packages:
glibc-doc python-doc python-tk python2.7-doc binfmt-support
The following NEW packages will be installed:
libapache2-mod-wsgi libc-dev-bin libc6-dev libexpat1-dev libpython-dev libpython-stdlib
libpython2.7-dev linux-libc-dev manpages-dev python python-dev python-minimal python2.7
python2.7-dev python2.7-minimal
The following packages will be upgraded:
libexpat1 libpython2.7 libpython2.7-minimal libpython2.7-stdlib
4 upgraded, 15 newly installed, 0 to remove and 532 not upgraded.
Need to get 39.7 MB/39.7 MB of archives.
After this operation, 77.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Flask는 Python Web Framework로 Python Library이며, Werkzeug WSGI와 Jinja2 템플릿 엔진을 기반으로 작동합니다.
해당 라이브러리(Flask)를 사용하기 위해 pip를 패키지로 설치하도록 하겠습니다.
dongdonge@dongdonge$ sudo apt-get install python-pip
Python3는 "apt-get install python3-pip"로 설치하시면 됩니다.
pip를 설치 또는 기존에 존재한다면 아래 에시와 같이 pip2로 flask를 설치하시면 됩니다.
또한 Flask는 often referred micro framework이며, 요약하자면 애플리케이션의 핵심을 단순하면서도 확장이 가능합니다. 예를 들어 동적 웹페이지를 구동하기 위해 특정 데이터 (Python Code)와 템플릿 (HTML+CSS)를 결합할 수 있습니다.
dongdonge@dongdonge$ sudo pip2 install flask
The directory '/home/dongdonge/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/dongdonge/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting flask
Downloading https://files.pythonhosted.org/packages/9a/74/670ae9737d14114753b8c8fdf2e8bd212a05d3b361ab15b44937dfd40985/Flask-1.0.3-py2.py3-none-any.whl (92kB)
100% |████████████████████████████████| 92kB 162kB/s
Collecting Werkzeug>=0.14 (from flask)
Downloading https://files.pythonhosted.org/packages/9f/57/92a497e38161ce40606c27a86759c6b92dd34fcdb33f64171ec559257c02/Werkzeug-0.15.4-py2.py3-none-any.whl (327kB)
100% |████████████████████████████████| 327kB 460kB/s
Collecting click>=5.1 (from flask)
Downloading https://files.pythonhosted.org/packages/fa/37/45185cb5abbc30d7257104c434fe0b07e5a195a6847506c074527aa599ec/Click-7.0-py2.py3-none-any.whl (81kB)
100% |████████████████████████████████| 81kB 7.8MB/s
Collecting Jinja2>=2.10 (from flask)
Downloading https://files.pythonhosted.org/packages/1d/e7/fd8b501e7a6dfe492a433deb7b9d833d39ca74916fa8bc63dd1a4947a671/Jinja2-2.10.1-py2.py3-none-any.whl (124kB)
100% |████████████████████████████████| 133kB 7.9MB/s
Collecting itsdangerous>=0.24 (from flask)
Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask)****
Python 3.x일 경우 "pip3 install flask"로 설치하시면 됩니다.
Apache(Ubuntu 기준) Web Root Directory Default가 "/var/www/html" 입니다.
Flask를 사용하기 위해 해당 폴더로 Default로 설정해도 되나. 별도로 생성하도록 하겠습니다.
저는 아래와 같이 "/var/www/"에 "dongdonge" 이름으로 디렉토리를 생성 했습니다.
dongdonge@dongdonge$ sudo mkdir -p /var/www/dongdonge
dongdonge@dongdonge$ ls /var/www/
dongdonge html
이번에는 Apache와 Flask가 연동할 수 있도록 환경 설정해보겠습니다.
dongdonge@dongdonge$ sudo vim /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Flask Start
# URL "/" 접근시 먼저 run.wsgi 설정 참조
WSGIScriptAlias / /var/www/dongdonge/run.wsgi
# Flask 프로세스 사용자권한 설정 및 user group, thread 설정한다.
WSGIDaemonProcess dongdonge user=www-data group=www-data threads=5
WSGIApplicationGroup %{GLOBAL}
<Directory /var/www/dongdonge>
Order deny,allow
Allow from all
</Directory>
# Flask End
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
WSGIScriptAlias, WSGIDaemonProcess, WSGIApplicationGroup, "Directory /var/www/dongdonge"
등.. Flask 환경을 구축하기 위해 위와 같이 작성하였습니다.
import sys
sys.path.insert(0, "/var/www/dongdonge")
from app import app as application
"run.wsgi" 파일은 "/var/www/dongdonge/" 경로에 생성하였습니다.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "Good!"
"run.py" 파일도 "/var/www/dongdonge" 경로에 생성하며, URL "/"로 접근시 "hello_world" 함수가 실행이 되어 "Good!" 문자열을 반환하게 됩니다!
보다 구체적인 코드 설명은 다음 편에 설명하도록 하겠습니다.