Docker安装php运行环境(Nginx + Mysql5.7 + Php7.4 + Redis)

  • 拉取镜像

    # 拉取Nginx镜像
    docker pull nginx
    
    # 拉取Mysql镜像
    docker pull mysql:5.7
     
    # 拉取PHP7.4-fpm镜像
    docker pull php:7.4-fpm
    
    # 拉取Redis镜像
    docker pull redis
     
    
  • 创建容器

    # 创建mysql容器root密码为root
    docker run --name mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -d -v /d/DockerContainer/mysql:/var/lib/mysql mysql:5.7
    
    # 创建php容器 /d/DockerContainer/www:/project 需要和nginx容器保持一致
    docker run --name php7.4 -p 9001:9000 -d -v /d/DockerContainer/www:/project php:7.4-fpm
    
    # 创建nginx容器关联php-fpm
    docker run --name nginx -p 81:80 -p 444:443 -d -v /d/DockerContainer/www:/project -v /d/DockerContainer/nginx/conf:/etc/nginx/conf.d -v /d/DockerContainer/nginx/logs:/var/log/nginx -v /d/DockerContainer/nginx/cert:/etc/nginx/cert --link php7.4:php nginx
    
    # 创建redis容器
    docker run --name redis -p 6380:6379 -d redis
    

  • 安装php扩展

    # 进入php容器
    docker exec -it php7.4 /bin/bash
    
    # 安装mysqli扩展
    docker-php-ext-install mysqli
    
    # 安装bcmath扩展
    docker-php-ext-install bcmath
    
    # 安装pdo_mysql扩展
    docker-php-ext-install pdo_mysql
    
    # 安装redis扩展
    pecl install -o -f redis && rm -rf /tmp/pear && docker-php-ext-enable redis
    
    # 安装swoole扩展
    pecl install https://pecl.php.net/get/swoole-4.8.12.tgz && docker-php-ext-enable swoole
    
    # 修改php配置文件
    touch /usr/local/etc/php/conf.d/upload.ini
    
    # 在ini文件输入需要更新的内容
    file_uploads = On
    memory_limit = 800M
    upload_max_filesize = 800M
    post_max_size = 800M
    max_execution_time = 600
  • nginx配置文件

    # D:\DockerContainer\nginx\conf\localhost.conf
    
    server {
        listen       80;
        server_name  localhost;
        root   /project;
        index  index.html index.htm index.php;
        location ~ \.php$ {
            fastcgi_pass   php:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

你可能感兴趣的:(php,docker,nginx,swoole,mysql)