Linux学习-docker部署LAMP
Posted 丢爸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-docker部署LAMP相关的知识,希望对你有一定的参考价值。
创建数据卷映射目录
[root@docker ~]# mkdir -p /opt/vol/mysql /opt/vol/html
启动基础镜像容器并挂载数据卷和ssh端口映射
[root@docker ~]# docker run -it --name='centos_lamp' -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html -p 2222:22 centos:6.10
优化容器yum源并安装软件
[root@1d325b66b13f /]# cd /etc/yum.repos.d
[root@1d325b66b13f yum.repos.d]# mkdir bak
[root@1d325b66b13f yum.repos.d]# mv *.repo bak/
[root@1d325b66b13f yum.repos.d]# vi local.repo
[local]
name = linuxbase
baseurl = ftp://192.168.88.101/centos6new
gpgcheck = 0
enabled = 1
#更新yum本地缓存
[root@1d325b66b13f yum.repos.d]# yum makecache
#安装sshd
[root@1d325b66b13f yum.repos.d]# yum install -y openssh-server
#给root用户设置密码
[root@1d325b66b13f yum.repos.d]# echo "123456" | passwd --stdin root
#生成密钥文件
[root@6224937c547c yum.repos.d]# /etc/init.d/sshd start
Generating SSH2 RSA host key: [ OK ]
Generating SSH1 RSA host key: [ OK ]
Generating SSH2 DSA host key: [ OK ]
Starting sshd: [ OK ]
#安装httpd,mysql,php
[root@6224937c547c yum.repos.d]# yum install -y httpd mysql-server php php-mysql
#启动mysql-server
[root@6224937c547c yum.repos.d]# /etc/init.d/mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h 6224937c547c password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[ OK ]
Starting mysqld: [ OK ]
[root@6224937c547c yum.repos.d]#
#授权用户并创建discuz数据库
[root@6224937c547c yum.repos.d]# mysql
Welcome to the MySQL monitor. Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.
mysql> grant all on *.* to root@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to discusz@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> create database discuz charset utf8;
Query OK, 1 row affected (0.00 sec)
#httpd初使化
[root@6224937c547c yum.repos.d]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2 for ServerName [ OK ]
#查看服务启动情况
[root@6224937c547c yum.repos.d]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 358/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 399/sshd
tcp 0 0 :::22 :::* LISTEN 399/ssh
制作第一版镜像
[root@docker Packages]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6224937c547c centos:6.10 "/bin/bash" 16 minutes ago Up 16 minutes exciting_tesla
[root@docker Packages]# docker container commit 6224937c547c tye/centos_lamp:v1
sha256:a3542ea096dfe30a4476b6a327cc76411a754f4db1cc497f9ad96753299cedf5
#查看本地镜像
[root@docker Packages]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
tye/centos_lamp v1 a3542ea096df 46 seconds ago 440MB
nginx latest 87a94228f133 6 days ago 133MB
tye lotus 87a94228f133 6 days ago 133MB
centos 6.10 f1af727749c4 4 weeks ago 194MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
制作下一步
#通过制作好的第一版镜像启动容器
[root@docker ~]# docker run -it --name="tye_centos_bbs_v2" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html -p 8080:80 tye/centos_lamp:v1
#启动mysql和httpd服务
[root@899c6299ae5e /]# /etc/init.d/mysqld start
Starting mysqld: [ OK ]
[root@899c6299ae5e /]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2 for ServerName
[ OK ]
#在/var/www/html目录下创建index.php测试文件,测试php是否可用--访问结果如下图
[root@899c6299ae5e html]# pwd
/var/www/html
[root@899c6299ae5e html]# vim index.php
<?php
phpinfo();
?>
安装discuz论坛
#上传下载的zip文件至服务器通过unzip命令进行解压
[root@docker html]# unzip Discuz_X3.1_SC_UTF8.zip
#创建bbs目录
[root@docker html]# mkdir bbs
#将解压后的upload目录下的文件拷贝至bbs目录
[root@docker html]# cp -avf upload/* bbs/
#在浏览器输入http://192.168.88.101:8080/bbs/install/进入安装向导,根据向导向下一步一步执行,向导如下图
制作第二版镜像
[root@docker bbs]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
899c6299ae5e tye/centos_lamp:v1 "/bin/bash" 36 minutes ago Up 36 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp tye_centos_bbs_v2
[root@docker bbs]# docker container commit 899c6299ae5e tye/centos_lamp:v2
sha256:cec4d57d1f5afd14c848b39256d84f1d75ee17d988fe807b2a68a1ac68532525
制作启动脚本
[root@docker bbs]# cd /opt/vol/html
#在服务启动最后启动ssh然后夯住
[root@docker html]# vim init.sh
#!/bin/bash
/etc/init.d/httpd start
/etc/init.d/mysqld start
/usr/sbin/sshd -D
#给init.sh文件添加执行权限
[root@docker html]# chmod +x init.sh
启动容器
#启动容器并进行数据卷映射和端口映射
[root@docker ~]# docker container run -d --name="tye_lamp_bbs" -v /opt/vol/mysql:/var/lib/mysql -v /opt/vol/html:/var/www/html -p 22222:22 -p 8080:80 -p 3306:3306 tye/centos_lamp:v2 /var/www/html/init.sh
e1b9442b73dad12ea777f8e464f10f7b3e4f01bb9c586ef51954125e93a96d1b
[root@docker html]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1b9442b73da tye/centos_lamp:v2 "/var/www/html/init.…" 53 seconds ago Up 52 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 0.0.0.0:22222->22/tcp, :::22222->22/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp tye_lamp_bbs
#测试httpd,sshd服务是否运行正常,见下图
数据库连接信息
以上是关于Linux学习-docker部署LAMP的主要内容,如果未能解决你的问题,请参考以下文章
使用 docker compose 的最佳部署策略(LAMP 堆栈)