docker安装nginx、php、mysql(实际操作)

安装docker环境

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum -y install docker-ce
systemctl start docker

安装nginx:latest

docker pull nginx
docker run --name nginx --restart=always -d -p 80:80 nginx
mkdir -p images webps jsons videos www
cd /usr/local
mkdir -p nginx/{conf,log,html}
cd nginx/conf
mkdir cert
docker cp nginx:/etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /usr/local/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /usr/local/nginx
docker cp nginx:/var/log/nginx /usr/local/nginx/log
cd conf/conf.d
vim default.conf

在default.conf中插入如下代码,进行地址映射

location /files/ {
    alias /root/;
}
docker stop nginx
docker rm nginx
docker run --name nginx --restart=always -p 80:80 -p 443:443 -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /usr/local/nginx/log:/var/log/nginx -v /usr/local/nginx/html:/usr/share/nginx/html -v /usr/local/nginx/conf/cert:/etc/nginx/cert -v /achong:/root -d nginx

上传文件测试成功http://XXX.XXX.XXX.XXX/files/test.txt
压缩解压zip

yum install zip && yum install unzip

下载并上传证书到cert文件夹中

vim default.conf

配置https,实现https访问

server {
    listen       443 ssl;
    server_name  www.XXX.com;

    ssl_certificate      cert/xxxxxxx_www.XXX.com.pem;
    ssl_certificate_key  cert/xxxxxxx_www.XXXX.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers  on;

	location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /files/ {
        alias /root/;
    }
}

重启nginx服务

docker restart nginx

将IP地址换成域名,访问成功https://XXX.com/files/jsons/minyue.json

MySql-8.0.30安装

docker pull mysql:8.0.30
cd /usr/local
mkdir -p mysql/{conf,data}
docker run --name mysql --restart=always -d -p 3306:3306 -v /usr/local/mysql/conf:/etc/mysql/conf.d -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=XXXXXXXX mysql:8.0.30

PHP:7.4-fpm安装

docker pull php:7.4-fpm
docker run --name myphp -v /achong/www:/www --privileged=true -d --link mysql php:7.4-fpm
cd nginx/conf/conf.d
vim default.conf

在default.conf中插入如下代码,进行地址映射

location ~ \.php$ {
   #root           html;
    fastcgi_pass   php:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
    include        fastcgi_params;
}

重新启动nginx

docker run --name nginx --restart=always -p 80:80 -p 443:443 -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /usr/local/nginx/log:/var/log/nginx -v /usr/local/nginx/html:/usr/share/nginx/html -v /usr/local/nginx/conf/cert:/etc/nginx/cert -v /achong:/root --link myphp:php -d nginx

–link myphp:php链接php
如果只使用PDO进行数据库操作可以不进行下面三框代码(不过我也没试过)

// 进入php容器
// 安装扩展
docker exec -it myphp bash
cd /usr/local/bin
./docker-php-ext-install pdo_mysql
./docker-php-ext-install mysqli
cd /usr/local/etc/php
cp php.ini-development php.ini
// 无vim工具
// 从容其中复制php.ini出来
docker cp myphp:/usr/local/etc/php/php.ini /achong/
// 修改(去掉前面的;)
extension=mysqli
extension=pdo_mysql
// 复制回容器
docker cp /achong/php.ini myphp:/usr/local/etc/php/php.ini
// 退出容器,重启php,nginx服务
exit
docker restart myphp

在/achong/www/下创建文件hello.php


	echo phpinfo();
?>

测试http://XXX.XXX.XXX.XXX/hello.php成功

PHP连接MySql数据库

创建下面两个php
mysqli.php内容


	$conn = mysqli_connect("XXX.XXX.XXX.XXX", "root", "password", "dbname");
	if (!$conn) {
		die("连接失败: " . mysqli_connect_error());
	}

	$sql = "SELECT * FROM user_openid";
	$result = mysqli_query($conn, $sql);

	$data = array();
	while ($row = mysqli_fetch_assoc($result)) {
		$data[] = $row;
	}
	$json = json_encode($data);

	header('Content-Type: application/json');
	echo $json;

	mysqli_close($conn);
?>

pdo.php内容


	$servername = "XXX.XXX.XXX.XXX";
	$username = "root";
	$password = "password";
	$dbname = "dbname";
	try {
		$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		$sql = 'SELECT * FROM user_openid;';
		$stmt = $conn->query($sql);
		$res = $stmt->fetchAll(PDO::FETCH_ASSOC);

		header('Content-Type: application/json');
		echo json_encode($res);
	} catch(PDOException $e) {
		echo "连接失败: " . $e->getMessage();
	}
?>

测试之前创建记得数据库测试数据
测试方式同上hello.php
docker安装nginx、php、mysql(实际操作)_第1张图片
结果显示,两种情况下都可以运行
查询错误时有看到两者的区别
1、PDO支持多种数据库
2、MYSQLI仅支持MySql数据库

80端口配置

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /files/ {
        alias /root/;
    }

    #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;
    }

    # 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           html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$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;
    #}
}

443端口配置

server {
    listen       443 ssl;
    server_name  www.XXX.com;

    ssl_certificate      cert/xxxxxxx_www.XXX.com.pem;
    ssl_certificate_key  cert/xxxxxxx_www.XXX.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers  on;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /files/ {
        alias /root/;
    }
	
    location ~ \.php$ {
        # root           html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
	
}

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