使用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>
测试html
和php
文件
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
测试mysql
cd website/www/default
wget -c https://files.phpmyadmin.net/phpMyAdmin/5.2.0-rc1/phpMyAdmin-5.2.0-rc1-all-languages.zip
unzip phpMyAdmin-5.2.0-rc1-all-languages
cd phpMyAdmin-5.2.0-rc1-all-languages
cp config.sample.inc.php config.inc.php
vi config.inc.php
# 修改
$cfg['blowfish_secret'] = '123456';
$cfg['Servers'][$i]['host'] = '宿主机ip';
# 访问 http://宿主机ip/phpMyAdmin-5.2.0-rc1-all-languages/index.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);