LoginSignup
0
0

More than 1 year has passed since last update.

FlaskをCentOSで動かす

Last updated at Posted at 2022-11-20

Herokuの無料枠廃止に伴い、自分で作ったFlask,PostgreSQLを使ったアプリをさくらのVPSに移しました。
備忘録として、その際の手順をメモ。

バージョン

CentOS 7
Python 3.7.9
mod_wsgi 4.5.6

apacheのインストール

yum install httpd

apacheを起動

systemctl start httpd

サーバーの起動とともにapacheが自動で起動するように設定

systemctl enable httpd

http通信とhttps通信を許可するようにファイアーウォールを設定

firewall-cmd --zone=public --add-service=http --permanent
firewall-cmd --zone=public --add-service=https --permanent

ファイアーウォールを再起動

firewall-cmd --reload

Pythonのために必要なパッケージをインストール

yum groupinstall "Development Tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
yum install httpd-devel python-devel wget

Python3系のインストール

CentOSにはデフォルトでPython2系がインストールされていますが、
mod_wsgiを利用するにはPython3系が必要なのでインストールします。

cd /usr/local/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar zxvf Python-3.7.9.tgz
cd Python-3.7.9
./configure --enable-shared
make && make altinstall
ln -s /usr/local/bin/python3.7 /usr/bin/python3
ln -s /usr/local/lib/libpython3.7m.so.1.0 /lib64/
python3.7 --version //Pythonのバージョンが3.7.9になればOK
ln -s /usr/local/bin/pip3.5 /usr/bin/pip3.7
pip3.7 --version //pipのバージョンを確認

mod_wsgiのインストール

cd /usr/local/src
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.6.tar.gz
tar zxvf 4.5.6.tar.gz
cd mod_wsgi-4.5.6
./configure --with-apxs=/bin/apxs --with-python=/usr/local/bin/python3.7
make && make install
chmod 755 /etc/httpd/modules/mod_wsgi.so 

Flaskのインストール

pip3.7 install flask

アプリケーションの設定

ディレクトリ構成はこんな感じ

  • /var/www/html/
    - app.py
    - application.wsgi

app.pyファイルの設定

app.py
import sys

from flask import Flask,request

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello, World"

if __name__=="__main__":
    app.run()

application.wsgiファイルの設定

application.wsgi
import sys
sys.path.insert(0,"/var/www/html")
from app import app as application

(追記)Blueprintでアプリケーションを分割している場合のapplication.wsgiファイル

application.wsgi
import sys
sys.path.insert(0,"/var/www/html")
from app import create_app
application = create_app()

flask.confの追加

etc/httpd/conf.d/flask.confを作成して以下を記述

flask.conf
LoadModule wsgi_module ./modules/mod_wsgi.so
<VirtualHost *:80>
    WSGIScriptAlias / /var/www/html/application.wsgi
    <Directory /var/www/html/>
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

apacheを再起動

service httpd restart

postgreSQLをインストール

yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql-devel

Pythonのライブラリをインストール

pip3.7 install sqlalchemy
pip3.7 install flask_login
pip3.7 install openpyxl
pip3.7 install psycopg2

PostgreSQLとアプリケーションの連携は、こちらの記事で。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0