使用docker配置环境

下载镜像

docker pull php:7.4-fpm-alpine

docker pull nginx:alpine

docker pull mysql:5.7

docker pull redis:alpine

创建一个bridge模式的network

docker network create test

启动容器

# -d 后台运行 
# -p 端口映射 
# --name 设置容器名 
# --network 设置网段 
# -v 持久化存储
# MYSQL_ROOT_PASSWORD 设置数据库密码
# php 
docker container run -d -p 9000:9000 --name php-web --network test -v ~/website/www:/var/www/html <镜像名/镜像id>
# nginx
docker container run -d -p 80:80 --name nginx-web --network test -v ~/website/www:/usr/share/nginx/html -v ~/website/conf.d:/etc/nginx/conf.d <镜像名/镜像id>
# mysql
docker container run -d -p 3306:3306 --name mysql-web --network test -v ~/website/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 <镜像名/镜像id>
# redis
docker container run -d -p 6379:6379 --name redis-web --network test <镜像名/镜像id>

测试htmlphp文件

cd website/www/default

vi index.html
<h1>index.html</h1>

vi test.php
<?php
phpinfo();

# 访问 http://宿主机ip:nginx端口/index.html or http://宿主机ip:nginx端口/test.php

安装PHP扩展

# 进入容器
docker container exec -it <容器id> sh
cd /usr/local/bin
./docker-php-ext-install mysqli # mysqli扩展 下载很慢
cd /usr/src/php/ext
curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.3.tar.gz
tar xfz /tmp/redis.tar.gz # 解压
rm -r /tmp/redis.tar.gz # 删除安装包
mv phpredis-3.1.3 redis # 改名
docker-php-ext-install redis   # redis扩展 
# 完成后重启容器
docker container restart <容器id>

站点配置

cd website/conf.d/

# 默认站点
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/project/default; # nginx站点目录
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   172.18.0.2:9000;  # php的ip
        fastcgi_index  index.php;        # php映射目录 
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/default/$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
        include        fastcgi_params;
    }
}

# 站点1  web1
server {
    listen       80;
    server_name  web1.com;

    location / {
        root   /usr/share/nginx/project/web1.com; # nginx站点目录
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   172.18.0.2:9000;	 # php的ip
        fastcgi_index  index.php;        # php映射目录 
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/web1.com/$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
        include        fastcgi_params;
    }
}

# 站点2  web2
server {
    listen       80;
    server_name  web2.com;

    location / {
        root   /usr/share/nginx/project/web2.com; # nginx站点目录
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
        fastcgi_pass   172.18.0.2:9000;	 # php的ip
        fastcgi_index  index.php;        # php映射目录 
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/web2.com/$fastcgi_script_name;
        fastcgi_param  SCRIPT_NAME $fastcgi_script_name;
        include        fastcgi_params;
    }
}

测试redis

cd website/www/default
vi redis.php

# php内容
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->lpush("tutorial-list", "Redis");
$redis->lpush("tutorial-list", "Mongodb");
$redis->lpush("tutorial-list", "Mysql");
$arList = $redis->lrange("tutorial-list", 0 ,5);
echo "Stored string in redis:: "
print_r($arList);

作者 baiwangea

《docker 配置php+nginx+mysql+redis+phpmyadmin环境(多个站点)》有241条评论
  1. Have you ever thought about including a little bit more than just
    your articles? I mean, what you say is valuable and
    everything. However imagine if you added some great pictures or videos to give
    your posts more, “pop”! Your content is excellent but with images and clips,
    this website could undeniably be one of the most beneficial in its field.
    Great blog!

  2. Excellent beat ! I would like to apprentice while you amend your web
    site, how could i subscribe for a blog website?
    The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear concept

  3. Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter updates.

    I’ve been looking for a plug-in like this for
    quite some time and was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything. I truly enjoy reading your blog and
    I look forward to your new updates.

  4. Great beat ! I would like to apprentice at the same time as you amend your
    site, how can i subscribe for a blog website? The account helped
    me a appropriate deal. I were a little bit acquainted of this your broadcast provided bright clear concept

发表评论

您的电子邮箱地址不会被公开。