linux|如何在docker里部署lnmp
简介实验步骤:
docker容器是一个开源的应用容器,具有高移植性,让开发者同意将所有应用以及依赖包打包到容器上,用起来十分方便;lnmp服务,L指linux操作系统,是目前最流行的开源操作系统;N指Nginx,是一个高性能http和反向代理服务器;M指mysql数据库,是一个小型关系型数据库管理系统;P指php,是一种在服务器上执行的嵌入型HTML文档的脚本语言。
1.安装docker
[root@localhost ~]# curl -sSL https://get.daocloud.io/docker | sh
# Executing docker install script, commit: 7cae5f8b0decc17d6571f9f52eb840fbc13b2737
+ sh -c 'yum install -y -q yum-utils'
软件包 yum-utils-1.1.31-54.el7_8.noarch 已安装并且是最新版本
............
[root@localhost ~]# systemctl restart docker
2.拉取mysql镜像并查看,创建本地存储映射文件目录
[root@localhost ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
69692152171a: Pull complete
1651b0be3df3: Pull complete
951da7386bc8: Pull complete
............
[root@localhost ~]# docker images | grep mysql
mysql5.72c9028880e584 days ago447MB
[root@localhost ~]# mkdir -p /opt/mysql/conf /opt/mysql/logs /opt/mysql/data
2.1.测试mysql是否能运行,注意第二个命令那password是设置自己的密码,我设为0000,大家自行设置,如下图则数据库安装成功
[root@localhost ~]# cd /opt/mysql
[root@localhost mysql]# docker run -p3306:3306 --name mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=0000 -d mysql:5.7
17557f7031f3e533ab824861f5971150c39e415e9986cd6e804ddca56b4b2811
[root@localhost mysql]# docker ps
CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
17557f7031f3mysql:5.7"docker-entrypoint.s…"22 seconds agoUp 20 seconds0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcpmysql
[root@localhost mysql]# docker exec -it mysql bash
root@17557f7031f3:/# mysql -p0000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.Commands end with ;
or \g.
Your MySQL connection id is 3
Server version: 5.7.34 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;
' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| mysql|
| performance_schema |
| sys|
+--------------------+
4 rows in set (0.06 sec)mysql>
3.拉取php镜像
[root@localhost ~]# mkdir -p /opt/www/html
[root@localhost ~]# docker pull php:7.0-fpm
7.0-fpm: Pulling from library/php
177e7ef0df69: Pull complete
9bf89f2eda24: Pull complete
350207dcf1b7: Pull complete
[root@localhost ~]# cd /opt/www/html
[root@localhost html]# cat index.php
[root@localhost html]# docker run -d -p 9000:9000 --name rc-phpfpm --restart=always -v $PWD/html:/var/www/html --link mysql:mysql php:7.0-fpm
7cf5f1e28fceec9cc09a330827bc30c6cd83482f0d63cecaebda9f85cacf34cf
[root@localhost html]# docker exec -ti rc-phpfpm /bin/bash
root@7cf5f1e28fce:/var/www/html# docker-php-ext-install pdo_mysql
Configuring for:
PHP Api Version:20151012
Zend Module Api No:20151012
Zend Extension Api No:320151012
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
4.拉取nginx镜像
[root@localhost html]# cd
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
69692152171a: Already exists
49f7d34d62c1: Pull complete
[root@localhost ~]# cd /opt/www
[root@localhost www]# docker run -d -p 80:80 --name rc-nginx -v $PWD/html:/var/www/html --link rc-phpfpm:rc-phpfpm nginx
945625a91830465b74048cca874e112392293c94d4601030fc5fac8a67e31a11
[root@localhost ~]# cat /etc/nginx.conf
server {listen 80;
server_name localhost;
charset utf-8;
localtion ~ \.php$ {root /var/www/html;
fastcgi_index index.php;
fastcgi_pass rc-phpfpm:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
【linux|如何在docker里部署lnmp】5.大功告成。现在测试一下所有服务是否部署完成
[root@localhost ~]# docker restart rc-nginx
rc-nginx
[root@localhost ~]# docker restart rc-phpfpm
rc-phpfpm
[root@localhost ~]# docker exec -it mysql bash
root@17557f7031f3:/# mysql -p0000
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.Commands end with ;
or \g.
Your MySQL connection id is 4
Server version: 5.7.34 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;
' or '\h' for help. Type '\c' to clear the current input statement.mysql>
总结:docker中部署lnmp还是比较简单的,主要就是在docker中拉取三个镜像就好了!
推荐阅读
- 任时光绽放成六月繁花
- 我从来不做坏事
- 考研英语阅读终极解决方案——阅读理解如何巧拿高分
- 樱花雨
- 如何寻找情感问答App的分析切入点
- 拍照一年啦,如果你想了解我,那就请先看看这篇文章
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus使用queryWrapper如何实现复杂查询
- 人如果没梦想,和咸鱼有什么区别(自媒体时代把握住就能咸鱼翻身)
- 如何在Mac中的文件选择框中打开系统隐藏文件夹