網頁

2021年11月23日 星期二

nginx, tomcat and mariadb on QNAP

[/share/Dockers/mariadb] # cat 0_run.sh
docker run --detach --name mariadb \
--env MARIADB_ROOT_PASSWORD=mark1234 \
-v /share/Dockers/mariadb/test:/test \
-v /share/Dockers/mariadb/conf.d:/etc/mysql/conf.d \
mariadb:latest

/share/Dockers/mariadb/test 可放置一些 create database sql

[/share/Dockers/mariadb] # cat conf.d/char.cnf
[client]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8mb4

[mysql]
# Default is Latin1, if you need UTF-8 set this (also in server section)
default-character-set = utf8mb4

[/share/Dockers/mariadb] # cat cs_get.sh
CONTAINER_ID=$(docker ps -qf "name=mariadb")
echo ${CONTAINER_ID}
CONTAINER_ID=$(cd /var/lib/docker/containers && ls -d ${CONTAINER_ID}*)
echo ${CONTAINER_ID}
QIP=127.0.0.1
QPORT=8080
curl -sq -XPOST -c cookies.txt -d '{"username": "admin", "password": "password"}' http://${QIP}:${QPORT}/container-station/api/v1/login
curl -sq -XGET -b cookies.txt http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/all

[/share/Dockers/mariadb] # cat cs_set.sh
CONTAINER_ID=$(docker ps -qf "name=mariadb")
echo ${CONTAINER_ID}
CONTAINER_ID=$(cd /var/lib/docker/containers && ls -d ${CONTAINER_ID}*)
echo ${CONTAINER_ID}
QIP=127.0.0.1
QPORT=8080
curl -sq -XPOST -c cookies.txt -d '{"username": "admin", "password": "password"}' http://${QIP}:${QPORT}/container-station/api/v1/login
curl -sq -XPUT -b cookies.txt http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/autostart/on
curl -sq -XPOST -b cookies.txt -d \
    '{
        "cputime": 500,
        "memory": "1024m"
    }' http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/resource/limit

因為此版本已經不建議使用 manager-gui
直接利用 webapps
[/share/Dockers/tomcat] # ls
0_run.sh*  build_docker/  cookies.txt  cs_set.sh*  webapps/
aa.sh*     conf/          cs_get.sh*   test.war
[/share/Dockers/tomcat] # cat 0_run.sh
rm -rf webapps/*
cp test.war webapps

docker run -d \
--link mariadb:mariadb \
-p 8888:8080 \
-v /share/Dockers/tomcat/webapps:/usr/local/tomcat/webapps \
-v /share/Dockers/tomcat/conf/context.xml:/usr/local/tomcat/conf/context.xml \
-v /share/Dockers/tomcat/conf/server.xml:/usr/local/tomcat/conf/server.xml \
--name tomcat tomcat:9.0.46

新增 Resource 部分
[/share/Dockers/tomcat] # cat conf/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
...
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
    <Resource
        name="jdbc/MyDb"
        auth="Container"
        type="javax.sql.DataSource"
        maxActive="100"
        maxIdle="30"
        maxWait="500"
        driverClassName="org.mariadb.jdbc.Driver"
        url="jdbc:mariadb://mariadb:3306"
        username="mast"
        password="mast"
    />
</Context>

新增 maxDays 防止 log 太大
[/share/Dockers/tomcat] # cat conf/server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
...
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               rotatable="true" renameOnRotate="true" maxDays="3"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>


[/share/Dockers/tomcat] # cat cs_get.sh
CONTAINER_ID=$(docker ps -qf "name=tomcat")
echo ${CONTAINER_ID}
CONTAINER_ID=$(cd /var/lib/docker/containers && ls -d ${CONTAINER_ID}*)
echo ${CONTAINER_ID}
QIP=127.0.0.1
QPORT=8080
curl -sq -XPOST -c cookies.txt -d '{"username": "admin", "password": "password"}' http://${QIP}:${QPORT}/container-station/api/v1/login
curl -sq -XGET -b cookies.txt http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/all

[/share/Dockers/tomcat] # cat cs_set.sh
CONTAINER_ID=$(docker ps -qf "name=tomcat")
echo ${CONTAINER_ID}
CONTAINER_ID=$(cd /var/lib/docker/containers && ls -d ${CONTAINER_ID}*)
echo ${CONTAINER_ID}
QIP=127.0.0.1
QPORT=8080
curl -sq -XPOST -c cookies.txt -d '{"username": "admin", "password": "password"}' http://${QIP}:${QPORT}/container-station/api/v1/login
curl -sq -XPUT -b cookies.txt http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/autostart/on
curl -sq -XPOST -b cookies.txt -d \
    '{
        "cputime": 500,
        "memory": "1024m"
    }' http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/resource/limit

[/share/Dockers/nginx] # cat 0_run.sh
docker run -d \
--link tomcat:tomcat \
-p 8443:80 \
-v /share/Dockers/nginx/conf.d:/etc/nginx/conf.d \
-v /share/Dockers/nginx/conf:/etc/nginx/conf \
--name nginx nginx:1.14

[/share/Dockers/nginx] # ls conf
Certs/

[/share/Dockers/nginx] # cat conf.d/default.conf
upstream tomcat_server {
    server tomcat:8080 weight=1;
}
server {
    #listen       80;
    listen       80 ssl;
    ssl_certificate /etc/nginx/conf/Certs/server.pem;
    ssl_certificate_key /etc/nginx/conf/Certs/server.key;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate /etc/nginx/conf/Certs/caChain.crt;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    #server_name  localhost;
    server_name  www.sunhousetechnology.com.tw;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        #proxy_pass http://tomcat_server;
    }
    location ^~ /test/ {
        proxy_redirect off;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # for https
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://tomcat_server;
    }
    client_max_body_size 20M;

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

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

[/share/Dockers/nginx] # cat cs_get.sh
CONTAINER_ID=$(docker ps -qf "name=nginx")
echo ${CONTAINER_ID}
CONTAINER_ID=$(cd /var/lib/docker/containers && ls -d ${CONTAINER_ID}*)
echo ${CONTAINER_ID}
QIP=127.0.0.1
QPORT=8080
curl -sq -XPOST -c cookies.txt -d '{"username": "admin", "password": "password"}' http://${QIP}:${QPORT}/container-station/api/v1/login
curl -sq -XGET -b cookies.txt http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/all

[/share/Dockers/nginx] # cat cs_set.sh
CONTAINER_ID=$(docker ps -qf "name=nginx")
echo ${CONTAINER_ID}
CONTAINER_ID=$(cd /var/lib/docker/containers && ls -d ${CONTAINER_ID}*)
echo ${CONTAINER_ID}
QIP=127.0.0.1
QPORT=8080
curl -sq -XPOST -c cookies.txt -d '{"username": "admin", "password": "password"}' http://${QIP}:${QPORT}/container-station/api/v1/login
curl -sq -XPUT -b cookies.txt http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/autostart/on
curl -sq -XPOST -b cookies.txt -d \
    '{
        "cputime": 500,
        "memory": "1024m"
    }' http://${QIP}:${QPORT}/container-station/api/v1/container/docker/${CONTAINER_ID}/resource/limit


沒有留言:

張貼留言