CentOS7+nginx+apache2.4+Python3の環境を作る-apacheとPython3のインストール

追記
Pythonの環境が構築できる環境構築スクリプトがありますので、そちらをつかうとすぐに構築できます
まずはapacheとpython3のインストールをしてみたいと思います。環境は↓の通りです。
環境
- ベンダー:さくらのクラウド
- OS:CentOS7
- フロント:nginx(80番ポート)
- バックエンド:Apache2.4(8080番ポート)
- Python:3系(pyenvを使用)
- フレームワーク:bottle(8081番ポート)
基本的にsudo環境にする予定です。
手順
基本的には以下の手順になります
- Apacheのインストール←ここ
- Python(pyenv)のインストール←ここ
- pipの設定
- Apache+Pythonの連携
- nginxを使ってリバースプロキシ
今回の記事について
今回は手順の1と2を行います。
Apacheのインストール
Apacheのインストールを行います。今まではrootでやっていましたが今回は違います
[c]
[macan@localhost ~]$ sudo yum install httpd
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 7.3 kB 00:00:00
* elrepo: ftp.ne.jp
* epel: ftp.iij.ad.jp
base | 3.6 kB 00:00:00
elrepo | 2.9 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
epel/x86_64/updateinfo FAILED
http://ftp.iij.ad.jp/pub/linux/Fedora/epel/7/x86_64/repodata/a61f8647af32e93a9f55cfe1aabb93e9a6731df0b5f56308d80a714f0c64ce42-updateinfo.xml.bz2: [Errno 14] HTTP Error 404 - Not Found ] 0.0 B/s | 0 B --:--:-- ETA
他のミラーを試します。
To address this issue please refer to the below wiki article
https://wiki.centos.org/yum-errors
If above article doesn't help to resolve this issue please use https://bugs.centos.org/.
epel/x86_64/primary FAILED
http://ftp.iij.ad.jp/pub/linux/Fedora/epel/7/x86_64/repodata/77522a7efb909154b1032ba543be63704b1b8aef163ad2ed9ab1c10e2bf30b57-primary.xml.gz: [Errno 14] HTTP Error 404 - Not Found ] 0.0 B/s | 0 B --:--:-- ETA
他のミラーを試します。
(1/2): epel/x86_64/updateinfo | 932 kB 00:00:00
(2/2): epel/x86_64/primary | 3.6 MB 00:00:01
epel 12698/12698
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ httpd.x86_64 0:2.4.6-80.el7.centos.1 を インストール
--> 依存性の処理をしています: httpd-tools = 2.4.6-80.el7.centos.1 のパッケージ: httpd-2.4.6-80.el7.centos.1.x86_64
--> 依存性の処理をしています: /etc/mime.types のパッケージ: httpd-2.4.6-80.el7.centos.1.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 を インストール
---> パッケージ mailcap.noarch 0:2.1.41-2.el7 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
==============================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
==============================================================================================================================================================================================================================================
インストール中:
httpd x86_64 2.4.6-80.el7.centos.1 updates 2.7 M
依存性関連でのインストールをします:
httpd-tools x86_64 2.4.6-80.el7.centos.1 updates 90 k
mailcap noarch 2.1.41-2.el7 base 31 k
トランザクションの要約
==============================================================================================================================================================================================================================================
インストール 1 パッケージ (+2 個の依存関係のパッケージ)
総ダウンロード容量: 2.8 M
インストール容量: 9.6 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): mailcap-2.1.41-2.el7.noarch.rpm | 31 kB 00:00:00
(2/3): httpd-tools-2.4.6-80.el7.centos.1.x86_64.rpm | 90 kB 00:00:00
(3/3): httpd-2.4.6-80.el7.centos.1.x86_64.rpm | 2.7 MB 00:00:01
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計 2.7 MB/s | 2.8 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : httpd-tools-2.4.6-80.el7.centos.1.x86_64 1/3
インストール中 : mailcap-2.1.41-2.el7.noarch 2/3
インストール中 : httpd-2.4.6-80.el7.centos.1.x86_64 3/3
検証中 : mailcap-2.1.41-2.el7.noarch 1/3
検証中 : httpd-tools-2.4.6-80.el7.centos.1.x86_64 2/3
検証中 : httpd-2.4.6-80.el7.centos.1.x86_64 3/3
インストール:
httpd.x86_64 0:2.4.6-80.el7.centos.1
依存性関連をインストールしました:
httpd-tools.x86_64 0:2.4.6-80.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7
完了しました!
[/c]
インストールできました。
続いてhttpd-develのインストールを行います。
[c]
[macan@localhost ~]$ sudo yum install httpd-devel
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* elrepo: ftp.ne.jp
* epel: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ httpd-devel.x86_64 0:2.4.6-80.el7.centos.1 を インストール
--> 依存性の処理をしています: apr-util-devel のパッケージ: httpd-devel-2.4.6-80.el7.centos.1.x86_64
--> 依存性の処理をしています: apr-devel のパッケージ: httpd-devel-2.4.6-80.el7.centos.1.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ apr-devel.x86_64 0:1.4.8-3.el7_4.1 を インストール
---> パッケージ apr-util-devel.x86_64 0:1.5.2-6.el7 を インストール
--> 依存性の処理をしています: openldap-devel(x86-64) のパッケージ: apr-util-devel-1.5.2-6.el7.x86_64
--> 依存性の処理をしています: libdb-devel(x86-64) のパッケージ: apr-util-devel-1.5.2-6.el7.x86_64
--> 依存性の処理をしています: expat-devel(x86-64) のパッケージ: apr-util-devel-1.5.2-6.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ expat-devel.x86_64 0:2.1.0-10.el7_3 を インストール
---> パッケージ libdb-devel.x86_64 0:5.3.21-24.el7 を インストール
---> パッケージ openldap-devel.x86_64 0:2.4.44-15.el7_5 を インストール
--> 依存性の処理をしています: cyrus-sasl-devel(x86-64) のパッケージ: openldap-devel-2.4.44-15.el7_5.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ cyrus-sasl-devel.x86_64 0:2.1.26-23.el7 を インストール
--> 依存性の処理をしています: cyrus-sasl(x86-64) = 2.1.26-23.el7 のパッケージ: cyrus-sasl-devel-2.1.26-23.el7.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ cyrus-sasl.x86_64 0:2.1.26-23.el7 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
============================================================================================================================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
============================================================================================================================================================================================================================================================================================================================
インストール中:
httpd-devel x86_64 2.4.6-80.el7.centos.1 updates 196 k
依存性関連でのインストールをします:
apr-devel x86_64 1.4.8-3.el7_4.1 base 188 k
apr-util-devel x86_64 1.5.2-6.el7 base 76 k
cyrus-sasl x86_64 2.1.26-23.el7 base 88 k
cyrus-sasl-devel x86_64 2.1.26-23.el7 base 310 k
expat-devel x86_64 2.1.0-10.el7_3 base 57 k
libdb-devel x86_64 5.3.21-24.el7 base 38 k
openldap-devel x86_64 2.4.44-15.el7_5 updates 803 k
トランザクションの要約
============================================================================================================================================================================================================================================================================================================================
インストール 1 パッケージ (+7 個の依存関係のパッケージ)
総ダウンロード容量: 1.7 M
インストール容量: 6.8 M
Is this ok [y/d/N]: y
Downloading packages:
(1/8): apr-util-devel-1.5.2-6.el7.x86_64.rpm | 76 kB 00:00:00
(2/8): apr-devel-1.4.8-3.el7_4.1.x86_64.rpm | 188 kB 00:00:00
(3/8): cyrus-sasl-2.1.26-23.el7.x86_64.rpm | 88 kB 00:00:00
(4/8): expat-devel-2.1.0-10.el7_3.x86_64.rpm | 57 kB 00:00:00
(5/8): cyrus-sasl-devel-2.1.26-23.el7.x86_64.rpm | 310 kB 00:00:00
(6/8): libdb-devel-5.3.21-24.el7.x86_64.rpm | 38 kB 00:00:00
(7/8): httpd-devel-2.4.6-80.el7.centos.1.x86_64.rpm | 196 kB 00:00:00
(8/8): openldap-devel-2.4.44-15.el7_5.x86_64.rpm | 803 kB 00:00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
合計 1.6 MB/s | 1.7 MB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : apr-devel-1.4.8-3.el7_4.1.x86_64 1/8
インストール中 : expat-devel-2.1.0-10.el7_3.x86_64 2/8
インストール中 : cyrus-sasl-2.1.26-23.el7.x86_64 3/8
インストール中 : cyrus-sasl-devel-2.1.26-23.el7.x86_64 4/8
インストール中 : openldap-devel-2.4.44-15.el7_5.x86_64 5/8
インストール中 : libdb-devel-5.3.21-24.el7.x86_64 6/8
インストール中 : apr-util-devel-1.5.2-6.el7.x86_64 7/8
インストール中 : httpd-devel-2.4.6-80.el7.centos.1.x86_64 8/8
検証中 : apr-devel-1.4.8-3.el7_4.1.x86_64 1/8
検証中 : httpd-devel-2.4.6-80.el7.centos.1.x86_64 2/8
検証中 : apr-util-devel-1.5.2-6.el7.x86_64 3/8
検証中 : libdb-devel-5.3.21-24.el7.x86_64 4/8
検証中 : openldap-devel-2.4.44-15.el7_5.x86_64 5/8
検証中 : cyrus-sasl-2.1.26-23.el7.x86_64 6/8
検証中 : cyrus-sasl-devel-2.1.26-23.el7.x86_64 7/8
検証中 : expat-devel-2.1.0-10.el7_3.x86_64 8/8
インストール:
httpd-devel.x86_64 0:2.4.6-80.el7.centos.1
依存性関連をインストールしました:
apr-devel.x86_64 0:1.4.8-3.el7_4.1 apr-util-devel.x86_64 0:1.5.2-6.el7 cyrus-sasl.x86_64 0:2.1.26-23.el7 cyrus-sasl-devel.x86_64 0:2.1.26-23.el7 expat-devel.x86_64 0:2.1.0-10.el7_3 libdb-devel.x86_64 0:5.3.21-24.el7 openldap-devel.x86_64 0:2.4.44-15.el7_5
完了しました!
[/c]
無事インストール完了です
バージョン確認
Apacheのバージョンを確認してみます
[c]
[macan@localhost ~]$ httpd -v
Server version: Apache/2.4.6 (CentOS)
[/c]
2.4.6とでてます。ちゃんとインストールできてます。
起動してみる
Apacheのテストページを表示します
[c]
[macan@localhost ~]$ sudo service httpd start
Redirecting to /bin/systemctl start httpd.service
起動しているか確認
[macan@localhost ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2018-11-11 14:45:58 JST; 1min 7s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 16901 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─16901 /usr/sbin/httpd -DFOREGROUND
├─16902 /usr/sbin/httpd -DFOREGROUND
├─16903 /usr/sbin/httpd -DFOREGROUND
├─16904 /usr/sbin/httpd -DFOREGROUND
├─16905 /usr/sbin/httpd -DFOREGROUND
└─16906 /usr/sbin/httpd -DFOREGROUND
[/c]
起動しているので問題なさそうです。
ブラウザでアクセス
さくらのクラウドでこのhttp://IPアドレス/でテストページが見えない場合はfirewallの確認をします。デフォルトで起動しているのでこの場合はポート80を解放してあげてください
firewallの確認・追加
[c]
[macan@localhost ~]$ sudo firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[/c]
sshぐらいしか許可されていないです。というわけでhttpを許可します
[c]
[macan@localhost ~]$ sudo firewall-cmd --permanent --add-service=http
success
[macan@localhost ~]$ sudo firewall-cmd --reload
success
[/c]
リロードもします。これで問題無く見られるようになります。次はpyenvのインストールになります
pyenvのインストール
pyenvはCentOSのシステム全体にpyenvのインストールから実行までを見てやります。
pyenvに必要なライブラリのインストール
gitなどのライブラリをインストールします
[c]
[macan@localhost ~]$ sudo yum install git zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel gcc
多いので割愛
[/c]
必要なライブラリをインストールしました。ここからはrootで作業します
※rootにしたのはsuro権限ではインストールができなかったりなかったりしたためですsudo権限がそのままroot権限の場合は問題無くできます。
[c]
[root@localhost ~]# git clone git://github.com/yyuu/pyenv.git /usr/local/pyenv
Cloning into '/usr/local/pyenv'...
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 16518 (delta 12), reused 23 (delta 8), pack-reused 16484
Receiving objects: 100% (16518/16518), 3.19 MiB | 2.05 MiB/s, done.
Resolving deltas: 100% (11204/11204), done.
[root@localhost ~]# git clone git://github.com/yyuu/pyenv-virtualenv.git /usr/local/pyenv/plugins/pyenv-virtualenv
Cloning into '/usr/local/pyenv/plugins/pyenv-virtualenv'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 2009 (delta 0), reused 0 (delta 0), pack-reused 2005
Receiving objects: 100% (2009/2009), 568.83 KiB | 0 bytes/s, done.
Resolving deltas: 100% (1373/1373), done.
[root@localhost ~]#
[/c]
gitでpyenvに必要なファイルを/usr/local/に置きました。
起動時に読み込まれるファイルを書く
起動時に読み込まれるようにします
[c]
[macan@localhost ~]$ sudo vi /etc/profile.d/pyenv.sh
export PYENV_ROOT="/usr/local/pyenv"
export PATH="${PYENV_ROOT}/bin:${PATH}"
eval "$(pyenv init -)"
[/c]
書いたら保存をしてシェルの再起動をします
[c]
[macan@localhost ~]$ exec $SHELL -l
[/c]
これで完了です。
Pythonのインストール
pyenvでインストール可能なリストを表示します
[c]
[macan@localhost local]$ pyenv install --list
Available versions:
2.1.3
・・・・
3.6.0
3.6-dev
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
3.7.0
3.7-dev
3.7.1
3.8-dev
[/c]
3.6.7をインストールしてみたいと思います。
[c]
[root@localhost ~]# pyenv install 3.6.7
Downloading Python-3.6.7.tar.xz...
-> https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tar.xz
Installing Python-3.6.7...
Installed Python-3.6.7 to /usr/local/pyenv/versions/3.6.7
[/c]
インストールできました。
Pythonのバージョンの切り替えをします
[c]
[root@localhost /]# pyenv global 3.6.7
[/c]
確認
pythonのバージョンを確認します
[c]
[root@localhost ~]# which python
/usr/local/pyenv/shims/python
[root@localhost ~]# python --version
Python 3.6.7
[root@localhost ~]#
[/c]
rootで実行したらこんな感じでした。一般ユーザーで実行してみます
[c]
[macan@localhost ~]$ which python
/usr/local/pyenv/shims/python
[macan@localhost ~]$ python --version
Python 3.6.7
[macan@localhost ~]$
[/c]
問題ないですね
実行してみる
実際に実行してみたいと思います。
まずは書いてみる
[c]
[macan@localhost ~]$ vi hello.py
#coding:UTF-8
print ("こんにちは世界!")
[/c]
つづいて実行
[c]
[macan@localhost ~]$ python hello.py
こんにちは世界!
[/c]
問題無く実行できました
次回はpipのインストールと設定
次はpipのインストールや設定をしていきます
個人支援・寄付について
サイトラボでは個人支援・寄付を受けております。ご協力いただける方はお願いいたします。当サイトではビットコインで受け付けております。
- ビットコイン:3LHnADwZwUbic2L45EnVJEykiG6KfbqrwS