docker搭建lnmp环境的实现步骤
作者:码农也是人
近期闲来无事,用公司电脑部署了一套环境,记录一下部署经过和踩的坑。
Nginx
1、拉取镜像,需要使用镜像创建容器
拉取的Nginx版本可以自己设,我是用的nginx:latest
// 拉取镜像 docker pull nginx:1.18.0 1.18.0: Pulling from library/nginx f7ec5a41d630: Pull complete 0b20d28b5eb3: Pull complete 1576642c9776: Pull complete c12a848bad84: Pull complete 03f221d9cf00: Pull complete Digest: sha256:e90ac5331fe095cea01b121a3627174b2e33e06e83720e9a934c7b8ccc9c55a0 Status: Downloaded newer image for nginx:1.18.0 docker.io/library/nginx:1.18.0 // 安装完成后可以使用命令查看安装了哪些镜像 docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 605c77e624dd 2 years ago 141MB
2、宿主机创建Nginx所需的映射文件
这个需要提前创建,因为如果直接使用docker命令创建容器会报错本地没有映射文件
先把nginx所需目录创建了
[root@localhost ~]# mkdir -pv /lnmp/nginx/{conf/conf.d,data,logs,html} mkdir: created directory ‘/lnmp' mkdir: created directory ‘/lnmp/nginx' mkdir: created directory ‘/lnmp/nginx/conf' mkdir: created directory ‘/lnmp/nginx/conf/conf.d' mkdir: created directory ‘/lnmp/nginx/data' mkdir: created directory ‘/lnmp/nginx/logs' mkdir: created directory ‘/lnmp/nginx/html' [root@localhost ~]# ls /lnmp/nginx/ conf data logs html [root@localhost ~]#
在conf中创建nginx.conf,conf.d中也需要一个default.conf,这些后面映射都会用到,创建好nginx配置文件后打开将默认配置存进去,因为在映射时本地的这个文件会将容器中的文件覆盖掉(当然你也可以先创建容器,将容器中的nginx.conf复制出来)
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; autoindex on; } location ~ \.php$ { // 这里是我踩的一个大大的坑 // 如果不指定php-fpm运行目录,会为空导致nginx无法解析php文件 // 所以这里必须要明确指定运行根目录!!! root /var/www/html; fastcgi_pass php81-fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; } }
3、创建容器
这里有一个点需要注意,在创建之前我们需要先创建一个 Docker 网络,以便 Nginx、MySQL 和 PHP 容器可以相互通信
docker network create lnmp-network
之后创建容器,创建成功后可以使用ps命令查看有哪些容器
docker run -it --name nginx --network lnmp-network -p 80:80 -v D:/lnmp/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v D:/lnmp/nginx/conf/conf.d:/etc/nginx/conf.d -v D:/lnmp/nginx/html:/usr/share/nginx/html -v D:/lnmp/nginx/logs:/var/log/nginx -d nginx:latest docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a9af314321d6 nginx:latest "/docker-entrypoint.…" 25 hours ago Up 57 minutes 0.0.0.0:80->80/tcp nginx
命令中的镜像名称和地址需要改成自己的,我这里只映射了80端口,443端口也可以一起映射,有需要的话就加上。
Mysql
没什么好说的,mysql还是一样的流程,上才艺
#拉取Mysql镜像,这里使用5.7版本 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.18.0 c2c45d506085 12 months ago 133MB [root@localhost ~]# docker pull mysql:5.7.32 5.7.32: Pulling from library/mysql a076a628af6f: Pull complete f6c208f3f991: Pull complete 88a9455a9165: Pull complete 406c9b8427c6: Pull complete 7c88599c0b25: Pull complete 25b5c6debdaf: Pull complete 43a5816f1617: Pull complete 7065aaa2655f: Pull complete b4bc531db40f: Pull complete 8c3e9d7c9815: Pull complete fadfb9734ed2: Pull complete Digest: sha256:e08834258fcc0efd01df358222333919df53d4a0d9b2a54da05b204b822e3b7b Status: Downloaded newer image for mysql:5.7.32 docker.io/library/mysql:5.7.32 [root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.18.0 c2c45d506085 12 months ago 133MB mysql 5.7.32 cc8775c0fe94 15 months ago 449MB [root@localhost ~]# #创建数据目录 [root@localhost ~]# mkdir -pv /lnmp/mysql/{conf,data,logs} mkdir: 已创建目录 "/lnmp/mysql" mkdir: 已创建目录 "/lnmp/mysql/conf" mkdir: 已创建目录 "/lnmp/mysql/data" mkdir: 已创建目录 "/lnmp/mysql/logs" [root@localhost ~]# ls /lnmp/mysql/ conf data logs [root@localhost ~]# #启动mysql容器 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d4c36a62dde4 nginx:1.18.0 "/docker-entrypoint.…" 18 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@localhost ~]# docker run -d -p 3306:3306 --name mysql \ > -v /lnmp/mysql/conf:/etc/mysql/conf.d \ > -v /lnmp/mysql/logs:/logs \ > -v /lnmp/mysql/data:/var/lib/mysql \ > -e MYSQL_ROOT_PASSWORD=root \ > mysql:5.7.32 55cc5694695d26327abee0580af2cbcaf28c49f33da8da24aa50c5b049946e01 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 55cc5694695d mysql:5.7.32 "docker-entrypoint.s…" 5 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql d4c36a62dde4 nginx:1.18.0 "/docker-entrypoint.…" 18 minutes ago Up 12 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp nginx [root@localhost ~]#
PHP
其实也还一样,拉取镜像
docker pull nfqlt/php81-fpm:latest
创建映射目录、文件
cd /lnmp/php-fpm mkdir logs touch php.ini touch php-fpm.conf
当然了,php.ini这种配置文件我是万万不会去弄的,所以我创建了一个临时容器,cp了出来哈哈,感兴趣的也可以这么操作,我把命令贴出来
// 启动容器(不映射 php.ini 文件) docker run -d --name php81-fpm --network lnmp-network -v D:/lnmp/nginx/html:/var/www/html -v D:/lnmp/php-fpm/logs:/var/log/php-fpm nfqlt/php81-fpm:latest // 复制容器中的 php.ini 文件到本地 docker cp php81-fpm:/etc/php/8.1/fpm/php.ini D:/lnmp/php-fpm/php.ini // 停止并删除原容器 docker stop php81-fpm docker rm php81-fpm
然后,熟悉的环节,创建容器
docker run -d --name php81-fpm --network lnmp-network -p 9000:9000 -v D:/lnmp/nginx/html:/var/www/html -v D:/lnmp/php-fpm/php.ini:/etc/php/8.1/fpm/php.ini -v D:/lnmp/php-fpm/php-fpm.conf:/etc/php/8.1/fpm/php-fpm.conf -v D:/lnmp/php-fpm/logs/error.log:/tmp/log/error.log nfqlt/php81-fpm:latest
注:D:/lnmp/nginx/html:/var/www/html映射这个的时候是使用映射nginx的运行目录(D:/lnmp/nginx/html)的地址来映射,/var/www/html这个是我们上面提到的我踩的大坑,需要设置php运行根目录,如果搞错了就会导致无法解析php文件!!
到此这篇关于docker搭建lnmp环境的实现步骤的文章就介绍到这了,更多相关docker搭建lnmp环境内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!