LoginSignup
3
4

More than 5 years have passed since last update.

さくらのレンタルサーバでHTTPS (TLS) 対応のPython 2.7をビルドした記録

Posted at

はじめに

さくらのレンタルサーバにはデフォルトでPython 2.7が入っています(スタンダードプラン、2018年12月現在)が、TLSに対応していません。
そのため、今日のほとんどのHTTPSサイトにアクセスできず、pipなども使えません。つらい。

terminal
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py
Collecting pip
/tmp/tmpQzUTJP/pip.zip/pip/_vendor/urllib3/util/ssl_.py:369: SNIMissingWarning: An HTTPS request has been made, but the SNI (Server Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
/tmp/tmpQzUTJP/pip.zip/pip/_vendor/urllib3/util/ssl_.py:160: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '_ssl.c:507: error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version'),)': /simple/pip/

リンクしているOpenSSLのバージョンが古いようです。

terminal
$ python -c 'import ssl; print ssl.OPENSSL_VERSION'
OpenSSL 0.9.8zf 19 Mar 2015

というわけで、新しいOpenSSLを取ってきて、Pythonを自分でビルドしてみたいと思います。

なお、このサーバはFreeBSDなので、Linuxとはちょっとコマンドが違う部分があります。
例えば、Linuxでいうところのmake(GNU make)は gmake です。

必要なもの

  • Python 2.7のソースコード
  • OpenSSLのソースコード

OpenSSL

さくらのレンタルサーバでHTTPS (TLS) 対応のGitをビルドした記録で紹介している手順と同じです。

terminal
$ openssl version
OpenSSL 1.0.2o  27 Mar 2018

などと出るのですが、ライブラリやヘッダファイルは「0.9.8」であるらしく、このままだとうまくいきません。
よって、まずはOpenSSLを自前で入れます。

terminal
# 作業場所は適当に
$ mkdir -p ~/.local/src
$ cd ~/.local/src
$ wget https://www.openssl.org/source/openssl-1.0.2q.tar.gz
$ tar xvfz openssl-1.0.2q.tar.gz
$ cd openssl-1.0.2q
$ ./config --prefix=${HOME}/.local -fPIC
$ gmake
$ gmake install

バージョンを確認。

terminal
$ ~/.local/bin/openssl version
OpenSSL 1.0.2q  20 Nov 2018

第一段階突破。

Python 2.7

terminal
$ cd ~/.local/src
$ wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
$ tar xvfz Python-2.7.15.tgz
$ cd Python-2.7.15
$ env CXX=/usr/bin/g++ ./configure --prefix=${HOME}/.local --enable-unicode=ucs4
$ gmake
$ gmake install

これで新しいOpenSSLを使ってくれるはず。

terminal
$ ~/.local/bin/python -c 'import ssl; print ssl.OPENSSL_VERSION'
OpenSSL 1.0.2q  20 Nov 2018

キター。
そして get-pip.py も動きます。

terminal
$ ~/.local/bin/python get-pip.py
Collecting pip
  Downloading https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl (1.3MB)
Collecting setuptools
  Downloading https://files.pythonhosted.org/packages/37/06/754589caf971b0d2d48f151c2586f62902d93dc908e2fd9b9b9f6aa3c9dd/setuptools-40.6.3-py2.py3-none-any.whl (573kB)
Collecting wheel
  Downloading https://files.pythonhosted.org/packages/ff/47/1dfa4795e24fd6f93d5d58602dd716c3f101cfd5a77cd9acbe519b44a0a9/wheel-0.32.3-py2.py3-none-any.whl
Installing collected packages: pip, setuptools, wheel
Successfully installed pip-18.1 setuptools-40.6.3 wheel-0.32.3

あとは好きにパッケージを追加できます。

terminal
$ ~/.local/bin/pip install numpy

めでたしめでたし。

3
4
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
3
4