Alpine(apk add)从零搭建 php7.2-fpm + nginx

Nginx

sudo docker run -it --name=alpine-php  -d alpine:latest sh
apk add nginx
adduser -D -g 'www' www
mkdir /www
chown -R www:www /var/lib/nginx
chown -R www:www /www
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig
vi /etc/nginx/nginx.conf #user改为www

启动nginx遇到点问题,官方用rc-service,实际操作会报一层又一层的错。正确的启动方式是

mkdir -p /run/nginx/
nginx -g "daemon off;"
或者
/usr/sbin/nginx

PHP

apk add php7-fpm php7-mcrypt php7-soap php7-openssl php7-gmp php7-pdo_odbc php7-json php7-dom php7-pdo php7-zip php7-mysqli php7-bcmath php7-gd php7-odbc php7-pdo_mysql php7-gettext php7-xmlreader php7-xmlrpc php7-bz2 php7-iconv php7-curl php7-ctype php7-redis

编辑一个php7.sh,里面写上后,运行一下

PHP_FPM_USER="www"
PHP_FPM_GROUP="www"
PHP_FPM_LISTEN_MODE="0660"
PHP_MEMORY_LIMIT="512M"
PHP_MAX_UPLOAD="50M"
PHP_MAX_FILE_UPLOAD="200"
PHP_MAX_POST="100M"
PHP_DISPLAY_ERRORS="On"
PHP_DISPLAY_STARTUP_ERRORS="On"
PHP_ERROR_REPORTING="E_COMPILE_ERROR\|E_RECOVERABLE_ERROR\|E_ERROR\|E_CORE_ERROR"
PHP_CGI_FIX_PATHINFO=0

sed -i "s|;listen.owner\s*=\s*nobody|listen.owner = ${PHP_FPM_USER}|g" /etc/php7/php-fpm.conf
sed -i "s|;listen.group\s*=\s*nobody|listen.group = ${PHP_FPM_GROUP}|g" /etc/php7/php-fpm.conf
sed -i "s|;listen.mode\s*=\s*0660|listen.mode = ${PHP_FPM_LISTEN_MODE}|g" /etc/php7/php-fpm.conf
sed -i "s|user\s*=\s*nobody|user = ${PHP_FPM_USER}|g" /etc/php7/php-fpm.conf
sed -i "s|group\s*=\s*nobody|group = ${PHP_FPM_GROUP}|g" /etc/php7/php-fpm.conf
sed -i "s|;log_level\s*=\s*notice|log_level = notice|g" /etc/php7/php-fpm.conf #uncommenting line 

sed -i "s|display_errors\s*=\s*Off|display_errors = ${PHP_DISPLAY_ERRORS}|i" /etc/php7/php.ini
sed -i "s|display_startup_errors\s*=\s*Off|display_startup_errors = ${PHP_DISPLAY_STARTUP_ERRORS}|i" /etc/php7/php.ini
sed -i "s|error_reporting\s*=\s*E_ALL & ~E_DEPRECATED & ~E_STRICT|error_reporting = ${PHP_ERROR_REPORTING}|i" /etc/php7/php.ini
sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php7/php.ini
sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${PHP_MAX_UPLOAD}|i" /etc/php7/php.ini
sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php7/php.ini
sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php7/php.ini
sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= ${PHP_CGI_FIX_PATHINFO}|i" /etc/php7/php.ini

启动php-fpm

 /usr/sbin/php-fpm7

为启动fpm和nginx写个sh

# cat docker-run.sh 
#!/bin/sh
nohup php-fpm > /var/log/php-fpm.log 2>&1 &
nginx -g "daemon off;"
# 赋予可执行权限
mv docker-run.sh /usr/local/bin/.
chmod -R 755 /usr/local/bin/docker-run.sh

重启fpm和nginx没有找到好方便,粗暴的docker restart
访问 index.php,得到phpinfo就大功告成了


Alpine(apk add)从零搭建 php7.2-fpm + nginx_第1张图片
phpinfo

打成镜像

先commit 方式打第一个镜像

sudo docker commit -a "yidao" -m "my first php" 3f04a8b4585b php7-nginx-alpine:7.2.10

通过Dockerfile,运行docker-run.sh把fpm、nginx run起来

mkdir php7
vi Dockerfile

写入

FROM php7-nginx-alpine:7.2.10

ENTRYPOINT ["docker-run.sh"]

最后build一个终极版镜像

sudo docker build -t php7 .
# run一个容器跑跑看,看到http://127.0.0.1:8085/index.php能看到phpinfo就大功告成
sudo docker run --name=php7 -p 8085:80 -d php7:latest

参考文章
https://wiki.alpinelinux.org/wiki/Nginx_with_PHP#Nginx_with_PHP7
https://github.com/gliderlabs/docker-alpine/issues/183
https://github.com/gliderlabs/docker-alpine/issues/185

你可能感兴趣的:(Alpine(apk add)从零搭建 php7.2-fpm + nginx)