docker 搭建 lnmp 实践

10 篇文章 0 订阅
centos7 下 docker 的安装
sudo yum install -y yum-utils device-mapper-persistent-data lvm2

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

sudo yum install docker-ce -y
项目介绍

docker 环境搭建相关代码

git clone https://gitee.com/lvye1221/docker-deploy

【参考资料】docker compose php+mysql+nginx 连载,包含3篇
https://blog.csdn.net/tang05709/article/details/77996246

操作命令:


// 安装库依赖
docker-compose build

// 将会在后台启动并运行所有的容器
docker-compose up -d


// 启动服务器
docker-compose up
mysql

让装好的 mysql 支持远程连接

host为 % 表示不限制ip localhost表示本机使用 plugin非mysql_native_password 则需要修改密码

// 进入 mysql 容器 的命令行
docker exec -it docker_mysql_1 bash


mysql -uroot -p123456

mysql> ALTER user 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456'; 
Query OK, 0 rows affected (0.03 sec)

mysql> FLUSH PRIVILEGES;  
Query OK, 0 rows affected (0.00 sec)

参考网址: docker安装mysql遇到的问题
https://blog.csdn.net/zhaokejin521/article/details/80468908

docker 2.0 内存配置

docker_mysql_1 exited with code 137

问题
https://www.petefreitag.com/item/848.cfm

【解决办法】 设置 Linux 交换分区

详细参照此文档:
https://blog.csdn.net/zstack_org/article/details/53258588

fallocate -l 2G /swapfile
ls -lh /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile


vi /etc/fstab
/swapfile   swap    swap    sw  0   0



ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)

单独测试 mysql
docker search mysql

docker pull mysql:5.6

docker images |grep mysql


docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6


docker exec -it mymysql bash
安装vi

apt-get update

apt-get install -y vi
apt-get install -y vim

No input file specified.


<?php
echo phpinfo();

docker pull mysql

docker run -d -p 127.0.0.1:3306:3306 –name mysql -v ./testdb:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=”111111” mysql:latest

docker run –name docker_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/mysql restart=always

1 FastCGI sent in stderr: “Unable to open primary script: /code/t.php (Operation not permitted)”

1 FastCGI sent in stderr: “Unable to open primary script: (Operation not permitted)”

web_1 | 2018/08/13 08:28:36 [error] 6#6: *2 FastCGI sent in stderr: “Unable to open primary script: /code/t.php (Operation not permitted)” while reading response header from upstream, client: 183.214.113.54, server: localhost, request: “GET /t.php HTTP/1.1”, upstream: “fastcgi://172.19.0.2:9000”, host: “hope1995.me:8080”

[error] 6#6: *2 FastCGI sent in stderr: Unable to open primary script: (Operation not permitted)

【解决办法】删除 htdocs 目录下的 .user.ini 文件

连接 mysql 数据库

主机名,是写 docker-compse 中定义的镜像名字 mysql

用户名的权限要给够

docker-compose 密码加密16位改为41位


mysql
select host,user,password from mysql.user;
SET PASSWORD FOR 'root'@'%' = PASSWORD('123456'); 


---------
set @@session.old_passwords=0;
FLUSH PRIVILEGES; 

mysql> select password(“123456”); 
+——————————————-+ 
| password(“123456”) | 
+——————————————-+ 
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 
+——————————————-+ 
1 row in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是基于 Docker Swarm 搭建 LNMP 环境的步骤: 1. 安装 DockerDocker Swarm 2. 创建一个 Docker Swarm 集群 3. 创建一个 overlay 网络,用于容器之间的通信 ```bash docker network create --driver overlay my-network ``` 4. 创建一个 MySQL 服务 ```bash docker service create \ --name mysql \ --network my-network \ -e MYSQL_ROOT_PASSWORD=<password> \ -e MYSQL_DATABASE=<database> \ mysql:5.7 ``` 其中,`<password>` 是 MySQL 的 root 密码,`<database>` 是要创建的数据库名称。 5. 创建一个 Nginx 服务 ```bash docker service create \ --name nginx \ --network my-network \ -p 80:80 \ -p 443:443 \ -v /path/to/nginx.conf:/etc/nginx/nginx.conf \ --mount type=bind,source=/path/to/html,target=/usr/share/nginx/html \ nginx:latest ``` 其中,`/path/to/nginx.conf` 是自定义的 Nginx 配置文件路径,`/path/to/html` 是自定义的静态文件路径。 6. 创建一个 PHP-FPM 服务 ```bash docker service create \ --name php-fpm \ --network my-network \ --mount type=bind,source=/path/to/php,target=/var/www/html \ php:7-fpm ``` 其中,`/path/to/php` 是自定义的 PHP 代码路径。 7. 创建一个 Redis 服务 ```bash docker service create \ --name redis \ --network my-network \ redis:latest ``` 8. 创建一个运行 PHP 代码的服务 ```bash docker service create \ --name php \ --network my-network \ --mount type=bind,source=/path/to/php,target=/var/www/html \ --mount type=bind,source=/path/to/nginx.conf,target=/etc/nginx/nginx.conf \ --mount type=bind,source=/path/to/logs,target=/var/log/nginx \ --mount type=bind,source=/path/to/sessions,target=/var/lib/php/sessions \ --mount type=bind,source=/path/to/config,target=/usr/local/etc/php/conf.d \ php:7-fpm ``` 其中,`/path/to/logs` 是自定义的 Nginx 日志路径,`/path/to/sessions` 是自定义的 PHP 会话路径,`/path/to/config` 是自定义的 PHP 配置路径。 9. 验证 LNMP 环境是否正常工作 在浏览器中输入服务器的 IP 地址,应该能够看到 Nginx 的欢迎页面。在 PHP 代码中连接 MySQL 和 Redis 数据库,应该能够正常工作。 以上是基于 Docker Swarm 搭建 LNMP 环境的步骤,希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值