4
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-01-30

目論見

Pythonを使ってウェブアプリケーションを自作したい。それにあたり、Flaskをフレームワークとして使用する。
ゼロから始めることになるため、以下のステップで進める。

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

今回は1. 環境の準備(OSセットアップ)を行った際のメモ。なのでPythonもFlaskも出てきません。。

環境

Python: 2.7.12 (2.7のlatest)
フレームワーク: Flask 0.12
(テンプレートエンジン: jinja2)

ホスト:
OS: CentOS6.8 (on VirtualBox)

OSセットアップ

前提条件

  • 仮想化ソフト: Oracle VirtualBox 5.1.12
  • 仮想環境管理ソフト: Vagrant1.9.1
  • ホストマシン: Windows10
    上記は準備(インストール)されている前提。

ホストマシン作成

まずはvagrantで空のcentosを作成する。

2017/01/30 18:00:23|C:\Vagrant>mkdir CentOS0-2
2017/01/30 18:00:38|C:\Vagrant>cd CentOS0-2
2017/01/30 18:00:41|C:\Vagrant\CentOS0-2>dir
 ドライブ C のボリューム ラベルは OS です
 ボリューム シリアル番号は F4DB-7972 です

 C:\Vagrant\CentOS0-2 のディレクトリ

2017/01/30  18:00    <DIR>          .
2017/01/30  18:00    <DIR>          ..
               0 個のファイル                   0 バイト
               2 個のディレクトリ  317,036,650,496 バイトの空き領域

2017/01/30 18:00:43|C:\Vagrant\CentOS0-2>vagrant box list
bento/ubuntu-14.04 (virtualbox, 2.3.1)
centos/6           (virtualbox, 1611.01)
ubuntu/trusty64    (virtualbox, 20161214.0.0)

2017/01/30 18:08:33|C:\Vagrant\CentOS0-2>
2017/01/30 18:08:34|C:\Vagrant\CentOS0-2>vagrant init centos/6
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

上記はすでにCentOSのボックスを持っている想定だが、ない場合は以下でボックスをダウンロードしておくこと。

2016/12/27 20:24:49|C:\Users\ikedak2>vagrant box add centos/6
==> box: Loading metadata for box 'centos/6'
    box: URL: https://atlas.hashicorp.com/centos/6
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) libvirt
2) virtualbox
3) vmware_fusion
4) vmware_workstation

Enter your choice: 2
==> box: Adding box 'centos/6' (v1611.01) for provider: virtualbox
    box: Downloading: https://atlas.hashicorp.com/centos/boxes/6/versions/1611.01/providers/virtualbox.box
    box: Progress: 100% (Rate: 528k/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos/6' (v1611.01) for 'virtualbox'!

initすると、実行したフォルダに.Vagrantfileが作成される。このファイルはVMの各種設定を定義したもの。以下の行を追記しておく。

config.vm.network "forwarded_port", guest: 5000, host: 5000
config.vm.synced_folder ".", "/vagrant", type:nil

1つ目は、ゲスト(CentOS)上のポート5000をホストマシン(Windows10)のポート5000にフォワードする設定。Flaskのウェブサーバが5000を使用するので、あらかじめ入れておく。
2つ目は、ゲスト上の/vagrantというディレクトリをホストマシンの起動フォルダと共有させる設定。
デフォルトの設定(以下)では、../dataがないケースがあってエラーになるため、入れておく。またtypeはファイル共有の手法の指定で、nilはOSのシステムのデフォルト(このケースではvirtualbox)になる。
config.vm.synced_folder "../data", "/vagrant_data"

また、vbguestというプラグインがインストールされているかも確認しておく。

2017/01/30 18:37:43|C:\Vagrant\CentOS0-2>vagrant plugin list
vagrant-share (1.1.6, system)
vagrant-vbguest (0.13.0)
vagrant-vbox-snapshot (0.0.10)

これがないと起動(vagrant up)時にエラーになる。

Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

mount -t vboxsf -o uid=500,gid=500 vagrant /vagrant

The error output from the command was:

mount: unknown filesystem type 'vboxsf'
![image](https://qiita-image-store.s3.amazonaws.com/0/159980/5041dc12-02c2-d3d2-6b28-33d9b2f63bbe.png)

ここまでできていれば、OSを起動する。
(冗長ですが、一応ログをそのまま記載)

2017/01/30 18:37:48|C:\Vagrant\CentOS0-2>
2017/01/30 18:37:49|C:\Vagrant\CentOS0-2>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/6'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/6' is up to date...
==> default: Setting the name of the VM: CentOS0-2_default_1485769520180_74017
==> 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: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
[default] No installation found.
Loaded plugins: fastestmirror, security
Setting up Install Process
Package binutils-2.20.51.0.2-5.44.el6.x86_64 already installed and latest version
Package 1:make-3.81-23.el6.x86_64 already installed and latest version
Package 4:perl-5.10.1-141.el6_7.1.x86_64 already installed and latest version
Package bzip2-1.0.5-7.el6_0.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.4.7-17.el6 will be installed
--> Processing Dependency: libgomp = 4.4.7-17.el6 for package: gcc-4.4.7-17.el6.x86_64
--> Processing Dependency: cpp = 4.4.7-17.el6 for package: gcc-4.4.7-17.el6.x86_64
--> Processing Dependency: glibc-devel >= 2.2.90-12 for package: gcc-4.4.7-17.el6.x86_64
--> Processing Dependency: cloog-ppl >= 0.15 for package: gcc-4.4.7-17.el6.x86_64
--> Processing Dependency: libgomp.so.1()(64bit) for package: gcc-4.4.7-17.el6.x86_64
---> Package kernel-devel.x86_64 0:2.6.32-642.11.1.el6 will be installed
--> Running transaction check
---> Package cloog-ppl.x86_64 0:0.15.7-1.2.el6 will be installed
--> Processing Dependency: libppl_c.so.2()(64bit) for package: cloog-ppl-0.15.7-1.2.el6.x86_64
--> Processing Dependency: libppl.so.7()(64bit) for package: cloog-ppl-0.15.7-1.2.el6.x86_64
---> Package cpp.x86_64 0:4.4.7-17.el6 will be installed
--> Processing Dependency: libmpfr.so.1()(64bit) for package: cpp-4.4.7-17.el6.x86_64
---> Package glibc-devel.x86_64 0:2.12-1.192.el6 will be installed
--> Processing Dependency: glibc-headers = 2.12-1.192.el6 for package: glibc-devel-2.12-1.192.el6.x86_64
--> Processing Dependency: glibc-headers for package: glibc-devel-2.12-1.192.el6.x86_64
---> Package libgomp.x86_64 0:4.4.7-17.el6 will be installed
--> Running transaction check
---> Package glibc-headers.x86_64 0:2.12-1.192.el6 will be installed
--> Processing Dependency: kernel-headers >= 2.2.1 for package: glibc-headers-2.12-1.192.el6.x86_64
--> Processing Dependency: kernel-headers for package: glibc-headers-2.12-1.192.el6.x86_64
---> Package mpfr.x86_64 0:2.4.1-6.el6 will be installed
---> Package ppl.x86_64 0:0.10.2-11.el6 will be installed
--> Running transaction check
---> Package kernel-headers.x86_64 0:2.6.32-642.13.1.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch        Version                     Repository    Size
================================================================================
Installing:
 gcc                 x86_64      4.4.7-17.el6                base          10 M
 kernel-devel        x86_64      2.6.32-642.11.1.el6         updates       11 M
Installing for dependencies:
 cloog-ppl           x86_64      0.15.7-1.2.el6              base          93 k
 cpp                 x86_64      4.4.7-17.el6                base         3.7 M
 glibc-devel         x86_64      2.12-1.192.el6              base         988 k
 glibc-headers       x86_64      2.12-1.192.el6              base         617 k
 kernel-headers      x86_64      2.6.32-642.13.1.el6         updates      4.4 M
 libgomp             x86_64      4.4.7-17.el6                base         134 k
 mpfr                x86_64      2.4.1-6.el6                 base         157 k
 ppl                 x86_64      0.10.2-11.el6               base         1.3 M

Transaction Summary
================================================================================
Install      10 Package(s)

Total download size: 32 M
Installed size: 64 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           226 kB/s |  32 MB     02:24
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Importing GPG key 0xC105B9DE:
 Userid : CentOS-6 Key (CentOS 6 Official Signing Key) <centos-6-key@centos.org>
 Package: centos-release-6-8.el6.centos.12.3.x86_64 (@anaconda-CentOS-201605220104.x86_64/6.8)
 From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : kernel-headers-2.6.32-642.13.1.el6.x86_64                   1/10
  Installing : glibc-headers-2.12-1.192.el6.x86_64                         2/10
  Installing : glibc-devel-2.12-1.192.el6.x86_64                           3/10
  Installing : libgomp-4.4.7-17.el6.x86_64                                 4/10
  Installing : mpfr-2.4.1-6.el6.x86_64                                     5/10
  Installing : cpp-4.4.7-17.el6.x86_64                                     6/10
  Installing : ppl-0.10.2-11.el6.x86_64                                    7/10
  Installing : cloog-ppl-0.15.7-1.2.el6.x86_64                             8/10
  Installing : gcc-4.4.7-17.el6.x86_64                                     9/10
  Installing : kernel-devel-2.6.32-642.11.1.el6.x86_64                    10/10
  Verifying  : glibc-devel-2.12-1.192.el6.x86_64                           1/10
  Verifying  : glibc-headers-2.12-1.192.el6.x86_64                         2/10
  Verifying  : kernel-devel-2.6.32-642.11.1.el6.x86_64                     3/10
  Verifying  : ppl-0.10.2-11.el6.x86_64                                    4/10
  Verifying  : mpfr-2.4.1-6.el6.x86_64                                     5/10
  Verifying  : gcc-4.4.7-17.el6.x86_64                                     6/10
  Verifying  : libgomp-4.4.7-17.el6.x86_64                                 7/10
  Verifying  : kernel-headers-2.6.32-642.13.1.el6.x86_64                   8/10
  Verifying  : cpp-4.4.7-17.el6.x86_64                                     9/10
  Verifying  : cloog-ppl-0.15.7-1.2.el6.x86_64                            10/10

Installed:
  gcc.x86_64 0:4.4.7-17.el6      kernel-devel.x86_64 0:2.6.32-642.11.1.el6

Dependency Installed:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6
  cpp.x86_64 0:4.4.7-17.el6
  glibc-devel.x86_64 0:2.12-1.192.el6
  glibc-headers.x86_64 0:2.12-1.192.el6
  kernel-headers.x86_64 0:2.6.32-642.13.1.el6
  libgomp.x86_64 0:4.4.7-17.el6
  mpfr.x86_64 0:2.4.1-6.el6
  ppl.x86_64 0:0.10.2-11.el6

Complete!
Copy iso file C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso into the box /tmp/VBoxGuestAdditions.iso
Installing Virtualbox Guest Additions 5.1.12 - guest version is unknown
Verifying archive integrity... All good.
Uncompressing VirtualBox 5.1.12 Guest Additions for Linux...........
VirtualBox Guest Additions installer
Copying additional installer modules ...
Installing additional modules ...
vboxadd.sh: Building Guest Additions kernel modules.
vboxadd.sh: Starting the VirtualBox Guest Additions.

Could not find the X.Org or XFree86 Window System, skipping.

==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/Vagrant/CentOS0-2

2017/01/30 18:50:06|C:\Vagrant\CentOS0-2>

今回はここまで。次回は中身のセットアップ。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?