nginx with let’s encrypt ssl , php-fpm , tomcat , mariadb environment

nginx 啟動時會自動安裝let’s encrypt ssl ,nginx與tomcat及php-fpm間使用proxy_pass,大概如下:

server {
    # Listen to port 443 on both IPv4 and IPv6.
    listen 443 ssl default_server reuseport;
    listen [::]:443 ssl default_server reuseport;

    # Domain names this server should respond to.
    server_name a.yslifes.com;

    root /var/www/html;
    # Load the certificate files.
    ssl_certificate         /etc/letsencrypt/live/a.yslifes.com/fullchain.pem;
    ssl_certificate_key     /etc/letsencrypt/live/a.yslifes.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/a.yslifes.com/chain.pem;

    # Load the Diffie-Hellman parameter.
    ssl_dhparam /etc/letsencrypt/dhparams/dhparam.pem;

    #return 200 'Let\'s Encrypt certificate successfully installed!';
    #add_header Content-Type text/plain;


    gzip on;
    gzip_types text/plain application/xml application/json;
    gzip_comp_level 9;
    gzip_min_length 1000;


    #brotli on;
 
    # 預設為 6, 0 ~ 11; 值愈大壓縮率愈高,使用的 CPU 愈多~
    #brotli_comp_level 6;
    #brotli_static on;
 
    # 壓縮對像
    #brotli_types application/atom+xml application/javascript application/json application/rss+xml application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/eot font/opentype font/otf font/truetype image/svg+xml image/vnd.microsoft.icon image/x-icon image/x-win-bitmap text/css text/javascript text/plain text/xml;
 



    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    #location / {
    #        proxy_pass http://api/;
    #}


        # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;


#    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
#            try_files $uri $uri/ =404;
#    }
    location / {
        proxy_pass http://tomcat10:8080/;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Forwarded $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }
#    location ~ [^/]\.php(/|$) {
#       fastcgi_split_path_info ^(.+?\.php)(/.*)$;
#       if (!-f $document_root$fastcgi_script_name) {
#           return 404;
#       }

       # Mitigate https://httpoxy.org/ vulnerabilities
#       fastcgi_param HTTP_PROXY "";
   
#       fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
#       fastcgi_param PATH_INFO        $fastcgi_path_info;

#       fastcgi_pass phpfpm7.4:9000;
#       fastcgi_index index.php; 

       # include the fastcgi_param setting
#       include fastcgi_params;

       # SCRIPT_FILENAME parameter is used for PHP FPM determining
       #  the script name. If it is not set in fastcgi_params file,
       # i.e. /etc/nginx/fastcgi_params or in the parent contexts,
       # please comment off following line:
       # fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
#    }
}

docker-compose.yml檔如下:

Read More

Apache使用多個docker php-fpm

原本centos linux安裝Apache及php5.4(php-fpm),最近一些framework只支援php7以上,想到要昇級或共存就覺得會有一場災難,想說是否可以使用docker php7.4-fpm來直接使用,減少系統的昇級或更換過程的一些問題。

可以直接略過1,2二步奏直接pull build好的images

docker pull catyku/php-fpm7.4

https://hub.docker.com/r/catyku/php-fpm7.2/

1.首先建立自己需要的php-fpm Dockerfile

FROM php:7.4-fpm
MAINTAINER yku "[email protected]"
#  設定時區
ENV TZ=Asia/Tapiei
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#  更新系統及增加php外掛mysqli pdo_mysql等
RUN apt-get update && apt-get install -y \
        git apt-utils libzip-dev \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-jpeg=/usr/include/ --with-freetype=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
        && docker-php-ext-install zip \
        && docker-php-ext-install pdo_mysql \
        && docker-php-ext-install opcache \
        && docker-php-ext-install mysqli \
        && rm -r /var/lib/apt/lists

2.編譯Dockerfile

docker build -t catyku:php-fpm7.4 .

3.然後啟動docker

docker run -d --name php-fpm7.4 -p 127.0.0.1:9001:9000 -v /var/www/:/var/www catyku:php-fpm7.4

因為9000 port已經給原本系統的php5.4-fpm使用了,只好把它調開,並指定www工作資料夾給php7.4-fpm

(建議可以細調php-fpm的設定檔,或是把設定檔也-v出來)

Read More

docker安裝apache、mariadb、php

在docker安裝Apache php及Mariadb服務,可以快速部署安裝於多種OS,不管是在開發或是正式上線環境都可以一樣。

images使用是官方的apache php 7.2版本;mariadb:latest,其中apache-php部份還需要另外安裝mysqli擴展套件。

《Docker —— 從入門到實踐-》正體中文版

指令如下

資料庫部份

sudo docker search mariadb
sudo docker pull mariadb
sudo docker run -d --name mariadb -e MYSQL_ROOT_PASSWORD=youpass --restart unless-stopped mariadb

參數–restart unless-stopped可以讓os在重開機時自動啟動docker container,不過自已下指令停止時不會自動再啟動

Read More