EC2でphp環境構築 ① Nginx + php56 + php56-fpm + virtualhost
概要
サーバー構築の経験があんまりにもないので、今回やってみた流れをまとめてみた。
目次
- nginxインストール
- php及びphp-fpmインストール
- バーチャル環境構築
# EC2 IP Adress(仮)
52.111.111.111
nginxインストール
EC2へログイン
[hoge@local ~]$ ssh -i "XXX.pem" [email protected]
1. nginx情報確認
[ec2-user@ip-172-31-4-XXX ~]$ yum info nginx
2. nginxインストール
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install nginx -y
3. インストール確認
[ec2-user@ip-172-31-4-XXX ~]$ which nginx
/usr/sbin/nginx
4. nginx稼働
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx start
Starting nginx: [ OK ]
[ec2-user@ip-172-31-4-XXX ~]$ ps -ef | grep nginx
root 1563 1 0 09:06 ? 00:00:00 nginx: master process nginx
nginx 1564 1563 0 09:06 ? 00:00:00 nginx: worker process
ec2-user 1566 1385 0 09:06 pts/0 00:00:00 grep nginx
5. ブラウザへのアクセスを確認する
52.111.111.111
EC2へログイン
[hoge@local ~]$ ssh -i "XXX.pem" [email protected]
1. nginx情報確認
[ec2-user@ip-172-31-4-XXX ~]$ yum info nginx
2. nginxインストール
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install nginx -y
3. インストール確認
[ec2-user@ip-172-31-4-XXX ~]$ which nginx
/usr/sbin/nginx
4. nginx稼働
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx start
Starting nginx: [ OK ]
[ec2-user@ip-172-31-4-XXX ~]$ ps -ef | grep nginx
root 1563 1 0 09:06 ? 00:00:00 nginx: master process nginx
nginx 1564 1563 0 09:06 ? 00:00:00 nginx: worker process
ec2-user 1566 1385 0 09:06 pts/0 00:00:00 grep nginx
5. ブラウザへのアクセスを確認する
ブラウザで http://52.111.111.111 へアクセス。
以下のような画面が表示されれば OK 。
6. サービスの自動稼働に設定しておく
[ec2-user@ip-172-31-4-XXX ~]$ sudo chkconfig nginx on
7. nginxのログディレクトリへのアクセス確認
[ec2-user@ip-172-31-4-XXX ~]$ tail -f /var/log/nginx/access.log
tail: `/var/log/nginx/access.log' を 読み込み用に開くことが出来ません: 許可がありません
[ec2-user@ip-172-31-4-XXX ~]$ ls -ald /var/log/nginx
drwx------ 2 nginx nginx 4096 9月 20 08:26 /var/log/nginx
8. ログディレクトリへのアクセス権限を変更
[ec2-user@ip-172-31-4-XXX ~]$ sudo chmod 755 /var/log/nginx/
[ec2-user@ip-172-31-4-XXX ~]$ ls -ald /var/log/nginx
drwxr-xr-x 2 nginx nginx 4096 9月 20 08:26 /var/log/nginx
[ec2-user@ip-172-31-4-XXX ~]$ tail -f /var/log/nginx/access.log
XXX.XXX.XX.XXX - - [20/Sep/2015:08:29:09 +0000] ...
XXX.XXX.XX.XXX - - [20/Sep/2015:08:30:01 +0000] ...
php-fpmインストール
1. php5.6及びphp-fpmインストール
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install php56 php56-fpm -y
2. 確認用のphpファイル作成
/usr/share/nginx/html/phpinfo.php
<?php
phpinfo();
3. php-fpmの設定変更
userとgroupをapacheからnginxに変更
/etc/php-fpm-5.6.d/www.conf
- user = apache
+ user = nginx
- group = apache
+ group = nginx
4. nginxの設定変更
/etc/nginx/nginx.conf
http {
- index index.html index.htm;
+ index index.php index.html index.htm;
server {
root /usr/share/nginx/html;
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
+ location ~ \.php$ {
+ root html;
+ fastcgi_pass 127.0.0.1:9000;
+ fastcgi_index index.php;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ include fastcgi_params;
+ }
5. php-fpm稼働及び自動稼働設定
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm start
[ec2-user@ip-172-31-4-XXX ~]$ sudo chkconfig php-fpm on
6. nginx再起動
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
7. 設定変更の確認
[ec2-user@ip-172-31-4-XXX ~]$ curl -s http://localhost/phpinfo.php |grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.8 </td></tr>
8. nginxとphp-fpmの接続をsocketにする
php-fpmの設定変更
/etc/php-fpm-5.6.d/www.conf
- listen = 127.0.0.1:9000
+ listen = /var/run/php-fpm/php-fpm.sock
- ;listen.owner = nobody
- ;listen.group = nobody
+ listen.owner = nginx
+ listen.group = nginx
nginxの設定変更
/etc/nginx/nginx.conf
- fastcgi_pass 127.0.0.1:9000;
+ fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
サービス再起動
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm restart
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
php実行に問題ないか確認
[ec2-user@ip-172-31-4-XXX ~]$ curl -s http://localhost/phpinfo.php |grep "PHP Version"
バーチャル環境作成(feature1-develop.com)
1. php-fpmを設定
/etc/php-fpm-5.6.d/php-fpm-feature1-develop.conf
[feature1-develop]
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm-feature1-develop.sock
listen.owner = nginx
listen.group = nginx
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
2. nginxのvirtual.conf設定
/etc/nginx/conf.d/virtual.conf
server {
listen 80;
server_name feature1-develop.com;
access_log /var/log/nginx/feature1-develop.access.log main;
error_log /var/log/nginx/feature1-develop.error.log;
root /var/www/feature1;
index index.php;
location ~ \.php$ {
root /var/www/feature1;
fastcgi_pass unix:/var/run/php-fpm/php-fpm-feature1-develop.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3. ドキュメントルートディレクトリ作成
[ec2-user@ip-172-31-4-XXX ~]$ sudo mkdir -p /var/www/feature1
4. index.phpファイル作成
/var/www/feature1/index.php
<?php
phpinfo();
5. php-fpmとnginx再起動
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm restart
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
6. sockファイル追加確認
[ec2-user@ip-172-31-4-XXX ~]$ ls -al /var/run/php-fpm/php-fpm-feature1-develop.sock
srw-rw---- 1 nginx nginx 0 9月 20 08:54 /var/run/php-fpm/php-fpm-feature1-develop.sock
7. 自分のパソコンにバーチャルホストへのアクセス設定
[hoge@local ~]$ sudo -- sh -c "echo 52.111.111.111 feature1-develop.com >> /etc/hosts"
8. アクセス確認
shell上で確認
# アクセス確認
[hoge@local ~]$ curl -s http://feature1-develop.com/phpinfo.php | grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.12 </td></tr>
# 「DOCUMENT_ROOT」確認
[hoge@local ~]$ curl -s http://feature1-develop.com/phpinfo.php | grep "DOCUMENT_ROOT"
<tr><td class="e">_SERVER["DOCUMENT_ROOT"]</td><td class="v">/var/www/feature1</td></tr>
ブラウザ上で確認
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install php56 php56-fpm -y
/usr/share/nginx/html/phpinfo.php
<?php
phpinfo();
/etc/php-fpm-5.6.d/www.conf
- user = apache
+ user = nginx
- group = apache
+ group = nginx
/etc/nginx/nginx.conf
http {
- index index.html index.htm;
+ index index.php index.html index.htm;
server {
root /usr/share/nginx/html;
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME/scripts$fastcgi_script_name;
- # include fastcgi_params;
- #}
+ location ~ \.php$ {
+ root html;
+ fastcgi_pass 127.0.0.1:9000;
+ fastcgi_index index.php;
+ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
+ include fastcgi_params;
+ }
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm start
[ec2-user@ip-172-31-4-XXX ~]$ sudo chkconfig php-fpm on
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
[ec2-user@ip-172-31-4-XXX ~]$ curl -s http://localhost/phpinfo.php |grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.8 </td></tr>
/etc/php-fpm-5.6.d/www.conf
- listen = 127.0.0.1:9000
+ listen = /var/run/php-fpm/php-fpm.sock
- ;listen.owner = nobody
- ;listen.group = nobody
+ listen.owner = nginx
+ listen.group = nginx
/etc/nginx/nginx.conf
- fastcgi_pass 127.0.0.1:9000;
+ fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm restart
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
[ec2-user@ip-172-31-4-XXX ~]$ curl -s http://localhost/phpinfo.php |grep "PHP Version"
1. php-fpmを設定
/etc/php-fpm-5.6.d/php-fpm-feature1-develop.conf
[feature1-develop]
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm-feature1-develop.sock
listen.owner = nginx
listen.group = nginx
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
2. nginxのvirtual.conf設定
/etc/nginx/conf.d/virtual.conf
server {
listen 80;
server_name feature1-develop.com;
access_log /var/log/nginx/feature1-develop.access.log main;
error_log /var/log/nginx/feature1-develop.error.log;
root /var/www/feature1;
index index.php;
location ~ \.php$ {
root /var/www/feature1;
fastcgi_pass unix:/var/run/php-fpm/php-fpm-feature1-develop.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3. ドキュメントルートディレクトリ作成
[ec2-user@ip-172-31-4-XXX ~]$ sudo mkdir -p /var/www/feature1
4. index.phpファイル作成
/var/www/feature1/index.php
<?php
phpinfo();
5. php-fpmとnginx再起動
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm restart
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
6. sockファイル追加確認
[ec2-user@ip-172-31-4-XXX ~]$ ls -al /var/run/php-fpm/php-fpm-feature1-develop.sock
srw-rw---- 1 nginx nginx 0 9月 20 08:54 /var/run/php-fpm/php-fpm-feature1-develop.sock
7. 自分のパソコンにバーチャルホストへのアクセス設定
[hoge@local ~]$ sudo -- sh -c "echo 52.111.111.111 feature1-develop.com >> /etc/hosts"
8. アクセス確認
shell上で確認
# アクセス確認
[hoge@local ~]$ curl -s http://feature1-develop.com/phpinfo.php | grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.12 </td></tr>
# 「DOCUMENT_ROOT」確認
[hoge@local ~]$ curl -s http://feature1-develop.com/phpinfo.php | grep "DOCUMENT_ROOT"
<tr><td class="e">_SERVER["DOCUMENT_ROOT"]</td><td class="v">/var/www/feature1</td></tr>
ブラウザ上で確認
http://feature1-develop.com/phpinfo.php
「DOCUMENT_ROOT」が「/var/www/feature1」になっているか確認
次にやったこと
Author And Source
この問題について(EC2でphp環境構築 ① Nginx + php56 + php56-fpm + virtualhost), 我々は、より多くの情報をここで見つけました https://qiita.com/jjongs08/items/805aab3dc3483b499785著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .