Docker搭建LNP环境

我这里是基于nginx和php-fpm分离的环境

原理 细节-可以看 ubuntu中nginx使用php-fpm、主要区别是本文是基于dockerfile的

nginx Dockerfile

FROM ubuntu:19.04
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
WORKDIR /
RUN apt update \
&& apt install nginx -y 
RUN rm /etc/nginx/sites-available/default 
WORKDIR /etc/nginx/sites-available
COPY default .
CMD nginx -g "daemon off;"

nginx 配置文件default(需和dockerfile一个文件夹)

server{
        listen 80;
        root /var/www/html;
        index index.html index.htm index.php;
        server_name _;
        location / {
                try_files $uri $uri/ =404;
        }
	location ~ \.php$ {
  ### fastcgi_pass 是php-fpm主机地址和端口
          fastcgi_pass 2.2.2.2:9000;
          include fastcgi_params;
          root /var/www/html/;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

构建

docker build -t nginx:1.0 .

启动命令

docker run -idt -p 80:80 -v /var/www/html:/var/www/html --name nginx nginx:1.0

php-fpm Dockerfile

FROM ubuntu:18.04
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
WORKDIR /
RUN apt update \
&& apt install php-fpm -y \
&& apt install php-mysql php-gd php-xml php-mbstring php-curl php-zip -y \
&& apt install php-sqlite3 php-ldap php-json php-redis php-imagick -y \
&& echo "listen = 9000" >> /etc/php/7.2/fpm/pool.d/www.conf \
&& echo "#!/bin/bash" >> /start.sh \
&& echo "/etc/init.d/php7.2-fpm start">> /start.sh \
&& echo "chown -R www-data:www-data /var/www/html/">> /start.sh \
&& chmod +x /start.sh 
CMD ["sh", "-c", "./start.sh;bash"]

其中 有2排扩展,第一排扩展较为常用,第二排不常用,按需安装。

构建

docker build -t php:1.0 .

启动命令

docker run -idt -p 9000:9000 -v /var/www/html/:/var/www/html --name php php:1.0

Author: 佛西

Leave a Reply

Your email address will not be published.