用dockerfile构建基于ubuntu的lnmp环境的镜像

本文介绍如何使用Dockerfile创建包含Nginx、MySQL及PHP的LNMP环境镜像。通过逐步指导安装必要组件,并配置Nginx,最终实现自动化部署LNMP环境。

本文主要讲述的是如何用dockerfile构建lnmp环境的镜像,前提默认你已安装好docker

一、新建Dockerfile文件

FROM ubuntu:16.04

MAINTAINER turtle "turtle@anasit.com"

RUN \
        apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0xcbcb082a1bb943db && \
        apt-get update && \
        apt-get install -y mariadb-server mariadb-common && \
        apt-get install -y php && \
        apt-get install -y nginx && \
        apt-get install -y vim 



# 配置nginx

Copy ./default /etc/nginx/sites-available/default

# 添加启动脚本
ADD ./start.sh /start.sh
RUN chmod 755 /start.sh


CMD /start.sh && tail -f

# Expose ports.
EXPOSE 3306
EXPOSE 80

start.sh文件

## start nginx
service nginx start
## start mysql
service mysql start

## start php7.0-fpm
service php7.0-fpm start

default文件

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    # SSL configuration
    #   
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #   
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #   
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #   
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #   
    # include snippets/snakeoil.conf;
    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;
    server_name _;
    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # With php7.0-cgi alone:
#       fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#       deny all;
#}

}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#               try_files $uri $uri/ =404;
#       }
#}

二、build Dockerfile文件

docker build -t turtlell/lnmp:1.0 .

三、运行镜像

docker run -id -p 80:80 -v /var/www/html:/var/www/html turtlell/lnmp

四、访问localhost

访问localhost,运行成功,则说明搭建成功

详情请访问github

### 基于 Ubuntu 22.04 的 Dockerfile 构建 LAMP 和 LNMP 环境 #### 定义 LAMP 环境Dockerfile 为了创建一个基于 Ubuntu 22.04 的 LAMP (Linux, Apache, MySQL, PHP) 环境,可以编写如下所示的 `Dockerfile`: ```dockerfile FROM ubuntu:22.04 # 更新软件源并安装必要的工具和依赖项 RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common && \ add-apt-repository ppa:ondrej/php && \ apt-get update # 安装 Apache 及其相关组件 RUN apt-get install -y apache2 libapache2-mod-php7.4 php7.4-cli php7.4-mysql php7.4-gd php7.4-json php7.4-curl php7.4-zip unzip wget curl git vim # 配置 Apache 默认页面指向 /var/www/html/index.php 并启用 mod_rewrite 模块 COPY ./html /var/www/html/ RUN a2enmod rewrite # 启动 Apache 服务 CMD ["apachectl", "-D", "FOREGROUND"] EXPOSE 80 443 ``` 此文件定义了一个容器的基础镜像为官方发布的最新版 Ubuntu 22.04,并通过一系列指令完成了对 Apache Web Server 和 PHP 支持库以及一些常用扩展的支持。 对于数据库部分,则通常会单独启动另一个 MySQL/MariaDB 数据库容器并与之连接。这里假设已经有一个外部运行中的 MySQL 实例可供访问[^1]。 #### 定义 LNMP 环境Dockerfile 而要建立 LNMP (Linux, Nginx, MySQL, PHP),则需调整上述配置以适应 Nginx 而不是 Apache: ```dockerfile FROM ubuntu:22.04 ENV NGINX_VERSION="nginx/1.24.0" # 更新软件源并安装必要的工具和依赖项 RUN apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get install -y gnupg lsb-release ca-certificates build-essential openssl zlib1g-dev libpcre3 libpcre3-dev tar make gcc g++ WORKDIR /tmp/nginx-build/ # 下载、编译并安装 Nginx RUN wget http://nginx.org/download/${NGINX_VERSION}.tar.gz && \ tar zxvf ${NGINX_VERSION}.tar.gz && \ cd nginx-${NGINX_VERSION} && \ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-debug && \ make && \ make install # 清理临时文件 RUN rm -rf /tmp/* # 设置工作目录 WORKDIR /usr/local/nginx/conf # 复制自定义配置文件至目标位置 COPY conf/nginx.conf . # 创建日志存储路径 RUN mkdir -p /var/log/nginx # 将项目静态资源复制到默认根目录下 COPY html /usr/share/nginx/html # 开启Nginx进程守护模式 STOPSIGNAL SIGTERM CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"] EXPOSE 80 443 ``` 这段脚本同样是从最新的 Ubuntu 22.04 出发,在其中加入了下载、编译特定版本 Nginx 的过程,并进行了基本设置以便能够立即投入使用[^2]。 请注意以上两个例子均未涉及 MySQL 或 MariaDB 的部署;实际应用中建议采用独立的服务实例来提供数据持久化支持,可通过 Docker Compose 来简化多容器间的协作管理[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值