Docker安装LNMP

目录

使用Docker run命令进行安装

使用docker-compose进行安装


使用Docker run命令进行安装

拉取镜像你也可以直接run

docker pull nginx:1.23.1
docker pull mysql:5.7
docker pull php:7.4.26-fpm

 安装nginx 这次只是为了将nginx容器实例中的配置文件cp出来

docker run --name nginx --restart=always -d -p 80:80 nginx:1.23.1

mkdir -p /usr/local/nginx/{conf,log,html,conf.d}

docker cp nginx:/etc/nginx/nginx.conf /usr/local/nginx/conf/
docker cp nginx:/etc/nginx/conf.d /usr/local/nginx/conf/
docker cp nginx:/usr/share/nginx/html /usr/local/nginx/
docker cp nginx:/var/log/nginx /usr/local/nginx/log

 修改nginx配置文件

cd /usr/local/nginx/conf/conf.d

vi default.conf
location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
location ~ \.php$ {
        fastcgi_pass   php-fpm:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
docker rm -f nginx

安装MySQL

docker run --name mysql -p 3306:3306 --privileged=true --restart=always -v /Docker/mysql/data/:/var/lib/mysql -v /Docker/mysql/log/:/var/log/mysql -v /Docker/mysql/conf/:/etc/mysql/ -e MYSQL_ROOT_PASSWORD=123 -d mysql:5.7

安装PHP

docker run  --name php-fpm -p 9000:9000 --privileged=true --restart=always -v /Docker/php:/www --link mysql -d php:7.4.26-fpm 

安装nginx

docker run -p 80:80 --name nginx --privileged=true --restart=always -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/conf/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /usr/local/nginx/log/:/var/log/nginx -v /usr/local/nginx/html/:/usr/share/nginx/html --link php-fpm  -d nginx:1.23.1

编写测试文件

[root@localhost ~]# vim /Docker/php/index.php 

   

安装mysql扩展

[root@localhost ~]# docker exec -it php-fpm /bin/bash
root@d64b773a0f05:~# cd /usr/local/bin/
root@d64b773a0f05:/usr/local/bin# ./docker-php-ext-install pdo_mysql
root@d64b773a0f05:/usr/local/bin# ./docker-php-ext-install mysqli
root@d64b773a0f05:/usr/local/bin# cd /usr/local/etc/php
root@d64b773a0f05:/usr/local/etc/php# cp php.ini-production php.ini


[root@localhost ~]# docker cp php-fpm:/usr/local/etc/php/php.ini /Docker/

[root@localhost ~]# vim /Docker/php.ini 
extension=pdo_mysql
extension=mysqli

[root@localhost ~]# docker restart php-fpm
[root@localhost ~]# vim /Docker/php/mysql.php 

      

使用docker-compose进行安装

mkdir /compose
[root@localhost ~]# cd /compose/

[root@localhost compose]#  vim docker-compose.yml 

version: '3'
services:
  nginx:
    image: nginx:1.23.1
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./www:/usr/share/nginx/html
    depends_on:
      - php-fpm
  php-fpm:
    image: php:fpm
    volumes:
      - ./www:/usr/share/nginx/html
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: 123123
    ports:
       - "3306:3306"
    volumes:
       - ./mysql:/var/lib/mysql

创建nginx配置文件

[root@localhost compose]# vim nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    server {
        listen 80;
        server_name localhost;

        root /usr/share/nginx/html;
        index index.php index.html index.htm;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass php-fpm:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

创建PHP测试页面

[root@localhost compose]# mkdir www/
[root@localhost compose]# vim www/index.php 

创建MySQL连接PHP页面

[root@localhost compose]# vim www/mysql.php 

一键启动LNMP

[root@localhost compose]# docker-compose up -d

你可能感兴趣的:(docker,php)