docker lnmp配置
作者:互联网
1.lnmp网络与目录规划
172.16.10.0/24
nginx:172.16.10.10
mysql:172.16.10.20
php:172.16.10.30
网站访问主目录:/wwwroot
nginx的配置文件:/docker/nginx
mysql持久化的目录: /docker/mysql
mysql 密码: Aa123456
2.起临时nginx容器获取nginx配置文件
[root@docker ~]# mkdir /docker /wwwroot
[root@docker ~]# docker run -itd --name test nginx
[root@docker ~]# docker cp test:/etc/nginx /docker/
[root@docker ~]# docker cp test:/usr/share/nginx/html /wwwroot/
[root@docker ~]# docker rm -f test
3.创建自定义网络
#创建自定义网络
docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.1 lnmp
#服务器开启内核转发:
echo '1' >/proc/sys/net/ipv4/ip_forward
4. 启动php容器
docker run -itd --name lnmp-php7.4 \
-p 9000:9000 \
-v /wwwroot/html:/usr/share/nginx/html \
--network lnmp \
--ip 172.16.10.30 \
registry.cn-hangzhou.aliyuncs.com/chenleilei/php74:latest
另一个方法,启动nginx后执行:
docker run -itd --name lnmp-php7.4 \
-v /wwwroot/html:/usr/share/nginx/html \
--network=container:nginx \
--ip 172.16.10.30 \
phregistry.cn-hangzhou.aliyuncs.com/chenleilei/php74:latest
5. 启动nginx容器
docker run -d --name lnmp-nginx \
-p 80:80 \
-p 443:443 \
-v /wwwroot/html:/usr/share/nginx/html \
-v /docker/nginx:/etc/nginx \
--network lnmp \
--ip 172.16.10.10 nginx
#修改配置文件:
vim /docker/nginx/conf.d/default.conf
[root@node02 conf.d]# egrep -v '#|^$' /docker/nginx/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm; #添加默认首页文件 index.php
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
#解除PHP注释,改为如下:
location ~ \.php$ {
root /usr/share/nginx/html; #添加PHP解析能力
fastcgi_pass 172.16.10.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #修改为当前配置
include fastcgi_params;
}
}
#server区段改为上面的配置后保存
6. 测试代码
docker restart lnmp-nginx lnmp-php7.4
cd /wwwroot/html
echo test > index.html
cat index.php
<?php
phpinfo();
?>
chmod -R a+w /wwwroot/html/dede
chown -R www-data.www-data /wwwroot/html
7. 其他配置[php容器增加mysql支持]
部署动态站点需要赋予目录足够权限,推荐
docker exec -it lnmp-nginx bash
chown -R www-data.www-data /usr/share/nginx/html
chmod -R a+w /usr/share/nginx/html
exit
chown -R root.root /wwwroot/html/dede
此时已经可以运行php了,还有一些扩展需要部署如: GD支持 MySQL支持,可以在部署dede程序时看到
docker部署的php容器需要 安装扩展
#进入容器
docker exec -it lnmp-php7.4 bash
修改源:
sed -i 's#http://deb.debian.org#https://mirrors.163.com#g' /etc/apt/sources.list
apt-get update
安装软件支持:
apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libmcrypt-dev libpng-dev libonig-dev
docker-php-ext-install gd mysqli pdo pdo_mysql mbstring shmop
安装完成后退出重启php容器
exit
docker restart lnmp-php7.4
没有安装扩展提示:
安装扩展后:
8. 启动mysql容器
#启动mysql
[root@docker html]# docker run -itd --name test -e MYSQL_ROOT_PASSWORD=Aa123456 registry.cn-hangzhou.aliyuncs.com/chenleilei/mysql5.7
#测试创建库
[root@docker html]# docker exec -it test bash
root@d46a7cc8407f:/# mysql -uroot -pAa123456
mysql> show databases;
mysql> create database test;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> exit
root@d46a7cc8407f:/var/lib/mysql# exit
#获取mysql数据:
[root@docker html]# docker cp test:/var/lib/mysql /docker
[root@docker html]# docker rm -f test
[root@node02 conf.d]# ll /docker/mysql
total 188484
-rw-r----- 1 polkitd root 56 Jun 29 10:40 auto.cnf
-rw------- 1 polkitd root 1676 Jun 29 10:40 ca-key.pem
-rw-r--r-- 1 polkitd root 1112 Jun 29 10:40 ca.pem
-rw-r--r-- 1 polkitd root 1112 Jun 29 10:40 client-cert.pem
-rw------- 1 polkitd root 1680 Jun 29 10:40 client-key.pem
-rw-r----- 1 polkitd root 1352 Jun 29 10:40 ib_buffer_pool
-rw-r----- 1 polkitd root 79691776 Jun 29 10:45 ibdata1
-rw-r----- 1 polkitd root 50331648 Jun 29 10:45 ib_logfile0
-rw-r----- 1 polkitd root 50331648 Jun 29 10:40 ib_logfile1
-rw-r----- 1 polkitd input 12582912 Jun 29 10:45 ibtmp1
drwxr-x--- 2 polkitd root 4096 Jun 29 10:40 mysql
drwxr-x--- 2 polkitd root 8192 Jun 29 10:40 performance_schema
-rw------- 1 polkitd root 1676 Jun 29 10:40 private_key.pem
-rw-r--r-- 1 polkitd root 452 Jun 29 10:40 public_key.pem
-rw-r--r-- 1 polkitd root 1112 Jun 29 10:40 server-cert.pem
-rw------- 1 polkitd root 1680 Jun 29 10:40 server-key.pem
drwxr-x--- 2 polkitd root 8192 Jun 29 10:40 sys
启动mysql:
docker run -d --name lnmp_mysql \
--network lnmp --ip 172.16.10.20 \
-p 3306:3306 -p 33060:33060 \
-v /docker/mysql:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=Aa123456 \
registry.cn-hangzhou.aliyuncs.com/chenleilei/mysql5.7 \
--character-set-server=utf8
登录mysql:
[root@docker html]# docker exec -it lnmp_mysql bash
root@60b705b2b10e:/# mysql -uroot -p'Aa123456'
mysql> exit
9. 测试部署php项目
# 进入数据库容器:
docker exec -it lnmp_mysql bash
#创建运行wordpress的mysql数据库和用户:
mysql> create database dede;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on dede.* to dede@'%' identified by 'dede';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#下载DedeCMS
#wget https://cn.wordpress.org/latest-zh_CN.tar.gz
mkdir /wwwroot/html/dede
cd /wwwroot/html/dede
wget --no-check-certificate https://updatenew.dedecms.com/base-v57/package/DedeCMS-V5.7.96-UTF8.zip
unzip DedeCMS-V5.7.96-UTF8.zip
mv uploads up
mv up/* ./
rm -fr up
[root@node02 dede]# ll
total 12816
drwxr-xr-x 2 root root 6 Jun 27 09:24 a
drwxr-xr-x 5 root root 38 Jun 27 09:24 assets
drwxr-xr-x 20 root root 4096 Jun 27 09:24 data
drwxr-xr-x 7 root root 12288 Jun 27 09:24 dede
-rw-r--r-- 1 root root 13052376 Jun 28 15:03 DedeCMS-V5.7.96-UTF8.zip
-rw-r--r-- 1 root root 16958 Dec 31 1979 favicon.ico
drwxr-xr-x 6 root root 227 Jun 27 09:24 images
drwxr-xr-x 15 root root 4096 Jun 27 09:24 include
-rw-r--r-- 1 root root 1392 Dec 31 1979 index.php
drwxr-xr-x 5 root root 304 Jun 27 09:24 install
-rw-r--r-- 1 root root 3474 Dec 31 1979 license.txt
drwxr-xr-x 3 root root 69 Jun 27 09:24 m
drwxr-xr-x 5 root root 4096 Jun 27 09:24 member
drwxr-xr-x 5 root root 4096 Jun 27 09:24 plus
-rw-r--r-- 1 root root 505 Dec 31 1979 robots.txt
drwxr-xr-x 2 root root 23 Jun 27 09:24 special
-rw-r--r-- 1 root root 998 Dec 31 1979 tags.php
drwxr-xr-x 6 root root 142 Jun 27 09:24 templets
drwxr-xr-x 8 root root 104 Jun 27 09:24 uploads
#添加权限
[root@node02 html]# chmod -R a+w /wwwroot/html
#nginx容器添加权限
docker exec -it lnmp-nginx bash
chown -R www-data.www-data /usr/share/nginx/html/dede
exit
后台登录不显示验证码经检查属于程序bug与配置无关,安装wordpress 无此故障
标签:--,配置,Jun,lnmp,nginx,mysql,docker,root 来源: https://www.cnblogs.com/superlinux/p/16427562.html