ConoHaで借りているVPSにpython3系とdjangoのモジュールを入れたときの作業内容をメモしておきます。
どうしてモジュールをインストールしたか?
ConoHaのVPSにはdjangoを使うために用意されたサーバーイメージがあるのですが、pythonが3系でpipが使えなさそうだったのでCentOS7のサーバイメージに1から作りました。
djangoの追加モジュールをpip経由でインストールして(django-allauthとか)使いたかったのと、1から環境構築する経験をしておいてもいいかなと。
色々調べながらやったのでページを紹介しつつ作業していきます。
python3系のインストール
CentOS7ではデフォルトでpythonがインストールされているのですが、2系ではなく3系です。
djangoは2系でも使えるのですが、これからのアップデートなどを考えても3系を使うほうがいいでしょう。
しかしCentOS7にはpython3系が入っていないので追加でインストールします。
ちなみにこちらのサイト様を参考にしました。
python3系モジュールのインストール
まずはIUSのリポジトリをインストールします。
#yum install -y https://centos7.iuscommunity.org/ius-release.rpm
インストールができたら、python3系のモジュールをインストールします。
このとき、どういうモジュールがリポジトリ上にあるか調べるために、searchコマンドで一応見ておきましょう。
#yum search python3
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: ftp.iij.ad.jp
* epel: nrt.edge.kernel.org
* epel-debuginfo: nrt.edge.kernel.org
* epel-source: nrt.edge.kernel.org
* extras: ftp.iij.ad.jp
* updates: ftp.iij.ad.jp
ius | 1.3 kB 00:00:00
ius/x86_64/primary | 144 kB 00:00:00
ius 704/704
================================================= N/S matched: python3 =================================================
boost-python3-debuginfo.x86_64 : Debug information for package boost-python3
boost-python36-static.x86_64 : The Python3 Boost C++ static development libraries
libpeas-loader-python3-debuginfo.x86_64 : Debug information for package libpeas-loader-python3
python3-Cython-debuginfo.x86_64 : Debug information for package python3-Cython
python3-PyYAML-debuginfo.x86_64 : Debug information for package python3-PyYAML
python3-cairo-debuginfo.x86_64 : Debug information for package python3-cairo
python3-cffi-debuginfo.x86_64 : Debug information for package python3-cffi
・・・(中略)・・・
python36u-redis.noarch : Python interface to the Redis key-value store
python36u-setproctitle.x86_64 : Python module to customize a process title
python36u-setuptools.noarch : Easily build and distribute Python packages
python36u-tkinter.x86_64 : A GUI toolkit for Python
python36u-tools.x86_64 : A collection of tools included with Python including 2to3 and idle
shiboken-python36-devel.x86_64 : Development files for shiboken
shiboken-python36-libs.x86_64 : CPython bindings generator for C++ libraries - shared library
uwsgi-plugin-python34.x86_64 : uWSGI - Plugin for Python 3.4 support
uwsgi-plugin-python34-gevent.x86_64 : uWSGI - Plugin for Python 3.4 GEvent support
uwsgi-plugin-python34u.x86_64 : uWSGI - Plugin for Python support
uwsgi-plugin-python35u.x86_64 : uWSGI - Plugin for Python support
uwsgi-plugin-python36.x86_64 : uWSGI - Plugin for Python 3.6 support
uwsgi-plugin-python36-gevent.x86_64 : uWSGI - Plugin for Python 3.6 GEvent support
uwsgi-plugin-python36-tornado.x86_64 : uWSGI - Plugin for Tornado (Python 3.6) support
uwsgi-plugin-python36u.x86_64 : uWSGI - Plugin for Python support
znc-modpython.x86_64 : Python3 module for ZNC
Name and summary matches only, use "search all" for everything.
#yum install python36u python36u-libs python36u-devel python36u-pip
もしyumでinstallできなかった場合は、指定したモジュールがsearchの検索結果にあるか見てみましょう。
リンクの貼り直し
次にリンクを張り直します。
インストールしたpython3.6をpython3にして、pythonコマンドのリンクを削除。
それからpython3をpythonにリンクして、最後にpipかな?
# ln -s /bin/python3.6 /bin/python3
# unlink /bin/python
# ln -s /bin/python3 /bin/python
# ln -s /bin/pip3.6 /bin/pip
リンクを張り直したら、pythonのバージョンを確認しましょう。
# python --version
Python 3.6.8
# pip --version
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)
問題なしですね!
yum を使えるようにする
さてリンクを張り直すと、このシステムでは「python」というコマンドはpython2系ではなくて3系が動作するようになります。
これによってyumがうまく動けなくなるので、設定ファイルを変更します。
</usr/bin/yumファイル>
#!/usr/bin/python2 <---pythonからpython2に変更する
import sys
try:
import yum
except ImportError:
print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
・・・(省略)・・・
また同じようにこちらのファイルも変更します。
</usr/libexec/urlgrabber-ext-downファイル>
#! /usr/bin/python2 <---pythonからpython2に変更する
# A very simple external downloader
# Copyright 2011-2012 Zdenek Pavlas
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
django関連モジュールのインストール
ここまで来たらあとは簡単です。
djangoのモジュールをそのまま入れるだけなのでサクッと行きます。
# pip install django
# pip install django-allauth
# pip install django-bootstrap4
インストールしたのは、django本体、djangoのソーシャル認証アドイン、djangoとbootstrap4の連携用アドインの3つです。
djangoのソーシャル認証アドインを使ったtwitter認証についてはこちらの記事にまとめているのでぜひ参考にしてください。
コメント