今回さくらレンタルサーバにpythonを導入してみます。
スタンダードプラン以上だとSSHが利用可能なので、スタンダードプラン利用。
2023.07.31現在: さくらレンタルサーバの環境はFreeBSD13
$ uname -a FreeBSD www1564.sakura.ne.jp 13.0-RELEASE-p12
git clone git://github.com/yyuu/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init -)"' >> ~/.bash_profile echo 'export TMPDIR="$HOME/tmp"' >> ~/.bash_profile
$ pyenv -v pyenv 2.3.23-2-gac5efed3
Pythonのバージョン切り替えできるように、virtualenvインストール
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
今回は3.3.4をインストール
CPPFLAGS="-I/usr/local/ssl/include" LDFLAGS="-L/usr/local/ssl/lib" pyenv install 3.3.4 pyenv virtualenv 3.3.4 flask_3.3.4 pyenv global flask_3.3.4 $ pyenv version flask_3.3.4 (set by /home/hogehoge/.pyenv/version)
pip install --upgrade pip
pip install Flask
pip install numpy==1.9.3 pip install cython pip install pandas==0.19.0
.htaccess
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ /hello/index.cgi/$1 [QSA,L] <Files ~ "\.py$"> deny from all </Files>
app.py
#!/home/hogehoge/.pyenv/versions/flask_3.3.4/bin/python # -*- coding: utf-8 -*- from flask import Flask app = Flask(__name__) @app.route('/') def index(): return "Hello World!\n" if __name__ == '__main__': app.run()
index.cgi
#!/home/hogehoge/.pyenv/versions/flask_3.3.4/bin/python import cgitb cgitb.enable() from wsgiref.handlers import CGIHandler from app import app from sys import path path.insert(0, '/hogehoge/www/hello/') from app import app class ProxyFix(object): def __init__(self, app): self.app = app def __call__(self, environ, start_response): # ※要書き換え environ['SERVER_NAME'] = "hogehoge.sakura.ne.jp" environ['SERVER_PORT'] = "80" environ['REQUEST_METHOD'] = "GET" environ['SCRIPT_NAME'] = "" environ['PATH_INFO'] = "/" environ['QUERY_STRING'] = "" environ['SERVER_PROTOCOL'] = "HTTP/1.1" return self.app(environ, start_response) if __name__ == '__main__': app.wsgi_app = ProxyFix(app.wsgi_app) CGIHandler().run(app)
chmod 744 index.cgi chmod 400 .htaccess
python index.cgi