docker安装
略
下载镜像
- nginx:最新版
- php-fpm:根据自己需求而定
- mysql:根据自己需求定
以下是我搭建LNMP使用的镜像版本
root@VM-12-16-ubuntu:/docker/lnmp/php/etc# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0.34 5761fe35fa53 37 hours ago 577MB
bitnami/php-fpm 8.1.22 33c8b3ab8748 2 days ago 281MB
nginx latest 89da1fb6dcb9 2 weeks ago 187MB
创建路径
# 创建相关目录
mkdir -p /docker/lnmp/php
mkdir -p /docker/lnmp/nginx/conf.d
mkdir -p /docker/lnmp/mysql
compase文件
version: '3'
services:
php:
image: bitnami/php-fpm:8.1.22
volumes:
- /var/www/html:/usr/share/nginx/html
#- /docker/lnmp/php/etc:/opt/bitnami/php/etc
restart: always
nginx:
image: nginx:latest
ports:
- "80:80"
- "443:443"
volumes:
- /docker/lnmp/nginx/conf.d:/etc/nginx/conf.d
- /var/www/html:/usr/share/nginx/html
- /var/log/nginx:/var/log/nginx
depends_on:
- php
restart: always
mysql:
image: mysql:8.0.34
volumes:
- /docker/lnmp/mysql/data:/var/lib/mysql
- /docker/lnmp/mysql/conf.d:/etc/mysql/conf.d
environment:
- MYSQL_ROOT_PASSWORD=mysql123 #改成你自己的mysql密码
ports:
- "3306:3306"
restart: always
nginx 配置文件
touch /docker/lnmp/mysql/conf.d/project_a.conf
cat > /docker/lnmp/mysql/conf.d/project_a.conf << EOF
server {
listen 80;
server_name php.test;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php:9000; #这里是因为我compose.yml文件中php-fpm服务名称是“php”,你也可以改成你服务的私有地址
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location /.well-known/acme-challenge/ {
root /var/www/letsencrypt/;
log_not_found off;
}
error_log /var/log/nginx/php_error.log;
access_log /var/log/nginx/php_access.log;
}
EOF
安装mycli
因为docker安装mysql,服务上没有安装mysql客户端,查询不方便,推荐使用mycli,还带命令提示,还可以
apt install mycli -y
#或
yum install mycli -y
特殊要求
- 如果需要自己修改php.ini,请先运行php-fpm镜像,将配置文件拷贝到服务器上,修改后将文件挂到容器
# 临时启动一个php-fpm
docker run --name myphp -d bitnami/php-fpm:8.1.22
# 将配置文件拷贝到自己服务器上
docker cp myphp:/opt/bitnami/php/etc/ /docker/lnmp/php/etc/
# 删除临时php-fpm容器
docker rm -f myphp
测试
测试nginx是否正常
在根目录下创建index.html
<h1>hello,docker!</>
测试nginx是否能正确解析php文件
在根目录下创建index.php
<?php
phpinfo();
测试mysql
注意:如果你用的云服务器,$servername不要设置成公网地址,否则测试连接不成功,最好设置成你的服务器的私有地址
<?php
//$servername = "mysql" 因为我的compose.yml中mysql服务的名字是“mysql”,如果你也和我一样,可以这样写,不是的话,保持和compose.yml文件中mysql服务名一致
$servername = "mysql";
$username = "root";
$password = "mysql123";
$db_name = 'mysql';
$conn = new mysqli($servername, $username, $password, $db_name);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
php连接mysql8报错处理
# 创建mysql.cnf
touch /docker/lnmp/mysql/conf.d/mysql.cnf
# 修改密码验证插件
cat > /docker/lnmp/mysql/conf.d/mysql.cnf <<EOF
[mysqld]
default-authentication-plugin=mysql_native_password
EOF
# 重启mysql镜像
docker-compose -f /docker/lnmp/compose.yml restart mysql
# 登录用root登录mysql
mycli -uroot
# 没有库的可以创建一个,有库跳过这一步
create database blog;
#没有用户的的创建用户,有则跳过
CREATE USER ‘blog‘@‘%‘ IDENTIFIED WITH mysql_native_password BY 'Mysql@2023_!';
#用户授权,有用户的跳过
grant all privileges on blog.* to 'blog'@'%';
#修改密码
ALTER USER ‘native‘@‘localhost‘ IDENTIFIED WITH mysql_native_password BY ‘new_password‘;
# 刷新
flush privileges;