Docker搭建LNMP

本操作在VMware中完成。

优化Linux系统

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &> /dev/null
setenforce 0
systemctl stop firewalld &> /dev/null
systemctl disable firewalld &> /dev/null
iptables -F
systemctl stop NetworkManager &> /dev/null
systemctl disable NetworkManager &> /dev/null
echo 1 > /proc/sys/net/ipv4/ip_forward
hostnamectl set-hostname docker-server
exit

配置yum源

[root@docker-server ~]# vim /etc/yum.repos.d/CentOS7.repo
​
[aliyun-os]
name=aliyun-os
baseurl=https://mirrors.aliyun.com/centos/7/os/x86_64/
enabled=1
gpgcheck=0
​
[aliyun-epel]
name=aliyun-epel
baseurl=https://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=0
​
[aliyun-extra]
name=aliyun-extra
baseurl=https://mirrors.aliyun.com/centos/7/extras/x86_64/
enabled=1
gpgcheck=0
​
[root@docker-server ~]# yum clean all && yum makecache
[root@docker-server ~]# yum install -y yum-utils
[root@docker-server ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@docker-server ~]# yum clean all && yum makecache

配置pip

[root@docker-server ~]# mkdir ~/.pip
[root@docker-server ~]# vim ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

安装docker

[root@docker-server ~]# yum remove -y docker docker-io docker-selinux python-docer-py
[root@docker-server ~]# yum install -y docker-ce
[root@docker-server ~]# systemctl start docker
[root@docker-server ~]# systemctl enable docker
[root@docker-server ~]# systemctl status docker
[root@docker-server ~]# docker version

配置国内Docker仓库加速站点

登录阿里云,控制台,搜索容器镜像服务,找到镜像加速器,看对应的操作文档。

[root@docker-server ~]# vim /etc/docker/daemon.json
{
    "registry-mirrors": [......]
}
​
[root@docker-server ~]# systemctl daemon-reload
[root@docker-server ~]# systemctl restart docker && systemctl status docker

测试拉取镜像是否成功

[root@docker-server ~]# docker pull centos:latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
root@docker-server ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   14 months ago   231MB

搭建LNMP

获取MySQL5.7镜像

[root@docker-server ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete 
93619dbc5b36: Pull complete 
99da31dd6142: Pull complete 
626033c43d70: Pull complete 
37d5d7efb64e: Pull complete 
ac563158d721: Pull complete 
d2ba16033dad: Pull complete 
0ceb82207cd7: Pull complete 
37f2405cae96: Pull complete 
e2482e017e53: Pull complete 
70deed891d42: Pull complete 
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
​
启动容器weme_mysql
​
[root@docker-server ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=weme --name weme_mysql mysql:5.7
3eecbc7e374139c986f7dc7712ef7fe4c9e019952e3897429f262dc3de1b16e8
参数说明
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-e 设置环境变量,这里设置mysql的root用户的初始密码
--name 容器的名称、只要求唯一性

获取php7.2镜像

[root@docker-server ~]# docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
6ec7b7d162b2: Pull complete 
db606474d60c: Pull complete 
afb30f0cd8e0: Pull complete 
3bb2e8051594: Pull complete 
4d71313b39b0: Pull complete 
381de550657f: Pull complete 
e671c4250cc8: Pull complete 
111da53eb201: Pull complete 
e12697892372: Pull complete 
0540b5ed3310: Pull complete 
0461cc2270a7: Pull complete 
Digest: sha256:9c84ae47fddb97b94d1d2e289635b7306142a5336bc4ece0a393458c5e0d2cef
Status: Downloaded newer image for php:7.2-fpm
docker.io/library/php:7.2-fpm
​
启动PHP容器
​
[root@docker-server ~]# docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link weme_mysql:mysql --name weme_phpfpm php:7.2-fpm
5f0ee6a370e096f4852e2030aa3baca7f7090dedefcbea3a9d5e2ba03770c2e5
参数说明
-d 让容器在后台运行
-p 添加主机到容器的端口映射
-v 添加目录映射,主机上的/var/nginx/www/html映射到容器里面 的/var/www/html,如果主机没有这个目录就创建这个目录,或者映射别的目录,但是后面路径要统一
-name 容器的名字
-link 容器与另外一个容器建立联系,这样就可以在当前的容器去使用另一个容器里的服务
​
测试目录映射
在主机/var/nginx/www/html目录下创建PHP测试界面,会直接映射到容器里面。
​
[root@docker-server ~]# vim /var/nginx/www/html/index.php
<?php
        phpinfo();
?>
[root@docker-server ~]# docker exec -it 5f0ee6a370e0 /bin/bash
root@5f0ee6a370e0:/var/www/html# ls
index.php
root@5f0ee6a370e0:/var/www/html# cat index.php
<?php
        phpinfo();
?>

获取Nginx1.12.2镜像

root@5f0ee6a370e0:/var/www/html# exit
exit
[root@docker-server ~]# docker pull nginx:1.12.2
1.12.2: Pulling from library/nginx
f2aa67a397c4: Pull complete 
e3eaf3d87fe0: Pull complete 
38cb13c1e4c9: Pull complete 
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12.2
docker.io/library/nginx:1.12.2
​
启动Nginx容器
​
[root@docker-server ~]# docker run -d -p 80:80 --name weme_nginx -v /var/nginx/www/html:/var/www/html --link weme_phpfpm:phpfpm --name weme_nginx nginx:1.12.2
6e0f65dd757366a4709f16ceb75fcf4025b158a641a6cec8c271d5c7e8959a7b
参数说明
-d 让容器运行在后台
-p 添加主机到容器的端口映射
-v 添加目录映射,这里最好nginx容器的根目录最好写成和php容器中根目录一样。但是不一定非要一样,如果不一样在配置nginx的时候需要注意
-name 容器的名称
-link 容器之间建立起来联系
​

修改Nginx的配置文件,让其支持php,方式有多种,这里我用直接拷贝出配置文件修改再导入进去即可。

[root@docker-server ~]# cd /opt/
[root@docker-server opt]# docker cp 6e0f65dd7573:/etc/nginx/conf.d/default.conf /opt/
[root@docker-server opt]# ls
containerd  default.conf
[root@docker-server opt]# cat default.conf 
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;
    }
​
    # 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;
    #}
}
​
[root@docker-server opt]# vim default.conf
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /var/www/html;                       # 修改这里默认的路径
        index  index.html index.htm index.php;     #这里加入添加php的方式
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           html;
        fastcgi_pass   5f0ee6a370e0:9000;        #这里可以用容器ID,也可以用容器IP,都具备唯一性,注意这里的9000端口是容器的端口,不是宿主机的端口
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;      #修改这里的路径
        include        fastcgi_params;
    }
}

修改完毕后复制配置文件到Nginx容器里面

[root@docker-server opt]# docker cp default.conf 6e0f65dd7573:/etc/nginx/conf.d/default.conf
​
进入容器里面重新加载配置文件
​
[root@docker-server opt]# docker exec -it 6e0f65dd7573 /bin/bash
root@6e0f65dd7573:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@6e0f65dd7573:/# nginx -s reload
2022/11/14 07:58:26 [notice] 22#22: signal process started
​
root@6e0f65dd7573:/# exit
exit
参数说明
-t 在容器里面生产一个伪终端
-i 对容器内的标准输入(STDIN)进行交互

查看所有容器映射的端口

[root@docker-server opt]# ss -anlt
State       Recv-Q Send-Q    Local Address:Port                   Peer Address:Port              
LISTEN      0      128                   *:3306                              *:*                  
LISTEN      0      128                   *:80                                *:*                  
LISTEN      0      128                   *:22                                *:*                  
LISTEN      0      100           127.0.0.1:25                                *:*                  
LISTEN      0      128                   *:9000                              *:*                  
LISTEN      0      128                  :::3306                             :::*                  
LISTEN      0      128                  :::80                               :::*                  
LISTEN      0      128                  :::22                               :::*                  
LISTEN      0      100                 ::1:25                               :::*                  
LISTEN      0      128                  :::9000                             :::*               

查看主机IP

[root@docker-server opt]# ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:97ff:feec:5367  prefixlen 64  scopeid 0x20<link>
        ether 02:42:97:ec:53:67  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5  bytes 438 (438.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.21  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::20c:29ff:fe36:61ae  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:36:61:ae  txqueuelen 1000  (Ethernet)
        RX packets 431069  bytes 581393905 (554.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 95605  bytes 6335972 (6.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
veth265e233: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::e857:b3ff:fe57:81c9  prefixlen 64  scopeid 0x20<link>
        ether ea:57:b3:57:81:c9  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
veth66a4318: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::a89d:a0ff:feb9:4226  prefixlen 64  scopeid 0x20<link>
        ether aa:9d:a0:b9:42:26  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 13  bytes 1086 (1.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
vethc79aab6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::f48f:2eff:fe43:d10b  prefixlen 64  scopeid 0x20<link>
        ether f6:8f:2e:43:d1:0b  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

通过主机IP访问PHP测试页面

[root@docker-server opt]# elinks 192.168.10.21
-bash: elinks: 未找到命令
[root@docker-server opt]# yum install -y elinks
​
[root@docker-server opt]# elinks 192.168.10.21

[root@docker-server opt]# cd /var/nginx/www/html/
[root@docker-server html]# ls
index.php
[root@docker-server html]# cat index.php 
<?php
        phpinfo();
?>
[root@docker-server html]# mv index.php index.php.bak

上传世界级飞机大战游戏代码

[root@docker-server html]# rz
​
没有此airplane-game.tar.gz请自行查找。
​
[root@docker-server html]# ls
airplane-game.tar.gz  index.php.bak
[root@docker-server html]# tar xvf airplane-game.tar.gz 
./audio/
./audio/arrow_shot.wav
./audio/bgm_zhandou2.mp3
./audio/bgm_zhuxuanlv.mp3
./audio/effcet_sfx_sandan.mp3
./audio/meteorit_explode.mp3
./css/
./css/main.css
./image/
./image/big_1.png
./image/blow1.gif
./image/blow2.gif
./image/blow4.gif
./image/bullet1.png
./image/hit_3.png
./image/img_bg_level_3.jpg
./image/img_bg_logo.jpg
./image/LOGO.png
./image/My_plane.png
./image/My_zidan.png
./image/small_5.png
./index.html
./js/
./js/main.js
​
[root@docker-server html]# ls
airplane-game.tar.gz  audio  css  image  index.html  index.php.bak  js
[root@docker-server html]# ll
-rw-r--r-- 1 root root 3553280 10月 18 09:43 airplane-game.tar.gz
drwxr-xr-x 2 root root     134 5月  18 2015 audio
drwxr-xr-x 2 root root      22 5月  17 2015 css
drwxr-xr-x 2 root root     234 5月  17 2015 image
-rw-r--r-- 1 root root    1492 5月  18 2015 index.html
-rw-r--r-- 1 root root      28 11月 14 14:56 index.php.bak
drwxr-xr-x 2 root root      21 5月  17 2015 js
[root@docker-server html]# ls -dl /var/nginx/www/html/
drwxr-xr-x 6 root root 120 Sep 15 08:37 /var/nginx/www/html/
[root@docker-server html]# elinks 192.168.10.21

通过浏览器输入主机IP就可以玩世界级飞机大战游戏

 

查看数据库状态

[root@docker-server ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
6e0f65dd7573   nginx:1.12.2   "nginx -g 'daemon of…"   58 minutes ago      Up 58 minutes      0.0.0.0:80->80/tcp, :::80->80/tcp                      weme_nginx
5f0ee6a370e0   php:7.2-fpm    "docker-php-entrypoi…"   About an hour ago   Up About an hour   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp              weme_phpfpm
3eecbc7e3741   mysql:5.7      "docker-entrypoint.s…"   About an hour ago   Up About an hour   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   weme_mysql
[root@docker-server ~]# docker exec -it 3eecbc7e3741 /bin/bash
root@3eecbc7e3741:/# mysql -uroot -pweme
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 2
Server version: 5.7.36 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.00 sec)
​
mysql> exit
Bye
root@3eecbc7e3741:/# exit
exit
​
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WeMeHM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值