LoginSignup
21

More than 5 years have passed since last update.

Amazon EC2 (Amazon Linux)に PHP 5.6 + Laravel 5.3 + Apache 2.4をインストールする手順 (sedでphp.iniのdate.timezoneをAsia/Tokyoに変更するワンライナー)

Last updated at Posted at 2016-09-28

はじめに

Amazon EC2インスタンス(Amazon Linux)を作成し、PHP 5.6 + Laravel 5.3.11 + Apache 2.4をインストールする手順です。

また、php.iniのdate.timezoneが未設定の場合、composer create-projectコマンド実行よるLaravelプロジェクト作成時にエラーが発生しますので、php.iniのdate.timezoneを指定する必要があります。sedコマンドで/etc/php.iniのdate.timezoneをAsia/Tokyoに変更するワンライナーも合わせて記載します。

[root@example-laravel-server ~]# grep date.timezone /etc/php.ini
;date.timezone =
   ↓
date.timezone = "Asia/Tokyo"

環境

Amazon LinuxのEC2インスタンスは以下のAMIを使用して作成しました。

・Amazon Linux AMI 2016.09.0 (HVM), SSD Volume Type - ami-1a15c77b
  amzn-ami-hvm-2016.09.0.20160923-x86_64-gp2 (ami-1a15c77b)

EC2インスタンスにはPHP 5.6.25 + Laravel 5.3 + Apache 2.4.23をインストールしました。

参考資料

以下の資料を参考にさせて頂きました。とても参考になりました。
ありがとうございました!

Laravel 5.2 インストール
https://readouble.com/laravel/5.2/ja/installation.html

Laravel 4.2 Laravelクイックスタート
https://readouble.com/laravel/4.2/ja/quick.html

Laravel5をイチからインストール!
http://blog.mekachan.net/?p=158

Laravelをインストールしてみる
http://www.patience-time.com/php/709/

ララ帳 PHPフレームワーク LARAVEL の学習帳
https://laravel10.wordpress.com/

Composerのcreate-projectが何をやっているのか調べてみた
http://qiita.com/DQNEO/items/74f4bb8fe447e4582a97

Amazon EC2(Amazon Linux)にPHP 5.6 + Laravel 5.3 + Apache 2.4をインストールする手順

(1) AWAマネジメントコンソールからAmazon EC2インスタンス(Amazon Linux)を作成します。

(2) Amazon EC2インスタンス(Amazon Linux)にec2-userでsshログインします。

(3) yumパッケージをアップデートします。

[ec2-user@example-laravel-server ~]$ uname -a
Linux example-laravel-server 4.4.19-29.55.amzn1.x86_64 #1 SMP Mon Aug 29 23:29:40 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[ec2-user@example-laravel-server ~]$
[ec2-user@example-laravel-server ~]$ sudo yum -y update

(4) Apache 2.4をインストールします。

[ec2-user@example-laravel-server ~]$ sudo yum -y install httpd24
[ec2-user@example-laravel-server ~]$ httpd -v
Server version: Apache/2.4.23 (Amazon)
Server built:   Jul 29 2016 21:42:17
[ec2-user@example-laravel-server ~]$

(5) PHP 5.6と付随するパッケージをインストールします。

[ec2-user@example-laravel-server ~]$ sudo yum -y install php56 php56-devel php56-mbstring php56-mcrypt php56-mysqlnd php56-pdo
[ec2-user@example-laravel-server ~]$ sudo yum -y install php-pear
[ec2-user@example-laravel-server ~]$ sudo yum -y install git
[ec2-user@example-laravel-server ~]$ sudo yum -y install zlib-devel
[ec2-user@example-laravel-server ~]$ sudo yum -y install gcc
[ec2-user@example-laravel-server ~]$ php -v
PHP 5.6.25 (cli) (built: Aug 27 2016 05:06:49)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
[ec2-user@example-laravel-server ~]$

(6) composerをインストールします。

[ec2-user@example-laravel-server ~]$ sudo curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading 1.2.1...

Composer successfully installed to: /home/ec2-user/composer.phar
Use it: php composer.phar
[ec2-user@example-laravel-server ~]$

(7) インストールしたcomposerを/usr/local/bin/へ移動します。

[ec2-user@example-laravel-server ~]$ sudo mv composer.phar /usr/local/bin/composer
[ec2-user@example-laravel-server ~]$

[ec2-user@example-laravel-server ~]$ sudo chown root:root /usr/local/bin/composer
[ec2-user@example-laravel-server ~]$

[ec2-user@example-laravel-server ~]$ ll /usr/local/bin/composer
-rwxr-xr-x 1 root root 1704783 Sep 29 00:27 /usr/local/bin/composer
[ec2-user@example-laravel-server ~]$

(8) /var/www配下にLaravelプロジェクト作成用ディレクトリを作成し、ディレクトリにec2-userの書き込み権限を設定します。

[ec2-user@example-laravel-server ~]$ cd /var/www
[ec2-user@example-laravel-server www]$ pwd
/var/www
[ec2-user@example-laravel-server www]$ sudo mkdir /var/www/laravel
[ec2-user@example-laravel-server www]$ sudo chown ec2-user:ec2-user /var/www/laravel
[ec2-user@example-laravel-server www]$
[ec2-user@example-laravel-server www]$ ls -lrta /var/www/laravel/
total 8
drwxr-xr-x 8 root     root     4096 Sep 29 00:33 ..
drwxr-xr-x 2 ec2-user ec2-user 4096 Sep 29 00:33 .
[ec2-user@example-laravel-server www]$

(9) Laravelプロジェクト作成時、php.iniのdate.timezoneが未設定の場合、エラーが発生します。

[ec2-user@example-laravel-server laravel]$ cd /var/www/laravel/
[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$

Laravelプロジェクト作成時、php.iniのdate.timezoneが未設定の場合、以下のエラーが発生します。対応方法は後述します。

[ec2-user@example-laravel-server laravel]$ composer create-project laravel/laravel --prefer-dist
Installing laravel/laravel (v5.3.10)
  - Installing laravel/laravel (v5.3.10)
    Downloading: 100%
    Failed to download laravel/laravel from dist: Could not delete /var/www/laravel/: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.
    Now trying to download from source
  - Installing laravel/laravel (v5.3.10)
    Cloning 4eeec60d7a00114b5350f8bcf1c6d3a36a2206d3


  [RuntimeException]
  Could not delete /var/www/laravel/: date_default_timezone_get(): It is not safe to rely on the system's timezone settings. You are *required* to
  use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this w
  arning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your t
  imezone.


create-project [-s|--stability STABILITY] [--prefer-source] [--prefer-dist] [--repository REPOSITORY] [--repository-url REPOSITORY-URL] [--dev] [--no-dev] [--no-custom-installers] [--no-scripts] [--no-progress] [--no-secure-http] [--keep-vcs] [--no-install] [--ignore-platform-reqs] [--] [<package>] [<directory>] [<version>]

[ec2-user@example-laravel-server laravel]$

本エラー発生時、プロジェクト作成は失敗します。プロジェクト作成用ディレクトリには何も作成されません。

[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/
total 8
drwxr-xr-x 8 root     root     4096 Sep 29 00:33 ..
drwxr-xr-x 2 ec2-user ec2-user 4096 Sep 29 00:34 .
[ec2-user@example-laravel-server laravel]$

(10) php.iniのdate.timezoneをAsia/Tokyoに変更します。

sedでphp.iniのdate.timezoneをAsia/Tokyoに変更するワンライナーを実行します。

まずphp.iniをバックアップします。

[ec2-user@example-laravel-server laravel]$ sudo cp -p /etc/php.ini /etc/php.ini.`date '+%Y%m%d'`
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ ls -lrta /etc/php.ini*
-rw-r--r-- 1 root root 67145 Aug 27 14:11 /etc/php.ini.20160929
lrwxrwxrwx 1 root root    25 Sep 29 00:24 /etc/php.ini -> /etc/alternatives/php.ini
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ diff /etc/php.ini /etc/php.ini.`date '+%Y%m%d'`
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ grep date.timezone /etc/php.ini
; http://php.net/date.timezone
;date.timezone =
[ec2-user@example-laravel-server laravel]$

sedでphp.iniのdate.timezoneをAsia/Tokyoに変更します。

[ec2-user@example-laravel-server laravel]$ sudo sed -i -e 's/;date.timezone =/date.timezone = \"Asia\/Tokyo\"/g' /etc/php.ini
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ diff /etc/php.ini /etc/php.ini.`date '+%Y%m%d'`
889c889
< date.timezone = "Asia/Tokyo"
---
> ;date.timezone =
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ grep date.timezone /etc/php.ini
; http://php.net/date.timezone
date.timezone = "Asia/Tokyo"
[ec2-user@example-laravel-server laravel]$

(11) composerでLaravelをインストールして、Laravelプロジェクトを作成します。

composerでLaravelをインストールして、Laravelプロジェクトを作成します。

[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/
total 8
drwxr-xr-x 8 root     root     4096 Sep 29 01:07 ..
drwxr-xr-x 2 ec2-user ec2-user 4096 Sep 29 01:51 .
[ec2-user@example-laravel-server laravel]$
composer create-project laravel/laravel --prefer-dist
[ec2-user@example-laravel-server laravel]$ composer create-project laravel/laravel --prefer-dist
Installing laravel/laravel (v5.3.10)
  - Installing laravel/laravel (v5.3.10)
    Loading from cache

Created project in /var/www/laravel/laravel
> php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies (including require-dev)

 (コマンド出力結果長いので中略)

sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.4.0)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
phpunit/phpunit suggests installing ext-tidy (*)
phpunit/phpunit suggests installing ext-xdebug (*)
Writing lock file
Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
> php artisan key:generate
Application key [base64:********************************************] set successfully.
[ec2-user@example-laravel-server laravel]$

(12) Laravelプロジェクトが作成された事を確認します。

[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/
total 12
drwxr-xr-x  8 root     root     4096 Sep 29 01:07 ..
drwxr-xr-x  3 ec2-user ec2-user 4096 Sep 29 01:54 .
drwxrwxr-x 12 ec2-user ec2-user 4096 Sep 29 01:55 laravel
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/laravel/
total 216
drwxrwxr-x  2 ec2-user ec2-user   4096 Sep 20 22:38 tests
drwxrwxr-x  5 ec2-user ec2-user   4096 Sep 20 22:38 storage
-rw-rw-r--  1 ec2-user ec2-user    563 Sep 20 22:38 server.php
drwxrwxr-x  2 ec2-user ec2-user   4096 Sep 20 22:38 routes
drwxrwxr-x  5 ec2-user ec2-user   4096 Sep 20 22:38 resources
-rw-rw-r--  1 ec2-user ec2-user   1918 Sep 20 22:38 readme.md
drwxrwxr-x  4 ec2-user ec2-user   4096 Sep 20 22:38 public
-rw-rw-r--  1 ec2-user ec2-user    930 Sep 20 22:38 phpunit.xml
-rw-rw-r--  1 ec2-user ec2-user    400 Sep 20 22:38 package.json
-rw-rw-r--  1 ec2-user ec2-user    556 Sep 20 22:38 gulpfile.js
-rw-rw-r--  1 ec2-user ec2-user     80 Sep 20 22:38 .gitignore
-rw-rw-r--  1 ec2-user ec2-user     61 Sep 20 22:38 .gitattributes
-rw-rw-r--  1 ec2-user ec2-user    491 Sep 20 22:38 .env.example
drwxrwxr-x  5 ec2-user ec2-user   4096 Sep 20 22:38 database
drwxrwxr-x  2 ec2-user ec2-user   4096 Sep 20 22:38 config
-rw-rw-r--  1 ec2-user ec2-user   1283 Sep 20 22:38 composer.json
drwxrwxr-x  3 ec2-user ec2-user   4096 Sep 20 22:38 bootstrap
-rwxr-xr-x  1 ec2-user ec2-user   1646 Sep 20 22:38 artisan
drwxrwxr-x  6 ec2-user ec2-user   4096 Sep 20 22:38 app
drwxr-xr-x  3 ec2-user ec2-user   4096 Sep 29 01:54 ..
-rw-rw-r--  1 ec2-user ec2-user 124068 Sep 29 01:55 composer.lock
drwxrwxr-x 12 ec2-user ec2-user   4096 Sep 29 01:55 .
drwxrwxr-x 31 ec2-user ec2-user   4096 Sep 29 01:55 vendor
-rw-rw-r--  1 ec2-user ec2-user    542 Sep 29 01:55 .env
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/laravel/public/
total 32
-rw-rw-r--  1 ec2-user ec2-user  914 Sep 20 22:38 web.config
-rw-rw-r--  1 ec2-user ec2-user   24 Sep 20 22:38 robots.txt
drwxrwxr-x  2 ec2-user ec2-user 4096 Sep 20 22:38 js
-rw-rw-r--  1 ec2-user ec2-user 1782 Sep 20 22:38 index.php
-rw-rw-r--  1 ec2-user ec2-user  553 Sep 20 22:38 .htaccess
-rw-rw-r--  1 ec2-user ec2-user    0 Sep 20 22:38 favicon.ico
drwxrwxr-x  2 ec2-user ec2-user 4096 Sep 20 22:38 css
drwxrwxr-x  4 ec2-user ec2-user 4096 Sep 20 22:38 .
drwxrwxr-x 12 ec2-user ec2-user 4096 Sep 29 01:55 ..
[ec2-user@example-laravel-server laravel]$

(13) インストールしたLaravelのバージョンを確認します。

[ec2-user@example-laravel-server laravel]$ php /var/www/laravel/laravel/artisan --version
Laravel Framework version 5.3.11
[ec2-user@example-laravel-server laravel]$

(14) WebブラウザからApacheへアクセスした時、作成したLaravelプロジェクトを表示出来るようhttpd.confを修正します。

[ec2-user@example-laravel-server laravel]$ sudo cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
[ec2-user@example-laravel-server laravel]$ diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
[ec2-user@example-laravel-server laravel]$

httpd.confの以下の箇所を変更します。

[ec2-user@example-laravel-server laravel]$ sudo sed -i -e 's/#ServerName www.example.com:80/ServerName example-laravel-server:80/g' /etc/httpd/conf/httpd.conf
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ sudo sed -i -e 's/ServerAdmin root@localhost/ServerAdmin root@example.com/g' /etc/httpd/conf/httpd.conf
[ec2-user@example-laravel-server laravel]$

[ec2-user@example-laravel-server laravel]$ sudo vi /etc/httpd/conf/httpd.conf

以下の箇所を変更します。
 (中略)
DocumentRoot "/var/www/html"
 ↓
DocumentRoot "/var/www/laravel/laravel/public"
 (中略)

160行目に以下を追加します。
<Directory "/var/www/laravel/laravel/public">
    AllowOverride All
</Directory>
[ec2-user@example-laravel-server laravel]$ diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
86c86
< ServerAdmin root@example.com
---
> ServerAdmin root@localhost
95c95
< ServerName example-laravel-server:80
---
> #ServerName www.example.com:80
119,120c119
< ##DocumentRoot "/var/www/html"
< DocumentRoot "/var/www/laravel/laravel/public"
---
> DocumentRoot "/var/www/html"
158,161d156
< </Directory>
<
< <Directory "/var/www/laravel/laravel/public">
<     AllowOverride All
[ec2-user@example-laravel-server laravel]$

(15) Apacheを起動します。

[ec2-user@example-laravel-server laravel]$ sudo /etc/init.d/httpd configtest
Syntax OK
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ ps awux | grep -v grep | grep http
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ sudo /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
[ec2-user@example-laravel-server laravel]$
[ec2-user@example-laravel-server laravel]$ ps awux | grep -v grep | grep http
root      3952  0.5  2.1 407476 21568 ?        Ss   01:31   0:00 /usr/sbin/httpd
apache    3954  0.0  1.3 415672 13600 ?        Sl   01:31   0:00 /usr/sbin/httpd
apache    3955  0.0  1.3 415672 13600 ?        Sl   01:31   0:00 /usr/sbin/httpd
apache    3956  0.0  1.3 612344 13600 ?        Sl   01:31   0:00 /usr/sbin/httpd
apache    3957  0.0  1.3 415672 13600 ?        Sl   01:31   0:00 /usr/sbin/httpd
apache    3958  0.0  1.3 415672 13600 ?        Sl   01:31   0:00 /usr/sbin/httpd
[ec2-user@example-laravel-server laravel]$

(16) 作成したLaravelプロジェクトのログディレクトリやキャッシュディレクトリ等にApacheユーザの書き込み権限を設定します。

今回の例では/var/www/laravel/laravelというプロジェクトを作成したので/var/www/laravel/laravel/storageディレクトリのパーミッションに対して、Apacheユーザの書き込み権限を設定します。

まずApacheユーザを確認します。

[root@example-laravel-server ~]# grep User /etc/httpd/conf/httpd.conf | grep -v ^# | grep -v LogFormat
User apache
[root@example-laravel-server ~]#
[root@example-laravel-server ~]# grep Group /etc/httpd/conf/httpd.conf | grep -v ^# | grep -v LogFormat
Group apache
[root@example-laravel-server ~]#

Laravelプロジェクトのログディレクトリの現在のパーミッションを確認します。

[root@example-laravel-server ~]# ls -lrta /var/www/laravel/laravel/storage/logs
total 12
-rw-rw-r-- 1 ec2-user ec2-user   14 Sep 20 22:38 .gitignore
drwxrwxr-x 5 ec2-user ec2-user 4096 Sep 20 22:38 ..
drwxrwxr-x 2 ec2-user ec2-user 4096 Sep 20 22:38 .
[root@example-laravel-server ~]#

Laravelプロジェクトのログディレクトリとキャッシュディレクトリ等にApacheユーザの書き込み権限を設定します。

[root@example-laravel-server ~]# chown -R apache:apache /var/www/laravel/laravel/storage
[root@example-laravel-server ~]#
[root@example-laravel-server ~]# chown -R apache:apache /var/www/laravel/laravel/bootstrap/cache
[root@example-laravel-server ~]#

Laravelプロジェクトのログディレクトリのオーナーがapache:apacheに変わった事を確認します。これでApacheユーザがLaravelプロジェクトのログディレクトリにログを書き込めるようになりました。

[root@example-laravel-server ~]# ls -lrta /var/www/laravel/laravel/storage
total 20
drwxrwxr-x  5 apache   apache   4096 Sep 20 22:38 framework
drwxrwxr-x  3 apache   apache   4096 Sep 20 22:38 app
drwxrwxr-x  5 apache   apache   4096 Sep 20 22:38 .
drwxrwxr-x 12 ec2-user ec2-user 4096 Sep 29 01:55 ..
drwxrwxr-x  2 apache   apache   4096 Sep 29 02:11 logs
[root@example-laravel-server ~]#

(17) WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへhttpでアクセス出来るよう設定します。

Amazon VPCのNetwork ACLsやEC2インスタンスのセキュリティグループで、EC2インスタンスのhttp(TCP/80ポート)をブロックしている場合、Network ACLsやEC2インスタンスのセキュリティグループに対して、EC2インスタンスへのhttp(TCP/80ポート)通信許可を追加します。

(18) WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへアクセスします。

WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへアクセスします。URLはEC2インスタンスのIPアドレスを指定します。

「Laravel」と表示されれば、ひとまずPHP 5.6 + Laravel 5インストールは終了です。

111.JPG


Amazon EC2(Amazon Linux)に新しいLaravelプロジェクトを追加する手順

Amazon EC2(Amazon Linux)にLaravelプロジェクトを追加する手順を記します。

今回の例では、/var/www/laravelディレクトリ配下に/var/www/laravel/exampleappという新しいLaravelプロジェクトを作成します。

(19) Laravelインストーラをインストールします。

[ec2-user@example-laravel-server ~]$ id
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
[ec2-user@example-laravel-server ~]$ pwd
/home/ec2-user
[ec2-user@example-laravel-server ~]$ composer global require "laravel/installer=~1.1"
Changed current directory to /home/ec2-user/.composer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/process (v3.1.5)
    Downloading: 100%         

  - Installing psr/log (1.0.2)
    Downloading: 100%         

  - Installing symfony/debug (v3.1.5)
    Downloading: 100%         

  - Installing symfony/polyfill-mbstring (v1.2.0)
    Loading from cache

  - Installing symfony/console (v3.1.5)
    Downloading: 100%         

  - Installing guzzlehttp/promises (1.2.0)
    Downloading: 100%         

  - Installing psr/http-message (1.0.1)
    Downloading: 100%         

  - Installing guzzlehttp/psr7 (1.3.1)
    Downloading: 100%         

  - Installing guzzlehttp/guzzle (6.2.2)
    Downloading: 100%         

  - Installing laravel/installer (v1.3.4)
    Downloading: 100%         

symfony/console suggests installing symfony/event-dispatcher ()
Writing lock file
Generating autoload files
[ec2-user@example-laravel-server ~]$ 
[ec2-user@example-laravel-server ~]$ ls -lrta /home/ec2-user/.composer/vendor/bin/laravel
lrwxrwxrwx 1 ec2-user ec2-user 28 10月 23 15:39 /home/ec2-user/.composer/vendor/bin/laravel -> ../laravel/installer/laravel
[ec2-user@example-laravel-server ~]$ 

(20) ec2-userの.bashrcの環境変数にLaravelインストーラのパスを追加します。

[ec2-user@example-laravel-server ~]$ cp -p /home/ec2-user/.bashrc /home/ec2-user/.bashrc.ORG
[ec2-user@example-laravel-server ~]$ diff /home/ec2-user/.bashrc /home/ec2-user/.bashrc.ORG
[ec2-user@example-laravel-server ~]$ 

.bashrcの末尾に以下のパスを追加します。

[ec2-user@example-laravel-server ~]$ vi /home/ec2-user/.bashrc
 (中略)
export PATH=$HOME/.composer/vendor/bin:$PATH
[ec2-user@example-laravel-server ~]$ cat /home/ec2-user/.bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions
export PATH=$HOME/.composer/vendor/bin:$PATH
[ec2-user@example-laravel-server ~]$ 
[ec2-user@example-laravel-server ~]$ diff /home/ec2-user/.bashrc /home/ec2-user/.bashrc.ORG
9d8
< export PATH=$HOME/.composer/vendor/bin:$PATH
[ec2-user@example-laravel-server ~]$ 
[ec2-user@example-laravel-server ~]$ source /home/ec2-user/.bashrc
[ec2-user@example-laravel-server ~]$ 
[ec2-user@example-laravel-server ~]$ which laravel
~/.composer/vendor/bin/laravel
[ec2-user@example-laravel-server ~]$ 
[ec2-user@example-laravel-server ~]$ laravel --version
Laravel Installer version 1.3.3
[ec2-user@example-laravel-server ~]$ 

(21) Laravelプロジェクトを作成します。

/var/www/laravelディレクトリへ移動します。

[ec2-user@example-laravel-server ~]$ hostname
example-laravel-server
[ec2-user@example-laravel-server ~]$ id
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
[ec2-user@example-laravel-server ~]$ cd /var/www/laravel
[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$

[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel
合計 12
drwxrwxr-x 12 ec2-user ec2-user 4096  9月 29 01:55 laravel
drwxr-xr-x  3 ec2-user ec2-user 4096  9月 29 02:02 .
drwxr-xr-x  9 root     root     4096 10月 16 20:57 ..
[ec2-user@example-laravel-server laravel]$ 

Laravelプロジェクトを作成します。

[ec2-user@example-laravel-server laravel]$ laravel new exampleapp
Crafting application...
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
  - Installing jakub-onderka/php-console-color (0.1)
    Loading from cache

  - Installing vlucas/phpdotenv (v2.4.0)
    Loading from cache

  - Installing symfony/polyfill-mbstring (v1.2.0)
    Loading from cache

  - Installing symfony/var-dumper (v3.1.5)
    Downloading: 100%         

 (中略)

phpunit/php-code-coverage suggests installing ext-xdebug (>=2.4.0)
phpunit/phpunit suggests installing ext-xdebug (*)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Generating autoload files
> php -r "file_exists('.env') || copy('.env.example', '.env');"
> Illuminate\Foundation\ComposerScripts::postInstall
> php artisan optimize
Generating optimized class loader
The compiled class file has been removed.
> php artisan key:generate
Application key [base64:********************************************] set successfully.
Application ready! Build something amazing.
[ec2-user@example-laravel-server laravel]$ 

Laravelプロジェクト作成により、Laravelプロジェクトのディレクトリが作成された事を確認します。

[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/
合計 16
drwxrwxr-x 12 ec2-user ec2-user 4096  9月 29 01:55 laravel
drwxr-xr-x  9 root     root     4096 10月 16 20:57 ..
drwxr-xr-x  4 ec2-user ec2-user 4096 10月 23 16:44 .
drwxrwxr-x 12 ec2-user ec2-user 4096 10月 23 16:45 exampleapp
[ec2-user@example-laravel-server laravel]$ 

[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/exampleapp/
合計 360
drwxrwxr-x  2 ec2-user ec2-user   4096 10月 23 16:44 tests
drwxrwxr-x  4 ec2-user ec2-user   4096 10月 23 16:44 public
-rw-rw-r--  1 ec2-user ec2-user    560 10月 23 16:44 gulpfile.js
-rw-rw-r--  1 ec2-user ec2-user     95 10月 23 16:44 .gitignore
-rw-rw-r--  1 ec2-user ec2-user    491 10月 23 16:44 .env.example
-rw-rw-r--  1 ec2-user ec2-user 147456 10月 23 16:44 yarn.lock
drwxrwxr-x  5 ec2-user ec2-user   4096 10月 23 16:44 storage
-rw-rw-r--  1 ec2-user ec2-user    563 10月 23 16:44 server.php
drwxrwxr-x  2 ec2-user ec2-user   4096 10月 23 16:44 routes
drwxrwxr-x  5 ec2-user ec2-user   4096 10月 23 16:44 resources
-rw-rw-r--  1 ec2-user ec2-user    930 10月 23 16:44 phpunit.xml
-rw-rw-r--  1 ec2-user ec2-user    402 10月 23 16:44 package.json
drwxrwxr-x  5 ec2-user ec2-user   4096 10月 23 16:44 database
drwxrwxr-x  2 ec2-user ec2-user   4096 10月 23 16:44 config
-rw-rw-r--  1 ec2-user ec2-user 124085 10月 23 16:44 composer.lock
-rw-rw-r--  1 ec2-user ec2-user   1646 10月 23 16:44 artisan
-rw-rw-r--  1 ec2-user ec2-user     61 10月 23 16:44 .gitattributes
-rw-rw-r--  1 ec2-user ec2-user   1918 10月 23 16:44 readme.md
-rw-rw-r--  1 ec2-user ec2-user   1283 10月 23 16:44 composer.json
drwxrwxr-x  3 ec2-user ec2-user   4096 10月 23 16:44 bootstrap
drwxrwxr-x  6 ec2-user ec2-user   4096 10月 23 16:44 app
drwxr-xr-x  4 ec2-user ec2-user   4096 10月 23 16:44 ..
drwxrwxr-x 31 ec2-user ec2-user   4096 10月 23 16:45 vendor
drwxrwxr-x 12 ec2-user ec2-user   4096 10月 23 16:45 .
-rw-rw-r--  1 ec2-user ec2-user    542 10月 23 16:45 .env
[ec2-user@example-laravel-server laravel]$ 

(22) 作成したLaravelプロジェクトのディレクトリ配下に.env(データベース接続情報を記述する設定ファイル)が生成されている事を確認します。

Laravelプロジェクトを作成すると、Laravelプロジェクトのディレクトリ配下に.env(データベース接続情報を記述する設定ファイル)が生成されている事を確認します。

[ec2-user@example-laravel-server laravel]$ ls -lrta /var/www/laravel/exampleapp/.env
-rw-rw-r-- 1 ec2-user ec2-user 542 10月 23 16:45 /var/www/laravel/exampleapp/.env
[ec2-user@example-laravel-server laravel]$ 
[ec2-user@example-laravel-server laravel]$ cat /var/www/laravel/exampleapp/.env
APP_ENV=local
APP_KEY=base64:********************************************
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=********

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=
[ec2-user@example-laravel-server laravel]$ 

(23) 作成したLaravelプロジェクトの言語とタイムゾーンを設定します。

[ec2-user@example-laravel-server laravel]$ ll /var/www/laravel/exampleapp/config/app.php
-rw-rw-r-- 1 ec2-user ec2-user 9090 10月 23 16:44 /var/www/laravel/exampleapp/config/app.php
[ec2-user@example-laravel-server laravel]$ 
[ec2-user@example-laravel-server laravel]$ cp -p /var/www/laravel/exampleapp/config/app.php /var/www/laravel/exampleapp/config/app.php.ORG
[ec2-user@example-laravel-server laravel]$ diff /var/www/laravel/exampleapp/config/app.php /var/www/laravel/exampleapp/config/app.php.ORG
[ec2-user@example-laravel-server laravel]$ 

以下の箇所を変更します。

[ec2-user@example-laravel-server laravel]$ vi /var/www/laravel/exampleapp/config/app.php
 (中略)
    'timezone' => 'UTC',

   ↓

    'timezone' => 'Asia/Tokyo',
 (中略)

 (中略)
    'locale' => 'en',

   ↓

    'locale' => 'ja',
 (中略)
[ec2-user@example-laravel-server laravel]$ grep "'timezone' =>" /var/www/laravel/exampleapp/config/app.php | grep -v ^/
    'timezone' => 'Asia/Tokyo',
[ec2-user@example-laravel-server laravel]$ 
[ec2-user@example-laravel-server laravel]$ grep "'locale' =>" /var/www/laravel/exampleapp/config/app.php | grep -v ^/
    'locale' => 'ja',
[ec2-user@example-laravel-server laravel]$ 
[ec2-user@example-laravel-server laravel]$ diff /var/www/laravel/exampleapp/config/app.php /var/www/laravel/exampleapp/config/app.php.ORG
67,68c67
< //  'timezone' => 'UTC',
<     'timezone' => 'Asia/Tokyo',
---
>     'timezone' => 'UTC',
81,82c80
< //  'locale' => 'en',
<     'locale' => 'ja',
---
>     'locale' => 'en',
[ec2-user@example-laravel-server laravel]$ 

(24) 作成したLaravelプロジェクトのログディレクトリやキャッシュディレクトリ等にApacheユーザの書き込み権限を設定します。

[ec2-user@example-laravel-server laravel]$ sudo su - 

[root@example-laravel-server ~]# chown -R ec2-user:apache /var/www/laravel/exampleapp/storage
[root@example-laravel-server ~]# chown -R ec2-user:apache /var/www/laravel/exampleapp/bootstrap/cache
[root@example-laravel-server ~]# 

[root@example-laravel-server ~]# chmod -R 775 /var/www/laravel/exampleapp/storage
[root@example-laravel-server ~]# chmod -R 775 /var/www/laravel/exampleapp/bootstrap/cache
[root@example-laravel-server ~]# 
[root@example-laravel-server ~]# exit
logout
[ec2-user@example-laravel-server laravel]$ id
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$ 

(25) 作成したLaravelプロジェクトでPHPのビルトインサーバーを起動し、WebブラウザからLaravelプロジェクトを表示出来るか確認します。

作成したLaravelプロジェクトの動作確認を行います。
作成したLaravelプロジェクトのディレクトリ/var/www/laravel/exampleappへ移動します。

[ec2-user@example-laravel-server laravel]$ pwd
/var/www/laravel
[ec2-user@example-laravel-server laravel]$ cd /var/www/laravel/exampleapp
[ec2-user@example-laravel-server exampleapp]$ pwd
/var/www/laravel/exampleapp
[ec2-user@example-laravel-server exampleapp]$ id
uid=500(ec2-user) gid=500(ec2-user) groups=500(ec2-user),10(wheel)
[ec2-user@example-laravel-server exampleapp]$

PHPのビルトインサーバーを起動します。

[ec2-user@example-laravel-server exampleapp]$ php artisan serve --port=8000 --host=0.0.0.0 &
[1] 3913
[ec2-user@example-laravel-server exampleapp]$ Laravel development server started on http://0.0.0.0:8000/
 (Enterキーを押下します)
[ec2-user@example-laravel-server exampleapp]$ 

PHPのビルトインサーバーが起動した事を確認します。

[ec2-user@example-laravel-server exampleapp]$ ps awux | grep -v grep | grep php
ec2-user  3913  0.6  2.9 337200 30240 pts/1    S    17:04   0:00 php artisan serve --port=8000 --host=0.0.0.0
ec2-user  3917  0.0  1.6 325836 16868 pts/1    S    17:04   0:00 /usr/bin/php-5.6 -S 0.0.0.0:8000 /var/www/laravel/exampleapp/server.php
[ec2-user@example-laravel-server exampleapp]$ 

(26) WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへhttpでアクセス出来るよう設定します。

Amazon VPCのNetwork ACLsやEC2インスタンスのセキュリティグループで、EC2インスタンスのhttp(TCP/8000ポート)をブロックしている場合、Network ACLsやEC2インスタンスのセキュリティグループに対して、EC2インスタンスへのhttp(TCP/8000ポート)通信許可を追加します。

(27) WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへアクセスします。

WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへアクセスします。URLはEC2インスタンスのIPアドレスを指定します。

「Laravel」と表示される事を確認します。

スクリーンショット 2016-10-23 17.12.05.png

(28) WebブラウザからApacheへアクセスした時、作成したLaravelプロジェクトを表示出来るようhttpd.confを修正します。

[ec2-user@example-laravel-server exampleapp]$ sudo cp -p /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
[ec2-user@example-laravel-server exampleapp]$ diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
[ec2-user@example-laravel-server exampleapp]$ 

httpd.confの以下の箇所を変更します。

[ec2-user@example-laravel-server exampleapp]$ sudo vi /etc/httpd/conf/httpd.conf 

以下の箇所を変更します。
 (中略)
DocumentRoot "/var/www/laravel/laravel/public"
 ↓
DocumentRoot "/var/www/laravel/exampleapp/public"
 (中略)

 (中略)
<Directory "/var/www/laravel/laravel/public">
    AllowOverride All
</Directory>
 ↓
<Directory "/var/www/laravel/exampleapp/public">
    AllowOverride All
</Directory>
 (中略)
[ec2-user@example-laravel-server exampleapp]$ diff /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.`date '+%Y%m%d'`
120,121c120
< ##DocumentRoot "/var/www/laravel/laravel/public"
< DocumentRoot "/var/www/laravel/exampleapp/public"
---
> DocumentRoot "/var/www/laravel/laravel/public"
161,162c160
< ##<Directory "/var/www/laravel/laravel/public">
< <Directory "/var/www/laravel/exampleapp/public">
---
> <Directory "/var/www/laravel/laravel/public">
[ec2-user@example-laravel-server exampleapp]$ 
[ec2-user@example-laravel-server exampleapp]$ sudo su - 
[root@example-laravel-server ~]# /etc/init.d/httpd configtest
Syntax OK
[root@example-laravel-server ~]# /etc/init.d/httpd graceful
Gracefully restarting httpd: 
[root@example-laravel-server ~]# 
[root@example-laravel-server ~]# ps awux | grep -v grep | grep http
root      3060  0.0  2.2 407476 23272 ?        Ss   15:45   0:00 /usr/sbin/httpd
apache    4088  0.0  1.4 415672 14496 ?        Sl   17:20   0:00 /usr/sbin/httpd
apache    4089  0.0  1.4 415672 14496 ?        Sl   17:20   0:00 /usr/sbin/httpd
apache    4090  0.0  1.4 612344 14496 ?        Sl   17:20   0:00 /usr/sbin/httpd
apache    4091  0.0  1.4 415672 14496 ?        Sl   17:20   0:00 /usr/sbin/httpd
apache    4092  0.0  1.4 415672 14496 ?        Sl   17:20   0:00 /usr/sbin/httpd
[root@example-laravel-server ~]# 

(29) WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへアクセスします。

WebブラウザからPHP 5.6 + Laravel 5 をインストールしたEC2インスタンスへアクセスします。URLはEC2インスタンスのIPアドレスを指定します。

スクリーンショット 2016-10-23 17.30.23.png

「Laravel」と表示される事を確認します。
また、Laravelのログファイルにエラーが出ていなければ、Laravelプロジェクト作成は問題ありません。

[root@example-laravel-server ~]# tail -f /var/www/laravel/exampleapp/storage/logs/laravel.log 


以上になります。

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
21