docker-compose编排lnmp(nginx+php+mysql)环境

nginx+php+mysql环境是我以前经常部署过的环境,这里就使用docker-compose编排这个环境

软件版本约定

nginx:latest
php:7.2-fpm
mysql:5.7.28

首先我们来约束规划下目录
/data目录存放所有映射目录和配置以及日志文件

我们首先创建好所有目录

sudo mkdir -p /data/{www,nginx/etc,php/conf,mysql/conf,mysql/data,logs/nginx,logs/php,logs/mysql}
sudo vim docker-compose.yml

内容如下

version: '2.0'
services:
  nginx:
    image: nginx:latest
    restart: always
    ports:
      - 80:80
    depends_on:
      - php
    volumes:
      - ./nginx/etc/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./logs/nginx:/var/log/nginx:rw
      - ./www:/usr/share/nginx/html:rw
    networks:
      - lnmp_net
    container_name: lnmp-nginx
  php:
    image: php:7.2-fpm
    restart: always
    ports:
      - 9000:9000
    volumes:
      - ./www:/var/www/html:rw
      - ./php/conf/php.ini:/usr/local/etc/php/php.ini:ro
      - ./php/conf/php-fpm.conf:/usr/local/etc/php-fpm.conf:ro
      - ./php/conf/www.conf:/usr/local/etc/php-fpm.d/www.conf:ro
    networks:
      - lnmp_net
    container_name: lnmp-php
  mysql:
    image: mysql:5.7.28
    restart: always
    ports:
      - 3306:3306
    volumes:
      - ./mysql/conf/my.cnf:/etc/mysql/my.cnf:ro
      - ./mysql/data:/var/lib/mysql:rw
      - ./logs/mysql:/var/log/mysql:rw
    networks:
      - lnmp_net
    environment:
      - MYSQL_ROOT_PASSWORD=admin123
    container_name: lnmp-mysql
networks:
  lnmp_net:
    driver: bridge

然后是nginx.conf文件

sudo vim /data/nginx/etc/nginx.conf

内容如下

user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
    multi_accept on; 
    use epoll; 
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    client_header_timeout 30; 
    client_body_timeout 30;
    client_max_body_size 128m;
    reset_timedout_connection on;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  30;
    
    send_timeout 30;
    limit_conn_zone $binary_remote_addr zone=addr:5m; 
    limit_conn addr 100;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 16 64k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types text/plain application/x-javascript text/css application/xml application/x-httpd-php;
    gzip_vary on;
    proxy_buffer_size 128k;
    proxy_buffers 32 32k;
    proxy_busy_buffers_size 128k;
    open_file_cache max=100000 inactive=30s; 
    open_file_cache_valid 60s; 
    open_file_cache_min_uses 2; 
    open_file_cache_errors on;
    server {
        listen 80;
        server_name  localhost;
        root /usr/share/nginx/html;
        index index.php index.html ;
        location ~ .*\.(jpg|jpeg|png|gif|js|css)$ {
            expires 2h;
        }
        location / {
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
                break;
            }
        }
        location ~ \.php(/|$) {
            root /var/www/html;
            fastcgi_pass   lnmp-php:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include  fastcgi_params;
        }
    }
}


再附上my.cnf文件内容

sudo vim /data/mysql/conf/my.cnf
[mysqld]
server-id = 1
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7
log_error = /var/log/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
performance_schema = 0
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 500M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

最后就是php测试文件

sudo vim /data/www/index.php

内容如下:

<?php
phpinfo();
?>

然后创建两个mysql日志空文件,不知道为什么这个文件自动生成报错,需要手工创建下

sudo touch /data/logs/mysql/mysql-error.log
sudo touch /data/logs/mysql/mysql-slow.log
sudo chmod 666 /data/logs/mysql/*

我们使用docker-compose up -d创建编排好的镜像试试
上面完了以后,php还需要做一些配置,主要是要php.ini,php-fpm.conf,www.conf三个文件
复制php容器内的这三个文件到/data/php/conf/目录下,此处需要加sudo,复制的目录需要权限

sudo docker cp lnmp-php:/usr/local/etc/php/php.ini-development /data/php/conf/php.ini #复制本地并改名php.ini
sudo docker cp lnmp-php:/usr/local/etc/php-fpm.conf /data/php/conf/
sudo docker cp lnmp-php:/usr/local/etc/php-fpm.d/www.conf /data/php/conf/
sudo vim /data/php/conf/php-fpm.conf
pid = run/php-fpm.pid #去掉这一行前面的分号

其他文件复制出来再映射进取主要方便后期修改
上面修改完以后我们可以先删除容器再创建,也可以直接重启php的容器,完了以后再docker ps查看状态

CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
1e127852c209   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   0.0.0.0:80->80/tcp                  lnmp-nginx
35ea1d52b211   php:7.2-fpm    "docker-php-entrypoi…"   3 minutes ago   Up 3 minutes   0.0.0.0:9000->9000/tcp              lnmp-php
1be7d4d258b8   mysql:5.7.28   "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:3306->3306/tcp, 33060/tcp   lnmp-mysql

看到容器状态正常了,我们试着访问下网页,http://localhost/,打开页面如下,说明就部署成功了
2021-07-21_15-06.png 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/790.html