2022年7月28日木曜日

amazon linux 2 に MariaDB & Django 設定


【タイムゾーンと日本語】
$ sudo timedatectl set-timezone Asia/Tokyo
$ sudo timedatectl
$ sudo localectl set-locale LANG=ja_JP.utf8
$ sudo localectl

【Django/Python】
# mkdir /var/www/wsgi
# cd /var/www/wsgi
# mkdir Release
# ln -s Release test
# ln -s Release service
# ln -s Release link
# ln -s Release office

$ vi env
   3.6→3.7

# yum  install httpd httpd-devel
# yum  install gcc
# yum  install python3-devel

$ vi requirements.txt
dataclasses==0.8 → 0.6

※Collecting pyreportjasper==2.1.2                         # 違うサーバでは再現しなかった。?
Killed
# pip3 --no-cache-dir install pyreportjasper $INTERNET_PROXY  で回避


→epelを有効にしてインストール
# amazon-linux-extras install epel -y
# yum install zbar

【MariaDB】
#----------------------------------
#  MariaDB
#----------------------------------
# yum remove mariadb
# yum remove mariadb-devel
# yum remove mariadb-libs

# vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name=MariaDB
baseurl=http://yum.mariadb.org/10.8/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

【クライアント】
# yum install MariaDB-client MariaDB-devel
【サーバ】
# yum install -y MariaDB-server MariaDB-client MariaDB-shared MariaDB-devel

# vi /etc/my.cnf.d/server.cnf
[mariadb]
character-set-server=utf8
[mariadb-10.7]
character-set-server=utf8


# systemctl start mariadb
# systemctl enable mariadb

【プロキシ】
# vi /etc/yum.conf
proxy=http://192.168.13.101:3128

# vi  /etc/amazon-linux-extras.conf
export http_proxy=http://192.168.13.101:3128
export https_proxy=http://192.168.13.101:3128

【zabbix-agent2】
# yum remove zabbix*
# yum install https://repo.zabbix.com/zabbix/6.2/rhel/7/x86_64/zabbix-agent2-6.2.1-1.el7.x86_64.rpm
# vi /etc/zabbix/zabbix_agent2.conf
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
Server=192.168.23.103
ServerActive=192.168.23.103:10051
Hostname=zabbix
HostMetadata=Rockylinux
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock
AllowKey=system.run[*]

※ AllowKeyを設定した場合は、下記も設定
# visudo
# Allows zabbix to run all commands without password.
zabbix ALL=NOPASSWD: ALL
# semanage boolean -l | grep zabbix
# setsebool -P httpd_can_connect_zabbix on
# setsebool -P zabbix_can_network on
# setsebool -P zabbix_run_sudo  on

# firewall-cmd --add-port=10050/tcp --zone=public --permanent
# firewall-cmd --reload

# systemctl restart zabbix-agent2
# systemctl enable zabbix-agent2

$ zabbix_get -s 192.168.20.201 -k agent.versio


【s3 Storage マウント】
# mkdir -p /s3/logs
# mkdir /s3/assets
# mkdir /s3/media

# vi /etc/fstab
192.168.210.101:/test-cxdnext-logs            /s3/logs    nfs    rw,hard,nolock    0   0
192.168.210.101:/test-cxdnext-assets          /s3/assets  nfs    rw,hard,nolock    0   0
192.168.210.101:/test-cxdnext-media           /s3/media   nfs    rw,hard,nolock    0   0

# mount -a
# mount

【clam】
# amazon-linux-extras install epel -y
# install clamav clamav-scanner-systemd clamav-update
#  vi /etc/clamd.conf
以降は、前の記事と同じ。

2022年7月27日水曜日

AWS ロードバランサ EC2

 1) VPC 作成

・Name: test
・CLDR: 192.168.0.0/16
・IPv6ブロック無し

2) サブネット作成
サブネット名 アベイラビリティーゾーン IPv4 CIDR Block
subnet-public-1a ap-northeast-1a 192.168.10.0/24
subnet-public-1c ap-northeast-1c 192.168.20.0/24
subnet-public-1d ap-northeast-1d 192.168.30.0/24
subnet-private-api-1a ap-northeast-1a 192.168.11.0/24
subnet-private-api-1c ap-northeast-1c 192.168.21.0/24
subnet-private-api-1d ap-northeast-1d 192.168.31.0/24
subnet-private-web-1a ap-northeast-1a 192.168.12.0/24
subnet-private-web-1c ap-northeast-1c 192.168.22.0/24
subnet-private-web-1d ap-northeast-1d 192.168.32.0/24
subnet-private-nat-1a ap-northeast-1a 192.168.13.0/24
subnet-private-nat-1c ap-northeast-1c 192.168.23.0/24
subnet-private-nat-1d ap-northeast-1d 192.168.33.0/24
subnet-private-db-1a ap-northeast-1a 192.168.200.0/24
subnet-private-storage-1c ap-northeast-1c 192.168.210.0/24

3) インターネットゲートウェイの作成&VPCへのアタッチ
Name: test-igw
Action→VPCにアタッチ

4) ルートテーブル設定
Test-public-rtb (ルートテーブル作成)
subnet-public-1a/1c/1dを関連付け
ルート編集(0.0.0.0 igw-xxxxx )

5) パブリック内に踏み台インスタンス作成
AMI 選択 (amazon Linux)
ネットワーク: test (PVC選択)
サブネット: subnet-pubic-1a 選択
自動割り当てパブリックIP: 有効
Instance に名前を付ける
ネットワークインターフェース→プライマリIPに固定IPを設定

6) セキュリティグループの設定
Name : test-public-ec2-sg
ssh マイIP 150.246.20.210
Http カスタム 0.0.0.0 (フルオープン) ※ LB下のprivate時は、public のCLDRを指定( 192.168.10.0/24)

7) NAT ゲートウェイ 
   NATゲートウェイをパブリックサブネットに作成
     vpc->ナットゲートウェイ
  test-pablic-1a-ngw
  NAT下に置きたいプライベートサブネットのルートテーブルにデフォルトGWを設定

8) ssh鍵ペアの作成
test-key.pem download
$ cp test-key.pem ~/.ssh/
$ chmod 600 ~/.ssh/test-key.pem
$ scp -i test-key.pem ~/.ssh/test-key.pem  ec2-user@xx.xx.xx.xx:~/.ssh/
$ ssh ec2-user@xx.xx.xx.xx -i test-key.pem
$ chmod 600 ~/.ssh/test-key.pem

9) インスタンスからAMIを作る
Image-name: linux-base
EC2 auto scaling

10) セキュリティグループ作成
Scurity group name: test-oracle19c-1a
vpc
InBound Oracle-RDS  カスタム 192.168.0.0/24 Oracle-RDS
InBound ssh                   カスタム 192.168.10.0/24   from Step Server
InBound 10050             カスタム  192.168.23.0/24 from Zabbix Server
OutBoud  すべて          カスタム  192.168.0.0/16    to This VPC
Tag              Name           test-oracle19c-1a


11) EC2起動(oracleサーバ)
Name: test-oracle19c
OS image:Amazon Linux 2  64ビット
Instance type: t3.small
Key Pair:test-step-key
vpc:test-vpc
subnet:test-subnet-private-db-1a
Pabulic IP 自動割当: 無効
Security-group: test-oracle19c-1a
高度なネトワーク-プライマリIP:192.168.200.201
ストレージ設定:100G
EFS:test-common /u01

その他環境設定
vi /etc/yum.conf   proxy=http://192.168.13.101:3128
rpm -Uvh https://repo.zabbix.com/zabbix/6.2/rhel/7/x86_64/zabbix-agent2-6.2.6-release1.el7.x86_64.rpm  [ --httpproxy http://192.168.13.101:3128 ]
[ yum install zabbix-agent2.x86_64 ]

11-1) EC2起動テンプレートの作成
Template name: linux-base
Machine-image: linux-base?
Instance type : t2.micro
Key pair: hoge
Network : Virtual Private Cloud(VPC)
Security group: test-ec2-sg

12) autoScaling
Auto Scaling group Name : test-asg
起動テンプレート: linux-base
Vpc: test vpc選択
Subnet:subnet-private-api-1a subnet-private-api-1c subnet-private-api-1d

13) ロードバランサの設定
ALB選択
test-public-alb
Internet-facing
VPC: test
Subnet:
Listener: 80/443
AZ 選択 ( AZを跨って作成 1a/1c)
SG設定(NEW SG: test-alb-sg 80/443 0.0.0.0)
Target grop: test-alb-tg ( 新しいターゲットグループ作成 test-alb-tg/instance/http(80)/http1/helthcheck: /test/health )
ルールの設定で転送先を変更。DNS名で分離?
ルーティング設定

14) グループサイズとスケーリングポリシー設定
希望する容量:2
希望する最小キャパ:2
希望する最大キャパ:2

スケーリングポリシー:無し


【参考】 

https://dev.classmethod.jp/articles/introduction-to-aws-networking-and-autoscaling-web-server/

Django シンプルヘルスチェック

$ vi Config/urls.py
from django.http           import     HttpResponse

urlpatterns = [
path( 'health', lambda r: HttpResponse()),
]

$ curl -i http:///localhost:8000/health

2022年7月5日火曜日

グローバルIP確認

$ curl inet-ip.info
$ curl globalip.me
$ curl ipinfo.io
$ curl ipecho.net/plain
$ curl ifconfig.me
$ curl ifconfig.me -4
$ curl ifconfig.io -4
$ curl inet-ip.info
$ curl inet-ip.info/ip
$ curl inet-ip.info/json
$ curl ip-api.com
$ curl httpbin.org/ip

2022年5月10日火曜日

Rocky Linux chrony 設定

# dnf install chrony

NTPD停止
# systemctl stop ntpd
# systemctl disable ntpd

# systemctl start chronyd
# systemctl enable chronyd
# systemctl status chronyd
# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^-   v118-27-19-72.cxxt.stati>        2  10   377   518    +1271us[+1271us] +/-   13ms
^+  i45-32-25-115.vpsv.d.rsp>      3  10   207   878    +33us[  -61us] +/- 4448us
^-  y.ns.gin.ntt.net                            2  10   377   45m   -6994us[-7482us] +/-   91ms
^*  ipv4.ntp3.rbauman.com         2  10   377   874    +507us[ +412us] +/- 4536us

# chronyc -a makestep                    # 手動で同期 

【サーバ設定】
# vi /etc/chrony.conf
allow 192.168.254.0/24
# systemctl restart chronyd
# firewall-cmd --add-service=ntp --permanent
# firewall-cmd --reload

【クライアント設定】
# vi /etc/chrony.conf
#pool 2.pool.ntp.org iburst
pool 192.168.254.253 iburst
pool 192.168.254.254 iburst
# systemctl restart chronyd
# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.254.253               2   6   377    33    +58us[ +130us] +/- 2715us
M (Mode)
ソースのモードを示す。記号ごとにそれぞれ以下の意味がある。
^ はサーバー
= はピア
# はローカル

S (State)
この列は、ソースの状態を示します。
「*」は、chronyd が現在同期しているソースを表す。
「+」は、選択したソースと結合する、受け入れ可能なソースを表す。
「-」は、受け入れ可能なソースで、結合アルゴリズムにより除外されたものを表す。
「?」は、接続が切断されたソース、またはパケットがすべてのテストをパスしないソースを表す。
「x」は、chronyd が falseticker と考える (つまり、その時間が他の大半のソースと一致しない) クロックを表す。
「~」は、時間の変動性が大きすぎるように見えるソースを表します。
「?」条件は、少なくとも 3 つのサンプルが収集されるまで開始時にも表示されます。

2022年5月6日金曜日

Django web Serverの構築(websv)

# dnf install httpd httpd-devel
# dnf install python36 python3-libs python36-devel
# pip3 install --upgrade pip
# mkdir /var/www/wsgi
# chmod 0755 /var/www/wsgi
# mkdir /var/log/python/
# chmod  a+w /var/log/python                           ※ あとで直す。
# vi /etc/httpd/conf/httpd.conf
ServerName websv1:80
# dnf install gcc rpm-build
# pip3 install mod-wsgi
# vi /etc/httpd/conf.modules.d/20-wsgi.conf
LoadModule wsgi_module /usr/local/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so
# httpd -M|grep wsgi
proxy_uwsgi_module (shared)
wsgi_module (shared)
#  vi /etc/httpd/conf.d/wsgi.conf
#
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 300
WSGIApplicationGroup %{GLOBAL}
WSGISocketPrefix /var/run/wsgi

WSGIDaemonProcess test user=apache group=apache processes=1 threads=100 maximum-                               requests=10000 \
                             home=/var/www/wsgi/test \
                            python-home=/var/www/wsgi/test/myenv/venv36-d324 \
                           python-path=/var/www/wsgi/test/django:/var/www/wsgi/test/myenv/venv36-d324/lib/python3.6/site-packages \
                           lang=ja_JP.utf8

WSGIScriptAlias /test /var/www/wsgi/test/Config/wsgi.py process-group=test

Alias /static/ /var/www/wsgi/test/static/
Alias /media/ /var/www/wsgi/test/media/
WSGIPassAuthorization on

# for s3
Alias /assets/ /s3/assets/
Alias /media/  /s3/media/
<Directory /s3/assets>
   Require all granted
</Directory>

<Directory /s3/media>
   Require all granted
</Directory>

<Location /test>
WSGIProcessGroup test
</Location>

#   cd  /var/www/wsgi
#    scp -rp root@websv1:/var/www/wsgi/test  .
# chown -R apache:apache test

# firewall-cmd --add-service=http --zone=public --permanent
# firewall-cmd --reload

# dnf remove mariadb* mysql*
# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash
# dnf install MariaDB-client 
# dnf install zbar





apache バーチャルホスト リバースプロキシ 設定(仮想ホスト)

#---------------------------------------
# LOCATION リバースプロキシ
#---------------------------------------
# vi /etc/httpd/conf.d/wsgi.conf

<Proxy *>
  Require all granted
  #Order Deny,Allow
  #Deny from all
  #Allow from 192.168.25.146
</Proxy>

ProxyRequests Off
ProxyPreserveHost On

<Location /office>
  proxyPass http://192.168.25.146:80/office/ keepalive=On
  ProxyPassReverse http://192.168.25.146:80/office/
</Location>




#----------------------------------
# VirtualHost   ( 仮想ホスト )
#----------------------------------
インターフェースに複数IPを割り当てる
  VLAN=global_network
  VLAN_IP_1=192.168.1.241/24
  VLAN_IP_1=192.168.1.242/24
  VLAN_IP_1=192.168.1.243/24
  VLAN_IP_1=192.168.1.244/24
  VLAN_GW=192.168.1.1
  nmcli c delete $VLAN
  nmcli c add type ethernet ifname $DEVICE con-name "$VLAN"
  nmcli c mod $VLAN    ipv4.addresses $VLAN_IP_1
  nmcli c mod $VLAN +ipv4.addresses $VLAN_IP_2
  nmcli c mod $VLAN +ipv4.addresses $VLAN_IP_3
  nmcli c mod $VLAN +ipv4.addresses $VLAN_IP_4
  nmcli c mod $VLAN ipv4.method manual
  nmcli c mod $VLAN connection.autoconnect yes
  nmcli c mod $VLAN ipv4.gateway $VLAN_GW
  nmcli c up $VLAN


# dnf install httpd mod_ssl
# vi /etc/httpd/conf.d/httpd-vhost.conf
#-------------------------------------------
#  Link Server
#-------------------------------------------

<VirtualHost *:80>
  ServerName link.mydomain.example

  RewriteEngine on
  RewriteRule (.*)?$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</VirtualHost>

<VirtualHost *:443>
ServerName link.mydomain.example

<Proxy *>
Require all granted
</Proxy>

ProxyRequests Off
ProxyPreserveHost On

# LogLevel warn
# TransferLog logs/ssl_access_log

ProxyPass / http://10.0.1.101:80/ keepalive=On
ProxyPassReverse / http://10.0.1.101:80/
RequestHeader set X-Forwarded-Proto "https"

SSLEngine on

# SSLv2、SSLv3, TLS1.0 TLS1.1を無効化する
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite ECDSA+AESGCM
# SSLCipherSuite PROFILE=SYSTEM
SSLHonorCipherOrder Off

SSLCertificateFile /etc/ssl/certs/ssl-cert-link.crt
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-link.key

</VirtualHost>

# cd /etc/ssl
 # ln -s  ../pki/tls/private private

#  apachectl configtest

# firewall-cmd --add-service=https --permanent
# firewall-cmd --reload

※ 下記は、httpdを起動すると勝手につくられる。
/etc/pki/tls/certs/localhost.crt
/etc/pki/tls/private/localhost.key
※ ssl.conf内の<VirtualHost _default_:443>は、マッチしなかった場合のデフォルト

【自己証明書】
# cd ~/Cert
#  openssl version
OpenSSL 1.1.1k  FIPS 25 Mar 2021
# openssl genrsa  -out ssl-cert-link.key  2048
# openssl req -new -key ssl-cert-link.key -out ssl-cert-link.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:link.mydomain.example
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
# openssl x509 -days 3650 -req -signkey ssl-cert-link.key -in ssl-cert-link.csr -out  ssl-cert-link.crt

# cp  ssl-cert-link.crt  /etc/ssl/certs/
# cp ssl-cert-link.key /etc/ssl/private/

※確認方法
# openssl req -noout -text -in ssl-cert-link.csr
# openssl x509 -text -fingerprint -noout -in  ssl-cert-service.crt

URL: https://link.mydomain.example/test
URL: https://service.mydomain.example/test
URL: https://office.mydomain.example/test

※ key : RSA PRIVATE KEY(鍵)
     csr: CERTIFICATE REQUES(証明書の署名リクエスト)
     crt: CERTIFICATE( サーバ証明書)
     pem: BEGIN/END行で挟んだ書式(入れ物)

Rocky Linux 同士でRDP

sudo dnf -y install epel-release sudo dnf install xrdp -y sudo firewall-cmd --add-port=3389/tcp --zone=public --permanent sudo firewall-cmd ...