Google Compute EngineにFlask環境作ってみたので、メモ。
お断り
突貫で作ってるものなので、uwsgiの自動起動設定とかまでしてません。(=sshのセッションが切れたらuwsgiのプロセスが死ぬので、Flaskアプリは見れません)
気が向いたらあとで追記しようかな。
環境
- Google Compute Engine (CentOS Linux release 7.3.1611 (Core))
- Python 3.6.0
- Flask 0.12
- Nginx 1.11.10
- uWSGI 2.0.14
Google Compute Engine
- GCPのコンパネから、インスタンスを作る。
- とりあえずCentos7で(おこのみ)
- マシンタイプはとりあえずなので
f1-micro
で作成
- 同コンパネから、SSH認証鍵を作って登録
- 同コンパネから、SSHログインして、rootと上記ユーザにPW設定しとく。
- 以降、MacのteminalからSSH接続。
pyenv
参考)http://enomotodev.github.io/post/install-pyenv/
pyenvに必要なものをインストール
どーん
[user@instance ~]$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline readline-devel sqlite sqlite-devel openssl openssl-devel git
読み込んだプラグイン:fastestmirror
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 12 kB 00:00:00
epel | 4.3 kB 00:00:00
extras | 3.4 kB 00:00:00
google-cloud-compute/signature | 454 B 00:00:00
google-cloud-compute/signature | 1.4 kB 00:00:00 !!!
google-cloud-sdk/signature | 454 B 00:00:00
google-cloud-sdk/signature | 1.4 kB 00:00:00 !!!
updates | 3.4 kB 00:00:00
(1/9): epel/x86_64/group_gz | 170 kB 00:00:00
(2/9): epel/x86_64/updateinfo | 749 kB 00:00:00
(3/9): base/7/x86_64/group_gz | 155 kB 00:00:01
(4/9): base/7/x86_64/primary_db | 5.6 MB 00:00:01
(5/9): google-cloud-compute/primary | 1.9 kB 00:00:00
(6/9): google-cloud-sdk/primary | 2.8 kB 00:00:00
(7/9): epel/x86_64/primary_db | 4.6 MB 00:00:00
(8/9): extras/7/x86_64/primary_db | 122 kB 00:00:01
(9/9): updates/7/x86_64/primary_db | 3.8 MB 00:00:06
Determining fastest mirrors
* base: centos-distro.cavecreek.net
* epel: mirrors.cat.pdx.edu
* extras: mirrors.cat.pdx.edu
* updates: mirrors.kernel.org
google-cloud-compute 4/4
google-cloud-sdk 7/7
パッケージ readline-6.2-9.el7.x86_64 はインストール済みか最新バージョンです
パッケージ sqlite-3.7.17-8.el7.x86_64 はインストール済みか最新バージョンです
パッケージ 1:openssl-1.0.1e-60.el7_3.1.x86_64 はインストール済みか最新バージョンです
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ bzip2.x86_64 0:1.0.6-13.el7 を インストール
---> パッケージ bzip2-devel.x86_64 0:1.0.6-13.el7 を インストール
---> パッケージ gcc.x86_64 0:4.8.5-11.el7 を インストール
--> 依存性の処理をしています: cpp = 4.8.5-11.el7 のパッケージ: gcc-4.8.5-11.el7.x86_64
--> 依存性の処理をしています: glibc-devel >= 2.2.90-12 のパッケージ: gcc-4.8.5-11.el7.x86_64
--> 依存性の処理をしています: libmpfr.so.4()(64bit) のパッケージ: gcc-4.8.5-11.el7.x86_64
--> 依存性の処理をしています: libmpc.so.3()(64bit) のパッケージ: gcc-4.8.5-11.el7.x86_64
---> パッケージ git.x86_64 0:1.8.3.1-6.el7_2.1 を インストール
--> 依存性の処理をしています: perl-Git = 1.8.3.1-6.el7_2.1 のパッケージ: git-1.8.3.1-6.el7_2.1.x86_64
--> 依存性の処理をしています: perl(Term::ReadKey) のパッケージ: git-1.8.3.1-6.el7_2.1.x86_64
--> 依存性の処理をしています: perl(Git) のパッケージ: git-1.8.3.1-6.el7_2.1.x86_64
--> 依存性の処理をしています: perl(Error) のパッケージ: git-1.8.3.1-6.el7_2.1.x86_64
--> 依存性の処理をしています: libgnome-keyring.so.0()(64bit) のパッケージ: git-1.8.3.1-6.el7_2.1.x86_64
---> パッケージ openssl-devel.x86_64 1:1.0.1e-60.el7_3.1 を インストール
--> 依存性の処理をしています: krb5-devel(x86-64) のパッケージ: 1:openssl-devel-1.0.1e-60.el7_3.1.x86_64
---> パッケージ readline-devel.x86_64 0:6.2-9.el7 を インストール
--> 依存性の処理をしています: ncurses-devel のパッケージ: readline-devel-6.2-9.el7.x86_64
---> パッケージ sqlite-devel.x86_64 0:3.7.17-8.el7 を インストール
---> パッケージ zlib-devel.x86_64 0:1.2.7-17.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ cpp.x86_64 0:4.8.5-11.el7 を インストール
---> パッケージ glibc-devel.x86_64 0:2.17-157.el7_3.1 を インストール
--> 依存性の処理をしています: glibc-headers = 2.17-157.el7_3.1 のパッケージ: glibc-devel-2.17-157.el7_3.1.x86_64
--> 依存性の処理をしています: glibc-headers のパッケージ: glibc-devel-2.17-157.el7_3.1.x86_64
---> パッケージ krb5-devel.x86_64 0:1.14.1-27.el7_3 を インストール
--> 依存性の処理をしています: libkadm5(x86-64) = 1.14.1-27.el7_3 のパッケージ: krb5-devel-1.14.1-27.el7_3.x86_64
--> 依存性の処理をしています: libverto-devel のパッケージ: krb5-devel-1.14.1-27.el7_3.x86_64
--> 依存性の処理をしています: libselinux-devel のパッケージ: krb5-devel-1.14.1-27.el7_3.x86_64
--> 依存性の処理をしています: libcom_err-devel のパッケージ: krb5-devel-1.14.1-27.el7_3.x86_64
--> 依存性の処理をしています: keyutils-libs-devel のパッケージ: krb5-devel-1.14.1-27.el7_3.x86_64
---> パッケージ libgnome-keyring.x86_64 0:3.8.0-3.el7 を インストール
---> パッケージ libmpc.x86_64 0:1.0.1-3.el7 を インストール
---> パッケージ mpfr.x86_64 0:3.1.1-4.el7 を インストール
---> パッケージ ncurses-devel.x86_64 0:5.9-13.20130511.el7 を インストール
---> パッケージ perl-Error.noarch 1:0.17020-2.el7 を インストール
---> パッケージ perl-Git.noarch 0:1.8.3.1-6.el7_2.1 を インストール
---> パッケージ perl-TermReadKey.x86_64 0:2.30-20.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ glibc-headers.x86_64 0:2.17-157.el7_3.1 を インストール
--> 依存性の処理をしています: kernel-headers >= 2.2.1 のパッケージ: glibc-headers-2.17-157.el7_3.1.x86_64
--> 依存性の処理をしています: kernel-headers のパッケージ: glibc-headers-2.17-157.el7_3.1.x86_64
---> パッケージ keyutils-libs-devel.x86_64 0:1.5.8-3.el7 を インストール
---> パッケージ libcom_err-devel.x86_64 0:1.42.9-9.el7 を インストール
---> パッケージ libkadm5.x86_64 0:1.14.1-27.el7_3 を インストール
---> パッケージ libselinux-devel.x86_64 0:2.5-6.el7 を インストール
--> 依存性の処理をしています: libsepol-devel(x86-64) >= 2.5-6 のパッケージ: libselinux-devel-2.5-6.el7.x86_64
--> 依存性の処理をしています: pkgconfig(libsepol) のパッケージ: libselinux-devel-2.5-6.el7.x86_64
--> 依存性の処理をしています: pkgconfig(libpcre) のパッケージ: libselinux-devel-2.5-6.el7.x86_64
---> パッケージ libverto-devel.x86_64 0:0.2.5-4.el7 を インストール
--> トランザクションの確認を実行しています。
---> パッケージ kernel-headers.x86_64 0:3.10.0-514.10.2.el7 を インストール
---> パッケージ libsepol-devel.x86_64 0:2.5-6.el7 を インストール
---> パッケージ pcre-devel.x86_64 0:8.32-15.el7_2.1 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
===========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
===========================================================================================================================================================================================================================================
インストール中:
bzip2 x86_64 1.0.6-13.el7 base 52 k
bzip2-devel x86_64 1.0.6-13.el7 base 218 k
gcc x86_64 4.8.5-11.el7 base 16 M
git x86_64 1.8.3.1-6.el7_2.1 base 4.4 M
openssl-devel x86_64 1:1.0.1e-60.el7_3.1 updates 1.2 M
readline-devel x86_64 6.2-9.el7 base 138 k
sqlite-devel x86_64 3.7.17-8.el7 base 104 k
zlib-devel x86_64 1.2.7-17.el7 base 50 k
依存性関連でのインストールをします:
cpp x86_64 4.8.5-11.el7 base 5.9 M
glibc-devel x86_64 2.17-157.el7_3.1 updates 1.1 M
glibc-headers x86_64 2.17-157.el7_3.1 updates 668 k
kernel-headers x86_64 3.10.0-514.10.2.el7 updates 4.8 M
keyutils-libs-devel x86_64 1.5.8-3.el7 base 37 k
krb5-devel x86_64 1.14.1-27.el7_3 updates 651 k
libcom_err-devel x86_64 1.42.9-9.el7 base 31 k
libgnome-keyring x86_64 3.8.0-3.el7 base 109 k
libkadm5 x86_64 1.14.1-27.el7_3 updates 173 k
libmpc x86_64 1.0.1-3.el7 base 51 k
libselinux-devel x86_64 2.5-6.el7 base 186 k
libsepol-devel x86_64 2.5-6.el7 base 74 k
libverto-devel x86_64 0.2.5-4.el7 base 12 k
mpfr x86_64 3.1.1-4.el7 base 203 k
ncurses-devel x86_64 5.9-13.20130511.el7 base 713 k
pcre-devel x86_64 8.32-15.el7_2.1 base 479 k
perl-Error noarch 1:0.17020-2.el7 base 32 k
perl-Git noarch 1.8.3.1-6.el7_2.1 base 53 k
perl-TermReadKey x86_64 2.30-20.el7 base 31 k
トランザクションの要約
===========================================================================================================================================================================================================================================
インストール 8 パッケージ (+19 個の依存関係のパッケージ)
総ダウンロード容量: 37 M
インストール容量: 91 M
Is this ok [y/d/N]: y
Downloading packages:
(1/27): bzip2-1.0.6-13.el7.x86_64.rpm | 52 kB 00:00:00
(2/27): bzip2-devel-1.0.6-13.el7.x86_64.rpm | 218 kB 00:00:00
(3/27): cpp-4.8.5-11.el7.x86_64.rpm | 5.9 MB 00:00:01
(4/27): git-1.8.3.1-6.el7_2.1.x86_64.rpm | 4.4 MB 00:00:00
(5/27): keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm | 37 kB 00:00:00
(6/27): gcc-4.8.5-11.el7.x86_64.rpm | 16 MB 00:00:02
(7/27): libcom_err-devel-1.42.9-9.el7.x86_64.rpm | 31 kB 00:00:00
(8/27): libgnome-keyring-3.8.0-3.el7.x86_64.rpm | 109 kB 00:00:00
(9/27): glibc-headers-2.17-157.el7_3.1.x86_64.rpm | 668 kB 00:00:01
(10/27): libmpc-1.0.1-3.el7.x86_64.rpm | 51 kB 00:00:00
(11/27): libselinux-devel-2.5-6.el7.x86_64.rpm | 186 kB 00:00:00
(12/27): libsepol-devel-2.5-6.el7.x86_64.rpm | 74 kB 00:00:00
(13/27): libverto-devel-0.2.5-4.el7.x86_64.rpm | 12 kB 00:00:00
(14/27): mpfr-3.1.1-4.el7.x86_64.rpm | 203 kB 00:00:00
(15/27): ncurses-devel-5.9-13.20130511.el7.x86_64.rpm | 713 kB 00:00:00
(16/27): libkadm5-1.14.1-27.el7_3.x86_64.rpm | 173 kB 00:00:01
(17/27): krb5-devel-1.14.1-27.el7_3.x86_64.rpm | 651 kB 00:00:01
(18/27): pcre-devel-8.32-15.el7_2.1.x86_64.rpm | 479 kB 00:00:00
(19/27): perl-Git-1.8.3.1-6.el7_2.1.noarch.rpm | 53 kB 00:00:00
(20/27): perl-TermReadKey-2.30-20.el7.x86_64.rpm | 31 kB 00:00:00
(21/27): openssl-devel-1.0.1e-60.el7_3.1.x86_64.rpm | 1.2 MB 00:00:00
(22/27): readline-devel-6.2-9.el7.x86_64.rpm | 138 kB 00:00:00
(23/27): sqlite-devel-3.7.17-8.el7.x86_64.rpm | 104 kB 00:00:00
(24/27): perl-Error-0.17020-2.el7.noarch.rpm | 32 kB 00:00:00
(25/27): zlib-devel-1.2.7-17.el7.x86_64.rpm | 50 kB 00:00:00
(26/27): glibc-devel-2.17-157.el7_3.1.x86_64.rpm | 1.1 MB 00:00:03
(27/27): kernel-headers-3.10.0-514.10.2.el7.x86_64.rpm | 4.8 MB 00:00:17
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計 1.9 MB/s | 37 MB 00:00:19
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : mpfr-3.1.1-4.el7.x86_64 1/27
インストール中 : libmpc-1.0.1-3.el7.x86_64 2/27
インストール中 : 1:perl-Error-0.17020-2.el7.noarch 3/27
インストール中 : cpp-4.8.5-11.el7.x86_64 4/27
インストール中 : perl-TermReadKey-2.30-20.el7.x86_64 5/27
インストール中 : libgnome-keyring-3.8.0-3.el7.x86_64 6/27
インストール中 : perl-Git-1.8.3.1-6.el7_2.1.noarch 7/27
インストール中 : git-1.8.3.1-6.el7_2.1.x86_64 8/27
インストール中 : ncurses-devel-5.9-13.20130511.el7.x86_64 9/27
インストール中 : kernel-headers-3.10.0-514.10.2.el7.x86_64 10/27
インストール中 : glibc-headers-2.17-157.el7_3.1.x86_64 11/27
インストール中 : glibc-devel-2.17-157.el7_3.1.x86_64 12/27
インストール中 : libverto-devel-0.2.5-4.el7.x86_64 13/27
インストール中 : libcom_err-devel-1.42.9-9.el7.x86_64 14/27
インストール中 : pcre-devel-8.32-15.el7_2.1.x86_64 15/27
インストール中 : libsepol-devel-2.5-6.el7.x86_64 16/27
インストール中 : libselinux-devel-2.5-6.el7.x86_64 17/27
インストール中 : libkadm5-1.14.1-27.el7_3.x86_64 18/27
インストール中 : keyutils-libs-devel-1.5.8-3.el7.x86_64 19/27
インストール中 : krb5-devel-1.14.1-27.el7_3.x86_64 20/27
インストール中 : zlib-devel-1.2.7-17.el7.x86_64 21/27
インストール中 : 1:openssl-devel-1.0.1e-60.el7_3.1.x86_64 22/27
インストール中 : gcc-4.8.5-11.el7.x86_64 23/27
インストール中 : readline-devel-6.2-9.el7.x86_64 24/27
インストール中 : sqlite-devel-3.7.17-8.el7.x86_64 25/27
インストール中 : bzip2-1.0.6-13.el7.x86_64 26/27
インストール中 : bzip2-devel-1.0.6-13.el7.x86_64 27/27
検証中 : zlib-devel-1.2.7-17.el7.x86_64 1/27
検証中 : keyutils-libs-devel-1.5.8-3.el7.x86_64 2/27
検証中 : libkadm5-1.14.1-27.el7_3.x86_64 3/27
検証中 : libsepol-devel-2.5-6.el7.x86_64 4/27
検証中 : pcre-devel-8.32-15.el7_2.1.x86_64 5/27
検証中 : cpp-4.8.5-11.el7.x86_64 6/27
検証中 : libselinux-devel-2.5-6.el7.x86_64 7/27
検証中 : bzip2-devel-1.0.6-13.el7.x86_64 8/27
検証中 : readline-devel-6.2-9.el7.x86_64 9/27
検証中 : libcom_err-devel-1.42.9-9.el7.x86_64 10/27
検証中 : libverto-devel-0.2.5-4.el7.x86_64 11/27
検証中 : bzip2-1.0.6-13.el7.x86_64 12/27
検証中 : kernel-headers-3.10.0-514.10.2.el7.x86_64 13/27
検証中 : 1:openssl-devel-1.0.1e-60.el7_3.1.x86_64 14/27
検証中 : 1:perl-Error-0.17020-2.el7.noarch 15/27
検証中 : glibc-devel-2.17-157.el7_3.1.x86_64 16/27
検証中 : glibc-headers-2.17-157.el7_3.1.x86_64 17/27
検証中 : perl-Git-1.8.3.1-6.el7_2.1.noarch 18/27
検証中 : ncurses-devel-5.9-13.20130511.el7.x86_64 19/27
検証中 : sqlite-devel-3.7.17-8.el7.x86_64 20/27
検証中 : libmpc-1.0.1-3.el7.x86_64 21/27
検証中 : libgnome-keyring-3.8.0-3.el7.x86_64 22/27
検証中 : krb5-devel-1.14.1-27.el7_3.x86_64 23/27
検証中 : perl-TermReadKey-2.30-20.el7.x86_64 24/27
検証中 : mpfr-3.1.1-4.el7.x86_64 25/27
検証中 : git-1.8.3.1-6.el7_2.1.x86_64 26/27
検証中 : gcc-4.8.5-11.el7.x86_64 27/27
インストール:
bzip2.x86_64 0:1.0.6-13.el7 bzip2-devel.x86_64 0:1.0.6-13.el7 gcc.x86_64 0:4.8.5-11.el7 git.x86_64 0:1.8.3.1-6.el7_2.1 openssl-devel.x86_64 1:1.0.1e-60.el7_3.1 readline-devel.x86_64 0:6.2-9.el7
sqlite-devel.x86_64 0:3.7.17-8.el7 zlib-devel.x86_64 0:1.2.7-17.el7
依存性関連をインストールしました:
cpp.x86_64 0:4.8.5-11.el7 glibc-devel.x86_64 0:2.17-157.el7_3.1 glibc-headers.x86_64 0:2.17-157.el7_3.1 kernel-headers.x86_64 0:3.10.0-514.10.2.el7 keyutils-libs-devel.x86_64 0:1.5.8-3.el7
krb5-devel.x86_64 0:1.14.1-27.el7_3 libcom_err-devel.x86_64 0:1.42.9-9.el7 libgnome-keyring.x86_64 0:3.8.0-3.el7 libkadm5.x86_64 0:1.14.1-27.el7_3 libmpc.x86_64 0:1.0.1-3.el7
libselinux-devel.x86_64 0:2.5-6.el7 libsepol-devel.x86_64 0:2.5-6.el7 libverto-devel.x86_64 0:0.2.5-4.el7 mpfr.x86_64 0:3.1.1-4.el7 ncurses-devel.x86_64 0:5.9-13.20130511.el7
pcre-devel.x86_64 0:8.32-15.el7_2.1 perl-Error.noarch 1:0.17020-2.el7 perl-Git.noarch 0:1.8.3.1-6.el7_2.1 perl-TermReadKey.x86_64 0:2.30-20.el7
完了しました!
pyenvのインストール
gitからclone
[user@instance ~]$ git clone https://github.com/yyuu/pyenv.git ~/.pyenv
Cloning into '/home/user/.pyenv'...
remote: Counting objects: 14647, done.
remote: Compressing objects: 100% (140/140), done.
remote: Total 14647 (delta 99), reused 0 (delta 0), pack-reused 14503
Receiving objects: 100% (14647/14647), 2.63 MiB | 643.00 KiB/s, done.
Resolving deltas: 100% (10069/10069), done.
環境変数設定
[user@instance ~]$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
[user@instance ~]$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
[user@instance ~]$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
[user@instance ~]$ source ~/.bash_profile
sudo でpyenvコマンドが使えなくなるのが不便なので、visudoで登録。
参考)
http://qiita.com/akito1986/items/e9ca48cfcd56fdbf4c9d
http://qiita.com/akito1986/items/be5dcd1a502aaf22010b
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
↑これをコメントアウトして↓
#Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults env_keep += "PATH"
Defaults env_keep += "PYENV_ROOT"
確認
[user@instance ~]$ pyenv --version
pyenv 1.0.8-12-g0e68316
[user@instance ~]$ python --version
Python 2.7.5
python 3.6.0をインストール
イントールコマンド
それなりに時間がかかるのでおとなしく待つこと。
[user@instance ~]$ pyenv install 3.6.0
Downloading Python-3.6.0.tar.xz...
-> https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz
Installing Python-3.6.0...
Installed Python-3.6.0 to /home/user/.pyenv/versions/3.6.0
確認
[user@instance ~]$ pyenv versions
* system (set by /home/user/.pyenv/version)
3.6.0
Nginx
参考)http://vdeep.net/centos7-nginx
RPMパッケージのインストール
どーん
[user@instance ~]$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm を取得中
警告: /var/tmp/rpm-tmp.6D0hOl: ヘッダー V4 RSA/SHA1 Signature、鍵 ID 7bd9bf62: NOKEY
準備しています... ################################# [100%]
更新中 / インストール中...
1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
rpm設定
ファイルがなければ作る。
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=0
Nginxをインストール
どーん
[user@instance ~]$ sudo yum -y install --enablerepo=nginx nginx
読み込んだプラグイン:fastestmirror
nginx | 2.9 kB 00:00:00
nginx/x86_64/primary_db | 57 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: centos-distro.cavecreek.net
* epel: mirrors.cat.pdx.edu
* extras: mirrors.cat.pdx.edu
* updates: mirrors.kernel.org
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ nginx.x86_64 1:1.11.10-1.el7.ngx を インストール
--> 依存性解決を終了しました。
依存性を解決しました
===========================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
===========================================================================================================================================================================================================================================
インストール中:
nginx x86_64 1:1.11.10-1.el7.ngx nginx 715 k
トランザクションの要約
===========================================================================================================================================================================================================================================
インストール 1 パッケージ
総ダウンロード容量: 715 k
インストール容量: 2.5 M
Downloading packages:
nginx-1.11.10-1.el7.ngx.x86_64.rpm | 715 kB 00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告: RPMDB は yum 以外で変更されました。
インストール中 : 1:nginx-1.11.10-1.el7.ngx.x86_64 1/1
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
検証中 : 1:nginx-1.11.10-1.el7.ngx.x86_64 1/1
インストール:
nginx.x86_64 1:1.11.10-1.el7.ngx
完了しました
selinuxを切る
注)セキュリティは各自のご判断で!
$ sudo setenforce 0
確認
[user@instance ~]$ nginx -V
nginx version: nginx/1.11.10
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
動作確認
[user@instance ~]$ sudo systemctl start nginx
で、 http://ホスト/
にアクセスしてNginxのページが出ればNginxは正常作動中。
自動起動設定
[user@instance ~]$ sudo systemctl enable nginx
config
[user@instance ~]$ cd /etc/nginx/conf.d/
[user@instance conf.d]$ ls
default.conf
[user@instance conf.d]$ sudo cp default.conf default_conf_orig
[user@instance conf.d]$ ls -al
合計 12
drwxr-xr-x. 2 root root 49 3月 7 03:08 .
drwxr-xr-x. 3 root root 4096 3月 7 03:06 ..
-rw-r--r--. 1 root root 1097 2月 14 16:37 default.conf
-rw-r--r--. 1 root root 1097 3月 7 03:08 default_conf_orig
- とりあえずドキュメントルートを変更。/usr/share/nginx/html → /usr/share/nginx/test-web
- とりあえずログの出力先を変更。
$ sudo mkdir /var/log/nginx/log
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/test-web;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
virtualenv
参考)http://qiita.com/Kodaira_/items/feadfef9add468e3a85b
gitから
[user@instance ~]$ git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
Cloning into '/home/user/.pyenv/plugins/pyenv-virtualenv'...
remote: Counting objects: 1871, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 1871 (delta 2), reused 0 (delta 0), pack-reused 1865
Receiving objects: 100% (1871/1871), 535.57 KiB | 774.00 KiB/s, done.
Resolving deltas: 100% (1279/1279), done.
環境変数に登録
[user@instance ~]$ echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
仮想環境の作成
Nginxのドキュメントルートに設定したディレクトリ /usr/share/nginx/test-web
に移動して。
(このへん、急いでたので正直ちょっと怪しいところあり)
[user@instance test-web]$ pyenv virtualenv 3.6.0 test-web
Requirement already satisfied: setuptools in /home/user/.pyenv/versions/3.6.0/envs/test-web/lib/python3.6/site-packages
Requirement already satisfied: pip in /home/user/.pyenv/versions/3.6.0/envs/test-web/lib/python3.6/site-packages
[user@instance test-web]$ sudo virtualenv --no-site-packages test-web
[user@instance test-web]$ pyenv versions
system
3.6.0
* test-web (set by /usr/share/nginx/test-web/.python-version)
Flask
(test-web)[user@instance test-web]$ pip install Flask
で以下必要なものをもりもりと。
今回は、アプリ本体はすでにローカル開発していてgitにあげていたため、そこからpullしてきたので、Flaskの初期プロジェクトについては省略。
公式をどうぞ。 http://flask.pocoo.org/docs/0.12/
以降、仮に、最初に実行するpythonファイルは、 testweb.py
としておきます。
uWSGI
インストール
(test-web)[user@instance test-web]$ pip install uwsgi
Collecting uwsgi
Downloading uwsgi-2.0.14.tar.gz (788kB)
100% |████████████████████████████████| 798kB 1.6MB/s
Installing collected packages: uwsgi
Running setup.py install for uwsgi ... done
Successfully installed uwsgi-2.0.14
NginxとuWSGIの連携
locationの設定を、uWSGIのものに変更。
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log /var/log/nginx/log/host.access.log main;
# location / {
# root /usr/share/nginx/test-web;
# index index.html index.htm;
# }
location / {
include uwsgi_params;
uwsgi_pass unix:///var/tmp/uwsgi.sock;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
変えたら、Nginxを再起動しておくこと。
$ sudo systemctl restart nginx
起動確認
まずはコマンドラインで。
(test-web)[user@instance test-web]$ uwsgi --socket /var/tmp/uwsgi.sock --module testweb --callable app --chmod-socket=666
ポイントは、
* --socket は、 default.conf
の uwsgi_pass
で設定したパス。
* --module は、Flaskアプリの、実行pyファイル。
これで、 http://ホスト/
にアクセスして、Flaskアプリのページがでれば成功。
python内でprint()しているものがだーっと出たりします。
プロセス止めると、uwsgiだけがとまるので、Nginxのエラーページが出る。
iniファイル作成
設定をiniを作ってまとめる。
中身は↑のコマンドのオプションと同じ。
[uwsgi]
module = testweb
callable = app
socket = /var/tmp/uwsgi.sock
chmod-socket = 666
起動
$ uwsgi --ini uwsgi.ini
これで同じく、ブラウザからFlaskアプリのページがでれば成功。
同じく、プロセスが死んだらNginxのエラーページになる。
** ここまで **
uWSGIのサービス登録
気が向いたらやってみて追記。
たぶんこのへん)
http://qiita.com/mykysyk@github/items/3ebbd5a787fdbefaf5e8#uwsgi%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
https://kodak-ism.com/how-to-release-django-app-on-centos7-and-nginx/#sec-7
http://uwsgi-docs.readthedocs.io/en/latest/Emperor.html
http://uwsgi-docs.readthedocs.io/en/latest/Systemd.html
参考
ありがとうございます
http://enomotodev.github.io/post/install-pyenv/
http://qiita.com/akito1986/items/e9ca48cfcd56fdbf4c9d
http://qiita.com/akito1986/items/be5dcd1a502aaf22010b
http://vdeep.net/centos7-nginx
http://qiita.com/Kodaira_/items/feadfef9add468e3a85b
http://flask.pocoo.org/docs/0.12/
http://qiita.com/mykysyk@github/items/3ebbd5a787fdbefaf5e8
http://qiita.com/mykysyk@github/items/3ebbd5a787fdbefaf5e8#uwsgi%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB
https://kodak-ism.com/how-to-release-django-app-on-centos7-and-nginx/#sec-7