创建项目目录结构
首先建立用于存放项目的根目录。
mkdir php
在该目录下构建如下层级结构:
- 将网站项目文件存放于sites目录中。
- 各类服务的配置文件统一放置在services目录内。
- 自定义的执行脚本则置于script目录。
├── Readme.md
├── docker-compose.yml
├── script
├── services
│ ├── mariadb
│ │ └── Dockerfile
│ ├── nginx
│ │ ├── Dockerfile
│ │ ├── conf.d
│ │ │ └── default.conf
│ │ └── nginx.conf
│ ├── php
│ │ └── Dockerfile
│ └── redis
│ └── Dockerfile
└── sites
├── index.html
└── index.php
8 directories, 10 files
接下来编写docker-compose.yml编排文件。
version: "3"
services:
php:
build: ./services/php
# ports:
# - "9001:9000"
container_name: lnmp-php
restart: always
volumes:
- ./sites:/www
networks:
lnmp_net:
ipv4_address: 101.11.11.10
nginx:
build: ./services/nginx
ports:
- "81:80"
- "444:443"
container_name: lnmp-nginx
restart: always
volumes:
- ./sites:/www
- ./services/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./services/nginx/conf.d:/etc/nginx/conf.d:rw
networks:
lnmp_net:
ipv4_address: 101.11.11.11
networks:
lnmp_net:
driver: bridge
ipam:
config:
- subnet: 101.11.11.0/20
对services/nginx下的配置文件进行编辑。
FROM nginx:1.17.0-alpine
# 更新安装源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
# 设置时区为上海
RUN apk update && apk add --upgrade \
&& apk add tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata
同时配置services/php相关的设置。
FROM php:7.3.6-fpm-alpine3.9
# 更新安装源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
# 设置时区为上海
RUN apk update && apk add --no-cache tzdata autoconf gcc g++ make zlib-dev curl-dev\
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& apk del tzdata \
&& docker-php-ext-install mysqli pdo_mysql opcache \
&& pecl install grpc protobuf xdebug yaf yar swoole \
&& docker-php-ext-enable xdebug yaf yar swoole grpc protobuf
至此,LNMP基础环境已搭建完成,后续需针对PHP解析功能进行细化配置。
调整services/nginx/nginx.conf内容,用户可根据实际需求进行修改。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
修改services/nginx/conf.d目录下的相关配置文件。
此目录用于存放各独立项目的配置,支持按需配置单个或多个服务实例。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#root /usr/share/nginx/html;
root /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 101.11.11.10: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;
#}
}
执行命令以启动所有服务。
cd php && docker-compose up --build -d
上述步骤的执行耗时因网络状况及Docker配置而异,由于PHP镜像中编译了部分扩展,用户也可根据需求进行删减。 通过浏览器访问http://127.0.0.1:81,若页面显示phpinfo信息,则表明LNMP环境已成功部署。
以上内容为完整教程,希望能对读者的学习提供帮助,同时也欢迎继续支持本站。