LoginSignup
9

More than 5 years have passed since last update.

posted at

updated at

PHP7 + PHP-FPM + nginx + Phalcon3 + EC2でAPIサーバ構築

はじめに

この記事は、オールアバウト Advent Calendar 2016の15日目の記事です。

こんばんわ。
オールアバウトではPhalconは使ってないですがプライベートネタで。

なぜPhalcon?

jsonデータのやりとりをするだけのサーバだったのでシンプルなマイクロフレームワークでよかったのと、PHP7にも対応してたので。(あと名前がカッコイイ...)
https://phalconphp.com/

ちなみに、今年のPHPカンファレンスでフレームワークアンケートでは6位だったようです。そこそこ人気?(全体数の5%だけど...orz)
https://freelance.levtech.jp/guide/detail/76/

EC2 Amazon AMI (t2.nano)構築

画面からポチポチするだけなのでここは割愛。

PHP7, nginx, その他のインストール

APIサーバで使いそうなのも一緒にインストール。
PHP7はremiから。

su -
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum install nginx
yum --enablerepo=remi-php70 install php-devel php-mcrypt
yum --enablerepo=remi-php70 --disablerepo=amzn-main install php php-opcache php-mbstring php-pecl-apc php-mysqlnd php-xml php-mysql php-fpm php-pecl-zip php-bcmath
rpm -qa | grep php

chkconfigの設定

再起動時にonになる様にレベルを設定。

chkconfig --add nginx
chkconfig nginx on
chkconfig --add php-fpm 
chkconfig php-fpm on

phalcon3の設定

Phalconはgithubにあるのでgitとインストールに必要なのを取得。
Phalcon3はzephirで動くのでgitでこちらも一緒にとってきます。
https://zephir-lang.com/

yum install pcre-devel gcc make re2c git

cd /usr/local/src
git clone https://github.com/phalcon/zephir
cd zephir/
./install -c

git clone http://github.com/phalcon/cphalcon
cd cphalcon
git checkout 3.0.x
zephir build --backend=ZendEngine3

エクステンションの設定はPHP7だと少し違う(のか?)様で引っかかりました。
/etc/php.d/配下にそれぞれiniファイルがナンバリングされているので以下の様にして作成。

php --ini

して場所を探して以下に追加しました。

/etc/php.d/99-phalcon.ini
extension=phalcon.so

composerも。
composer.jsonの内容は以下から。
https://phalconphp.com/ja/download/tools

mkdir /var/composer
cd /var/composer
curl -s http://getcomposer.org/installer | php
vi composer.json
php composer.phar install
sudo ln -s /var/composer/vendor/phalcon/devtools/phalcon.php /usr/bin/phalcon
sudo chmod ugo+x /usr/bin/phalcon

で、再起動すると無事に動きました。

/etc/init.d/nginx configtest
/etc/init.d/nginx start
/etc/init.d/php-fpm start

nginxとPHP-FPMの設定

nginxはバーチャルホストを使うのでポートをお好みで設定。

/etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
/etc/nginx/conf.d/virtual.conf
server {
    listen [お好きなポート];
    server_name localhost;

    index index.php index.html;
    set $root_path '[project-dir]/[project-name]/public';
    root $root_path;

    try_files $uri $uri/ @rewrite;

    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }

    location ~ \.php {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index /index.php;

        include /etc/nginx/fastcgi_params;

        fastcgi_split_path_info       ^(.+\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}

php−fpmの起動するユーザをnginxに。

/etc/php-fpm.d/www.conf
listen.owner = nginx
listen.group = nginx

phalconアプリ構築

phalconコマンドがあるのでこれで指定のディレクトリにアプリが作られます。

cd [project-dir]
phalcon project [project-name]

ちなみに、権限ゆるくしないとエラーになりました。

chmod -R 777 [project-dir]/[project-name]/cache/
chmod -R 777 /var/run/php-fpm/

で、起動。

/etc/init.d/nginx configtest
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart

で、ブラウザからアクセス。

http://127.0.0.1:[設定したポート]/

あとは、json返すプログラムを書けば完成です。
なんか勘違いしてたらコメントくださいw
ではー。

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
What you can do with signing up
9