使用Docker容器来搭建LNMP(Nginx+Mysql+php)+Wordpress

一、项目模拟

1. 项目环境

公司在实际的生产环境中,需要使用 Docker 技术在一台主机上创建 LNMP 服务并运行 Wordpress 网站平台。然后对此服务进行相关的性能调优和管理工作。
所有安装包下载:

wget http://101.34.22.188/lnmp_wordpress/mysql-boost-5.7.20.tar.gz
wget http://101.34.22.188/lnmp_wordpress/nginx-1.12.0.tar.gz
wget http://101.34.22.188/lnmp_wordpress/php-7.1.10.tar.bz2
wget http://101.34.22.188/lnmp_wordpress/wordpress-4.9.4-zh_CN.tar.gz
> 或者
wget -r -np http://101.34.22.188/lnmp_wordpress/
123456

2. 服务器环境

容器 操作系统 IP地址 主要软件
nginx CentOS 7.9 x86_64 172.111.0.10 Docker-Nginx
mysql Centos 7.9 x86_64 172.111.0.20 Docker-Mysql
php Centos 7.9 x86_64 172.111.0.30 Docker-php

3. 任务需求

(1)使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台。
(2)限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap。
(3)限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s。
(4)将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地。

二、【Linux】 系统基础镜像

[root@docker ~]# systemctl disable --now firewalld
[root@docker ~]# setenforce 0
setenforce: SELinux is disabled
[root@docker ~]# docker pull centos:7
#从公有仓库中下载 centos7 作为系统基础镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       7         eeb6ee3f44bd   4 weeks ago   204MB
12345678

三、Nginx

1. 建立工作目录

[root@docker ~]# mkdir /opt/nginx
[root@docker ~]# cd /opt/nginx
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 nginx 安装包 nginx-1.12.0.tar.gz
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 wordpress 服务包 wordpress-4.9.4-zh_CN.tar.gz
12345678

2. 编写 Dockerfile 脚本

[root@docker nginx]# vim Dockerfile
 
FROM centos:7
MAINTAINER this is nginx image <lnmp>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module;make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
VOLUME [ "/usr/local/nginx/html/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]
12345678910111213141516171819

3. 准备 nginx.conf 配置文件

[root@docker nginx]# ls
Dockerfile  nginx-1.12.0.tar.gz  nginx.conf  wordpress-4.9.4-zh_CN.tar.gz
[root@docker nginx]# egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
worker_processes  1;
events {
   
    worker_connections  1024;
}
http {
   
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
   
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
   
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
            root   html;
        }
        location ~ \.php$ {
   
            root           html;
            fastcgi_pass   172.111.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
123456789101112131415161718192021222324252627282930313233

4. 生成镜像

[root@docker nginx]# docker 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要配置 PHP 环境、NginxMySQL 和 Redis,你按照以下步骤进行操作: 1. 首,安装 DockerDocker Compose(如果尚未安)。 2. 创建一个新的目录,用于存放你的文件和配置文件。 在该目录下一个 `docker-compose.yml` 文件将以下内容复制到文件中: yaml version: '3' services: nginx: image: nginx:latest : - 80:80 volumes - ./nginx.conf://nginx/nginx.conf ./public:/var/www/html depends_on: - php php: image: php:latest volumes: - ./php.ini:/usr/local/etc/php/php.ini - ./public:/var/www/html mysql: image: mysql:latest ports: - 3306:3306 environment: - MYSQL_ROOT_PASSWORD=your_mysql_root_password - MYSQL_DATABASE=your_mysql_database - MYSQL_USER=your_mysql_user - MYSQL_PASSWORD=your_mysql_password volumes: - ./mysql:/var/lib/mysql redis: image: redis:latest ports: - 6379:6379 volumes: - ./redis:/data ``` 4. 创建一个 `nginx.conf` 文件,并将以下内容复制到文件中。你可以根据需要进行修改。 ```nginx events {} http { server { listen 80; server_name localhost; root /var/www/html; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 5. 创建一个 `php.ini` 文件,并根据需要进行修改。 6. 创建一个 `public` 文件夹用于存放你的 PHP 项目文件。 7. 运行以下命令启动 Docker 容器: ```shell docker-compose up -d ``` 这将会启动 NginxPHPMySQL 和 Redis 容器。 现在,你已经成功配置了 PHP 环境、NginxMySQL 和 Redis。你可以将你的 PHP 项目文件放在 `public` 文件夹中,然后通过访问 `http://localhost` 来访问你的应用程序。请确保修改 `mysql` 服务中的环境变量,设置你自己的 MySQL 根密码、数据库名称、用户名和密码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老赵学coding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值