轉載:但願對彼此有幫助php
實驗前準備: //導入鏡像 1.[root@localhost ~]# docker load < nginx.tar && docker load < php.7.2-fpm.tar && docker load < mysql-5.7.tar //複製配置文件 1.[root@localhost ~]# docker run -itd --name test nginx:latest 2.[root@localhost ~]# docker cp test:/etc/nginx /root/compose-lnmp/docker/ 3.[root@localhost ~]# docker cp test:/usr/share/nginx/html /root/compose-lnmp/wwwroot 4.[root@localhost ~]# vim /root/compose-lnmp/wwwroot/html/index.html 5.[root@localhost ~]# cat /root/compose-lnmp/wwwroot/html/index.html 6.hello LNMP! //添加php測試界面 1.[root@localhost ~]# vim /root/compose-lnmp/wwwroot/html/test.php 2.[root@localhost ~]# cat /root/compose-lnmp/wwwroot/html/test.php 3.<?php 4.phpinfo(); 5.?> //設置tab鍵的空格數量 1.[root@localhost ~]# vim .vimrc 2.[root@localhost ~]# source .vimrc 3.[root@localhost ~]# cat .vimrc 4.set tabstop=2 //編寫docker-compose.yml文件 1.[root@localhost ~]# cd /root/compose-lnmp/ 2.[root@localhost compose-lnmp]# vim docker-compose.yml 3.version: "3.1" 4.services: 5. nginx: 6. container_name: nginx 7. image: nginx 8. networks: 9. lnmp: 10. ipv4_address: 172.16.10.10 11. restart: always 12. ports: 13. - 80:80 14. volumes: 15. - /root/compose-lnmp/wwwroot/html:/usr/share/nginx/html 16. - /root/compose-lnmp/docker/nginx:/etc/nginx 17. mysql: 18. container_name: mysql 19. image: mysql:5.7 20. networks: 21. lnmp: 22. ipv4_address: 172.16.10.20 23. restart: always 24. ports: 25. - 3306:3306 26. environment: 27. MYSQL_ROOT_PASSWORD: 123.com 28. php: 29. container_name: phpfpm 30. image: phpmysql 31. networks: 32. lnmp: 33. ipv4_address: 172.16.10.30 34. restart: always 35. ports: 36. - 9000:9000 37. volumes: 38. - /root/compose-lnmp/wwwroot/html:/usr/share/nginx/html 39.networks: 40. lnmp: 41. driver: bridge 42. ipam: 43. config: 44. - subnet: 172.16.10.0/24 45.[root@localhost compose-lnmp]# docker-compose up -d
//修改nginx配置文件,nginx和php鏈接 1.[root@localhost compose-lnmp]# cd docker/nginx/conf.d/ 2.[root@localhost conf.d]# vim default.conf 3. location / { 4. root /usr/share/nginx/html; 5. index index.html index.htm index.php; //添加php解析 6.} //打開此模塊,並更改相應信息: 1. location ~ \.php$ { 2. root /usr/share/nginx/html; 3. fastcgi_pass 172.16.10.30:9000; 4. fastcgi_index index.php; 5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 6. include fastcgi_params; 7.} //重啓 1.[root@localhost conf.d]# docker-compose restart
//php和mysql鏈接 1.[root@localhost compose-lnmp]# cd wwwroot/html/ 2.[root@localhost html]# unzip phpMyAdmin-4.9.1-all-languages.zip 3.[root@localhost html]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin //更改nginx配置文件 1.[root@localhost compose-lnmp]# cd docker/nginx/conf.d/ 2.[root@localhost conf.d]# vim default.conf //在27行添加 1.location /phpmyadmin { 2. root /usr/share/nginx/html; 3. index index.html index.htm index.php; 4.} //在43行添加 1.location ~ /phpmyadmin/(?<after_ali>(.*)\.(php|php5)?$) { 2. root /usr/share/nginx/html; 3. fastcgi_pass 172.16.10.30:9000; 4. fastcgi_index index.php; 5. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 6. include fastcgi_params; 7.} //重啓 1.[root@localhost conf.d]# docker-compose restart
//須要對php鏡像作出更改,添加php和mysql鏈接的模塊 寫一個Dockerfile 1.[root@localhost ~]# vim Dockerfile 2.FROM php:7.2-fpm 3.RUN apt-get update && apt-get install -y \ 4. libfreetype6-dev \ 5. libjpeg62-turbo-dev \ 6. libpng-dev \ 7. && docker-php-ext-install -j$(nproc) iconv \ 8. && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ 9. && docker-php-ext-install -j$(nproc) gd \ 10. && docker-php-ext-install mysqli pdo pdo_mysql 11.[root@localhost ~]# docker build -t phpmysql . //刪除容器,更改docker-compose.yml文件,並從新運行 1.[root@localhost compose-lnmp]# docker-compose stop 2.[root@localhost compose-lnmp]# docker-compose rm 3.[root@localhost compose-lnmp]# vim docker-compose.yml //將php使用的鏡像改成剛纔建立的鏡像 1.image: phpmysql 2.[root@localhost compose-lnmp]# docker-compose up -d //修改phpmyadmin的配置文件,指定鏈接數據庫的IP,而後重啓 1.[root@localhost compose-lnmp]# cd wwwroot/html/phpmyadmin/ 2.[root@localhost phpmyadmin]# cp config.sample.inc.php config.inc.php 3.[root@localhost phpmyadmin]# vim config.inc.php 4.$cfg['Servers'][$i]['host'] = '172.16.10.20'; 5.[root@localhost phpmyadmin]# cd - 6./root/compose-lnmp 7.[root@localhost compose-lnmp]# docker-compose restart //再次訪問 用戶名:root 密碼:123.com