Docker 安装Nginx + PHP-FPM 7.2 + Reids + Composer

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 安装Nginx + PHP-FPM 7.2 + Reids + Composer相关的知识,希望对你有一定的参考价值。

原文:Docker 安装Nginx + PHP-FPM 7.2 + Reids + Composer

  1. 安装docker

参考 https://www.runoob.com/docker/centos-docker-install.html
- 安装一些必要的系统工具:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
- 添加软件源信息:
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
- 更新 yum 缓存:
sudo yum makecache fast
- 安装 Docker-ce:
sudo yum -y install docker-ce
- 启动 Docker 后台服务
sudo systemctl start docker
- 安装docker-compose
curl -L curl -L https://github.com/docker/compose/releases/download/1.17.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
-执行 运行权限
chmod -R 755 /usr/local/bin/docker-compose
- 测试运行 hello-world
docker run hello-world

  1. 安装nginx

参考 https://www.runoob.com/docker/docker-install-nginx.html
- 拉取Nginx镜像
docker pull nginx
- 新增目录
mkdir -p ~/nginx/html ~/nginx/logs ~/nginx/conf ~/nginx/conf.d
- 创建容器
docker run -v /root/nginx/html:/usr/share/nginx/html -v /root/nginx/conf.d:/etc/nginx/conf.d -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -p 80:80 -p 8888:8888 --name nginx -d nginx

  1. 安装php-fpm 7.2

参考 https://www.runoob.com/docker/docker-install-php.html
- 创建目录
mkdir ~/php/conf.d
- 拉取Nginx镜像
docker pull php:7.2-fpm
- 创建容器
docker run -v /root/nginx/html:/var/www/html -v /root/php:/usr/local/etc/php --name php-fpm -d php:7.2-fpm

  1. 新增~nginx/conf/nginx.conf文件
 user  nginx;	
				worker_processes  1;
				
				error_log  /var/log/nginx/error.log warn;
				pid        /var/run/nginx.pid;
				
				events {
				    worker_connections  1024;
				}
				http {
				    include       /etc/nginx/mime.types;
				    default_type  application/octet-stream;
				
				    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
				                      ‘$status $body_bytes_sent "$http_referer" ‘
				                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;
				
				    access_log  /var/log/nginx/access.log  main;
				    client_max_body_size 50m;
				    sendfile        on;
				    #tcp_nopush     on;
				
				    keepalive_timeout  65;
				
				    #gzip  on;
				    include /etc/nginx/conf.d/*.conf;
				}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  1. 新增~/nginx/conf.d/www.conf 文件

    #Carbonuni-PHP 项目目录自己根据项目定义
    #fastcgi_pass 根据下一步的FPM 地址定义 ,通过过 docker inspect php-fpm查看fpm容器ip地址

    server {
      		    listen       80;
      		    server_name  localhost;
      		
      		    #charset koi8-r;
      		    #access_log  /var/log/nginx/host.access.log  main;
      		
      		    location / {
      		        root   /usr/share/nginx/html/Carbonuni-PHP;
      		        index  index.php index.html index.htm;
      		        try_files $uri $uri/ /index.php?$query_string;
      		    }
      		
      		    #error_page  404              /404.html;
      		
      		    # redirect server error pages to the static page /50x.html
      		    #
      		    error_page   500 502 503 504  /50x.html;
      		    location = /50x.html {
      		        root   /usr/share/nginx/html/Carbonuni-PHP;
      		    }
      		
      		    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      		    #
      		    #location ~ .php$ {
      		    #    proxy_pass   http://127.0.0.1;
      		    #}
      		
      		    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      		    #
      		    location ~ .php {
      		        root           /usr/share/nginx/html/Carbonuni-PHP;
      		        fastcgi_pass   172.17.0.5:9000;
      		        fastcgi_index  index.php;
      		        fastcgi_split_path_info ^(.+.php)(.*)$;
      		        fastcgi_param   PATH_INFO $fastcgi_path_info;
      		        fastcgi_param   SCRIPT_FILENAME  /var/www/html/kod$fastcgi_script_name;
      		        include        fastcgi_params;
      		    }
      		
      		    # deny access to .htaccess files, if Apache‘s document root
      		    # concurs with nginx‘s one
      		    #
      		    #location ~ /.ht {
      		    #    deny  all;
      		    #}
      		}
      		```
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  2. 修改Nginx配置重启容器
    docker restart nginx

  3. 安装Redis

进入php-fpm 容器
docker exec -it php-fpm bash

curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.6.tar.gz

tar xfz /tmp/redis.tar.gz

rm -r /tmp/redis.tar.gz

mkdir -p /usr/src/php/ext

mv phpredis-3.1.6 /usr/src/php/ext/redis

docker-php-ext-install redis

  1. 安装一些扩展 可以通过 docker-php-ext-install

例如安装mysqli
docker-php-ext-install mysqli

  1. 安装GD库
    docker-php-ext-install gd
    这里有个报错

技术图片

#更新软件源
apt update
#安装各种库
apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev
#解压源码
docker-php-source extract
#gd源码文件夹
cd /usr/src/php/ext/gd
#准备编译
docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2
创建目录
mkdir /usr/local/etc/php/conf.d/
#编译安装
docker-php-ext-install gd
#查看安装进度
php -m | grep gd
#重启容器
docker restart php

  1. 增加composer

拉取composer
docker pull composer
进入XXX这项目 里面必须包含composer.json
docker run -it --name composer -v /root/nginx/html/XXX:/app --privileged=true composer composer install

  1. 运行云服务器ip 即可访问

以上是关于Docker 安装Nginx + PHP-FPM 7.2 + Reids + Composer的主要内容,如果未能解决你的问题,请参考以下文章

docker 部署nginx、php-fpm错误记录

docker创建nginx+php-fpm环境

Docker 生产就绪 php-fpm 和 nginx 配置

Docker - Nginx + Php-fpm 开发环境

docker 安装nginxphp-fpm

如何为多个PHP-FPM容器构建单一的Nginx Docker镜像