Last active
May 20, 2019 08:10
-
-
Save zhangludi/274de59e54452ee56d6687bd6bc038ba to your computer and use it in GitHub Desktop.
docker ngixn php redis mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://segmentfault.com/a/1190000018643542 | |
https://blog.csdn.net/qq_36373262/article/details/79727223 | |
查看版本信息 | |
[root@ganbing ~]# cat /etc/redhat-release | |
CentOS Linux release 7.4.1708 (Core) | |
[root@ganbing ~]# docker version | |
Client: | |
Version: 17.12.0-ce | |
API version: 1.35 | |
Go version: go1.9.2 | |
Git commit: c97c6d6 | |
Built: Wed Dec 27 20:10:14 2017 | |
OS/Arch: linux/amd64 | |
Server: | |
Engine: | |
Version: 17.12.0-ce | |
API version: 1.35 (minimum version 1.12) | |
Go version: go1.9.2 | |
Git commit: c97c6d6 | |
Built: Wed Dec 27 20:12:46 2017 | |
OS/Arch: linux/amd64 | |
Experimental: false | |
建议用centos 7.4的版本,docker用的是17.12.0-ce 版本(ce是docker社区版、ee是docker企业版)。 | |
2.3创建自定义网络lnmp | |
[root@ganbing ~]# docker network create lnmp | |
baa8fe1582a7c38f9663668c4abbf1863e45d2c9ad6c140a93a8b1a41b78e2f6 | |
[root@ganbing ~]# docker network ls | |
NETWORK ID NAME DRIVER SCOPE | |
4e09a7db5303 bridge bridge local | |
d77497b635bb host host local | |
baa8fe1582a7 lnmp bridge local | |
637e45b21ed3 none null local | |
说明:从上面显示可以看出,docker服务运行后,默认有bridge、host、none这3个网络,网络介绍不是本文的重点,可以看到lnmp网络已创建好了。 | |
2.4修改镜像源 | |
我们默认去下载镜像走的是国外的源,下载会很慢,建议配置国内的镜像源来下载: | |
[root@ganbing ~]# vim /etc/docker/daemon.json | |
{ | |
"registry-mirrors": [ "https://registry.docker-cn.com"] | |
} | |
修改改镜像源后,还需要重启docker服务: | |
[root@ganbing ~]# systemctl restart docker | |
2.安装 mysql nginx redis php 相关的镜像 | |
docker pull mysql:latest | |
docker pull nginx:latest | |
docker pull redis:latest | |
docker pull php:7.2-fpm | |
这个我都是下载官方纯净的,然后你可以后面跟着版本号,如果不跟的话,默认是latest最后版本 | |
3. | |
启动 php-fpm | |
解释执行 php 需要 php-fpm,先让它运行起来: | |
docker run --name php72 -d -v ~/宿主机目录:容器目录 php:7.1-fpm | |
eg: docker run --name php72 -d -v /data:/usr/share/nginx/html php:7.2-fpm | |
说明: | |
php72 是容器的名字 | |
启动mysql | |
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.6 | |
–name 容器名称 | |
mysql 为自定义名称 | |
-p:暴露端口,映射端口(可以映射多个端口) 外部端口:容器内部端口 | |
-p 3306:3306 映射端口 | |
-e:设置任意环境变量(容器内) | |
MYSQL_ROOT_PASSWORD=root 这里指 设置数据库密码为root | |
启动redis | |
docker run --name redis -p 6379:6379 -d redis:latest | |
redis 为自定义名称 | |
-p 6379:6379 映射端口, 即 外部端口:容器内端口 | |
redis:latest 镜像名称:版本 | |
nginx ,php-fpm ,mysql,redis容器内链接 | |
# docker run --name lnmp --link mysql:db --link redis:redis --link php72:php -p 8888:80 -v 宿主机目录:容器目录 -d nginx:latest | |
eg : docker run --name lnmp --link mysql:db --link redis:redis --link php72:php -p 8888:80 -v /data:/usr/share/nginx/html -d nginx:latest | |
编辑 nginx 配置文件 | |
docker将容器中的conf.d中的default.conf的配置文件cp到本地 | |
docker cp nginx:/etc/nginx/conf.d/default.conf default.conf | |
查看所有容器内部的通讯ip | |
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) | |
/lnmp - 172.17.0.5 | |
/php72 - 172.17.0.2 | |
/redis - 172.17.0.4 | |
/mysql - 172.17.0.3 | |
server { | |
listen 80; | |
server_name localhost; | |
#charset koi8-r; | |
#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; | |
} | |
location ~ \.php$ { | |
root html; | |
fastcgi_pass 172.17.0.2:9000;#此处需要填写你的php容器的docker内部通讯ip | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
# 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 127.0.0.1:9000; | |
# fastcgi_index index.php; | |
# fastcgi_param SCRIPT_FILENAME /scripts$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; | |
#} | |
} | |
4.覆盖本地的配置文件到lnmp这个容器的某个目录中 | |
# docker cp /data/default.conf lnmp:/etc/nginx/conf.d/default.conf | |
5. | |
docker container stop lnmp | |
docker container start lnmp | |
重新启动容器,访问 # curl http://localhost:8888/index.php 就可以了 | |
6.链接数据库 | |
try { | |
$db = new PDO('mysql:host=172.17.0.3;dbname=test','root', 'root'); | |
$arr = $db->query("select * from user")->fetch(); | |
var_dump($arr);die; | |
} catch (PDOException $e) { | |
print "Error: " . $e->getMessage() . "<br/>"; | |
die(); | |
} | |
需要开启PDO 扩展 | |
docker exec -it php bash #进入容器 | |
cd /usr/local/bin #进入该目录 | |
./docker-php-ext-install pdo_mysql #安装pdo模块 | |
6.之后需要重新配置mysql | |
连接宿主机的数据时请分配好可连接的ip,修改可连接ip方法如下,注意172.17.0.1是容器的ip,而不是宿主机的ip | |
docker exec -it mysql bash #进入容器 | |
mysql -u root -p | |
mysql>use mysql; | |
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.17.0.3' IDENTIFIED BY 'password' WITH GRANT OPTION; | |
#mysql 8.0会报 语法错误 | |
修改如下两行 | |
########################################### | |
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root'; | |
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; | |
##################################################### | |
mysql> flush privileges; #刷新数据库 | |
报错: | |
The server requested authentication method unknown to the client | |
解决: | |
This issue still exists, but I don't know if this is PMA or something else, anyway as workaround: | |
$ docker-compose exec mysql bash | |
$ mysql -u root -p | |
You should be able to login as 'root' or your other password. | |
Execute the following commands: | |
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root'; | |
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root'; | |
ALTER USER 'default'@'%' IDENTIFIED WITH mysql_native_password BY 'secret'; | |
This should give you access in PMA. | |
CREATE DATABASE test; | |
USE test; | |
CREATE TABLE `user` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`name` varchar(45) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) | |
insert into user(name) values ('tom'); | |
redis扩展下载连接 | |
phpize | |
./configure | |
make && make install | |
这个时候编译安装完毕,然后会生成一个redis.so的扩展,这个时候要问他的配置文件在哪呢? | |
他的配置文件直接扔到/usr/local/etc/php/conf.d下面 | |
redis.ini文件名字 | |
extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/redis.so | |
重启php-fpm容器 | |
docker restart php72 | |
localhost:8888/index.php | |
echo phpinfo(); | |
会发现redis扩展已经装好 | |
写段代码吧 | |
$redis = new Redis(); | |
$redis->connect('172.17.0.5',6379); //容器ip | |
// print_r($redis);die; | |
$strname = 'push'; | |
$re = $redis->publish($strname,"来自小编自己写的 {$strname}推送!"); | |
echo "-----{$strname}----推送成功<br />"; | |
$redis->close(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment