Linux系统下使用docker来搭建现在市面上最火的LNMP架构

想要交流可以加入🐧群:748360476

LNMP动态网站部署架构是一套由Linux + Nginx + MySQL + PHP组成的动态网站系统解决方案。LNMP中的字母L是Linux系统的意思,不仅可以是RHEL、CentOS、Fedora,还可以是Debian、Ubuntu等系统。

搭建LNMP架构,使用源码包安装: 使用源码包来安装服务程序具有两个优势。 一、源码包的可移植性非常好,几乎可以在任何Linux系统中安装使用,而RPM软件包是针对特定系统和架构编写的指令集,必须严格地符合执行环境才能顺利安装(即只会去“生硬地”安装服务程序)。 二、使用源码包安装服务程序时会有一个编译过程,因此可以更好地适应安装主机的系统环境,运行效率和优化程度都会强于使用RPM软件包安装的服务程序。也就是说,可以将采用源码包安装服务程序的方式看作是针对系统的“量体裁衣”。 一般来讲,在安装软件时,如果能通过Yum软件仓库来安装,就用Yum方式;反之则去寻找合适的RPM软件包来安装;如果是在没有资源可用,那就只能使用源码包来安装了。

1、首先我们要对我们的服务器进行优化,这有利于我们后续的操作能够顺利进行。

systemctl stop firewalld
systemctl disable firewalld
iptables -F
systemctl stop NetworkManager
systemctl disable NetworkManager
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

2、配置yum源和pip

配置阿里源


vim /etc/yum.repos.d/CentOS7.repo

cat /etc/yum.repos.d/CentOS7.repo

[aliyun-os] name=aliyun-os baseurl=centos-7-os-x86_64安装包下载_开源镜像站-阿里云 enabled=1 gpgcheck=0

[aliyun-epel] name=aliyun-epel baseurl=epel-7-x86_64安装包下载_开源镜像站-阿里云 enabled=1 gpgcheck=0

[aliyun-extra] name=aliyun-extra baseurl=centos-7-extras-x86_64安装包下载_开源镜像站-阿里云 enabled=1 gpgcheck=0

yum clean all && yum makecache

yum install -y yum-utils

​

​
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #添加 docker-ce 源

cat /etc/yum.repos.d/docker-ce.repo

[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-debuginfo]
name=Docker CE Stable - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/debug-$basearch/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-stable-source]
name=Docker CE Stable - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/source/stable
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test]
name=Docker CE Test - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-debuginfo]
name=Docker CE Test - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/debug-$basearch/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-test-source]
name=Docker CE Test - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/source/test
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly]
name=Docker CE Nightly - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-debuginfo]
name=Docker CE Nightly - Debuginfo $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/debug-$basearch/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[docker-ce-nightly-source]
name=Docker CE Nightly - Sources
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/source/nightly
enabled=0
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

yum clean all && yum makecache

配置pip

​
mkdir ~/.pip

vim ~/.pip/pip.conf

cat ~/.pip/pip.conf

[global] index-url = Simple Index [install] trusted-host=mirrors.aliyun.com

3、安装docker,点击我查看如何安装

安装完docker后测试拉取镜像是否可以成功

docker pull centos:latest #拉取Centos最新版本

docker images #查看当前已有镜像

4、搭建LNMP

经过测试发现,MySQL5.7搭配php7.2、nginx 1.12.2时比较合适的,不会有太大的冲突

(1)获取 MySQL5.7 镜像

docker pull mysql:5.7

启动容器

docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name gang_mysql mysql:5.7

参数说明:

#-d 让容器在后台运行

#-p 添加主机到容器的端口映射,都为3306端口

#-e 设置环境变量,这里是设置MySQLroot用户的初始密码为123456

#--name 容器的名称,只要求唯一性,我们将它命名为gang_mysql

(2)获取 php7.2 镜像

docker pull php:7.2-fpm

启动 php 容器

docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link gang_mysql:mysql --name gang_phpfpm php:7.2-fpm

参数说明:

#-d 让容器在后台运行 #-p 添加主机到容器的端口映射

#-v 添加目录映射,主机上的/var/nginx/www/html映射到容器里面 的/var/www/html,如果主机没有这个目录就创建这个目录,或者映射别的目录,但是后面路径要统一

#--name 容器的名字 #-link 容器与另外一个容器建立联系,这样就可以在当前的容器去使用另一个容器里的服务

测试目录映射

我们可以发现,主机创建了 /var/nginx/www/html 目录,现在我们在这里面创建php测试页面,它会映射到容器内

vim /var/nginx/www/html/index.html

cat /var/nginx/www/html/index.php

<?
  php phpinfo(); 
?>

(3)拉取 nginx 1.12.2镜像

docker pull nginx:1.12.2

启动 Nginx 容器

docker run -d --name gang_nginx -v /var/nginx/www/html:/var/www/html -p 80:80 --link gang_phpfpm:phpfpm --name gang_nginx nginx:1.12.2

参数说明

-d 让容器运行在后台

-p 添加主机到容器的端口映射

-v 添加目录映射,这里最好nginx容器的根目录最好写成和php容器中根目录一样。但是不一定非要一样,如果不一样在配置nginx的时候需要注意

-name 容器的名称

-link 容器之间建立起来联系

docker ps

(4)修改Nginx的配置文件,使它支持php,方式有多种,像直接进去nginx容器里面改,或者直接拷贝出配置文件修改再导进去也可以等

这里我们采用拷贝出docker里面的配置文件修改再导进去的方法

cd /opt/

docker cp 83defc72f4f7:/etc/nginx/conf.d/default.conf /opt/ #这里的容器ID是Nginx的ID

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 index.php;    #添加index.php
}
​
#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   fddfe4898bf5:9000;    #这里可以用容器ID,也可以用容器IP,都具备唯一性,注意这里的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;
#}
}

修改上面的配置文件后复制回 Nginx 容器里面

docker cp ./default.conf 83defc72f4f7:/etc/nginx/conf.d/default.conf

进入到Nginx容器里面重新加载配置文件

docker exec -it 83defc72f4f7 /bin/bash

nginx -t

nginx -s reload

root@83defc72f4f7:/# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

root@83defc72f4f7:/# nginx -s reload

2022/11/12 13:40:37 [notice] 25#25: signal process started

5、查看所有容器映射的端口

[root@server99 ~]$ 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                                                          :::*

6、查看主机IP

[root@server99 ~]$ 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:27ff:fe40:e90b  prefixlen 64  scopeid 0x20<link>
        ether 02:42:27:40:e9:0b  txqueuelen 0  (Ethernet)
        RX packets 109  bytes 277586 (271.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 186  bytes 17187 (16.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.20.99  netmask 255.255.255.0  broadcast 192.168.20.255
        inet6 fe80::20c:29ff:feb2:d310  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:b2:d3:10  txqueuelen 1000  (Ethernet)
        RX packets 463744  bytes 663253528 (632.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 48840  bytes 4236398 (4.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 72  bytes 5600 (5.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 72  bytes 5600 (5.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
veth3facad3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fc91:e4ff:fe8b:f214  prefixlen 64  scopeid 0x20<link>
        ether fe:91:e4:8b:f2:14  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 16  bytes 1212 (1.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
vethb097f1b: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::c4e4:33ff:fe3b:f2bc  prefixlen 64  scopeid 0x20<link>
        ether c6:e4:33:3b:f2:bc  txqueuelen 0  (Ethernet)
        RX packets 40  bytes 271552 (265.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 6332 (6.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
vethcaf217c: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::1026:44ff:fe31:79c3  prefixlen 64  scopeid 0x20<link>
        ether 12:26:44:31:79:c3  txqueuelen 0  (Ethernet)
        RX packets 148  bytes 284670 (277.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 229  bytes 288949 (282.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

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

http://192.168.20.99/

到了这里,我们通过docker来搭建的LNMP架构就成功了,按照我的方法,是不是很简单快捷呢?

想要交流可以加入🐧群:748360476

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值