본문 바로가기

Development

ubuntu16.04 + apache2 + mod_wsgi + python3-flask + gcp sdk

#우분투 기설치 패키지 업데이트 

$sudo apt-get update

 

#아파치2 설치
$sudo apt-get install apache2

 

#wsgi 설치 (web server gateway interface server 설치 = middleware)

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

$sudo a2enmod wsgi -- 안해도 됨 위에 명령어에서 자동으로 처리

 

#pip 배포 패키지 설치

$sudo apt-get install python3-pip

 

#pip 최신 버전으로 업그레이드

$sudo pip3 install --upgrade pip

 

#flask 웹 프레임워크 설치

$sudo pip3 install flask

 

#gcp sdk 설치

$sudo pip3 install --upgrade google-cloud-storage

 

아파치 웹서버 가상 호스트 기본 설정 : /etc/apache2/site-available/000-default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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
        # 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
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
cs

$cd ~/

$sudo mkdir flask

$sudo ln -sT ~/flask /var/www/flask  #링크 파일 생성(심볼릭 링크: 윈도우 바로가기와 유사) ln 옵션 원본파일 대상파일

$cd flask

$sudo vi flaskapp.py

1
2
3
4
5
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return 'Hello, World!'
cs

$sudo vi app.wsgi

1
2
3
import sys
sys.path.insert(0'/var/www/flask')
from flaskapp import app as application
cs

$sudo vi /etc/apache2/sites-available/flask.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<VirtualHost *:80>
        ServerName localhost
        WSGIDaemonProcess app threads=5
        WSGIScriptAlias / /var/www/flask/app.wsgi
        DocumentRoot /var/www/flask
        <Directory /var/www/flask>
            WSGIProcessGroup app
            WSGIApplicationGroup %{GLOBAL}
            Order deny,allow
            Allow from all
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
cs

웹서버의 기본 설정을 새로 생성한 flask.conf 로 변경

$cd /etc/apache2/sites-available

$sudo a2dissite 000-default

$sudo a2ensite flask.conf 

$sudo service apache2 restart

 

** permission error 방지를 위해

$cd ~

$sudo chmod 777 flask