Akiraka Akiraka
  • Home
  • Linux
    • ELK
    • PHP
    • Shell
    • Nginx
    • Docker
      • Docker Compose
    • Centos
    • Ubuntu
    • Jenkins
  • Python
  • Mac OS
  • Windows
  • Big Data
    • Hadoop
    • CDH
    • Hive
    • Spark
    • ZooKeeper
  • K8S
    • Kubernetes
    • Helm
  • Other
  • Quark
  • Contribute
  • Home
  • Linux
    • ELK
    • PHP
    • Shell
    • Nginx
    • Docker
      • Docker Compose
    • Centos
    • Ubuntu
    • Jenkins
  • Python
  • Mac OS
  • Windows
  • Big Data
    • Hadoop
    • CDH
    • Hive
    • Spark
    • ZooKeeper
  • K8S
    • Kubernetes
    • Helm
  • Other
  • Quark
  • Contribute
首页 Linux Docker DockerFile 源码编译 Nginx 容器

DockerFile 源码编译 Nginx 容器

Akiraka 2年前

文章目录

  • 前言
  • Docker容器编译
  • 编译Nginx 1.17.5版本
    • 1、使用方法
    • 2、开始构建Docker Nginx镜像
    • 3、运行Nginx容器
    • 4、查看Nginx容器是否运行
    • 5、通过浏览器访问,默认可通过宿主机ip访问
  • 通过Dokcer compose运行容器
  • DockerFile 源码

前言

  • 过去通过自己编译完成 nginx 现在开始使用官方容器然后加上自己修改的

Docker容器编译

  • 码云地址:https://gitee.com/G-Akiraka/DockerFile-Nginx.git
  • 编译访问里面编译 nginx 目录,点开后有详细描述的

编译Nginx 1.17.5版本

特性:
1) 默认调优好Nginx配置
2) 默认添加虚拟配置,宿主机ip访问即可
3) 类传统部署方式,源码编译安装
4) 新增logrotate将nginx日志按日期切割
5) 定义Nginx日志默认JSON输出,方便ELK
目录说明:
1) Nginx项目路径 /data/wwwroot
2) Nginx日志路径 /data/wwwlogs
3) Nginx虚拟路径 /usr/local/nginx/conf/vhost
4) Nginx重写路径 /usr/local/nginx/conf/rewrite
其他说明:
1) 默认创建www用户组与用户
2) Nginx 默认运行用户组与用户 www
3) 如遇权限问题,请将项目授权chown -R www:www 项目目录

1、使用方法

git clone https://github.com/G-Akiraka/DockerFile-Nginx.git && cd DockerFile-Nginx

2、开始构建Docker Nginx镜像

docker build -f Build.yml -t nginx:1.17.5 .

3、运行Nginx容器

docker run -d -p 80:80 nginx:1.17.5

4、查看Nginx容器是否运行

docker ps
# 调试容器,如果需要的话
docker run -it nginx:1.17.5 /bin/bash

5、通过浏览器访问,默认可通过宿主机ip访问

通过Dokcer compose运行容器

# 启动并构建Nginx容器
sudo docker-compose up -d build

DockerFile 源码

FROM ubuntu:18.04
MAINTAINER akiraka@qq.com

#   设置ENV
ENV SRC_PATH="/usr/local/src"
ENV NGINX_PATH="/usr/local/nginx"
ENV NGINX_CONF="/usr/local/nginx/conf"
#   设置容器中文,否则中文乱码
ENV LANG C.UTF-8
#   定义时区参数
ENV TZ Asia/Shanghai

#   使用阿里源并设置时区
RUN sed -i s@/security.ubuntu.com/@/mirrors.163.com/@g /etc/apt/sources.list \
    && sed -i s@/archive.ubuntu.com/@/mirrors.163.com/@g /etc/apt/sources.list \
    && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone

#   更新系统\安装依赖包
RUN apt-get update -y \
    && apt-get install -y  --no-install-recommends libssl-dev zlib1g-dev gcc g++ make tzdata logrotate nano rsyslog \
    && rm -r /var/lib/apt/lists/* 

#   准备编译要的文件
ADD script/aka_nginx.sh /root
ADD src/pcre-8.43.tar.gz ${SRC_PATH}
ADD src/nginx-1.17.5.tar.gz ${SRC_PATH}
ADD src/jemalloc-5.2.0.tar.bz2 ${SRC_PATH}
ADD config/logrotate-nginx /etc/logrotate.d/nginx

#   编译 jemalloc
WORKDIR ${SRC_PATH}/jemalloc-5.2.0
RUN ./configure
RUN make && make install
RUN ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib/libjemalloc.so.1 \
    && echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf
RUN ldconfig

# 编译 Nginx
RUN useradd -M -s /sbin/nologin www
WORKDIR ${SRC_PATH}/nginx-1.17.5
RUN ./configure --prefix=${NGINX_PATH} --user=www --group=www --with-http_stub_status_module \
    --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module \
    --with-http_flv_module --with-http_mp4_module --with-pcre=../pcre-8.43 --with-pcre-jit --with-ld-opt='-ljemalloc'
RUN make && make install

#   Nginx 复制配置文件
ADD conf ${NGINX_CONF}

#   后续操作
RUN mkdir -p /data/wwwlogs \
    && chown -R www:www ${NGINX_PATH} \
    #   删除源码文件
    && /bin/rm -rf ${SRC_PATH}/* \
    && chmod +x /root/aka_nginx.sh \
    #   添加定时任务,切割 nginx 日志
    && echo "1 0 * * * /usr/sbin/logrotate -f /etc/logrotate.d/nginx >/dev/null 2>&1" >> /var/spool/cron/crontabs/root

#   默认进入 Nginx 工作目录
WORKDIR ${NGINX_PATH}

#   设置环境变量
ENV PATH /usr/local/nginx/sbin:$PATH

#   配置端口
EXPOSE 80 443

#   添加开机启动项
CMD "/root/aka_nginx.sh"
#docker#linux#nginx
1
猜你喜欢
  • Jenkins 升级失败
  • Dockerfile 构建 FreeRadis 镜像
  • KeyCloak Docker Compose 部署
  • KeyCloak 身份验证 Nginx 502网关错误
  • 密码保护:KeyCloak 结合 Gitlab SSO 一键登录认证
Akiraka
站长
本人擅长 Ai、Au、Fl、Ae、Pr、Ps 等软件的安装与卸载,精通 CSS、JavaScript、PHP、Python、Shell、Go 等单词的拼写,熟悉 Windows、Linux、Mac、Android、IOS 等系统的开关机!
160
文章
24
评论
73
获赞
Popular Articles
TOP1
Kubernetes(k8s)Helm 部署 EFK 集群
2年前
TOP2
Kubernetes(k8s)helm 搭建 prometheus + Grafana 监控
2年前
TOP3
Container 命令ctr、crictl 命令使用说明
11月前
TOP4
Kubernetes(k8s)Helm 部署 Jenkins 持续化集成部署
2年前
TOP5
Helm 部署 Loki 日志聚合
2年前
Related Articles
Centos与Ubuntu安装 Docker 教程
2年前
Linux systemd-resolve占用53端口的解决方法
6月前
Docker Nginx反向代理支持websocket
2年前
ubuntu 18.04 设置开机启动脚本
2年前
ubuntu 虚拟机初始化设置
2年前
Copyright © 2019-2022 Akiraka. 沪18036911号