美文网首页
利用docker配置php+nginx运行环境并配置虚拟域名

利用docker配置php+nginx运行环境并配置虚拟域名

作者: Chting | 来源:发表于2019-01-30 17:25 被阅读0次

目录结构说明

首先构建你的docker环境目录
大概如下

docker_php7_nginx
    nginx
        sites
            default.conf
        Dockerfile
        nginx.conf
    php-fpm
        Dockerfile
    docker-compose.yml


 

nginx配置,

Dockerfile配置如下

FROM nginx:1.13.3-alpine

ADD nginx.conf /etc/nginx/

ARG PHP_UPSTREAM=php-fpm
RUN echo "http://mirrors.ustc.edu.cn/alpine/v3.5/main" > /etc/apk/repositories \
&& echo "http://mirrors.ustc.edu.cn/alpine/v3.5/community" >> /etc/apk/repositories
RUN apk update \
    && apk upgrade \
    && apk add --no-cache bash \
    && adduser -D -H -u 1000 -s /bin/bash www-data \
    && rm /etc/nginx/conf.d/default.conf \
    && echo "upstream php-upstream { server ${PHP_UPSTREAM}:9000; }" > /etc/nginx/conf.d/upstream.conf

CMD ["nginx"]

EXPOSE 80 443

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;

events {
  worker_connections  2048;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 15;
  types_hash_max_size 2048;
  client_max_body_size 20M;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  gzip on;
  gzip_disable "msie6";
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-available/*;
  open_file_cache max=100;
  charset UTF-8;
}

default.conf

server {
        listen       80;
        server_name  charm.www.baidu.com;##你的虚拟域名
        set    $root "/var/www/html/wwwroot";##路径
        root   $root;
        location / {
            index  index.html index.htm index.php;
            try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php(.*)$ {
            fastcgi_pass   php-upstream;##也可以是127.0.0.1:9000
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

        location ~ /\.ht {
            deny  all;
        }
}

php-fpm配置

我使用的是php7.1其他版本自行更换源
Dockerfile

FROM php:7.1.9-fpm
 
# Install any custom system requirements here
RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    curl \
    libicu-dev \
    libmemcached-dev \
    libz-dev \
    libpq-dev \
    libjpeg-dev \
    libpng12-dev \
    libfreetype6-dev \
    libssl-dev \
    libmcrypt-dev \
    libxml2-dev \
    libbz2-dev \
    libjpeg62-turbo-dev \
    php-pear \
    curl \
    git \
    subversion \
  && rm -rf /var/lib/apt/lists/*
 
# Install various PHP extensions
RUN docker-php-ext-configure bcmath --enable-bcmath \
    && docker-php-ext-configure pcntl --enable-pcntl \
    && docker-php-ext-configure pdo_mysql --with-pdo-mysql \
    && docker-php-ext-configure pdo_pgsql --with-pgsql \
    && docker-php-ext-configure mbstring --enable-mbstring \
    && docker-php-ext-configure soap --enable-soap \
    && docker-php-ext-install \
        bcmath \
        intl \
        mbstring \
        mcrypt \
        mysqli \
        pcntl \
        pdo_mysql \
        pdo_pgsql \
        soap \
        sockets \
        zip \
  && docker-php-ext-configure gd \
    --enable-gd-native-ttf \
    --with-jpeg-dir=/usr/lib \
    --with-freetype-dir=/usr/include/freetype2 && \
    docker-php-ext-install gd \
  && docker-php-ext-install opcache \
  && docker-php-ext-enable opcache
 
# AST
RUN git clone https://github.com/nikic/php-ast /usr/src/php/ext/ast/ && \
    docker-php-ext-configure ast && \
    docker-php-ext-install ast
 
# ICU - intl requirements for Symfony
# Debian is out of date, and Symfony expects the latest - so build from source, unless a better alternative exists(?)
RUN curl -sS -o /tmp/icu.tar.gz -L http://download.icu-project.org/files/icu4c/58.2/icu4c-58_2-src.tgz \
    && tar -zxf /tmp/icu.tar.gz -C /tmp \
    && cd /tmp/icu/source \
    && ./configure --prefix=/usr/local \
    && make \
    && make install
 
RUN docker-php-ext-configure intl --with-icu-dir=/usr/local \
    && docker-php-ext-install intl
 
# Install the php memcached extension
RUN curl -L -o /tmp/memcached.tar.gz "https://github.com/php-memcached-dev/php-memcached/archive/php7.tar.gz" \
  && mkdir -p memcached \
  && tar -C memcached -zxvf /tmp/memcached.tar.gz --strip 1 \
  && ( \
    cd memcached \
    && phpize \
    && ./configure \
    && make -j$(nproc) \
    && make install \
  ) \
  && rm -r memcached \
  && rm /tmp/memcached.tar.gz \
  && docker-php-ext-enable memcached
 
# Copy opcache configration
#暂时屏蔽掉了,好像是php配置
#COPY ./opcache.ini /usr/local/etc/php/conf.d/opcache.ini
 
# Install xDebug, if enabled
ARG INSTALL_XDEBUG=false
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
    # Install the xdebug extension
    pecl install xdebug && \
    docker-php-ext-enable xdebug \
;fi
 
# Copy xdebug configration for remote debugging
#COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
 
# Copy timezone configration
#COPY ./timezone.ini /usr/local/etc/php/conf.d/timezone.ini
 
# Set timezone
RUN rm /etc/localtime
RUN ln -s /usr/share/zoneinfo/Europe/London /etc/localtime
RUN "date"
 
# Short open tags fix - another Symfony requirements
#COPY ./custom-php.ini /usr/local/etc/php/conf.d/custom-php.ini
 
# Composer
ENV COMPOSER_HOME /var/www/.composer
 
RUN curl -sS https://getcomposer.org/installer | php -- \
    --install-dir=/usr/bin \
    --filename=composer
 
RUN chown -R www-data:www-data /var/www/
 
RUN mkdir -p $COMPOSER_HOME/cache
 
VOLUME $COMPOSER_HOME

windows配置

上面弄完之后,只需要在windows的host文件加上你的虚拟域名就可以了

192.168.1.101       charm.www.baidu.com ##你的ip和你的虚拟域名

运行测试

启动docker环境
在docker环境目录下本文中为docker_php7_nginx
执行命令,没有安装docker-compose 或者docker的请查找此文集相关文章

docker-compose up -d

成功后就可以用你的虚拟域名访问了。

相关文章

网友评论

      本文标题:利用docker配置php+nginx运行环境并配置虚拟域名

      本文链接:https://www.haomeiwen.com/subject/syucsqtx.html