编程教育资源分享平台

网站首页 > 技术文章 正文

《Docker基础》- 构建 PHP 运行环境

luoriw 2024-01-17 11:15:22 技术文章 16 ℃ 0 评论


1. 新建目录

mkdir -p /makeit/docker/nginx/conf.d \
/makeit/logs/nginx \
/makeit/web/homea \
/home/src

2. 获取镜像

docker pull php:7.3.7-fpm
docker pull nginx
docker pull mysql
docker pull redis

3. 下载应用

注:下载相关应用,解压至 /home/src(可用于默认配置文件的获取,有配置文件的可略过)

解压相应文件,获取默认配置 nginx.conf / php.ini 等

wget http://nginx.org/download/nginx-1.14.0.tar.gz
wget https://files.phpmyadmin.net/phpMyAdmin/4.8.2/phpMyAdmin-4.8.2-all-languages.zip
wget http://101.96.10.64/jp2.php.net/distributions/php-7.3.0.tar.gz

# 复制文件
tar zxvf phpMyAdmin-4.8.2-all-languages.zip \
tar zxvf php-7.2.8.tar.gz \
tar zxvf nginx-1.14.0.tar.gz
cp /home/src/php-7.2.8/php.ini-production /home/docker/php/php.ini

4. 运行镜像


4.1. MySQL

# 创建文件夹
mkdir -p /makeit/docker/mysql/data
cd /makeit/docker/mysql

# 5.5.61 版本
docker run -p 3306:3306 --name mysql \
-v /makeit/docker/mysql/conf:/etc/mysql/conf.d \
-v /makeit/docker/mysql/data:/etc/mysql/data \
-v /makeit/logs/mysql:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD="123456" \
--restart=always -d mysql:5.5.61

# 8.0.18 版本
# 启动一个临时 MySQL
docker run \
-p 3306:3306 \
--name mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--restart=always \
-d mysql

# 创建文件夹并复制容器文件
cd /makeit/docker/mysql
mkdir plugin private
docker cp mysql:/usr/lib/mysql/plugin /makeit/docker/mysql
docker cp mysql:/usr/lib/mysql/private /makeit/docker/mysql
docker cp mysql:/etc/mysql/ /makeit/docker/mysql/conf

# 删除临时容器后, 重新启动
# 密码可以简单些, 成功后进入容器修改密码
docker stop mysql
docker rm mysql
docker run \
-p 3306:3306 \
--name mysql \
-v /miit/docker/mysql/plugin:/usr/lib/mysql/plugin \
-v /miit/docker/mysql/private:/usr/lib/mysql/private \
-v /miit/docker/mysql/conf:/etc/mysql \
-v /miit/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--restart=always \
-d mysql

# 查看用户
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| root             | %         |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

# 修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '12345678';
Query OK, 0 rows affected (0.01 sec)

# 远程登录的帐户密码也需要修正
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '12345678';
Query OK, 0 rows affected (0.01 sec)


4.2. PHP

# 启动一临时 php, 查看 php 配置文件及其扩展目录
docker run --name php -p 9000:9000 \
-d --restart=always php:7.3.7-fpm

# 进入容器查看信息
# 注意 Configuration File (php.ini) Path 及 extension_dir 两个属性值
docker exec -it php /bin/bash
php -r 'phpinfo();'

# 知道目录后将内容复制到宿主机下(放置在 /makeit/docker/php 下,666f8de6d689 为容器 ID)
docker cp 666f8de6d689:/usr/local/lib/php/extensions/no-debug-non-zts-20180731/ /makeit/docker/php/ext
docker cp 666f8de6d689:/usr/local/etc/php/php.ini /makeit/docker/php/php.ini

# 删除临时后再重新启动
# www 目录需要与 nginx 保持一致
docker stop php
docker rm php
docker run --name php -p 9000:9000 \
-v /makeit/web:/www:rw \
-v /makeit/logs/php:/var/log/php \
-v /makeit/docker/php/ext:/usr/local/lib/php/extensions/no-debug-non-zts-20180731 \
-v /makeit/docker/php/php.ini:/usr/local/etc/php/php.ini \
-d --restart=always --link mysql:mysql php:7.3.7-fpm

# 运行成功后,进入容器,可用 `docker-php-ext-install` 命令来安装扩展;如 `docker-php-ext-install pdo_mysql`


4.3. Nginx

4.3.1 新建配置

# fastcgi_params
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  REQUEST_SCHEME     $scheme;
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;

# nginx.conf
# user  nginx;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

daemon off;
 
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;

    	sendfile        on;
    	# tcp_nopush     on;
 
    	keepalive_timeout  65;
 
	autoindex  on;
	
    	# gzip  on;
 
    	include /etc/nginx/conf.d/*.conf;
 
    	client_max_body_size 100M;
 
    	client_header_buffer_size    128k;
    	large_client_header_buffers  4  128k;
}

# default.conf
server {
    listen	80;
    listen	443 ssl;
    server_name	wildidea.cn www.wildidea.cn;

    root /www/makeit;

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;

    ssl_certificate               cert/www.pem;
    ssl_certificate_key        cert/www.key;
    ssl_protocols                TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                    ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers   on;
    ssl_session_timeout         5m;
    ssl_session_tickets         on;
    ssl_dhparam                 ssl/private/dhparam.pem;

    location / {
        index   index.html index.htm index.php index.jsp;
    }

    error_page  500 502 503 504 /50x.html;
    location /50x.html {
        root /www;
    }

    location ~ /\. {
        return 403;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|bmp|css|js|swf)$ {
        access_log off;
    }

    location ~ \.php$ {
        root           /www/makeit;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param  PATH_INFO        $fastcgi_path_info;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

4.3.2. 运行容器

docker run -it \
--name nginx \
-d -p 80:80 -p 443:443 --restart=always \
-v /makeit/web:/www \
-v /makeit/logs/nginx:/var/log/nginx \
-v /makeit/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /makeit/docker/nginx/conf.d:/etc/nginx/conf.d \
-v /makeit/docker/nginx/ssl:/etc/nginx/ssl \
-v /makeit/docker/nginx/letsencrypt:/etc/nginx/letsencrypt \
--link php:php nginx

4.3.3. 命令说明

-p 80:80:将容器的80端口映射到主机的80端口
--name nginx:将容器命名为 nginx
-v /wildidea/web:/www:将宿主机中的目录 web 挂载到容器的 www 目录下,以下同理
--link php:关联 php

注意 `nginx` 配置文件的路径,所有路径皆为 docker 容器路径,非宿主机的本地路径 ;

`nginx` 配置中的 fastcgi_pass,监听的是 php-fpm ;

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表
最新留言