``这是大二实训时的一个热身内容,当时很仔细地写了 文档,希望会对别人有些帮助。
LAMP
L Linux
A Apache
M MySQL
P PHP
下面就是文档内容啦!
实训软硬件环境
硬件:64位操作系统,8GB安装内存的一台台式机
软件:安装了Centos7的Linux虚拟机
实现步骤
(1) 在Linux中安装docker,并启用docker
# yum install –y docker //安装docker
# systemctl start docker //启用docker
#systemctl enable docker //保持docker始终开启
为加快后续镜像等安装,执行docker_mirror.py脚本
# vi docker_mirror.py //创建并打开docker_mirror.py脚本
将脚本内容复制上后保存退出
# python docker_mirror.py //执行该脚本
如上述命令执行失败,查找系统里是否有python-docker-py.noarch
# yum list | grep docker
找到python-docker-py.noarch后安装
# yum install python-docker-py.noarch
重启docker服务
# systemctl restart docker.service
(2) 在docker中查找并安装相关镜像(httpd、mysql)
# docker search mysql //查找mysql相关镜像
#docker pull docker.io/centos/mysql-57-centos7 //本次安装的是mysql-57-centos7
# docker search httpd //查找httpd相关镜像
# docker pull docker.io/centos/httpd//安装httpd镜像
#docker images //查看安装上的镜像
(3) 利用安装好的两个镜像分别创建mysql容器和httpd容器并建立httpd与mysql的链接
#docker run –d -–name=mysql –e MYSQL_ROOT_PASSWORD=123456 docker.io/centos/mysql-57-centos7 //创建mysql容器
# docker run –d –P –u root –link mysql docker.io/centos/httpd //创建httpd容器,并与mysql容器建立连接
# docker ps –a //查看已创建容器
# docker exec –ti +mysql容器ID /bin/bash //可进入mysql容器,exit可退出
# docker exec –ti +httpd容器ID /bin/bash //可进入httpd容器
接下来在httpd容器中配置php,本次实验配置版本为php7.1(配置过程参考了)
首先配置yum源
#yum install -y epel-release
#rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
#yum update –y
安装php7.1及常用扩展
#yum install -y --enablerepo=remi --enablerepo=remi-php71 php php-devel php-mbstring php-mcrypt php-mysqlnd php-pdo php-gd
可清理yum缓存
#yum clean all
# rm -rf /var/cache/yum
使用phpinfo验证php是否连接成功
# docker restart httpd//重启httpd容器
进入容器中 /var/www/html 目录中,创建index.php脚本
# cd /var/www/html
# vi index.php
编辑脚本内容
<?php
Phpinfo();
?>
保存退出
退出容器
# docker port +httpd容器ID //查看端口号
在宿主机浏览器中输入宿主机IP:端口号
php环境部署成功
将部署成功的容器生成新的镜像
# docker commit 容器ID 新镜像名
# docker images //查看新的镜像
在mysql容器中创建数据库录入内容
# mysql –u root //进入mysql
# mysql-> show databases;
#mysql-> use mysql
#mysql-> show tables;
#mysql-> select host,user,authentication_string=password(“123456”) where user=’root’ and host=’localhost’;
#mysql-> flush privileges;
#mysql-> create database learndb;
退出mysql重新登录
# mysql –u root –p
# mysql -> use learndb;
# mysql ->CREATE TABLE personnel
(
id int NOT NULL AUTO_INCREMENT,
firstname varchar(25),
lastname varchar(20),
nick varchar(12),
email varchar(35),
salary int,
PRIMARY KEY (id),
UNIQUE id (id)
);
#mysql ->INSERT INTO personnel VALUES (‘1’,‘John’,‘Lever’,‘John’, ‘john@everywhere.net’,‘75000’);
#mysql ->INSERT INTO personnel VALUES (‘2’,‘Camilla’,‘Anderson’,‘Rose’, ‘rose@flower.com’,‘66000’);
退出mysql
#mysql-> exit
查询mysql容器的IP
#cat /etc/hosts
退出mysql容器
# exit
进入httpd容器
# docker exec –ti httpd容器号/bin/bash
#cd /var/www/html
#vi viewdb.php
将脚本复制进去后更改IP为刚刚查询到的mysql的 IP,保存退出
#yum install php-mysqlnd
# yum install php71-php-mysqlnd
# yum install mysqly
# yum install php71-php-pecl-mysql
退出httpd容器
在浏览器里输入mysql中localhost的ip:端口号/viewdb.php
可得到下面界面
至此,LAMP环境搭建完成。
下附docker_mirror.py文件的内容,记得看看有备注标明的地方,用自己的呀!!网上其实也有一些类似的脚本,大家可以自行选择~
#!/usr/bin/python
# coding=utf8
import platform
import re
import time
import os
import docker
import json
mirror_prefix = "--registry-mirror="
mirrors = {
"tencent": "https://mirror.ccs.tencentyun.com",
"netease": "http://hub-mirror.c.163.com",
"ustc": "https://docker.mirrors.ustc.edu.cn",
"official": "https://registry.docker-cn.com",
"aliyun": "https://……………………" # use your own aliyun mirror url instead.
}
docker_config_map = {
"Ubuntu": {
"config": "/etc/default/docker",
"prefix": "DOCKER_OPTS="
},
"CentOS Linux": {
"config": "/etc/sysconfig/docker",
"prefix": "OPTIONS="
},
"Deepin": {
"config": "/etc/default/docker",
"prefix": "DOCKER_OPTS="
}
}
docker_ce_config_map = {
"Deepin": {
"config": "/etc/docker/daemon.json",
# "prefix": "registry-mirror"
}
}
def get_dist():
return platform.linux_distribution()[0]
def get_config(dist):
return docker_config_map[dist]["config"]
def get_config_ce(dist):
return docker_ce_config_map[dist]["config"]
def get_prefix(dist):
return docker_config_map[dist]["prefix"]
def get_new_options(option, mirror):
option = option.strip()
quota = option[len(option) - 1]
if mirror_prefix in option:
r1 = re.compile('[\'\"]')
results1 = r1.split(option)
r2 = re.compile('[\s]')
results2 = r2.split(results1[1].strip())
for i in range(len(results2)):
if results2[i].startswith(mirror_prefix):
results2[i] = mirror_prefix + mirror
new_option = results1[0] + quota + " ".join(results2) + quota
else:
new_option = option[:-1] + " " + mirror_prefix + mirror + quota
new_option += "\n"
return new_option
def execute_sys_cmd(cmd):
result = os.system(cmd)
return result
def set_docker_config(mirror):
dist = get_dist()
docker_config = get_config(dist)
prefix = get_prefix(dist)
new_line = ""
options = ""
with open(docker_config, "r") as f:
for line in f:
if line.startswith(prefix):
options = get_new_options(line, mirror)
else:
new_line += line
if options == "":
options = prefix + "\'" + mirror_prefix + mirror + "\'"
with open(docker_config, "w") as f:
f.write(new_line)
f.writelines(options)
def set_docker_config_ce(mirror):
dist = get_dist()
docker_config = get_config_ce(dist)
config_dict={}
if os.path.exists(docker_config) != True:
# if file not exist, create it first.
os.mknod(docker_config, 0644)
else:
with open(docker_config, "r") as f:
config_dict = json.load(f)
config_dict[u'registry-mirrors'] = [mirror]
with open(docker_config, "w") as f:
json.dump(config_dict, f)
def restart_docker_daemon():
execute_sys_cmd("systemctl restart docker")
def get_speed(mirror, mirror_url):
client = docker.from_env()
version = client.version()[u'Version']
if "ce" in version:
set_docker_config_ce(mirror_url)
else:
set_docker_config(mirror_url)
restart_docker_daemon()
# try to delete busybox image in case.
execute_sys_cmd("docker rmi registry:2 -f 1> /dev/null 2>&1")
print "pulling registry:2 from {mirror}".format(mirror=mirror)
begin_time = time.time()
execute_sys_cmd("docker pull registry:2 1> /dev/null 2>&1")
end_time = time.time()
cost_time = end_time - begin_time
print "mirror {mirror} cost time: {cost_time}\n".format(mirror=mirror, cost_time=cost_time)
# delete centos images every time.
execute_sys_cmd("docker rmi registry:2 -f 1> /dev/null 2>&1")
return 204800 / cost_time
if __name__ == "__main__":
max_speed = 0
best_mirror = ""
best_mirror_url = ""
for k, v in mirrors.items():
speed = get_speed(k, v)
if speed > max_speed:
max_speed = speed
best_mirror = k
best_mirror_url = v
print "best mirror is: {mirror}, set docker config and restart docker daemon now.".format(mirror=best_mirror)
set_docker_config(best_mirror_url)
restart_docker_daemon()