有一天,主机商跑路了。迫于无奈之下,我需要进行数据迁移。原先用的主机我可以什么都不管,直接用就行。而现在,只有一个centos服务器。一切从零开始...
我曾试过一个一个安装,从docker入手,一步一步搭建mysql,nginx,php。正当我即将看到胜利的曙光时,现实又给了我当头一棒。php扩展没有pdo_mysql,php.ini配置文件找不到等等。各种问题接踵而至。我曾多次想过放弃。不知道到底哪个环节出错了,如果让我从新再来,想想就让人心生疲惫。直到我看到了这个: DNMP(LNMP)一键安装程序 。注意:这不是广告,亲测,亲测。
我们先初步了解下他的强大之处:
好家伙,口气还真不小。事实是否真就如此呢。我们就来动手实操一番。是骡子是马拉出来溜溜。我们就按照他的要求,一步步操作。
1.准备工作(docker+docker-compose 1.7以上+git)
docker安装就不多加叙述,根据服务器系统的不同,百度一下对应的安装命令即可。
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
国内 daocloud 一键安装命令:
curl -sSL https://get.daocloud.io/docker | sh
安装完之后可以输入:docker -version查看版本。
docker-compose安装命令:先确定安装目录。我这里打算安装到home目录下的dnmp子目录:
下载docker-compose命令:要安装其他版本的 Compose,请替换 v2.2.2。
$ sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
至此docker和docker-compose已安装完成,而git只是为了clone项目。我们可以直接选择去git下载即可。这里就不安装了。下载完之后的目录结构如下:
我们将文件上传到dnmp目录下,这里我是通过xftp上传的。
2.启动项目
第一步和第二步我们已经完成,第三步不用管,现在我们直接进行第四步,继续终端:依次输入命令,别忘了启动docker,启动命令:start docker.service
[root@localhost dnmp]# docker-compose --version
Docker Compose version v2.2.2
[root@localhost dnmp]# ls
bash.alias.sample data docker-compose.sample.yml env.sample LICENSE logs README-en.md README.md services snapshot.png www
[root@localhost dnmp]# cp env.sample .env
[root@localhost dnmp]# cp docker-compose.sample.yml docker-compose.yml
[root@localhost dnmp]# docker-compose up
至此,基于docker的lnmp集成环境就安装完成啦~~,让我们奔放一下~
very good !!!接下来就是自由发挥时间了,根据说明,我们可以更换mysql版本和php版本以及php扩展,另外,该集成还自备https。这就很nice了。下面我们继续深入一下。
3.数据库
数据库一共有两个版本,第一个是mysql8.0,第二个是mysql5.7,集成环境默认搭建的配置是:
如果要下载其他的php版本或者mysql版本,找到dnmp目录下的docker-compose.yml配置文件,把前面的#号去掉皆可,再使用命令 docker-compose up创建启动一下就行了。数据库的默认用户名root,密码123456。
注意:另外下载的mysql5.7对应的端口为3305
通过docker进入mysql,修改密码sql语句如下:两个用户都修改
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d4ece6148f1 dnmp_nginx "/docker-entrypoint.…" 13 minutes ago Up 13 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp nginx
89b79105ce9c mysql:8.0.13 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
a5333f2cfde3 dnmp_php "docker-php-entrypoi…" 13 minutes ago Up 13 minutes 9000/tcp, 9501/tcp php
[root@localhost ~]# docker exec -it 89b79105ce9c /bin/bash
root@89b79105ce9c:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.13 MySQL Community Server - GPL
Copyright (c) 2000, 2018, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
#mysql5.7修改密码
use mysql;
update user set authentication_string=password('liuqing') where user='root' and host='localhost';
update user set authentication_string=password('liuqing') where user='root' and host='%';
#mysql8.0修改密码
use mysql;
ALTER user 'root'@'localhost' IDENTIFIED BY 'liuqing';
ALTER user 'root'@'%' IDENTIFIED BY 'liuqing';
4.nginx静态配置
lz的项目是thinkphp5.0的,有两个,分别位于站点根目录www/api/tp5和www/dream。
dnmp/services/nginx/conf.d文件夹下的localhost.conf配置如下:
server {
listen 80 default;
server_name localhost;
root /www/localhost;
index index.php index.html index.htm;
#charset koi8-r;
access_log /dev/null;
#access_log /var/log/nginx/nginx.localhost.access.log main;
error_log /var/log/nginx/nginx.localhost.error.log warn;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .+\.php($|/){
fastcgi_pass php:9000;
include fastcgi-php.conf;
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /api/tp5/public/ {
index index.php;
#如果文件不存在则尝试TP解析
if (!-e $request_filename) {
rewrite ^/api/tp5/public/(.*)$ /api/tp5/public/index.php?s=/$1 last;
break;
}
}
location /dream/public/ {
index index.php;
#如果文件不存在则尝试TP解析
if (!-e $request_filename) {
rewrite ^/dream/public/(.*)$ /dream/public/index.php?s=/$1 last;
break;
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 443 default ssl http2;
server_name localhost;
root /www/localhost;
index index.php index.html index.htm;
#charset koi8-r;
access_log /dev/null;
#access_log /var/log/nginx/nginx.localhost.access.log main;
error_log /var/log/nginx/nginx.localhost.error.log warn;
#error_page 404 /404.html;
ssl_certificate /ssl/localhost/localhost.crt;
ssl_certificate_key /ssl/localhost/localhost.key;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass php:9000;
include fastcgi-php.conf;
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /api/tp5/public/ {
index index.php;
#如果文件不存在则尝试TP解析
if (!-e $request_filename) {
rewrite ^/api/tp5/public/(.*)$ /api/tp5/public/index.php?s=/$1 last;
break;
}
}
location /dream/public/ {
index index.php;
#如果文件不存在则尝试TP解析
if (!-e $request_filename) {
rewrite ^/dream/public/(.*)$ /dream/public/index.php?s=/$1 last;
break;
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
5.配置SSL
这个就不多说了dnmp/services/ngin/ssl/localhost文件下的两个证书文件替换成自己的就行。
以上,就是docker安装集成环境的全部步骤,lz一路摸索一路爬坑。索性,幸不辱命!!
6.一键篇
2024年7月31日记——
上面的记得有点乱,可能和我第一次搭建有关,经验尚浅。后面又经过几次测试服务器的搭建,总结下来整个流程就是三步曲。这里以我测试的纯净版服务器为例:Ubuntu
tips:何谓纯净版,就是你新买的服务器,还什么都没干,纯洁无瑕。如果非纯净版,可以重装系统
第一步:安装docker、docker-compose、git(远程拉取代码,谁用谁方便。比手动上传更强)
第二步:git clone https://gitee.com/liuqingwushui/dnmp.git 拉取代码到服务器
第三步:复制拷贝环境变量和配置文件。启动编译docker-compose up。OK,完啦。
$ cp env.sample .env # 复制环境变量文件 $ cp docker-compose.sample.yml docker-compose.yml # docker容器相关配置
所以把这些汇总一下,于是就有了下面这个dnmp.sh 一键脚本命令
dnmp.sh
#!/bin/bash
#wget http://114.116.118.165/dnmp.sh && bash dnmp.sh
# 定义颜色代码
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # 无颜色
# 第一步:安装 Docker、Docker Compose 和 Git
function install_tools() {
echo "开始安装 Docker、Docker Compose 和 Git..."
# 更新软件包列表
sudo apt update
# 安装 Docker
if ! sudo apt install -y docker.io; then
echo -e "${RED}Docker 安装失败,请检查错误并重试。${NC}"
exit 1
fi
# 安装 Docker Compose
if ! curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose; then
echo -e "${RED}Docker Compose 下载失败,请检查错误并重试。${NC}"
exit 1
fi
if ! sudo chmod +x /usr/local/bin/docker-compose; then
echo -e "${RED}Docker Compose 权限修改失败,请检查错误并重试。${NC}"
exit 1
fi
# 安装 Git
if ! sudo apt install -y git; then
echo -e "${RED}Git 安装失败,请检查错误并重试。${NC}"
exit 1
fi
echo -e "${GREEN}Docker、Docker Compose 和 Git 安装完成。${NC}"
}
# 第二步:通过 Git 克隆 DNMP 项目
function clone_dnmp() {
echo "开始克隆 DNMP 项目..."
if ! git clone https://gitee.com/yeszao/dnmp.git; then
echo -e "${RED}Git 克隆失败,请检查错误并重试。${NC}"
exit 1
fi
echo -e "${GREEN}DNMP 项目克隆成功。${NC}"
}
# 第三步:复制配置文件并进入项目目录
function setup_dnmp() {
echo "开始设置 DNMP 项目..."
dnmp_dir="dnmp" # 假设 dnmp 目录相对于当前脚本的路径
# 确保 dnmp 目录存在
if [ ! -d "$dnmp_dir" ]; then
echo "dnmp 目录不存在: $dnmp_dir"
exit 1
fi
# 进入 dnmp 目录并执行命令
(cd "$dnmp_dir" && {
# 复制环境变量文件
if ! cp env.sample .env; then
echo -e "${RED}复制 env.sample 失败,请检查错误并重试。${NC}"
exit 1
fi
# 复制 docker-compose 配置文件
if ! cp docker-compose.sample.yml docker-compose.yml; then
echo -e "${RED}复制 docker-compose.sample.yml 失败,请检查错误并重试。${NC}"
exit 1
fi
# 启动 docker-compose
if ! docker-compose up -d; then
echo -e "${RED}docker-compose up 失败,请检查错误并重试。${NC}"
exit 1
fi
echo -e "${GREEN}DNMP 项目设置完成,docker-compose 启动成功。${NC}"
}) || {
echo "在 dnmp 目录中执行命令失败"
exit 1
}
}
# 主函数,按顺序执行上述步骤
function main() {
install_tools
clone_dnmp
setup_dnmp
echo "所有操作已完成,DNMP集成环境搭建并启动成功!"
}
# 执行主函数
main
将该脚本文件放到线上直接一键命令,奔放即可:
wget http://114.116.118.165/dnmp.sh && bash dnmp.sh
当然也可以放到本地:bash dnmp.sh 启动也行,然后静待。
后面到 docker-compose up 命令运行时间会比较长,请耐心等待,直到出现下面情况,就算安装完成了。
然后输入你的:http://服务器ip 就能访问啦,是不是很简单呢.