Linux操作文档——Docker部署LNMP环境


一、创建存储路径及网络

[root@localhost ~]# mkdir -p /lnmp/{wwwroot,docker,mysql}
[root@localhost ~]# docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 lnmp

二、运行nginx容器

[root@localhost ~]# docker run -itd --name test nginx
[root@localhost ~]# docker cp test:/etc/nginx /lnmp/docker/
[root@localhost ~]# docker cp test:/usr/share/nginx/html /lnmp/wwwroot/
[root@localhost ~]# docker run -itd --restart=always --name nginx -v /lnmp/docker/nginx:/etc/nginx -v /lnmp/wwwroot/html:/usr/share/nginx/html -p 80:80 --network lnmp --ip 172.16.10.10 nginx 
[root@localhost ~]# echo 123456 > /lnmp/wwwroot/html/index.html
[root@localhost ~]# curl 127.0.0.1
123456

在这里插入图片描述

三、运行mysql容器

[root@localhost ~]# docker run -itd --name mysqltest -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
[root@localhost ~]# docker cp mysqltest:/var/lib/mysql /lnmp/mysql/
[root@localhost ~]# docker run -itd --name mysql -p 3306:3306 -v /lnmp/mysql/mysql/:/var/lib/mysql --network lnmp --ip 172.16.10.20 mysql:5.7 
[root@localhost ~]# yum -y install mariadb
[root@docker01 ~]# mysql -uroot -p123456 -h 127.0.0.1
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

MySQL [(none)]> exit
Bye

四、运行php容器

[root@localhost ~]# docker run -itd --name phpfpm -p 9000:9000 -v /lnmp/wwwroot/html/:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 php:7.4-fpm
[root@localhost ~]# vim /lnmp/docker/nginx/conf.d/default.conf 
    location / {
    
    
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    location ~ \.php$ {
    
    
        root           /usr/share/nginx/html;
        fastcgi_pass   172.16.10.30:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
[root@localhost ~]# vim /lnmp/wwwroot/html/index.php
<?php
phpinfo();
?>
[root@localhost ~]# docker restart nginx
nginx

在这里插入图片描述

五、nginx和php连接

[root@localhost ~]# cd /lnmp/wwwroot/html/
[root@localhost html]# wget https://files.phpmyadmin.net/phpMyAdmin/5.0.2/phpMyAdmin-5.0.2-all-languages.zip
[root@localhost html]# unzip phpMyAdmin-5.0.2-all-languages.zip 
[root@localhost html]# mv phpMyAdmin-5.0.2-all-languages phpmyadmin
[root@localhost html]# cd /lnmp/docker/nginx/conf.d/
[root@localhost conf.d]# vim default.conf 
    location /phpmyadmin {
    
    
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
    
    location ~ /phpmyadmin/(?<after_ali>(.*)\.(php|php5)?$) {
    
    
        root           /usr/share/nginx/html;
        fastcgi_pass   172.16.10.30:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
     }
[root@localhost ~]# docker restart nginx
nginx

在这里插入图片描述

六、php和mysql的连接

[root@localhost ~]# cd /lnmp/
[root@localhost lnmp]# vim Dockerfile
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
        && docker-php-ext-install mysqli pdo pdo_mysql
[root@localhost lnmp]# docker build -t phpmysql .
[root@localhost ~]# docker stop phpfpm 
phpfpm
[root@localhost ~]# docker rm phpfpm 
phpfpm
[root@localhost ~]# docker run -itd --name phpfpm -p 9000:9000 -v /lnmp/wwwroot/html/:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 phpmysql
[root@localhost ~]# cd /lnmp/wwwroot/html/phpmyadmin/
[root@localhost phpmyadmin]# cp config.sample.inc.php config.inc.php
[root@localhost phpmyadmin]# vim config.inc.php 
$cfg['Servers'][$i]['host'] = '172.16.10.20';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
[root@localhost ~]# docker restart nginx
nginx

在这里插入图片描述
在这里插入图片描述

七、部署web应用

[root@localhost ~]# cd /lnmp/wwwroot/html
[root@localhost html]# unzip wordpress-4.9.4-zh_CN.zip 
[root@localhost html]# chmod -R 777 wordpress
[root@localhost ~]# mysql -uroot -p123456 -h 127.0.0.1
MySQL [(none)]> create database blog;
Query OK, 1 row affected (0.01 sec)

MySQL [(none)]> grant all on blog.* to lisi@'172.16.10.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)

MySQL [(none)]> exit
Bye
[root@localhost ~]# docker restart nginx
nginx

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/g950904/article/details/108509584
今日推荐