安裝docker
yum install docker
按照此篇文章使用docker安裝lnmp成功,記錄下來防止忘記:https://blog.csdn.net/weixin_42890981/article/details/86749240
docker部署 lnmp
關閉防火牆、關閉selinux
[root@docker-server ~]# setenforce 0
[root@docker-server ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config //永久關閉selinux
[root@docker-server ~]# systemctl stop firewalld
[root@docker-server ~]# systemctl disable firewalld //開機不啟動防火牆
下載所需要的鏡像 (nginx 1.12.2 ,mysql 5.7,php 7.2) 版本可自選
獲取 Mysql 5.7 鏡像
[root@docker-server ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
5e6ec7f28fb7: Pull complete
4140e62498e1: Pull complete
e7bc612618a0: Pull complete
1af808cf1124: Pull complete
.....此處省略
啟動容器 cs_mysql
[root@docker-server ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=chens --name cs_mysql mysql:5.7
8cb2b1568775f01286a9b9f17979585ac582166644ab6b3ed24dea7e4d977357
參數說明
-d 讓容器在后台運行
-p 添加主機到容器的端口映射
-e 設置環境變量,這里設置mysql的root用戶的初始密碼
-name 容器的名稱、只要求唯一性
獲取php 7.2鏡像
[root@docker-server ~]# docker pull php:7.2-fpm
7.2-fpm: Pulling from library/php
5e6ec7f28fb7: Already exists
cf165947b5b7: Pull complete
7bd37682846d: Pull complete
99daf8e838e1: Pull complete
5e5a056cae49: Pull complete
dc6a1020a503: Pull complete
46ccabdcf8f0: Pull complete
啟動php 容器
[root@docker-server ~]# docker run -d -v /var/nginx/www/html:/var/www/html -p 9000:9000 --link cs_mysql:mysql --name cs_phpfpm php:7.2-fpm
92fc30be9a853759612646fb1bcc1067c348fedc90fc21b9c5afeb9d47506081
參數說明
-d 讓容器在后台運行
-p 添加主機到容器的端口映射
-v 添加目錄映射,主機上的/var/nginx/www/html映射到容器里面 的/var/www/html,如果主機沒有這個目錄就創建這個目錄,或者映射別的目錄,但是后面路徑要統一
-name 容器的名字
-link 容器與另外一個容器建立聯系,這樣就可以在當前的容器去使用另一個容器里的服務
測試目錄映射
在主機/var/nginx/www/html 目錄下創建php測試頁面,會直接映射到容器里面
[root@docker-server ~]# cat /var/nginx/www/html/index.php
<?
phpinfo();
?>
//看直接映射到了容器內部了
[root@docker-server ~]# docker exec -it 92fc30be9a85 /bin/bash //進入到php的容器里面了
root@92fc30be9a85:/var/www/html# ls
index.php
php的擴展安裝
安裝php-redis
root@92fc30be9a85:~# pecl install redis && docker-php-ext-enable redis
downloading redis-4.2.0.tgz ...
Starting to download redis-4.2.0.tgz (235,569 bytes)
.................................................done: 235,569 bytes
25 source files, building
running: phpize
Configuring for:
PHP Api Version: 20170718
Zend Module Api No: 20170718
Zend Extension Api No: 320170718
enable igbinary serializer support? [no] : //no 或者空格都可以
enable lzf compression support? [no] :
building in /tmp/pear/temp/pear-build-defaultuserJI3qgU/redis-4.2.0
running: /tmp/pear/temp/redis/configure --with-php-config=/usr/local/bin/php-config --enable-redis-igbinary=no --enable-redis-lzf=no
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for cc... cc
checking whether the C compiler works... yes
......此處省略
安裝后執行 php -m //查看安裝了那些
root@92fc30be9a85:~# php -m
[PHP Modules]
.。...此處省略
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
redis //這就是安裝的redis
Reflection
獲取Nginx 1.12.2鏡像
[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
啟動Nginx容器
[root@docker-server ~]# docker run -d -p 80:80 --name cs_nginx -v /var/nginx/www/html:/var/www/html --link cs_phpfpm:phpfpm --name cs_nginx nginx:1.12.2
2a8e910b40dbacf47b9487789d205e9e012bea918a7e2d0878539b7f18f23cda
參數說明
-d 讓容器運行在后台
-p 添加主機到容器的端口映射
-v 添加目錄映射,這里最好nginx容器的根目錄最好寫成和php容器中根目錄一樣。但是不一定非要一樣,如果不一樣在配置nginx的時候需要注意
-name 容器的名稱
-link 容器之間建立起來聯系
修改Nginx 的配置文件,使他支持php ,方式有多種,這里我直接拷貝出配置文件修改在導進去即可
[root@docker-server opt]# docker cp 2a8e910b40db:/etc/nginx/conf.d/default.conf /opt/
[root@docker-server opt]# cat default.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html; // 修改這里默認的路徑
index index.html index.htmi 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 92fc30be9a85:9000; //這里可以用容器ID,也可以用容器IP,都具備唯一性
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 2a8e910b40db:/etc/nginx/conf.d/default.conf
進入到容器里面重新加載配置文件
[root@docker-server ~]# docker exec -it 2a8e910b40db /bin/bash
root@2a8e910b40db:/# nginx -t Nginx檢測配置文件是否正確
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@2a8e910b40db:/# nginx -s reload
2019/02/02 15:56:49 [notice] 24#24: signal process started
參數說明
-t 在容器里面生產一個偽終端
-i 對容器內的標准輸入(STDIN)進行交互
查看所有容器映射的端口
[root@docker-server opt]# ss -anlt 注:容器映射的端口可以修改的
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::9000 :::*
LISTEN 0 128 :::3306 :::*
訪問瀏覽器測試
看到這個頁面說明搭建成功了!