π§ Ubuntu * wsgiλ₯Ό μ΄μ©ν Apache httpd μ°λνκΈ° (Flask)
μνμΉλ₯Ό μν ν¨ν€μ§λ μ€μΉνκ³ μνμΉλ μ€μΉνλ€λ©΄,
μ΄μ λ wsgiλ₯Ό μ΄μ©ν΄μ μνμΉ httpd μΉ μλ²κ° flask appμ μλΉμ€ν μ μλλ‘ μ€μΉ ν μ°κ²°μμΌλ³΄μ!
WSGIλ 무μμΌκΉ?
μΉ μλ² κ²μ΄νΈμ¨μ΄ μΈν°νμ΄μ€(WSGI, Web Server Gateway Interface)λ μΉμλ²μ μΉ μ΄ν리μΌμ΄μ μ μΈν°νμ΄μ€λ₯Ό μν νμ΄μ¬ νλ μμν¬λΌκ³ μ μλμ΄μλ€.
μ¦, μΉ μλ²(Apache)μ Flask μ ν리μΌμ΄μ μ μ°λν΄μ£ΌκΈ° μν νλ μμν¬μ΄λ€.
μ€μΉν΄λ³΄μ
μΌλ¨ WSGI νμ΄μ§μμ λ€μ΄λ‘λ λΆλΆμ ν΄λ¦νμ¬ μ€μΉν λ²μ μ μ ννκ³ λ€μ΄λ°μ.
ν΄λΉ ν¬μ€νΈμμλ λͺ λ Ήμ΄λ₯Ό μ΄μ©νμ¬ λ€μ΄ν κ²μ΄λ©° / μλ src ν΄λλ₯Ό μμ±νμ¬ κ·Έκ³³μμ μμ μ μ§νν κ² μ΄λ€.
$ cd /src
$ wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.6.5.tar.gz
$ tar xvfz 4.6.5.tar.gz
$ cd mod_wsgi-4.6.5
$ ./configure --with-apxs=/usr/local/apache2.4/bin/apxs --with-python=/usr/bin/python3.6
--with-apxsμλ λ΄κ° μ€μΉν μνμΉμ apxs κ²½λ‘λ₯Ό μ μ΄μ£Όλ©΄ λλ€. μ°λλ /usr/local μλμ μ€μΉνκΈ°λλ¬Έμ κ²½λ‘κ° μμ κ°μ κ²μ΄λ€.
--with-pythonλΆλΆμ λ΄κ° κΈ°λ³ΈμΌλ‘ μ§μ λμ΄μλ pythonμ μ¬μ©νμ§ μμ κ²μ΄λΌλ©΄ μ€μ ν΄μ£Όμ΄μΌ νλ€.
λ³΄ν΅ λ¦¬λ μ€μλ 2.7μ΄ κΈ°λ³ΈμΌλ‘ μ€μΉλμ΄μκ³ κΈ°λ³Έ κ°μΌλ‘ κΉλ €μκΈ° λλ¬Έμ μ§μ ν΄μ£Όμλ€.
$ make
$ make install
μ΄λ κ² λͺ¨λ κ³Όμ μ΄ λλκ³ λλ©΄ μνμΉ μλ modules λλ ν 리 μμ mod_wsgi.so νμΌμ΄ μκΈ΄λ€.
κ·Έ κ²½λ‘κ° μ΄λλ°μ? λΌκ³ λ¬ΌμΌμ λ€λ©΄ μλ λΉ¨κ° λ°μ€ λΆλΆμ λλ€!!!
μμ μ ! νμΌ κ΅¬μ± μμ보기
μ΄μ μ€μΉκ° λλ¬μΌλ μ€μ ν μ°λμ ν΄ λ³Ό κ²μ΄λ€.
μλ μ€λͺ μμ μ΄λ‘μμΌλ‘ μ μ κ²μ μ¬μ©μ μμλ‘ λ³κ²½λ μ μλ μ΄λ¦λ€μ΄λ©°, μ νν λͺ μΉμ΄ μκΈ°μ μ μ΄λμ κ²μ΄ μ΄ν΄κ° μ λμ§ μμ μλ μλ€κ³ μκ°νλ€.
ν΄μ, λ΄κ° μ΄λ»κ² μ€μ νλμ§λ κΈ°μ¬ν΄λμμΌλ©° λμμ κ·Έ νμΌλ€μ μμΉκ° μ΄λμ μλμ§ μ΄λ―Έμ§λ‘ 보μ¬μ£Όκ³ μ νλ€.
Apache httpd.conf μ€μ νκΈ°
μνμΉμ mod_wsgi λͺ¨λμ μμ±νκ³ λ λ€ μ΄λ₯Ό μ μ©νκΈ° μν΄μλ μνμΉ κΈ°λ³Έ μ€μ νμΌ( httpd.conf )μ μμ ν΄μΌ νλ€.
μμ λ°©λ²μ λν μμΈν λ΄μ©μ 곡μλ¬Έμ λΆλΆμ νμΈνμ.
$ vi /usr/local/apache2.4/conf/httpd.conf
# λͺ¨λ μ¬μ©μ μ μ
LoadModule wsgi_module modules/mod_wsgi.so
<IFModule wsgi_module>
# μΉμλΉμ€ "/" κ²½λ‘μ wsgi μΈν°νμ΄μ€ κ²½λ‘ μ§μ
WSGIScriptAlias / wsgi_νμ₯μ_νμΌ_κ²½λ‘
# Python μ ν리μΌμ΄μ
νλ‘μΈμ€ μ€ν νκ²½ μ€μ
WSGIDaemonProcess μ ν리μΌμ΄μ
_μ΄λ¦ user=κ³μ λͺ
group=κ·Έλ£Ήλͺ
threads=μ°λ λκ°―μ_intν python-path=python_site-packages_path:APP_λλ ν 리_κ²½λ‘
WSGIProcessGroup μ ν리μΌμ΄μ
_μ΄λ¦
WSGIScriptAlias / μ ν리μΌμ΄μ
_νμ΄μ¬_νμΌ_κ²½λ‘
</IFModule>
<Directory APP_λλ ν 리_κ²½λ‘>
# ν΄λΉ μ ν리μΌμ΄μ
μ μ²λ¦¬νλ νλ‘μΈμ€μμ 첫λ²μ§Έλ‘ μμ±λ νμ΄μ¬ μΈν°ν리ν°λ₯Ό μ¬μ©
Options FollowSymLinks
AllowOverride none
Require all granted
</Directory>
LoadModule wsgi_module modules/mod_wsgi.so
<IFModule wsgi_module>
WSGIScriptAlias / /home/leni/Coding/Pythoneer/FlaskAPP/httpd.wsgi
WSGIDaemonProcess web_service user=leni group=leni threads=1 python-path=/home/leni/.local/lib/python3.6/site-packages:/home/leni/Coding/Pythoneer/FlaskAPP
WSGIProcessGroup web_service
WSGIScriptAlias / /home/leni/Coding/Pythoneer/FlaskAPP/web_service.py
</IFModule>
<Directory /home/leni/Coding/Pythoneer/FlaskAPP>
Options FollowSymLinks
AllowOverride none
Require all granted
</Directory>
μ€μ νμλ λ¬Έλ²μ λ¬Έμ κ° μλμ§ νμΈμ ν΄λ΄μΌκ²μ£ ?
$ httpd -t
λ€μκ³Ό κ°μ΄ Syntax OKκ° λ¨λ©΄ μ μμ μΌλ‘ μ μ€μ λ κ²μ λλ€!
Tip . κ²½λ‘λ₯Ό PATHμ μΆκ° ν΄λμ§ μμλ€λ©΄ μνμΉ μ€μΉν κ²½λ‘ λ€ λΆμ¬μ£Όμ μΌν΄μ!
μλ ν¬μ€ν μ 맨 λ§μ§λ§ λΆλΆμ νμΈν΄μ£ΌμΈμ!
β 2020/03/09 - [OS/π§ Linux] - π§ Ubuntu * Apache μ€μΉ λ° μ€ννκΈ°
wsgi νμΌ μμ±
μ μ€μ μμ wsgi_νμ₯μ_νμΌ_κ²½λ‘μ μμΉν wsgi νμΌμ λ€μκ³Ό κ°μ΄ μμ±ν΄μ€λ€.
sys.path.insert ꡬ문μ λλ²μ§Έ μΈμλ μ€μ μ€ν λ Flask μ ν리μΌμ΄μ μ΄ μλ κ²½λ‘λ₯Ό μ μ΄μ£Όλ©΄ λκ³ ,
python_νμ΄λ¦μ Flask μ΄ν리μΌμ΄μ νμΌμ νμ₯μλ₯Ό μ μΈν μ΄λ¦μ μ μ΄μ£Όλ©΄ λλ€.
$ cd ~/Coding/Pythoneer/Flask
$ vi httpd.wsgi
import sys
sys.path.insert(0, APP_λλ ν 리_κ²½λ‘)
# μ€μ μ€νμν€κ³ μ νλ μ ν리μΌμ΄μ
μ μλ python_νμΌμ΄λ¦ μμ€μ λ€μ΄μκ³ νμ¬ httpd.wsgi νμΌμ κ·Έμ κ²μ΄νΈμ¨μ΄ μν μ ν λΏμ΄λ€.
from python_νμΌμ΄λ¦ import νλΌμ€ν¬_λ³μ_μ΄λ¦ as application
μμμ as application λΆλΆμ μμ νμ§ μκ³ κ·Έλλ‘ λ°λμ μ¨μΌνλ λΆλΆμ΄λ€!
WSGI κ·μ½μ λ°λΌ applicationμ΄λΌκ³ μ§μ ν΄μ£Όμ΄μΌ λμμ ν μ μλ€!
import sys
sys.path.insert(0, '/home/leni/Coding/Pythoneer/FlaskAPP')
from web_service import app as application
APP μμ± λ° μ€ν
"/" λ° "/test" κ²½λ‘ μμ²μ λν μλ΅μ μ£Όλ μ ν리μΌμ΄μ μ μμ±νκ³ μ€νμμΌλ΄ μλ€!
$ vi ~/Coding/Pythoneer/FlaskAPP/web_service.py
import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/', methods=['GET',])
def index():
return "<html><body><h1>Thansk to read my post !</h1></body></html>"
@app.route('/test', methods=['GET',])
def test():
return "<html><body><h1>Hello World!</h1></body></html>"
httpd μ€ν
$ httpd -k start
μ λλ‘ λμκ°λμ§ νμΈνκΈ°
$ curl http://127.0.0.1
$ curl http://127.0.0.1/test
λͺ λ Ήμ΄ μ€νμ ν°λ―Έλ μ°½μ λνλλ κ²μ λ³Ό μ μμΌλ©°, λ΄λΆipλ₯Ό μ¬μ©ν λ€λ₯Έ PCμμ μ μ κ°λ₯ ν κ²μ λ³Ό μ μλ€.
ν°λ―Έλ μ°½μ ubuntu 18.04 LTSμ κ°μλ¨Έμ μ΄λ©°, μΈν°λ· μ°½μ Windows10μμ μ€νν κ²°κ³Όμ΄λ€.
Thanks for π