文章

使用docker-compose快速搭建lnmp环境

  编辑于:2020-12-29
文章目录

介绍

使用docker-compose可以快速的搭建lnmp环境,便于使用。

安装

安装docker-compose

curl -fsSL https://get.docker.com | sudo bash
#docker-compose的最新版本,可以查看https://github.com/docker/compose/releases
curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

lnmp

这里推荐一个github项目:https://github.com/nanoninja/docker-nginx-php-mysql
最简单的使用方法:

apt-get update
apt-get install git -y
git clone https://github.com/nanoninja/docker-nginx-php-mysql
cd docker-nginx-php-mysql
#默认的MySQL为8.0,MySQL的默认账户密码均为root,如需更改,输入vi .env修改.env文件即可。
#其他的配置(开放端口等)可以修改docker-compose.yml文件。
docker-compose up -d

自此lnmp已经可以使用了,网站目录位于docker-nginx-php-mysql/web/public
当然也可以自己编写docker-compose.yml
新建一个目录保存docker-compose.yml

version: '3'
services:
    php:
        hostname: lnmp_php
        container_name: lnmp_php
        image: php:fpm
        networks:
            - lnmp
        volumes:
            - ./wwwroot:/data/www

    nginx:
        hostname: nginx
        container_name: nginx
        image: nginx:latest
        ports:
            - 80:80
            - 443:443
        depends_on:
            - php
            - mysql
        networks:
            - lnmp
        volumes:
            - ./wwwroot:/data/www
            - ./nginx/conf.d:/etc/nginx/conf.d

    mysql:
        hostname: mysql
        container_name: mysql
        image: mysql:5.6
        ports:
            - 3306:3306
        networks:
            - lnmp
        volumes:
            - ./mysql/conf/my.cnf:/etc/my.cnf
            - ./mysql/data:/var/lib/mysql

        environment:
            MYSQL_ROOT_PASSWORD: 123456
            MYSQL_DATABASE: wordpress
            MYSQL_USER: user
            MYSQL_PASSWORD: user123

networks:
    lnmp:

在同目录下新建nginx/conf.d/default.conf

server {
    listen       80;
#    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

        #root   /usr/share/nginx/html;
    root  /data/www;
    index index.php index.html index.htm;


    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
#    error_page   500 502 503 504  /50x.html;
#    location = /50x.html {
#        root   /usr/share/nginx/html;
#    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    #    root           html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$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;
    #}
}

然后运行

docker-compose up -d

即可。
同目录下新建wwwroot作为网站目录。

发表回复