Docker Compose 一键部署LNMP

时间:2023-01-01 01:33:26

Docker Compose 一键部署LNMP

目录结构

[root@localhost ~]# tree compose_lnmp/
compose_lnmp/
├── docker-compose.yml
├── mysql
│ ├── conf
│ │ └── my.cnf
│ └── data
├── nginx
│ ├── Dockerfile
│ ├── nginx-1.12.1.tar.gz
│ └── nginx.conf
├── php
│ ├── Dockerfile
│ ├── php-5.6.31.tar.gz
│ └── php.ini
└── wwwroot
└── test.php

一、创建Nginx Compose

1.1、创建compose目录作为LNMP的项目管理目录

mkdir compose_lnmp
cd compose_lnmp/

1.2、创建nginx管理目录

mkdir nginx
cd nginx

1.3、将nginx源码包下载到本地

  • Nginx-1.12.1
  • 下载地址:https://pan.baidu.com/s/1IAdODW63jbpwbQX992coYg
  • 密码:p89j

1.4、创建Dockerfile文件

vim Dockerfile

# 指定镜像
FROM centos:6
# 指定管理员
MAINTAINER xiangsikai
# 执行命令安装编译库文件
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
# 添加解压nginx包到/tmp目录下
ADD nginx-1.12.1.tar.gz /tmp
# 进入目录进行编译安装
RUN cd /tmp/nginx-1.12.1 && ./configure --prefix=/usr/local/nginx && make -j 2 && make install
# 删除容器内置配置文件
RUN rm -f /usr/local/nginx/conf/nginx.conf
# 复制本地配置文件到容器内
COPY nginx.conf /usr/local/nginx/conf
# 声明暴露端口
EXPOSE 80
# 启动容器Nginx服务,指定全局命令daemon off保证服务在前台运行不会关闭
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]

1.5、创建nginx.conf配置文件

vim nginx.conf

user  root;
worker_processes auto; error_log logs/error.log info; pid logs/nginx.pid; events {
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 logs/access.log main;
sendfile on;
keepalive_timeout 65; server {
listen 80;
server_name localhost;
root html;
index index.html index.php; location ~ \.php$ {
root html;
fastcgi_pass php-cgi:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

nginx配置文件


二、创建Mysql Compose

2.1、创建Mysql管理目录

mkdir mysql
cd mysql
mkdir conf
cd conf

2.2、创建mysql配置文件

vim my.cnf

[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
pid-file=/var/run/mysqld/mysqld.pid
log_error=/var/log/mysql/error.log
character_set_server = utf8
max_connections=3600

mysql配置文件


三、创建PHP Compose

3.1、创建Mysql管理目录

mkdir php
cd php

3.2、下载php源码包

  • php-5.6.31
  • 下载地址:https://pan.baidu.com/s/17cC8Ad9h4llMb7JC57N1GQ
  • 密码:wl2w

3.3、创建Dockerfile文件

vim Dockerfile

# 指定镜像
FROM centos:6
# 指定系统管理员
MAINTAINER xiangsikai
# 安装编译库
RUN yum install -y gcc gcc-c++ gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
# 下载压缩包解压到指定目录
ADD php-5.6.31.tar.gz /tmp/
# 编译安装php,执行相关命令
RUN cd /tmp/php-5.6.31 && \
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql --with-mysqli \
--with-openssl --with-zlib --with-curl --with-gd \
--with-jpeg-dir --with-png-dir --with-iconv \
--enable-fpm --enable-zip --enable-mbstring && \
make -j 4 && make install && \
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.conf && \
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod +x /etc/init.d/php-fpm
# 复制本地配置文件到容器内
COPY php.ini /usr/local/php/etc
# 声明暴露端口
EXPOSE 9000
# 启动php
CMD /etc/init.d/php-fpm start && tail -F /var/log/messages

3.4、创建php.ini文件

vim php.ini

[PHP]
engine = On
short_open_tag = Off
asp_tags = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = 17
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQL]
mysql.allow_local_infile = On
mysql.allow_persistent = On
mysql.cache_size = 2000
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[Sybase-CT]
sybct.allow_persistent = On
sybct.max_persistent = -1
sybct.max_links = -1
sybct.min_server_severity = 10
sybct.min_client_severity = 10
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.bug_compat_42 = Off
session.bug_compat_warn = Off
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.hash_function = 0
session.hash_bits_per_character = 5
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"
[MSSQL]
mssql.allow_persistent = On
mssql.max_persistent = -1
mssql.max_links = -1
mssql.min_error_severity = 10
mssql.min_message_severity = 10
mssql.compatability_mode = Off
mssql.secure_connection = Off
[Assertion]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]

php配置文件


四、创建DockerCompose文件

4.1 进入compose_lnmp目录下创建DockerCompose文件

[root@localhost compose_lnmp]# ls
mysql nginx php

vim docker-compose.yml

# 指定服务版本号
version: ''
# 服务
services:
# 服务名称
nginx:
# 指定服务容器名字
hostname: nginx
# 构建
build:
# 指定目录上下文构建镜像
context: ./nginx
# 指定dockerfile文件名称
dockerfile: Dockerfile
# 映射数组级的端口
ports:
- "80:80"
# 映射php文件服务别名
links:
- "php:php-cgi"
# 映射服务数据卷路径
volumes:
- ./wwwroot:/usr/local/nginx/html # 服务名称
php:
# 指定服务容器名字
hostname: php
# 构建
build: ./php
# 映射mysql服务别名
links:
- mysql:mysql-db
# 映射服务数据卷路径
volumes:
- ./wwwroot:/usr/local/nginx/html # 服务名称
mysql:
# 指定服务容器名字
hostname: mysql
# 指定使用官方mysql5.6版本
image: mysql:5.6
# 映射端口
ports:
- "3306:3306"
# 映射服务数据卷路径
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
# 指定数据库变量
environment:
# 设置数据库密码
MYSQL_ROOT_PASSWORD: 123456
# 添加user用户
MYSQL_USER: user
# 设置user用户密码
MYSQL_PASSWORD: user123

4.2、执行docker Compose一键部署LNMP

# 需要在yml文件同级目录下执行此命令
docker-compose up

4.3、进入网站目录创建php测试页并进行测试

cd wwwroot/
vim test.php
#-----------------
<?php phpinfo()?>
#-----------------

4.4、测试:192.168.1.77/test.php

Docker Compose 一键部署LNMP

注:执行docker-compose up之前需要关闭SELINUX。博主踩的坑。