前言 : 本篇文章仅仅使用docker基础 并未使用dockerfile等
部署lnmp之间通信的网络
[root@server nginx]# docker network create --subnet 192.168.0.0/16 --gateway 192.168.0.1 lnmp
[root@server nginx]# docker network ls
NETWORK ID NAME DRIVER SCOPE
96127bef1da0 bridge bridge local
1ed463648b6e host host local
e4b3d4a45cf5 lnmp bridge local
90e0f2c3f851 none null local
部署Nginx
# nginx
[root@server lnmp]# docker run \
-d \
-p 10002:80 \
--name Nginx \
-v /docker/lnmp/nginx/www/:/var/www/html \
-v /docker/lnmp/nginx/conf:/etc/nginx/conf.d \
--network lnmp nginx
-d 后台运行
-p 暴露端口
--name 设置容器名
-v 挂载目录
--network 设置网络连接方式
vim /docker/lnmp/nginx/conf/default.conf
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;
}
#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 /var/www/html$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;
#}
}
[root@server www]# docker exec -it Nginx bash service nginx reload
Reloading nginx: nginx.
部署mysql
# mysql
[root@server mysql]# docker run
-e MYSQL_ROOT_PASSWORD=root \
-d \
--name Mysql \
--network lnmp \
-v /docker/lnmp/mysql/data/:/var/lib/mysql \
-v /docker/lnmp/mysql/conf:/etc/mysql/conf.d \
-p 10000:3306 mysql:5.7
-e 设置环境变量
-d 后台运行
--name 设置容器名
--network 设置网络连接方式
-v 挂载目录
-p 暴露端口
部署PHP
[root@server lnmp]# docker run \
--name PHP \
-d \
-v /docker/lnmp/nginx/www:/var/www/html \
--network lnmp php:8.0.2-fpm
--name 设置容器名
-d 后台运行
-v 挂载目录
--network 设置网络连接方式
# php安装mysql扩展
[root@server www]# docker exec -it PHP docker-php-ext-install pdo_mysql mysqli
Configuring for:
PHP Api Version: 20200930
Zend Module Api No: 20200930
Zend Extension Api No: 420200930
...
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la modules/* libs/*
[root@server nginx]# docker exec -it PHP php -m
[PHP Modules]
...
mysqli
...
pdo_mysql # 刚刚安装的pdo_mysql扩展
...
[Zend Modules]
[root@server nginx]# docker restart PHP
# 测试
# 创建数据表
[root@server conf]# docker exec -it Mysql bash
root@b8316b59a84b:/# mysql -u root -p
mysql> create database phpLinkTest;
Query OK, 1 row affected (0.00 sec)
mysql> use phpLinkTest;
Database changed
mysql> create table test(
-> id int primary key,
-> name varchar(255) not null,
-> age int not null);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test value (1 , 'kafuu_chino' , 13);
Query OK, 1 row affected (0.01 sec)
exit
vim /docker/lnmp/nginx/www/test.php
<?php
define('HOSTNAME' , 'root');
define('PASSWORD' , 'root');
define('HOSTNAME' , '<hostname>:10000');
define('DATABASE' , 'phpLinkTest');
$link = mysqli_connect(HOSTNAME , USERNAME , PASSWORD , DATABASE);
$result = mysqli_query($link , 'select * from test');
print_r(mysqli_fetch_assoc($result));
?>
访问 : <hostname>:10002/test.php