ラベル PaaS の投稿を表示しています。 すべての投稿を表示
ラベル PaaS の投稿を表示しています。 すべての投稿を表示

2016年2月8日月曜日

OpenShiftでPHP Gearと一緒にMySQLを使えるようにする

このエントリーをはてなブックマークに追加
PHP動いているんだったらやっぱりDBも使いたいよね。ってことでMySQLを動かしてみようと。
公式のDocumentにはこんな感じで書いてあった。
If you want to add a database to an existing application the command is similar:
$ rhc cartridge-add <dbcartridge> --app <myappname>
This command will also spin up your database server and insert the environment variables.
前回のHerokuよりも無料枠が良さげなOpenShiftでPHPをデプロイするmyphpappというアプリ名で作ったので、これで使えるようにしてみる。
まずは、使えるカートリッジを確認する。
$ rhc cartridge list
RSA 1024 bit CA certificates are loaded due to old openssl compatibility
jbossas-7                JBoss Application Server 7              web
jboss-dv-6.1.0 (!)       JBoss Data Virtualization 6             web
jbosseap-6 (*)           JBoss Enterprise Application Platform 6 web
jboss-unified-push-1 (!) JBoss Unified Push Server 1.0.0.Beta1   web
jboss-unified-push-2 (!) JBoss Unified Push Server 1.0.0.Beta2   web
jenkins-1                Jenkins Server                          web
nodejs-0.10              Node.js 0.10                            web
perl-5.10                Perl 5.10                               web
php-5.3                  PHP 5.3                                 web
php-5.4                  PHP 5.4                                 web
zend-6.1                 PHP 5.4 with Zend Server 6.1            web
python-2.6               Python 2.6                              web
python-2.7               Python 2.7                              web
python-3.3               Python 3.3                              web
ruby-1.8                 Ruby 1.8                                web
ruby-1.9                 Ruby 1.9                                web
ruby-2.0                 Ruby 2.0                                web
jbossews-1.0             Tomcat 6 (JBoss EWS 1.0)                web
jbossews-2.0             Tomcat 7 (JBoss EWS 2.0)                web
jboss-vertx-2.1 (!)      Vert.x 2.1                              web
jboss-wildfly-10 (!)     WildFly Application Server 10           web
jboss-wildfly-8 (!)      WildFly Application Server 8.2.1.Final  web
jboss-wildfly-9 (!)      WildFly Application Server 9            web
diy-0.1                  Do-It-Yourself 0.1                      web
cron-1.4                 Cron 1.4                                addon
jenkins-client-1         Jenkins Client                          addon
mongodb-2.4              MongoDB 2.4                             addon
mysql-5.1                MySQL 5.1                               addon
mysql-5.5                MySQL 5.5                               addon
phpmyadmin-4             phpMyAdmin 4.0                          addon
postgresql-8.4           PostgreSQL 8.4                          addon
postgresql-9.2           PostgreSQL 9.2                          addon
rockmongo-1.1            RockMongo 1.1                           addon
switchyard-0             SwitchYard 0.8.0                        addon
haproxy-1.4              Web Load Balancer                       addon

Note: Web cartridges can only be added to new applications.

(*) denotes a cartridge with additional usage costs.

(!) denotes a cartridge that will not receive automatic security updates.
mysql-5.5というのがあったので、これが使えそうだ。
なので、作成してみる。
$ rhc cartridge-add mysql-5.5 --app myphpapp
RSA 1024 bit CA certificates are loaded due to old openssl compatibility
Adding mysql-5.5 to application 'myphpapp' ... done

mysql-5.5 (MySQL 5.5)
---------------------
  Gears:          Located with php-5.4
  Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/
  Database Name:  myphpapp
  Password:       ***********
  Username:       ***********

MySQL 5.5 database added.  Please make note of these credentials:

       Root User: ***********
   Root Password: ***********
   Database Name: myphpapp

Connection URL: mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/

You can manage your new MySQL database by also embedding phpmyadmin.
The phpmyadmin username and password will be the same as the MySQL credentials above.
MySQLへの接続は環境変数に入っているようだ。myphpappにSSHで接続して、envで確認してみる。
$ rhc ssh myphpapp

\> env | grep OPENSHIFT_MYSQL -
OPENSHIFT_MYSQL_DIR=xxxxxx
OPENSHIFT_MYSQL_DB_PORT=xxxxxx
OPENSHIFT_MYSQL_DB_HOST=xxxxxx
OPENSHIFT_MYSQL_DB_PASSWORD=xxxxxx
OPENSHIFT_MYSQL_IDENT=xxxxxx
OPENSHIFT_MYSQL_DB_USERNAME=xxxxxx
OPENSHIFT_MYSQL_DB_SOCKET=xxxxxx
OPENSHIFT_MYSQL_DB_URL=xxxxxx
OPENSHIFT_MYSQL_VERSION=5.5
OPENSHIFT_MYSQL_DB_LOG_DIR=xxxxxx
OPENSHIFT_MYSQL_LD_LIBRARY_PATH_ELEMENT=xxxxxx
接続に必要な環境変数がいっぱい出てきたので、これを元にSSHでMySQLへログインできるのかやってみる。
\> mysql -u ($OPENSHIFT_MYSQL_DB_USERNAME) -p -h ($OPENSHIFT_MYSQL_DB_HOST)
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.45 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
普通にデータベースとか見えるのかな。
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myphpapp           |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.01 sec)

mysql> use myphpapp;
Database changed
mysql> show tables;
Empty set (0.00 sec)
結構普通に使えて超便利!

2016年2月7日日曜日

OpenShiftってSSHでログインもできるんだ

このエントリーをはてなブックマークに追加
Remote Connection (SSH)を見てみたら普通にSSHでGearにログインできるみたいなのでやってみた。


$ rhc ssh myphpapp
RSA 1024 bit CA certificates are loaded due to old openssl compatibility
Connecting to 56b61d5c89f5cf92a000003f@myphpapp-xxxxxx.rhcloud.com ...

    *********************************************************************

    You are accessing a service that is for use only by authorized users.
    If you do not have authorization, discontinue use at once.
    Any use of the services is subject to the applicable terms of the
    agreement which can be found at:
    https://www.openshift.com/legal

    *********************************************************************

    Welcome to OpenShift shell

    This shell will assist you in managing OpenShift applications.

    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!
    Shell access is quite powerful and it is possible for you to
    accidentally damage your application.  Proceed with care!
    If worse comes to worst, destroy your application with "rhc app delete"
    and recreate it
    !!! IMPORTANT !!! IMPORTANT !!! IMPORTANT !!!

    Type "help" for more info.


[myphpapp-xxxxxx.rhcloud.com 56b61d5c89f5cf92a000003f]\>
でも使えるコマンドは限られてるみたい。
\> help
Help menu: The following commands are available to help control your openshift
application and environment.

gear            control your application (start, stop, restart, etc)
                or deps with --cart      (gear start --cart mysql-5.1)
tail_all        tail all log files
export          list available environment variables
rm              remove files / directories
ls              list files / directories
ps              list running applications
kill            kill running applications
mysql           interactive MySQL shell
mongo           interactive MongoDB shell
psql            interactive PostgreSQL shell
quota           list disk usage

Deprecated:
ctl_app         control your application (start, stop, restart, etc)
ctl_all         control application and deps like mysql in one command
簡単なの使ってみよう。
[myphpapp-xxxxxx.rhcloud.com 56b61d5c89f5cf92a000003f]\> ls
app-deployments  app-root  git php
[myphpapp-xxxxxx.rhcloud.com 56b61d5c89f5cf92a000003f]\> pwd
/var/lib/openshift/56b61d5c89f5cf92a000003f
ファイルやディレクトリは見える。
[myphpapp-xxxxxx.rhcloud.com 56b61d5c89f5cf92a000003f]\> ps -aef
UID         PID   PPID  C STIME TTY          TIME CMD
2385     182328 181214  0 13:13 ?        00:00:00 sshd: 56b61d5c89f5cf92a000003f@pts/1
2385     182339 182328  0 13:13 pts/1    00:00:00 /bin/bash --init-file /usr/bin/rhcsh -i
2385     189163 182339  2 13:14 pts/1    00:00:00 ps -aef
2385     423174      1  0 12:12 ?        00:00:00 /usr/sbin/httpd -c Include /var/lib/openshift/56b61d5c89f5cf92a000003f
2385     423175      1  0 12:12 ?        00:00:00 /usr/bin/logshifter -tag php
2385     423184 423174  0 12:12 ?        00:00:00 tee
2385     423185 423174  0 12:12 ?        00:00:00 tee
2385     423186 423174  0 12:12 ?        00:00:00 /usr/sbin/httpd -c Include /var/lib/openshift/56b61d5c89f5cf92a000003f
2385     434288 423174  0 12:14 ?        00:00:00 /usr/sbin/httpd -c Include /var/lib/openshift/56b61d5c89f5cf92a000003f
動いているプロセスも見えるみたい。
[myphpapp-xxxxxx.rhcloud.com 56b61d5c89f5cf92a000003f]\> cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.7 (Santiago)
OSはRedHat EnterPrise 6.7か。
[myphpapp-xxxxxx.rhcloud.com 56b61d5c89f5cf92a000003f]\> env
MANPATH=/opt/rh/php54/root/usr/share/man:
OPENSHIFT_SECRET_TOKEN=wD_1nmlzQ0FN8WHQ_Xs5hsUAiermXB5i7GVLGFP1N7w6_wGfnAT9PZNgwecEUpOGAJIp1ER8Ao-lGBwx8dU_8Q9fqn-R482B6JF9aySaXIXp9ScZZ8xY0Q_MeqZPzFJ1
OPENSHIFT_PHP_IDENT=redhat:php:5.4:0.0.28
OPENSHIFT_GEAR_MEMORY_MB=512
SELINUX_ROLE_REQUESTED=
OPENSHIFT_DEPLO
:
:
helpには書いてないけどenvで環境変数も見えるみたい。

Herokuよりも無料枠が良さげなOpenShiftでPHPをデプロイする

このエントリーをはてなブックマークに追加
Herokuよりも無料枠が良さげなOpenShiftをつかってみるでアカウントとGearを作ってPHP Cartridgeを用意した。

今度はGetting Started with PHPを辿ってブラウザで確認できるまでやってみる。

アカウントは作ったけどrhcコマンドはインストールしていないのでリンク先からインストールする。僕の場合はMac版をインストール。

$ sudo gem install rhc
Password:
Fetching: net-ssh-2.9.2.gem (100%)
Successfully installed net-ssh-2.9.2
Fetching: net-scp-1.2.1.gem (100%)
Successfully installed net-scp-1.2.1
Fetching: net-ssh-gateway-1.2.0.gem (100%)
Successfully installed net-ssh-gateway-1.2.0
Fetching: net-ssh-multi-1.2.1.gem (100%)
Successfully installed net-ssh-multi-1.2.1
Fetching: archive-tar-minitar-0.5.2.gem (100%)
Successfully installed archive-tar-minitar-0.5.2
Fetching: highline-1.6.21.gem (100%)
Successfully installed highline-1.6.21
Fetching: commander-4.2.1.gem (100%)
Successfully installed commander-4.2.1
Fetching: httpclient-2.7.1.gem (100%)
Successfully installed httpclient-2.7.1
Fetching: open4-1.3.4.gem (100%)
Successfully installed open4-1.3.4
Fetching: rhc-1.38.4.gem (100%)
===========================================================================

If this is your first time installing the RHC tools, please run 'rhc setup'

===========================================================================
Successfully installed rhc-1.38.4
Parsing documentation for net-ssh-2.9.2
Installing ri documentation for net-ssh-2.9.2
Parsing documentation for net-scp-1.2.1
Installing ri documentation for net-scp-1.2.1
Parsing documentation for net-ssh-gateway-1.2.0
Installing ri documentation for net-ssh-gateway-1.2.0
Parsing documentation for net-ssh-multi-1.2.1
Installing ri documentation for net-ssh-multi-1.2.1
Parsing documentation for archive-tar-minitar-0.5.2
Installing ri documentation for archive-tar-minitar-0.5.2
Parsing documentation for highline-1.6.21
Installing ri documentation for highline-1.6.21
Parsing documentation for commander-4.2.1
Installing ri documentation for commander-4.2.1
Parsing documentation for httpclient-2.7.1
Installing ri documentation for httpclient-2.7.1
^@Parsing documentation for open4-1.3.4
Installing ri documentation for open4-1.3.4
Parsing documentation for rhc-1.38.4
Installing ri documentation for rhc-1.38.4
10 gems installed

初めてのインストールだったら'rhc setup'を実行しろと書いてあるので素直にやってみる。

$ rhc setup
OpenShift Client Tools (RHC) Setup Wizard

This wizard will help you upload your SSH keys, set your application namespace, and check that other programs like Git
are properly installed.

If you have your own OpenShift server, you can specify it now. Just hit enter to use the server for OpenShift Online:
openshift.redhat.com.
Enter the server hostname: |openshift.redhat.com|

サブドメインでそのまま使っているのでEnter。

You can add more servers later using 'rhc server'.
RSA 1024 bit CA certificates are loaded due to old openssl compatibility

Login to openshift.redhat.com: xxxxxxxx@hogehoge.com
Password: *********

素直に作成したアカウントのメールアドレスとパスワードを入力。

OpenShift can create and store a token on disk which allows to you to access the server without using your password. The
key is stored in your home directory and should be kept secret.  You can delete the key at any time by running 'rhc
logout'.
Generate a token now? (yes|no) yes

鍵を作っておくかと聞かれるので作っておく。

Generating an authorization token for this client ... lasts about 1 month

Saving configuration to /Users/xxxxx/.openshift/express.conf ... done

No SSH keys were found. We will generate a pair of keys for you.

    Created: /Users/xxxxxxx/.ssh/id_rsa.pub

Your public SSH key must be uploaded to the OpenShift server to access code.  Upload now? (yes|no) yes

鍵を作った後、今、サーバにアップロードをするか聞かれるのでyes

Since you do not have any keys associated with your OpenShift account, your new key will be uploaded as the 'default'
key.

Uploading key 'default' ... done

Checking for git ... found git version 2.5.4 (Apple Git-61)

Checking common problems .. done

Checking for a domain ... xxxxxxxxx

Checking for applications ... found 1

  php http://php-xxxxxxx.rhcloud.com/

  You are using 1 of 3 total gears
  The following gear sizes are available to you: small

Your client tools are now configured.

とりあえずrhcコマンドの準備は整った。

Once that is completed, open a terminal on a local machine and change into the directory where you would like the OpenShift application source code to be located. At the command prompt, enter the following command to create a PHP application:

$ rhc app create myphpapp php-5.4

アプリケーションを作成しよう。
今回は~/Documentフォルダの下に"OpenShift Project"を作成した上で、そこに移動しコマンドを入力する。

$ rhc app create myphpapp php-5.4
RSA 1024 bit CA certificates are loaded due to old openssl compatibility
Application Options
-------------------
Domain:     xxxxxxxxxx
Cartridges: php-5.4
Gear Size:  default
Scaling:    no

Creating application 'myphpapp' ... done


Waiting for your DNS name to be available ... done

Cloning into 'myphpapp'...
The authenticity of host 'myphpapp-xxxxxxxxxx.rhcloud.com (52.91.58.98)' can't be established.
RSA key fingerprint is SHA256:FeJCkIUL7D41FMGNtM5NYM4XoboxZGYEqH0WcAmOX+I.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'myphpapp-xxxxxxxxxx.rhcloud.com,52.91.58.98' (RSA) to the list of known hosts.

Your application 'myphpapp' is now available.

  URL:        http://myphpapp-xxxxxxxxxx.rhcloud.com/
  SSH to:     56b61d5c89f5cf92a000003f@myphpapp-xxxxxxxxxx.rhcloud.com
  Git remote: ssh://56b61d5c89f5cf92a000003f@myphpapp-xxxxxxxxxx.rhcloud.com/~/git/myphpapp.git/
  Cloned to:  /Users/xxxxxx/Documents/OpenShift Project/myphpapp

Run 'rhc show-app myphpapp' for more details about your app.

これでリモートリポジトリとアプリが作成された。ローカルの中にもmyphpappというディレクトリができているので、中を見てみよう。

$ cd myphpapp/
$ ls -aFCl
total 80
drwxr-xr-x   5 xxxxxx  staff    170  2  7 01:22 ./
drwxr-xr-x   3 xxxxxx  staff    102  2  7 01:22 ../
drwxr-xr-x  13 xxxxxx  staff    442  2  7 01:22 .git/
drwxr-xr-x   7 xxxxxx  staff    238  2  7 01:22 .openshift/
-rw-r--r--   1 xxxxxx  staff  39585  2  7 01:22 index.php

ここで上で表示されたURLにブラウザからアクセスしてみる。


phpinfo()を書いたphpファイルを作成してみる。test.phpとかでいいか。

<?php
phpinfo();

gitでローカルリポジトリにコミットする。

$ git add --all
$ git commit -m "add test.php"
[master 6da6e33] add test.php
 Committer: xxxxxx xxxxxx@xxxxxx-no-MacBook-Pro.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 2 insertions(+)
 create mode 100644 test.php

その後、サイトにpushする。
$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 343 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Stopping PHP 5.4 cartridge (Apache+mod_php)
remote: Waiting for stop to finish
remote: Waiting for stop to finish
remote: Building git ref 'master', commit 6da6e33
remote: Checking .openshift/pear.txt for PEAR dependency...
remote: Preparing build for deployment
remote: Deployment id is ffac86ca
remote: Activating deployment
remote: Starting PHP 5.4 cartridge (Apache+mod_php)
remote: Application directory "/" selected as DocumentRoot
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success
To ssh://56b61d5c89f5cf92a000003f@myphpapp-xxxxxx.rhcloud.com/~/git/myphpapp.git/
   9b470d5..6da6e33  master -> master

これでブラウザでアクセスしてみるときちんと反映された。

2016年2月5日金曜日

Herokuよりも無料枠が良さげなOpenShiftをつかってみる

このエントリーをはてなブックマークに追加
遅まきのHerokuデビューを無料枠でしたよ。準備編〜その1で書いたように今はdynoを落としておかないといけない時間も多くなったので、まだ制限の緩いOpenShiftを試してみることにした。

アカウント登録してサインインするとこんな画面になり新しいアプリケーションを登録することができる。


"Create your first application now"をクリックして進もう。

結構色々な"Application Type"が選べる。この中でシンプルに"PHP 5.4"を選ぶ。


ほぼデフォルトでいいだろう。Gearがサーバ、Cartridgeがパッケージみたいなものだと考えればいいのかな。セキュリティアップデートは勝手にかけてくれるらしい。若葉マークってどこの国でも一緒なのかw

PHP 5.4 Cartridge

PHP is a general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages. Popular development frameworks include CakePHP, Zend, Symfony, and Code Igniter.
OpenShift maintained
Receives automatic security updates

"Create Application"を押して、数十秒ぐらい待つと下記の画面が現れてGearが作成されたようだ。


今は"Not now, Continue"を選んで進む。


ここまででGearのベース設定は終了した。

次はGetting Started with PHPを元にソースをGearにデプロイしてブラウザで確認できるとこまでやってみよう。



2016年2月3日水曜日

遅まきのHerokuデビューを無料枠でしたよ。phpinfo()を作ってデプロイ編

このエントリーをはてなブックマークに追加
チュートリアルは分かったので、今度はスクラッチで作成して公開するまでの手順を確認しておこう。

まずは適当なディレクトリを作る。そしてそのディレクトリの下に移動。

$ mkdir phpinfo
$ cd phpinfo/

index.phpとcomposer.jsonを作る。composer.jsonは空に近い状態でいい。

phpinfo();
{
    "require": {
    }
}
このままcomposer updateを実行する。

$ composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
そうするとvenderディレクトリが作成されているので、ここでgit initする。

$ git init
Initialized empty Git repository in /Users/hogahoga/Documents/HerokuProject/phpinfo/.git/
全てのファイルをaddし、ローカルリポジトリにコミットする。
$ git add *
$ git commit -m "Initial Commit"
[master (root-commit) 3852af9] Initial Commit
 Committer: Taro Yamada 
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly. Run the
following command and follow the instructions in your editor to edit
your configuration file:

    git config --global --edit

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 10 files changed, 520 insertions(+)
 create mode 100644 composer.json
 create mode 100644 index.php
 create mode 100644 vendor/autoload.php
 create mode 100644 vendor/composer/ClassLoader.php
 create mode 100644 vendor/composer/LICENSE
 create mode 100644 vendor/composer/autoload_classmap.php
 create mode 100644 vendor/composer/autoload_namespaces.php
 create mode 100644 vendor/composer/autoload_psr4.php
 create mode 100644 vendor/composer/autoload_real.php
 create mode 100644 vendor/composer/installed.json
ここでHerokuにログインする。
$ heroku login
Enter your Heroku credentials.
Email: Taro.Yamada@hogehoge.com
Password (typing will be hidden):
Logged in as Taro.Yamada@hogehoge.com
Heroku上にアプリを作成し、ローカルリポジトリにコミットしたファイルをHerokuにプッシュする。
$ heroku create
Creating app... done, stack is cedar-14
https://mysterious-depths-86346.herokuapp.com/ | https://git.heroku.com/mysterious-depths-86346.git
$ git push heroku master
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (14/14), 4.72 KiB | 0 bytes/s, done.
Total 14 (delta 2), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote: -----> Bootstrapping...
remote: -----> Installing system packages...
remote:        NOTICE: No runtime required in composer.lock; using PHP ^5.5.17
remote:        - php (5.6.17)
remote:        - Apache (2.4.16)
remote:        - Nginx (1.8.0)
remote: -----> Enabling PHP extensions...
remote:        - ext-zend-opcache (automatic)
remote: -----> Installing dependencies...
remote:        Composer version 1.0.0-alpha11 2015-11-14 16:21:07
remote:
remote:  !     WARNING: Your Composer vendor dir is part of your Git repository.
remote:        This directory should not be under version control; only your
remote:        'composer.json' and 'composer.lock' files should be added, which
remote:        will let Composer handle installation of dependencies on deploy.
remote:        To suppress this notice, first remove the folder from your index
remote:        by running 'git rm --cached vendor/'.
remote:        Next, edit your project's '.gitignore' file and add the folder
remote:        '/vendor/' to the list.
remote:        For more info, refer to the Composer FAQ: http://bit.ly/1rlCSZU
remote:
remote:        Loading composer repositories with package information
remote:        Installing dependencies
remote:        Nothing to install or update
remote:        Generating optimized autoload files
remote: -----> Preparing runtime environment...
remote:        NOTICE: No Procfile, using 'web: vendor/bin/heroku-php-apache2'.
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 73.9M
remote: -----> Launching...
remote:        Released v3
remote:        https://mysterious-depths-86346.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/mysterious-depths-86346.git
 * [new branch]      master -> master
ブラウザで確認する。
$ heroku open
Opening mysterious-depths-86346... done

よしよし、コツが分かったぞ。

2016年2月2日火曜日

遅まきのHerokuデビューを無料枠でしたよ。準備編〜その2

このエントリーをはてなブックマークに追加
さて昨日の遅まきのHerokuデビューを無料枠でしたよ。準備編〜その1からの続き。

さてHeroku上にアプリケーションを作成する。

Deploy the app

In this step you will deploy the app to Heroku.
Create an app on Heroku, which prepares Heroku to receive your source code:
$ heroku create
Creating sharp-rain-871... done, stack is cedar-14
http://sharp-rain-871.herokuapp.com/ | https://git.heroku.com/sharp-rain-871.git
Git remote heroku added
When you create an app, a git remote (called heroku) is also created and associated with your local git repository.
Heroku generates a random name (in this case sharp-rain-871) for your app, or you can pass a parameter to specify your own app name.

この後、GitHubから取ってきたソースをHerokuにデプロイしていくようだ。ローカルのgitリポジトリからHerokuのリモートリポジトリへアップする。なんかいっぱい入った。

$ git push heroku master
Counting objects: 140, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (75/75), done.
Writing objects: 100% (140/140), 28.30 KiB | 0 bytes/s, done.
Total 140 (delta 51), reused 140 (delta 51)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote: -----> Bootstrapping...
remote: -----> Installing system packages...
remote:        NOTICE: No runtime required in composer.json; requirements
remote:        from dependencies in composer.lock will be used for selection
remote:        - php (7.0.2)
remote:        - Apache (2.4.16)
remote:        - Nginx (1.8.0)
remote: -----> Enabling PHP extensions...
remote:        - ext-zend-opcache (automatic)
remote: -----> Installing dependencies...
remote:        Composer version 1.0.0-alpha11 2015-11-14 16:21:07
remote:        Loading composer repositories with package information
remote:        Installing dependencies from lock file
remote:          - Installing psr/log (1.0.0)
remote:            Downloading: 100%
remote:
remote:          - Installing monolog/monolog (1.16.0)
remote:            Downloading: 100%
remote:
remote:          - Installing symfony/routing (v2.7.3)
remote:            Downloading: 100%
remote:
remote:          - Installing symfony/http-foundation (v2.7.3)
remote:            Downloading: 100%
remote:
remote:          - Installing symfony/event-dispatcher (v2.7.3)
remote:            Downloading: 100%
remote:
remote:          - Installing symfony/debug (v2.7.3)
remote:            Downloading: 100%
remote:
remote:          - Installing symfony/http-kernel (v2.7.3)
remote:            Downloading: 100%
remote:
remote:          - Installing pimple/pimple (v1.1.1)
remote:            Downloading: 100%
remote:
remote:          - Installing silex/silex (v1.3.1)
remote:            Downloading: 100%
remote:
remote:          - Installing twig/twig (v1.19.0)
remote:            Downloading: 100%
remote:
remote:          - Installing symfony/twig-bridge (v2.7.3)
remote:            Downloading: 100%
remote:
remote:        Generating optimized autoload files
remote: -----> Preparing runtime environment...
remote: -----> Checking for additional extensions to install...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 83.6M
remote: -----> Launching...
remote:        Released v3
remote:        https://dry-springs-12517.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy.... done.
To https://git.heroku.com/dry-springs-12517.git
 * [new branch]      master -> master

ここでwebインスタンスを1つ動かす。

The application is now deployed. Ensure that at least one instance of the app is running:
$ heroku ps:scale web=1

これでサイトの準備ができたので、下記のコマンドでデプロイされたアプリがブラウザで確認できる。
Now visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows:
$ heroku open

できた!


起動させた時のログはheroku logsを使う。
$ heroku logs
2016-02-01T15:47:23.734744+00:00 heroku[api]: Enable Logplex by xxx@example.com
2016-02-01T15:47:23.734926+00:00 heroku[api]: Release v2 created by xxx@example.com
2016-02-01T15:58:25.346315+00:00 heroku[api]: Scale to web=1 by xxx@example.com
2016-02-01T15:58:25.441370+00:00 heroku[api]: Deploy b6e3898 by xxx@example.com
2016-02-01T15:58:25.441432+00:00 heroku[api]: Release v3 created by xxx@example.com
2016-02-01T15:58:25.818992+00:00 heroku[slug-compiler]: Slug compilation started
2016-02-01T15:58:25.818998+00:00 heroku[slug-compiler]: Slug compilation finished
2016-02-01T15:58:30.607022+00:00 heroku[web.1]: Starting process with command `vendor/bin/heroku-php-apache2 web/`
2016-02-01T15:58:33.451421+00:00 app[web.1]: 4 processes at 128MB memory limit.
2016-02-01T15:58:33.246369+00:00 app[web.1]: DOCUMENT_ROOT changed to 'web/'
2016-02-01T15:58:33.457058+00:00 app[web.1]: Starting php-fpm...
2016-02-01T15:58:33.289875+00:00 app[web.1]: Optimizing defaults for 1X dyno...
2016-02-01T15:58:35.463051+00:00 app[web.1]: Starting httpd...
2016-02-01T15:58:36.118632+00:00 heroku[web.1]: State changed from starting to up
2016-02-01T16:31:55.317376+00:00 heroku[web.1]: Idling
2016-02-01T16:31:55.318091+00:00 heroku[web.1]: State changed from up to down
2016-02-01T16:31:57.921272+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-02-01T16:31:58.417151+00:00 app[web.1]: Going down, terminating child processes...
2016-02-01T16:31:59.165323+00:00 heroku[web.1]: Process exited with status 143
2016-02-01T16:56:47.735067+00:00 heroku[web.1]: Unidling
2016-02-01T16:56:47.735426+00:00 heroku[web.1]: State changed from down to starting
2016-02-01T16:56:53.261661+00:00 heroku[web.1]: Starting process with command `vendor/bin/heroku-php-apache2 web/`
2016-02-01T16:56:55.081581+00:00 app[web.1]: 4 processes at 128MB memory limit.
2016-02-01T16:56:54.967333+00:00 app[web.1]: DOCUMENT_ROOT changed to 'web/'
2016-02-01T16:56:54.980799+00:00 app[web.1]: Optimizing defaults for 1X dyno...
2016-02-01T16:56:55.085368+00:00 app[web.1]: Starting php-fpm...
2016-02-01T16:56:57.086777+00:00 app[web.1]: Starting httpd...
2016-02-01T16:56:57.159387+00:00 heroku[web.1]: State changed from starting to up

WEBワーカを止めるにはheroku ps:scale web=0で行う。

$ heroku ps:scale web=0
2016-02-01T18:14:22.506008+00:00 heroku[run.2596]: State changed from up to complete
2016-02-01T18:14:22.519362+00:00 heroku[run.2596]: Process exited with status 0
2016-02-01T18:21:32.196197+00:00 heroku[api]: Scale to web=0

2016年2月1日月曜日

遅まきのHerokuデビューを無料枠でしたよ。準備編〜その1

このエントリーをはてなブックマークに追加
結構前(3年ぐらい?)からHerokuは流行ってるのは知ってたんだけど、仕事内容でいまいち関わることが無かったのでスルーしてた。
でも最近になって触るような機会とPaaSの流行りがやっと来た感があるので試してみた。もうこんな記事はちまたに出回りすぎるぐらい出ているんだけど。

ちょっと前(2015年5月)にプライス設定が見直しになってたみたいで今は無料利用での制限が色々と厳しくなってる模様。それでも開発の時やテスト的には全然使えると思う。


  • SLEEPS AFTER 30 MINS OF INACTIVITY
    • 30分稼働がないとスリープしてしまう
  • MUST SLEEP 6 HOURS IN A 24 HOUR PERIOD
    • 24時間中、6時間はスリープする必要あり
  • CUSTOM DOMAINS
    • 独自ドメインが使える
  • 512 MB RAM │ 1 web/1 worker
    • dynoと呼ばれるプロセス?コンテナ?みたいな考えのWebタイプとWorkerタイプが1つずつ使える
    • Dynos and the Dyno Managerに書いてあった

さっそくサインアップを済ませてログイン。ダッシュボードは本日時点でこんな感じ。


"PHP Get Started"を選ぶ。


StartまでのTutorialが出てきた。

Introduction

This tutorial will have you deploying a PHP app in minutes.
Hang on for a few more minutes to learn how it all works, so you can make the most out of Heroku.
The tutorial assumes that you have:
・a free Heroku account.
・PHP installed locally.
・Composer installed.

はじめにPCにHerokuを使うために"Heroku Toolbelt"なるものをインストールしなきゃならんらしい。なのでこれをダウンロードしてインストールする。Windows版しかなかったら暴れているところだが、無事Mac版も用意されていた。

Set up

In this step you will install the Heroku Toolbelt. This provides you access to the Heroku Command Line Interface (CLI), which can be used for managing and scaling your applications and add-ons. A key part of the toolbelt is the heroku local command, which can help in running your applications locally.

Once installed, you can use the heroku command from your command shell.
Log in using the email address and password you used when creating your Heroku account:

pkgをダウンロードし、インストール完了したらおもむろにTerminalを開いてheroku loginと打ってコマンド実行。
$ heroku login
heroku-cli: Installing Toolbelt v4... done
For more information on Toolbelt v4: https://github.com/heroku/heroku-cli
heroku-cli: Adding dependencies... done
heroku-cli: Installing core plugins... done
Enter your Heroku credentials.
Email: hogehoge@example.com
Password (typing will be hidden):
Logged in as hogehoge@example.com
$

成功したみたいだけれど、恐ろしく成功感がない。次はGitHubからサンプルソースを引っ張ってこいみたいなことが書いてあるので、適当な場所にHeroku用のフォルダを作ってそこで作業することにした。

Prepare the app

In this step, you will prepare a simple application that can be deployed.
Execute the following commands to clone the sample application:
$ git clone https://github.com/heroku/php-getting-started.git
$ cd php-getting-started
You now have a functioning git repository that contains a simple application as well as a composer.json file. Make sure you've installed Composer. Heroku uses Composer for dependency management in PHP projects, and the composer.json file indicates to Heroku that your application is written in PHP.

パッケージ管理にComposerを使っているみたい。だからComposer入れとけとか書いている。正直準備でだいぶだるい。

先にComposerをインストールする。
$ curl -sS https://getcomposer.org/installer | php
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:

The OpenSSL library (0.9.8zc) used by PHP does not support TLSv1.2 or TLSv1.1.
If possible you should upgrade OpenSSL to version 1.0.1 or above.

Downloading...

Composer successfully installed to: /Users/fumio/Documents/HerokuProject/php-getting-started/composer.phar
Use it: php composer.phar

/usr/local/binあたりに移動させる。

$mv composer.phar /usr/local/bin/composer

実行してこうなったら入ってるんじゃないかと思う。
$ composer
______
/ ____/___ ____ ___ ____ ____ ________ _____
/ / / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__ ) __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
/_/
Composer version 1.0-dev (cff35071b68e286f68ac64908c990c15b3ac4a0d) 2016-01-31 15:50:19

Usage:
command [options] [arguments]

Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this app



さてGitHubから引っ張ってくる。
$ git  clone https://github.com/heroku/php-getting-started.git
Cloning into 'php-getting-started'...
remote: Counting objects: 148, done.
remote: Total 148 (delta 0), reused 0 (delta 0), pack-reused 148
Receiving objects: 100% (148/148), 32.44 KiB | 0 bytes/s, done.
Resolving deltas: 100% (55/55), done.
Checking connectivity... done.

php-getting-startedの中身はこんな感じ。
$ ls -aFC
./ .git/ Procfile app.json composer.lock
../ .gitignore README.md composer.json web/
$ ls -aFC web/
./ ../ .htaccess images/ index.php* stylesheets/ views/
$ ls -aFC web/views/
./ ../ header.html index.twig layout.html nav.html
$ ls -aFC web/images/
./ ../ favicon.png lang-logo.png
$ ls -aFC web/stylesheets/
./ ../ main.css

index.phpの中身だけ見てみた。
require('../vendor/autoload.php');

$app = new Silex\Application();
$app['debug'] = true;

// Register the monolog logging service
$app->register(new Silex\Provider\MonologServiceProvider(), array(
  'monolog.logfile' => 'php://stderr',
));

// Register view rendering
$app->register(new Silex\Provider\TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/views',
));

// Our web handlers

$app->get('/', function() use($app) {
  $app['monolog']->addDebug('logging output.');
  return $app['twig']->render('index.twig');
});

$app->run();
だめだ。今日はここまで!