docker-compose
创建docker-compose.yaml文件
#版本号
version '1'
services:
# 配置php5.6
php56:
# 构建路径
build:./php/5.6-fpm-alpine
# 容器名称
container_name:lnp_php-fpm_56-alpine
# 当docker重启后容器自动重启
restart:always
# 挂载路径
volumes:
# 服务器路径:docker路径 - 根据直接需求来添加
- ./wwwroot/html:/var/www/html
- ./php/conf/php-fpm.conf:/etc/php-fpm.conf
- ./php/conf/www.conf:/etc/php-fpm.d/www.conf
- ./php/log:/var/log/
- ./php/conf/php.ini:/usr/local/etc/php/php.ini
# 环境变量
environment:
- TZ=Asia/Shanghai
- LC_ALL=C.UTF-8
# 链接容器 - 可互相通信 如不需要且去除
links:
mysql
#redis
# 配置nginx
nginx:
# 直接拉取镜像
image: nginx:latest
# 容器名称
container_name: lnmp_nginx
# 端口
ports:
- "80:80"
restart: always
volumes:
- ./wwwroot/html:/var/www/html
- ./wwwroot/files:/var/www/files
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d/:ro
- ./nginx/log:/var/log/nginx
- ./nginx/include:/etc/nginx/include
environment
- TZ=Asia/Shanghai
links:
- php56
- 创建nginx配置文件夹
conf.d文件夹放置nginx配置文件 - 根据自己需求配置nginx
2.创建nginx配置文件
添加以下nginx配置文件 - 根据自己需求来配置
vim default.conf
server {
listen 80;
server_name aa.com;
root /var/www/html/;
location / {
if ($flag = "11") {
rewrite ^(.*)$ /index.php last;
break;
}
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 30d;
}
location ~ .*.(js|css)$ {
expires 1h;
add_header Cache_Control private;
}
set $flag "";
if (!-f $request_filename) {
set $flag "${flag}1";
}
if ($request_filename !~* .(gif|jpg|jpeg|png|bmp|swf|js|css)$) {
set $flag "${flag}1";
}
location /admin {
if ($flag = "11") {
rewrite ^(.*)$ /admin.php last;
break;
}
}
location ~ \.php$ {
fastcgi_pass php56:9000;
fastcgi_index index.php;
include include/fastcgi.conf;
}
}
- include
vim server_html.conf
listen 80;
charset utf-8;
index index.html index.htm;
error_page 404 /404.html;
location = /404.html {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /.ht {
deny all;
}
location ~ /themes/.*\.(dwt|lbi) {
deny all;
}
location ~ .*/\.svn.* {
deny all;
}
vim server.conf
listen 80;
charset utf-8;
index index.php index.html index.htm;
error_page 404 /404.html;
location = /404.html {
root html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /.ht {
deny all;
}
location ~ /themes/.*\.(dwt|lbi) {
deny all;
}
location ~ .*/\.svn.* {
deny all;
}
location / {
error_page 405 =200 $uri;
}
location ~ \.php$ {
fastcgi_pass php56:9000;
#fastcgi_pass unix:/var/run/php7-fpm.sock;
fastcgi_index index.php;
include include/fastcgi.conf;
}
vim fastcgi.conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
3.nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
fastcgi_connect_timeout 30;
fastcgi_send_timeout 3m;
fastcgi_read_timeout 3m;
# large_client_header_buffers 8k;
large_client_header_buffers 4 64k;
client_header_buffer_size 8k;
client_max_body_size 50m;
# large_client_header_buffers 4 16k;
# client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 64k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
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;
gzip on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
}
创建php docker配置文件
php文件夹下创建5.6-fpm-alpine文件,conf,log文件夹
php版本根据需求更换
添加Dockerfile
vim Dockerfile
5.6-fpm-alpine
vim Dockerfile
FROM php:5.6-fpm-alpine
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update \
&& apk add libpng-dev freetype-dev libjpeg-turbo-dev \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install mysql mysqli pdo
EXPOSE 9000
CMD ["php-fpm"]
添加php配置文件
vim php-fpm.conf
vim php.ini
vim php-fpm.conf
执行docker-compose
以下命令选一种执行
启动
#聚合容器的输出,命令退出,所有容器都停止
docker-compose up
#后台启动容器
docker-compose up -d
#选择执行yaml文件 后台启动容器
docker-compose -f xxxx.yaml up -d
如第一次执行的会拉取镜像,稍等即可
停止
docker-compose -f docker-compose.yaml down
docker-compose down
创建web文件夹
mkdir -p ./wwwroot/{html,files}
可根据nginx配置来设置服务web存放处