Centos7 docker容器搭建LNMP服务 + redis(一步一步跟着做,肯定会有所收获)

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"]."<br>";
    }
?>
	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);				#这里connect()中一定要是redis,使用127.0.0.1会出错
	echo "<br />";
	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;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /usr/share/nginx/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html index.php;

        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
               # With php-fpm (or other unix sockets):
               fastcgi_pass   php:9000;
               fastcgi_index  index.php;
               fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
               include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
	}
}

4:创建docker-compose.yml文件

version: "3"										#版本号
services:			
  nginx:			
    image: nginx:alpine								#nginx服务和最小的linux服务
    ports:
     - 80:80										#映射80端口给80
    volumes:
     - /root/html:/usr/share/nginx/html				#将html目录映射到/usr/share/nginx/html
     - /root/conf/nginx.conf:/etc/nginx/nginx.conf  #将conf目录映射到/etc/nginx/
  php:
    image: devilbox/php-fpm:5.2-work-0.89			
    volumes:
     - /root/html:/var/www/html						#php文件映射
  mysql:
    image: mysql:5.6								#mysql服务安装
    environment:
     - MYSQL_ROOT_PASSWORD=123456					
  redis:
    image: redis									#redis服务安装

5:运行docker-compose.yml文件

docker-compose -f dockers up -d				#后台运行dockers文件
docker-compose up -d						#后台运行docker-compose文件

结果验证:

	访问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

猜你喜欢

转载自blog.csdn.net/weixin_45005209/article/details/107368367
今日推荐