よちよち歩きのITエンジニアのメモ

Web技術など学んだ内容をメモしておくブログです。どんなにちっちゃくてもいいから、 一歩、ほんの小さな一歩でも夢や目標に近づくように頑張ります

Amazon LinuxにLAMP環境を構築してみた

以下記事を参考にAmazon LinuxにLAMP環境を構築してみました
AWSのEC2に基本的なLAMP環境を作ってみる(2) - Qiita

LAMP環境とは

L → Linux
A → Apache
M → MySQL
P → PHP
で構成された動作環境のこと

構築手順

ec2-userからrootにする

$ sudo su

Linuxの日付設定、言語設定

$ vim /etc/sysconfig/i18n
    LANG=ja_JP.UTF-8
$ cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

PHPのインストール(PHP5.5)

$ yum install -y php55
$ vim /etc/php.ini
    date.timezone = 'Asia/Tokyo' とする

MySQLのインストール

$ yum install -y mysql-server
$ service mysqld start
$ mysql -uroot
    mysql> set password for root@localhost=password('rootユーザーのパスワード');
    mysql> exit
$ /usr/bin/mysql_secure_installation
    Enter current password for root (enter for none): (rootユーザのパスワード)
    Change the root password? [Y/n] n
    Remove anonymous users? [Y/n] Y
    Disallow root login remotely? [Y/n] Y
    Remove test database and access to it? [Y/n] Y
    Reload privilege tables now? [Y/n] Y

phpMyAdminのインストール

$ yum --enablerepo=epel install -y phpMyAdmin

EPELとは、エンタープライズ Linux 用の拡張パッケージでFedora プロジェクトの貢献者たちが Fedora のパッケージを自分たちが使っているRed Hat Enterprise Linux (RHEL) やその 互換ディストリビューション上で使用したいという要望から作成された拡張パッケージです。

RHELクローンとしてサーバー用途に用いられることが多いCentOSは簡素で堅牢なパッケージを提供する傾向にあります。 CentOSの公式リポジトリは保守的でパッケージ数も多くないため、EPELやRemi, RPMforgeを追加し必要なパッケージを追加する といった使い方をされるようです。

EPELとは - bnoteより引用

アクセス権限の設定
$ vim /etc/httpd/conf.d/phpMyAdmin.conf
Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       # Require ip 127.0.0.1 <==(ここを修正!)
       Require all granted
       Require ip ::1
     </RequireAny>
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from ::1
   </IfModule>
</Directory>

MySQL, apacheの自動起動設定を有効にする

$ chkconfig mysqld on
$ chkconfig httpd on

再起動する

$ service mysqld restart
$ service httpd restart

MacでSSH接続する方法メモ

Amazon EC2にインスタンスを立てたので、SSH接続するためのメモ
こういうことは忘れることが多いので地味に便利

初期設定(秘密鍵と権限の設定)

$ mv 秘密鍵名.pem ~/.ssh/
$ chmod 600 ~/.ssh/秘密鍵名.pem
  • 秘密鍵はインスタンス生成時に作成、設定したもの

接続

$ ssh -i ~/.ssh/秘密鍵名.pem [ユーザ名]@[IPアドレス or DNS名]
  • Amazon EC2のデフォルトユーザ名は、"ec2-user"

PHPでの日付フォーマット変換方法

ハッカーランク(hackerrank)にて日付変換の問題をPHPで解くにあたって、日付関連の関数を調べたのでそのメモです。

問題

Sample Input

  • 07:05:45PM

Sample Output

  • 19:05:45

プログラム

要点

  • strtotime関数で、日付文字列をタイムスタンプに変換する
    • 日付文字列を良い具合に解釈して、タイムスタンプ (1970 年 1 月 1 日 00:00:00 UTC からの経過秒数) に変換してくれる関数
    • PHP: strtotime - Manual
      • JavaだとSimpleDateFormatクラスで、日付文字列を解析するには、日付形式を指定("yyyy-MM-dd"のような)する必要があるが、PHPではその必要がない(JavaよりもPHPの方が楽だね!)
    • 解釈できる日付書式は以下、ページに描かれている。
  • date関数で、タイムスタンプを日付文字列に変換する
    • タイムスタンプを与えられたフォーマット文字列によりフォーマットし、日付文字列を返す関数
    • PHP: date - Manual

詳細

<?php

$handle = fopen("php://stdin", "r");
fscanf($handle, "%s", $time);

date_default_timezone_set('Asia/Tokyo');

// strtotime: 日付文字列をタイムスタンプに変換する
// date: タイムスタンプを日付文字列に変換する
echo date("H:i:s", strtotime($time));

?>

MacでPHPの実行環境構築メモ

OS情報

f:id:yfj2:20160830062953p:plain

PHPのリポジトリを追加する

$ brew tap homebrew/php

PHP5.5とデバッグツールのインストール

$ brew install homebrew/php/php55
$ brew install homebrew/php/php55-xdebug
$ vim /usr/local/etc/php/5.5/php.ini
    date.timezone = 'Asia/Tokyo' とする

環境確認

$ brew doctor
  • ここで"Warning"が出た場合は、メッセージに沿って修正する

MySQLのインストール

  • インストール
$ brew install mysql
  • 起動
$ mysql.server start
  • rootユーザーのパスワード設定
$ mysql -uroot
    mysql> set password for root@localhost=password('rootユーザーのパスワード');
    mysql> exit
  • MySQL のセキュリティ設定
$ mysql_secure_installation
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.

All done! 

phpMyAdminのインストール

  • インストール
$brew install phpmyadmin
  • apacheの設定
$ sudo vim /etc/apache2/httpd.conf
LoadModule php5_module libexec/apache2/libphp5.so <= コメントを外す
~~~
<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>
<IfModule mine_module>
    AddType application/x-httpd-php .php
</IfModule>
~~~
  Alias /phpmyadmin /usr/local/share/phpmyadmin
  <Directory /usr/local/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
      Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
    </IfModule>
  </Directory>
~~~
  • config.inc.phpの修正
$ vim /usr/local/Cellar/phpmyadmin/4.6.4/share/phpmyadmin/config.inc.php

"localhost"を"127.0.0.1"に修正する

$cfg['Servers'][$i]['host'] = '127.0.0.1'; 

PhpStormの設定

  • PhpStorm > Preferences > Languages & Frameworks > PHP
    • f:id:yfj2:20161005170254p:plain
    • f:id:yfj2:20161005165803p:plain

その他

Warning: homebrew/php/php53: --homebrew-apxs was deprecated; using --with-homebrew-apxs instead!
Warning: homebrew/php/php53: --with-homebrew-apxs was deprecated; using --with-apache instead!

  • "--with-homebrew-apxs"を指定すると、Apacheのhttpd.confに下記が自動で追記される
  • --

Macを買ってからやったことメモ

Macを買ってからやったことメモ

著者:ふじさわゆうき

先日、MacBook Airを購入しました。
バッテリーの持ちがとても良いです(12時間持続するとか)

今後、Macを買い換えることも考えて、セットアップのメモを
残しておきます

バージョン情報

MacBook Air 1600/13.3 MMGG2J/A

f:id:yfj2:20160830062953p:plain

(1) .bash_profileの作成

http://oxynotes.com/?p=5418

 $ vi .bash_profile
# custom prompt setting
PS1="[\u@\w]$"

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
## PATH=$PATH:path

export PATH
$ source .bash_profile

(3) Homebrewのインストール

http://brew.sh/index_ja.html

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

(4)Eclipseのインストール

https://www.eclipse.org/downloads/
Eclipse for Java Developersをインストール

f:id:yfj2:20160830065925p:plain

(5)gitのインストール

$ brew install git