LoginSignup
5
8

More than 5 years have passed since last update.

Python+Flaskでウェブアプリケーション②③

Last updated at Posted at 2017-02-06

前回の続き。
1. 環境の準備(OSセットアップ)
2. 環境の準備(OS内のセットアップ)★
3. FlaskのQuickStartの内容をトレースする(インストールと最低限のセットアップ)★
4. FlaskのTutrialの内容をトレースする(基本的なアプリケーションの作り方の学習)
5. オリジナルの内容を作る

今回は2. ,3.を行った際のメモ。

RootでのSSHアクセス許可

CentOSのデフォルトボックスではrootでsshアクセスはできず、vagrantユーザ⁺秘密鍵での認証を行う必要がある。
これはvagrant ssh-configで確認可能。

2017/01/30 19:26:06|C:\Vagrant\CentOS0-2>vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile C:/Vagrant/CentOS0-2/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

いちいちroot昇格するのが面倒なので、rootでのSSHアクセスを許可しておく。
まずゲスト上のsshd_configで以下の2か所を編集する。
PermitRootLogin yes
PasswordAuthentication yes

ゲストへのアクセスは、以下の設定で可能。
SSH port: 2222
ユーザ: vagrant
秘密鍵: C:\Vagrant\CentOS0-2.vagrant\machines\default\virtualbox\private_key

ログイン後su -でroot昇格(パスワードはvagrant)

[root@cnenyuy5l3c ~]# cp -p /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
[root@cnenyuy5l3c ~]# vi /etc/ssh/sshd_config
[root@cnenyuy5l3c ~]# diff /etc/ssh/sshd_config /etc/ssh/sshd_config.orig
42c42
< PermitRootLogin yes
---
> #PermitRootLogin yes
66c66
< PasswordAuthentication yes
---
> PasswordAuthentication no
[root@cnenyuy5l3c ~]#

次に、.Vagrantfileに以下を記載して、ゲストをリロードする。
config.ssh.username = 'root'
config.ssh.password = 'vagrant'
config.ssh.insert_key = 'true'

2017/01/30 19:41:18|C:\Vagrant\CentOS0-2>vagrant reload
==> default: Attempting graceful shutdown of VM...
    default: Guest communication could not be established! This is usually because
    default: SSH is not running, the authentication information was changed,
    default: or some other networking issue. Vagrant will force halt, if
    default: capable.
==> default: Forcing shutdown of VM...
==> default: Checking if box 'centos/6' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 5000 (guest) => 5000 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: root
    default: SSH auth method: password
==> default: Machine booted and ready!
[default] GuestAdditions 5.1.12 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Vagrant/CentOS0-2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

Pythonのインストール

まずは前提となるモジュールをインストール。
- Development Tools
- openssl-devel
- sqlite-devel

[root@cnenyuy5l3c ~]# yum install openssl-devel
[root@cnenyuy5l3c ~]# yum install sqlite-devel
[root@cnenyuy5l3c ~]# yum groupinstall "Development tools"

次にPythonをインストール。デフォルトでは2.6.6が入っているが、古いので最新(2.7.12)を入れる。

[root@cnenyuy5l3c ~]# python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[root@cnenyuy5l3c ~]# 
[root@cnenyuy5l3c ~]# curl -O https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
[root@cnenyuy5l3c ~]# ls
anaconda-ks.cfg  install.log  install.log.syslog  Python-2.7.12.tar.xz
[root@cnenyuy5l3c ~]# xz -dv Python-2.7.12.tar.xz
[root@cnenyuy5l3c ~]# tar xvf Python-2.7.12.tar
[root@cnenyuy5l3c ~]# cd Python-2.7.12
[root@cnenyuy5l3c Python-2.7.12]# ./configure --prefix=/opt/local
[root@cnenyuy5l3c Python-2.7.12]# make && make altinstall
[root@cnenyuy5l3c Python-2.7.12]# ls -l /usr/bin/python*
-rwxr-xr-x. 2 root root 9032 Aug 18 15:14 /usr/bin/python
lrwxrwxrwx. 1 root root    6 Dec 15 11:11 /usr/bin/python2 -> python
-rwxr-xr-x. 2 root root 9032 Aug 18 15:14 /usr/bin/python2.6

[root@cnenyuy5l3c Python-2.7.12]# rm /usr/bin/python
rm: remove regular file `/usr/bin/python'? y
[root@cnenyuy5l3c Python-2.7.12]# 
[root@cnenyuy5l3c Python-2.7.12]# cp -p /opt/local/bin/python2.7 /usr/bin/python
[root@cnenyuy5l3c Python-2.7.12]# python
Python 2.7.12 (default, Jan 30 2017, 11:04:49) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
[root@cnenyuy5l3c Python-2.7.12]# 

注意点として、yumは2.6でしか動かないため、yumだけ2.6を参照するように設定しておく。

[root@cnenyuy5l3c Python-2.7.12]# which yum
/usr/bin/yum
[root@cnenyuy5l3c Python-2.7.12]# vi /usr/bin/yum
[root@cnenyuy5l3c Python-2.7.12]# head -1 /usr/bin/yum
#!/usr/bin/python2.6

あとは開発環境を分離しておくために、virtualenv(とそのインストールに使うpip)をインストールする。

[root@cnenyuy5l3c Python-2.7.12]# curl -kL https://bootstrap.pypa.io/get-pip.py | /opt/local/bin/python2.7
[root@cnenyuy5l3c Python-2.7.12]# ls /opt/local/bin/
2to3  easy_install  easy_install-2.7  idle  pip  pip2  pip2.7  pydoc  python2.7  python2.7-config  smtpd.py  wheel
[root@cnenyuy5l3c Python-2.7.12]# cd ~
[root@cnenyuy5l3c ~]# vi .bashrc
[root@cnenyuy5l3c ~]# cat .bashrc

## CUSTOM START
export PATH=$PATH:/opt/local/bin/
## CUSTOM END

[root@cnenyuy5l3c ~]# 
[root@cnenyuy5l3c ~]# source .bashrc
[root@cnenyuy5l3c ~]# /opt/local/bin/pip2.7 install virtualenv

pipのパスを通しておく。最後のここまでのテスト。

[root@cnenyuy5l3c ~]# mkdir testpj
[root@cnenyuy5l3c ~]# cd testpj
[root@cnenyuy5l3c testpj]# 
[root@cnenyuy5l3c testpj]# 
[root@cnenyuy5l3c testpj]# virtualenv env
New python executable in /root/testpj/env/bin/python2.7
Also creating executable in /root/testpj/env/bin/python
Installing setuptools, pip, wheel...done.
[root@cnenyuy5l3c testpj]# ls
env
[root@cnenyuy5l3c testpj]# 
[root@cnenyuy5l3c testpj]# . env/bin/activate
(env) [root@cnenyuy5l3c testpj]# 
(env) [root@cnenyuy5l3c testpj]# python
Python 2.7.12 (default, Jan 30 2017, 11:04:49) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> quit()
(env) [root@cnenyuy5l3c testpj]# 
(env) [root@cnenyuy5l3c testpj]# deactivate
[root@cnenyuy5l3c testpj]#

Python2.7でvirtualenvが起動および停止された。

Flaskのインストール

virtualenvにflaskをインストール。

[root@cnenyuy5l3c testpj]# . env/bin/activate
(env) [root@cnenyuy5l3c testpj]#
(env) [root@cnenyuy5l3c testpj]# easy_install Flask

以下のようなテスト用appファイルを作成して、起動してみる。

app.py
from flask import Flask
app = Flask(__name__)

@app.route(/)
def hello_world():
    return "Hello World!"

if __name__ == '__main__':
    app.run(host='0.0.0.0')

アプリケーションを起動。

(env) [root@cnenyuy5l3c testpj]# python app.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

ホストマシン上で、5000にアクセスして動作確認。
20170130_008.jpg

今回はここまで。

5
8
2

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
5
8