Centos7 虚拟机下docker搭建(LNMP)+redis服务
**搭建这个服务可以用单个命令执行创建或者用docker-compose.yml文件来创建**
**本文采用docker-compose.yml文件创建容器**
1:首先建立conf和html目录
mkdir conf /*该目录下保存httpd服务nginx服务等的配置文件*/
mkdir html /*该目录下保存.html文件或者.php文件等*/
2:建立index.html以及index.php,mysql.php,redis.php文件
1:建立mysql.php文件,先在mysql中建立user1库和student表,包含id就可以,因为就查了个id
2:那怎么进入数据库呢,首先,docker ps 查看到运行的mysql服务,然后就直接进去就行了
我的mysql服务的ID是:597cfc652d75,所以用以下命令进入到mysql容器中
docker exec -it 597cfc652d75 /bin/sh
继续,mysql -uroot -p
密码:123456
建立数据库和表就可以了
<?php
$servername = "mysql";
$username = "root";
$password = "123456";
$dbname = "user1";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "select * from student";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]."
";
}
?>
2,建立redis.php文件,进入redis服务,set Hu 'moon'
怎么进入redis服务
docker exec -it redis服务id redis-cli
然后 set Hu ‘moon’
<?php
$redis = new Redis();
$redis->connect('redis', 6379);
echo "
";
echo $redis->get('Hu');
?>
3,建立index.html和test.php文件
vi test.php
输入: <?php phpinfo(); ?>
vi index.html
输入:Welcome to docker nginx or httpd
3:建立nginx.conf文件并输入以下内容
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80 default_server;
listen [::]:80 default_server;
root /usr/share/nginx/html;
index index.html index.htm index.nginx-debian.html index.php;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
4:创建docker-compose.yml文件
version: "3"
services:
nginx:
image: nginx:alpine
ports:
- 80:80
volumes:
- /root/html:/usr/share/nginx/html
- /root/conf/nginx.conf:/etc/nginx/nginx.conf
php:
image: devilbox/php-fpm:5.2-work-0.89
volumes:
- /root/html:/var/www/html
mysql:
image: mysql:5.6
environment:
- MYSQL_ROOT_PASSWORD=123456
redis:
image: redis
5:运行docker-compose.yml文件
docker-compose -f dockers up -d
docker-compose up -d
结果验证:
访问192.168.78.199,因为默认是访问index.html,所以访问192.168.78.199后面不用加index.html
访问192.168.78.199/test.php
访问192.168.78.199/mysql.php
访问192.168.78.199/redis.php
测试结束
(qq:九七二四三九三二九(972439329),有哪里出错欢迎指正,大家一起学习交流)
End