docker部署web项目需要用到的服务:nginx,php,mysql
一、部署nginx
1、下载nginx镜像:
docker pull nginx
2、run一个nginx容器:
docker run --name runoob-php-nginx -p 8083:80 -d \
-v ~/nginx/www:/usr/share/nginx/html:ro \
-v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
--link myphp-fpm:php \
nginx
参数说明:
- -p 8083:80: 端口映射,把 nginx 中的 80 映射到本地的 8083 端口。
- ~/nginx/www: 是本地 html 文件的存储目录,/usr/share/nginx/html 是容器内 html 文件的存储目录。
- ~/nginx/conf/conf.d: 是本地 nginx 配置文件的存储目录,/etc/nginx/conf.d 是容器内 nginx 配置文件的存储目录。
- --link myphp-fpm:php: 把 myphp-fpm 的网络并入 nginx,并通过修改 nginx 的 /etc/hosts,把域名 php 映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm。
二、部署php
1、下载php镜像,版本:5.6(最新版也可以):
docker pull php:5.6-fpm
2、run一个php容器:
docker run --name myphp-fpm -v ~/nginx/www:/www -d php:5.6-fpm
参数说明
1、--name myphp-fpm : 将容器命名为 myphp-fpm。
2、-v ~/nginx/www:/www: 将主机中目录 ~/nginx/www 挂载到容器的 /www
3、在主机家目录下创建 ~/nginx/conf/conf.d目录,新建runoob-test-php.conf 文件:
mkdir -vp ~/nginx/conf/conf.d
cd ~/nginx/conf/conf.d/
vi runoob-test-php.conf
4、添加以下内容到runoob-test-php.conf文件:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_pass php:9000; # php-fpm 服务的 URL
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name; # myphp-fpm 中 php 文件的存储路径,映射到本地的 ~/nginx/www 目录
include fastcgi_params;
}
}
5、在主机家目录下创建 ~/nginx/www 目录,新建index.php文件:
mkdir -vp ~/nginx/www
cd ~/nginx/www
vi index.php
6、添加以下内容到index.php文件:
<?php
echo phpinfo();
?>
7、浏览器输入主机ip:8083或直接在主机内curl localhost:8083查看php环境页面,能打开表示php部署成功。
三、部署mysql
1、下载mysql镜像:
docker pull mysql
2、run一个mysql容器
docker run -itd --name mysql-test \
--link myphp-fpm:phpfpm \
--link runoob-php-nginx:nginx \
-p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql
参数说明:
- –link phpfpm:phpfpm # 将此容器与phpfpm通过网络连接起来,这样在nginx容器里就可以访问到phpfpm容器里提供的phpfpm服务。
- –link runoob-php-nginx:nginx # 将此容器与nginx容器通过网络连接起来,在nginx容器以后即可以通过主机名访问到mysql容器进行链接数据库操作。
- -e MYSQL_ROOT_PASSWORD=123456 # 设置mysql的root用户密码为123456
- -p 3306:3306 # 映射容器端口
3、登录mysql进行测试:
mysql -u root -p
回车输入密码123456 看到以下内容表示mysql运行正常:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
4、复制项目内容到~/nginx/www 目录下
5、部署完成,浏览器输入主机ip:8083访问主页
常见问题:
1、浏览器访问网站提示缓存目录写入权限不足、**目录创建失败、**文件写入失败:
修改主机及nginx容器 www 目录权限:
chmod -R 777 www
2、访问数据库出错:
为php容器安装拓展(如果之前启用了gd,需要先在php.ini文件中注释掉extension=gd.so并重启容器后再执行以下步骤):
docker exec -it myphp-fpm /bin/bash
cd /usr/local/bin
docker-php-ext-install pdo_mysql
执行完毕退出容器,并重启php容器。
3、缺少扩展gd库:
# 进入php容器
docker exec -it myphp-fpm /bin/bash
# 更新软件源
apt update
# 安装库
apt install -y libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev
# 解压源码
docker-php-source extract
# 进入gd源码文件夹
cd /usr/src/php/ext/gd
# 准备编译
docker-php-ext-configure gd --with-webp-dir=/usr/include/webp --with-jpeg-dir=/usr/include --with-png-dir=/usr/include --with-freetype-dir=/usr/include/freetype2
# 编译安装
docker-php-ext-install gd
# 检查扩展是否安装成功
php -m | grep gd
# 退出php容器
exit
# 重启php容器
4、网站图形验证码显示不出来:
报错:Fatal error: Call to undefined function core\extend\code\imagettftext()
解决方案:
docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include
# 重新编译:
docker-php-ext-install gd
报错:configure: error: freetype-config not found.
解决方案:
apt install libfreetype6-dev
docker-php-ext-configure gd --enable-gd-native-ttf --with-freetype-dir=/usr/include/freetype2 --with-png-dir=/usr/include
# 重新编译:
docker-php-ext-install gd