LoginSignup
4
8

More than 5 years have passed since last update.

Dockerでアプリケーションサーバー(nginxとphp-fpm)

Posted at

はじめに

centos7コンテナー上で、
アプリケーションサーバー(nginx→PHP-FPM)みたいなところを目指す。

環境

Dockerはインストール済みということで。
foderaでやってます

cat /etc/redhat-release
->Fedora release 24 (Twenty Four)

アプリケーションサーバー

Dockerfile

FROM centos:7
RUN yum -y update && \
#git(特にいらんけど。。)
yum install -y git && \
#epel
yum -y install epel-release && \

#remi
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm  && \


#phpもろもろ
yum -y install --enablerepo=remi-php70 php && \
yum -y install --enablerepo=remi-php70 php-devel php-pear && \
yum -y install --enablerepo=remi-php70 php-mysqlnd php-mbstring && \
yum -y install --enablerepo=remi-php70 php-fpm && \

#nginxリポジトリ追加
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm && \
#nginxインストール
yum install nginx -y 

EXPOSE 80
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

buil&run

docker build -t php_app ./
docker run --privileged -d -p 8000:80 --name testphp php_app /sbin/init

コンフィグ周りの変更

とりあえずシェル起動

docker exec -it testphp /bin/bash

php.ini

vi /etc/php.ini
date.timezone = "Asia/Tokyo"←タイムゾーンの設定※行頭のコメントアウト;を外して、"Asia/Tokyo"と記述
mbstring.language = Japanese←デフォルト言語の設定※行頭のコメントアウト;を外す

nginx用にphp-fpmのユーザー周りの設定を変更する。

(デフォルトがapacheになっているので。)

vi /etc/php-fpm.d/www.conf
# 22行目あたり
userとgroupをapacheからnginxに変更

# 37行目あたり
listen = /var/run/php-fpm/php-fpm.sock

# 48行目あたり
listen.owner = nginx
listen.group = nginx
listen.mode = 0660

phpファイル作成

cd /usr/share/nginx/html
vi index.php

#ファイルの中身はこんな感じ。ま、wqで保存。
<?php
phpinfo();

nginxのコンフィグ変更

vi /etc/nginx/conf.d/default.conf

default.confの中身を変更


    # 30行目あたり
    location ~ \.php$ {
        #root   /usr/share/nginx/html;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

権限変える

chown -R nginx:nginx /usr/share/nginx/html
find /usr/share/nginx/html -type d -exec chmod 770 {} \;
find /usr/share/nginx/html -type f -exec chmod 660 {} \;

自動起動にして再起動

systemctl enable php-fpm.service
systemctl enable nginx.service
#抜けて再起動後、ちょっとまつ
exit
docker restart testphp

一応シェル起動して、プロセス確認(nginxとphp-fpmが起動してたらok)

docker exec -it testphp /bin/bash
ps aux | grep -e nginx -e php-fpm

ブラウザで確認

http://localhost:8000/index.php
ok

curlで確認(新しく覚えた技)

あんま使い方わからんけど。。

curl localhost/index.php | head

おまけ

nginxのエラーログをみる

tail -n 1000 -f /var/log/nginx/error.log

次回

phalcon3いれてみる

ゆくゆくは

WEBサーバー(nginx:FastCGI) → APPサーバー(PHP-FPM)
docker-composeも試す

参考

CentOS7.1でnginxを用いたウェブサーバの構築

4
8
0

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
4
8