๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

OS/๐Ÿง Linux

๐Ÿง 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 ๐Ÿ™ˆ