2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Redhat7でFlask/MySql/Apache/mod_wsgi/virtualenvの環境構築(Python2.7)2020年11月

Last updated at Posted at 2020-11-04

検証環境

  • Red Hat Enterprise Linux Server release 7.9 (Maipo)
  • Python 2.7.5
  • Apache/2.4.6 (Red Hat Enterprise Linux)
  • Flask 1.1.2
  • MySql mysql Ver 8.0.22 for Linux on x86_64 (MySQL Community Server - GPL)
  • pip 20.2.4
  • virtualenv 20.1.0

インストールとアプリケーションファイルの作成

Apache/mod_wsgiのインストール


$ sudo su
$ yum install httpd
$ chkconfig httpd on
$ service httpd start
$ service httpd status # 確認
$ yum install mod_wsgi # mod_wsgiのインストール

pipのインストール


$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py

virtualenvのインストール


$ pip install virtualenv

アプリケーションのディレクトリを作成


$ mkdir /var/www/myapp
$ cd /var/www/myapp
$ virtualenv venv # virtualenv環境の構築
$ source venv/bin/activate # virtualenv有効化
$ pip install flask # flaskのインストール
$ touch app.py # アプリケーションのファイルを作成
$ touch app.wsgi # アプリケーションのファイルを作成
app.py
from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def hello():
    return jsonify("Hello World!"), 201


if __name__ == "__main__":
    app.run()
app.wsgi
import os
import sys

DIR=os.path.dirname(__file__)
sys.path.append(DIR)
activate_this = os.path.join(DIR, 'venv/bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from app import app as application

最終的に下記の様なファイル構成になる

$ tree -L 2
.
├── app.py
├── app.wsgi
└── venv
    ├── bin
    ├── lib
    ├── lib64
    └── pyvenv.cfg

Apache/mod_wsgiの設定

$ vi /etc/httpd/conf/httpd.conf

下記の行を追加

Listen 8888

port 8888を加えて、apacheを再起動すると下記のようにエラーが出る

(13)Permission denied: AH00072: make_sock: could not bind to address [::]:8888
(13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:8888

selinuxでポートを追加する
参考 http://hetarena.com/archives/495

$ semanage port -a -t http_port_t -p tcp 8888

アプリケーション用の環境を構築

$ vi /etc/httpd/conf.d/myenv.conf
myenv.conf
<VirtualHost *:8888>
    WSGIDaemonProcess wsgi_flask user=apache group=apache threads=10
    WSGIScriptAlias / /var/www/myapp/app.wsgi

    WSGIScriptReloading On

    <Directory "/var/www/myapp">
	WSGIProcessGroup wsgi_flask
	WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>
  • 設定ファイルの確認
$ apachectl configtest

$ service httpd graceful

mysqlのインストール

$ sudo su
$ rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
$ yum install mysql-community-server
$ service mysqld start
$ service mysqld status # 確認

mysqlのログイン

$ sudo su
$ tail /var/log/mysqld.log # パスワードをログから確認
$ mysql -uroot
$ mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'YQM3rCyae8Ft?';

参考 https://dev.classmethod.jp/articles/how-to-serve-flask-with-apache-mod_wsgi-virtualenv-on-ec2/

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?