0
0

More than 1 year has passed since last update.

EC2 pythonで書かれたプログラムをwebからアクセスできるようにする方法

Last updated at Posted at 2021-09-27

今回は下記MWとアプリケーションを導入して、pythonで書かれたプログラムをwebからアクセスできるようにしていきます。

扱うMW
web(nginx) + ap(uwsgi)

アプリケーション
Python + Flask(フレームワーク)

〇nginx のインストール・スタート

amazon-linux-extras install nginx1
systemctl enable nginx
systemctl start nginx

〇Uwsgiのインストール

yum install python3-pip python3-devel
yum groupinstall "Development Tools"
pip3 install --upgrade pip
pip install wheel
pip install uwsgi

〇Python のインストール

yum install git
yum install gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel git
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

#python3をインストールしようとすると、Package libffi was not foundと出てくるのでlibffiをインストールする

yum install libffi-devel
pyenv install 3.7.4
python -V #バージョンの確認

〇uwsgiの設定ファイルを作成する(場所は今回opt配下に作業ディレクトリを作っていますが、どこでもOKです)

mkdir -p /opt/hogehoge/uwsgi
cd /opt/hogehoge/uwsgi
vi uwsgi.ini #下記コピペ
~~
[uwsgi]
module = app
callable = app
master = true
processes = 1
http = 127.0.0.1:8000
socket=/opt/hogehoge/uwsgi/app.sock  #unix domain socket を使用しない場合は不要
vacuum = true
die-on-term = true
chdir = /opt/hogehoge/uwsgi
chmod-socket = 666
~~

つづいてapp.pyファイルを作成

vi app.py #下記コピペ

~~
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
        return "hello world" #ブラウザを開いたときにhello worldと表示されます。別に文章などはなんでもOK

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

~~

つづいてnginxの設定ファイルを変更する。

vi /etc/nginx/nginx.conf

~~
server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
       location / {
       include uwsgi_params;
       uwsgi_pass unix:/opt/hogehoge/uwsgi/app.sock; #UNIX DOMAIN SOCKETを使用する場合はこれ
#       proxy_pass http://127.0.0.1:8000; #←ポート番号で接続する場合はこれ
        }
    }
~~

flaskのモジュールをインストール

pip install flask

ここまでできたらnginxを再起動して、uwsgi.iniを立ち上げる。

systemctl restart nginx
uwsgi --ini uwsgi.ini #このコマンドはuwsgi.iniのおいてあるディレクトリにて実行する

この状態でwebブラウザからグローバルIPを入力して hello world が出てくれば成功です。
以下に参考にさせていただいた記事をのせます。

〇Uwsgiのインストール
https://qiita.com/ajitama/items/33639e9622108f03b45a
〇Python のインストール
https://qiita.com/stu345/items/845d88222a8769eb5425
https://qiita.com/t-iguchi/items/f7847729631022a5041f
https://snickerjp.blogspot.com/2016/05/package-libffi-was-not-found.html
〇uwsgi、nginxの設定など
https://teratail.com/questions/297990
〇iniファイルの文法について
https://qiita.com/11ohina017/items/da2ae5b039257752e558

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