Background

最近剛換工作, 有需要建一個 php & lavarel 的環境在 CentOS上, 所以記錄一下. 在安裝之前要先確定 Server 的 timezone是否正確. 可以參考這篇 Changing the Time Zone on EC2

Installation

Install php

$ sudo yum -y update
$ sudo rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
$ sudo yum clean all
$ sudo yum install php56w php56w-common php56w-mcrypt php56w-dom php56w-mbstring php56w-fpm
$ php -v
PHP 5.6.30 (cli) (built: Jan 19 2017 22:50:24)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

Edit /etc/php.ini

為了增加security, 把cgi.fix_pathinfo=1改成0

$ sudo vim /etc/php.ini

/etc/php.ini

...
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec.  A setting
; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
...

Install nginx

$ sudo yum install nginx

Edit www.conf

$ sudo vim /etc/php-fpm.d/www.conf

/etc/php-fpm.d/www.conf

...

listen = 127.0.0.1:9000

...

# uncomment them
listen.owner = nobody
listen.group = nobody

...

# change their values from "apache" to "nginx"
user = nginx
group = nginx

$ sudo chown -R nginx:wwwnginxdata /var/log/php-fpm

Start php-fpm

$ sudo systemctl start php-fpm
or
$ sudo /etc/init.d/php-fpm start

Configure Nginx to Process PHP Pages

$ sudo vim /etc/nginx/conf.d/laravel.conf

/etc/nginx/conf.d/laravel.conf

server {
    listen       80;
    server_name  DOMAIN_NAME_OR_IP;

    # note that these lines are originally from the "location /" block
    root  /var/www/laravel/public;
    index index.php index.html index.htm;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
# start nginx
$ sudo systemctl start nginx
or 
$ sudo /etc/init.d/nginx start

Install composer for php

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
# 執行完後會發現一個 composer.phar 檔案. 你可以透過這個檔案來create a new laravel project
# 如果你希望可以在任何路徑下執行Composer PHAR. 執行下列指令.
$ sudo mv composer.phar /usr/local/bin/composer

Create a first laravel project

$ cd /var/www
# create project
$ composer create-project laravel/laravel laravel
or
$ sudo /usr/local/bin/composer create-project laravel/laravel laravel
# change permission
$ sudo chown -R :nginx /var/www/laravel
$ sudo chmod -R 775 /var/www/laravel/storage

Restart

$ sudo /etc/init.d/nginx restart
$ sudo /etc/init.d/php-fpm restart

Result

整個設定成功後再瀏覽器中輸入網址後應該會看到下圖.

References