EC2に自作アプリ(www.py)をインストールする
【補足】
- 自分の環境で開発したい場合どうすればいいの?
- yumコマンドを使って新しいソフトを入れる方法や systemctlコマンドとは何か? については付録を参照してください
他と少し違い、 他のディレクトリ (04_docker/04-04_debian-nginx-web.py-mysql/docker/files: 実のところ、このfiles/以下はdockerイメージのなかに組み込むファイル群のディレクトリ) からファイルをコピーします
自動で実行するスクリプトも用意しました。 03_ec2/03-03_web.py/scripts ディレクトリにある install.sh スクリプトを実行してください
実行例: 03_ec2/03-03_web.py/scripts ディレクトリに移動し、install.sh スクリプトを実行する
cd 03_ec2/03-03_web.py/scripts
sh install.sh
install.sh は以下の処理をやっています
- /var/www 以下に必要なファイルをコピー
- www.py が必要とする「pythonでmysqlに接続するためのライブラリ」をインストール
- systemd の設定: 起動/停止するための設定ファイルをインストール
- 設定の詳細は付録を参照
- www.py を起動
実行している様子は次のようになります
>>> installing www scripts to /var/www
>>> installing mysql-connector-python
Defaulting to user installation because normal site-packages is not writeable
Collecting mysql-connector-python
Downloading mysql_connector_python-8.0.27-1commercial-cp37-cp37m-manylinux1_x86_64.whl (37.5 MB)
|████████████████████████████████| 37.5 MB 22.2 MB/s
Collecting protobuf>=3.0.0
Downloading protobuf-3.19.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB)
|████████████████████████████████| 1.1 MB 31.5 MB/s
Installing collected packages: protobuf, mysql-connector-python
Successfully installed mysql-connector-python-8.0.27 protobuf-3.19.1
>>> setting up systemd configuration and start www.py
● www.py.service - Exercises-AWS Python Application Example
Loaded: loaded (/usr/local/lib/systemd/system/www.py.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2021-11-27 10:06:38 UTC; 31ms ago
Docs: http://exercises-aws.fml.org/ja/appendix/python/
Main PID: 31167 (www.py)
CGroup: /system.slice/www.py.service
└─31167 /usr/bin/python3 /var/www/libexec/www.py &
Nov 27 10:06:38 ip-172-31-93-133.ec2.internal systemd[1]: Started Exercises-AWS Python Application Example.
動作確認
127.0.0.1 の 8080/tcp にアクセスしてみてください。 EC2の上で curl コマンドを使うのが簡単です (注:まだセキュリティグループを設定していないので、 自宅のPCからは見えません)
curl http://127.0.0.1:8080/
次のように表示されればOKです
Welcome to our server exercise program !
www.py を起動
install.sh が systemd の設定をしこんでいますので systemctl で start/stop 出来ます (設定の詳細は付録を参照)。 すでに www.py は起動済みですが、もし手動で起動したい場合は
sudo systemctl start www.py
www.py を停止
sudo systemctl stop www.py
www.py の状態を確認
sudo systemctl status www.py