LNMP填坑笔记: docker-compose.yml 部署 Mysql 5.7 和 php:5.5.38-fpm 和最新 Nginx

lnmp.png_new.webp

部署需要的文件打包 lnmp.tar

昨天有朋友要使用php5.3才能运行一个框架程序,DockerHub找到只有 php:5.5.38-fpm 镜像,然后就是使用 docker-compose.yml 部署 LNMP,因为Nginx和Php-Fpm不是同一个容器,搞了个把小时 phpinfo 才跑起来,但是自己也没有摸透。
我是使用 php-fpm 容器中 apt安装一个 低版本Nginx,自动配置起来,但是其实有很多坑,后来在另一个虚拟机环境搞到晚上11点才算正式摸透了。

docker-compose.yml

version: '3.1'
services:
    db:
        image: mysql:5.7
        container_name: mysql
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        volumes:
            - /data/mysql:/var/lib/mysql
        environment:
            MYSQL_DATABASE: test
            MYSQL_ROOT_PASSWORD: Mysql@2021

    adminer:
        image: adminer
        container_name: adminer
        restart: always
        ports:
            - 10086:8080

    nginx:
        image: nginx
        container_name: nginx
        restart: always
        volumes:
            - /data/www/:/var/www/html
        ports:
            - 80:80
            - 443:443

    php:
        image: php:5.5.38-fpm
        container_name: php
        restart: always
        volumes:
            - /data/www/:/var/www/html
        ports:
            - 9000:9000

php.png

重点来说 docker-compose.yml,使用这个方式部署的容器都可以使用容器名和组件名来代替IP互相访问。

比如使用访问数据库使用 db或者mysql连接, php:9000 用来连接php-fpm , 测试http使用 curl nginx:80

php-fpm容器的目录挂载是重点,昨天一直被坑在这里,显示错误 File not found. 而网上的文章都没有提这个重点。

部署和修改配置命令

# 安装 docker-compose 部署工具
wget https://262235.xyz/docker-compose  -O /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# 先建立 docker-compose.yml ,然后使用命令部署
docker-compose up -d
docker ps -a
docker cp /usr/bin/nano nginx:/usr/bin
docker exec -it nginx bash
mkdir -p /var/www/html/   # 官方nginx默认没这个目录,所以要建立一个
nano /etc/nginx/conf.d/default.conf
nginx -t
nginx -s reload
docker restart nginx

在宿主机 /data/www/ 目录中创建 index.php 文件,用以 Nginx Web 服务器测试 PHP-FPM

<?php phpinfo(); ?>

Nginx 配置,参考apt安装的nginx配置,而不是官方镜像的配置

  • Nginx 反向代理 Php-fpm 解释 php脚本,配置两行命令
include snippets/fastcgi-php.conf;  # fastcgi配置,fastcgi-php.conf 文件又包含 fastcgi.conf
fastcgi_pass php:9000;  # php:9000 是 Php-fpm容器开放的端口
  • 所以实际部署的时候 完全可以把三个文件写到一个支持ssl证书的 https.conf 配置文件中

default.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.php index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
        include snippets/fastcgi-php.conf;
    #
    #    # With php5-cgi alone:
        fastcgi_pass php:9000;
    #    # With php5-fpm:
    #    fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny all;
    #}
}

fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

snippetsfastcgi-php.conf

# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+?.php)(/.*)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi.conf;

相关推荐: CF 永久(15年)SSL证书,支持泛域名,而且是免费; 还有这样的好事情!?

为什么SSL证书不能长期有效关注SSL证书的朋友应该都知道SSL证书的有效期一直在缩短,从2018年3月1日起,SSL证书最长期限设置为825天,这一新规延续了不到两年,就在2020年2月苹果在CA/B论坛的会议上宣布,将不再信任2020年9月1日之后签发的有…

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论