投稿

6月, 2020の投稿を表示しています

pythonでEC2のリソースチェックする(AWS APIを利用) on CentOS

前提 aws cliをインストール( Installing the AWS CLI )して、aws configureは設定しておく。 aws configureしておく。( What Is the AWS Command Line Interface? ) vagrantを使ってのインストールは前回の( ココ )を参照 やること 1. boto3のインストール 1. boto3のインストール # インストール $ pip install boto3 # バージョン確認 $ pip freeze | grep boto3 boto3==1.14.12 すべてのインスタンスの起動、停止 ec2_status.py #!/usr/bin/env python3 import sys import logging import boto3 import botocore from ec2.check import EC2_Instance_Check # ログレベル設定 logging.basicConfig(level=logging.INFO) ''' # ログ群 logging.critical('critical') logging.error('error') logging.warning('warning') logging.info('info') logging.debug('debug') ''' class RDS_Instance_Check: rds = None def __init__(self): self.rds = boto3.client('rds') def get_tags_for_db(self, db): try: logging.debug("get_tags_for_db start") instance_arn = db['DBInsta...

CentOS7.8にWordpressをインストールする

やること。 php7.2のインストール MySQLのインストール Apache起動 Wordpressインストール 1. php7.2のインストール やること。 # リポジトリ追加 $ yum -y install epel-release $ yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm # リポジトリ確認 $ ls -l /etc/yum.repos.d/ total 96 ----------------------- -rw-r--r--. 1 root root 456 Feb 18 01:07 remi-php54.repo -rw-r--r--. 1 root root 1314 Feb 18 01:07 remi-php70.repo -rw-r--r--. 1 root root 1314 Feb 18 01:07 remi-php71.repo -rw-r--r--. 1 root root 1314 Feb 18 01:07 remi-php72.repo -rw-r--r--. 1 root root 1314 Feb 18 01:07 remi-php73.repo -rw-r--r--. 1 root root 1314 Feb 18 01:07 remi-php74.repo -rw-r--r--. 1 root root 750 Feb 18 01:07 remi-safe.repo -rw-r--r--. 1 root root 2605 Feb 18 01:07 remi.repo # php7.4をインストール $ yum -y install httpd php74 php74-php php74-php-mysqlnd 2. MySQLのインストール # リポジトリインストール $ yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm # リポジトリ確認 $ ls -l /etc/yum.repos.d/ | grep mysql -rw-r--r--. 1 root root 1036...

Vagrant upでVirtualBox Guest Additions: Kernel headers not found for target kernel Error

Vagrant UPするといきなりエラー発生。 The following SSH command responded with a non-zero exit status. Vagrant assumes that this means the command failed! /usr/sbin/rcvboxadd setup Stdout from the command: VirtualBox Guest Additions: Starting. VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel modules. This may take a while. VirtualBox Guest Additions: To build modules for other installed kernels, run VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup &lt version &gt VirtualBox Guest Additions: or VirtualBox Guest Additions: /sbin/rcvboxadd quicksetup all VirtualBox Guest Additions: Kernel headers not found for target kernel 3.10.0-1127.13.1.el7.x86_64. Please install them and execute /sbin/rcvboxadd setup Stderr from the command: modprobe vboxguest failed The log file /var/log/vboxadd-setup.log may contain further information. まず、記載されているKernelをInstallする。 「Kernel headers not found for target kernel 3.10.0-1127.13.1.el7.x86_64.」これですね。 $ yum -y ...

CentOS7で利用するVagrantFileのまとめ

CentoOS環境で利用するVagrantFileを整理しておく。 VagrantFileは以下の通り。 Vagrantで生成したCentOSのバージョン確認 $ cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core) VagrantFileは以下の通り。 virtualenvインストール用shell $virtualenv = <<SHELL if ! type /usr/local/bin/virtualenv > /dev/null 2 > &1; then # echo $PATHは以下の結果となり、/usr/local/binは含まれていない。 # /usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin yum update yum install -y python3 pip3 install virtualenv # vagrantユーザとしてvirtualenvとansibleをインストール su -c "source /vagrant/provision/bash/install_ansible.sh" vagrant # vagrantユーザとしてaws apiをインストール su -c "source /vagrant/provision/bash/install_awsapi.sh" vagrant fi SHELL Vagrant.configure("2") do |config| config.vm.box = "bento/centos-7" config.vm.box_version = "202006.16.0" config.vm.network "private_network", ip: "192.168.33.10" config.vm.provider "virtualbox" do |vm| # vm name vm.n...

Ansibleを使ってVagrant上にmssqlserverを構築する

Ansibleを使ってVagrant上にmssqlserverを構築する。 基本、 ここのサイト をもとに勉強していますが、GuestAdditionsの競合回避のため、CentoOSは最新の7.8を使いました。 勉強もとのサイトのサンプルコードと異なる箇所のみ記載していきます。 大きな違いは、サンプルコードは、python3.7をインストールしていますが、CentOS7.8ではすでにインストール済であることです。 事前準備 プラグインのインストール vagrant-vbguestは、ホストOSとゲストOSのGuestAdditionsのバージョンを自動で合わせてくれるVagrantのプラグインです。 ホストとゲスト間で フォルダー を 共有 するには「 Guest Additions 」が必要です。 今回は、競合しないよう最新のViertualBoxとCentoOSを使いますが一応入れておく。 $ vagrant plugin install vagrant-vbguest ※不要な場合はVagrantfileでconfig.vbguest.auto_update = falseすること。 今回は、CentOSの最新版 config.vm.box = "bento/centos-7" config.vm.box_version = "202006.16.0" を使うことでhost側と差異がでないようにした。 ( vagrant cloud centos7 ) vagrant up後のバージョン確認 $ cat /etc/redhat-release CentOS Linux release 7.8.2003 (Core)   ansible構成作成 Vagrantファイル作成 # ansibleインストール用shell $ansible_install = < /dev/null 2>&1; then yum install -y python3 pip3 install virtualenv # vagrantユーザとしてvirtualenvとansibleをインストール su -c "source /vagrant/provisio...

Gitサーバを構築する(Ubuntu18.04)

Docker上のUbuntuにGitサーバを立てて使ってみる。 ここ( GitLab Official Linux Package )に沿ってインストールしていく。 1. Install and configure the necessary dependencies サーバ側にgitをインストール $ sudo apt install git $ git --version git version 2.17.1 $ sudo apt install curl openssh-server ca-certificates $ sudo apt install postfix Satellite system: All mail is sent to another machine, called a 'smarthost', for delivery. Local only: The only delivered mail is the mail for local users. There is no network. 1. No configuration 2. Internet Site 3. Internet with smarthost 4. Satellite system 5. Local only General type of mail configuration: 5 .............. .............. System mail name: test@gitserver.jp 2. Add the GitLab package repository and install the package GitLabのrepositoryを追加しInstall # Repositoryの追加とgitlabのインストール $ curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash $ sudo apt install gitlab-ce # gitlabの設定 $ sudo vim /etc/gitlab/gi...

dotnet coreでLoggingする

dotnet coreのログ取得方法を勉強する。 やり方は以下の通りとする。 プロジェクトは 前回 のをそのまま流用する appsettings.jsonにログ設定を記載する controllerで出力させる。 1. appsettings.jsonの修正 デフォルトは「すべてのプロバイダー用」である"LogLevel"で設定すればよいのだが、namespace個別にログレベルを設定する場合は「Debugプロバイダー用」を使う。 { "TestUpdateData": { "EndPointUrl": "http://localhost:8080", "AuthorizationKey": "12345678" }, // Debug プロバイダー用 "Logging": { "LogLevel": { "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", "WebApplication4.Controllers": "Debug" } }, // Console プロバイダー用 "Console": { "IncludeScopes": false, "LogLevel": { "Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning", "Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug", "Microsoft.AspNetCor...

Dotnet Core Webアプリを使ってDIを勉強する

イメージ
Dotnet Core WebアプリでDI(Dependency Injection)を勉強する。 今回は、以下をターゲットとする。 appsettings.jsonを使ったDI Controllerで利用 作成、修正するファイル appsettings.json Models/AppSettings.cs(新規作成) Startup.cs Controllers/HomeControllers 0. Dotnet Core Webアプリプロジェクトを作成する VisualStudioのファイル→新規作成→プロジェクト →ASP.NET Core Webアプリケーションを選択。 次に、Webアプリケーション(モデルビューコントローラー)を選択。 これでプロジェクトが作成される。 このプロジェクトに以下の修正を加える。 1. appsettings.json DIしたいデータを作成する。appsettings.jsonのデータを次のAppSettingsへInjectionする。 { "TestUpdateData": { "EndPointUrl": "http://localhost:8080", "AuthorizationKey": "12345678" }, "AllowedHosts": "*" } 2. Models/AppSettings.cs(新規作成) 上記で作成したjsonデータをここにInjectionする。 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebApplication4.Models { public class AppSettings { public TestDB TestUpdateData { get; set; } } public class TestDB { public string EndP...

DockerでShareフォルダを使う

イメージ
Dockerを使う際、ホストとゲストで共通フォルダ作成してデータのやり取りを行いたい。 Shareフォルダはdocker run時に指定するようだが、それだと今まで様々なモジュールをインストールして設定してきたものが無駄になる。 解決策としては、以下の通り。 1. 現在のコンテナを一旦イメージ化してImageIDを取得する。 2. そのImageIDを使用して、Shareフォルダを指定しdocker runする。 これだと、今までの設定を無駄にせず、Shareフォルダを作成できる。 事前準備 Dockerのsettingsでホスト側Shareフォルダを設定しておく。 1. 現在のコンテナを一旦イメージ化してImageIDを取得する $ docker commit --author="Hoge-san" --message="ssh, sqlcmd modules in ubuntu18.04LTS." ssh_ubuntu sqlcmd_srv ※注意:ここでタグを指定すると、なぜかdocker run時にエラーが発生する 2. そのImageIDを使用して、Shareフォルダを指定しdocker runする $ docker run -it -d -v $PWD/share:/tmp -p 10000:22 --name sqlcmd_ubuntu sqlcmd_srv ※$PWD/share→ホスト側:Windowsでディレクトリを作成しておく。  /tmp→ゲスト側:一番楽なのでここにした。 Shareフォルダを確認する $ ssh ubuntu@localhost -p 10000 ubuntu@ebb915f7517e:~$ ls -l /tmp total 3096 -rwxr-xr-x 1 root root 34941 Jun 12 06:57 docker_settings.png ※WindowsのShareフォルダに入れたファイルがUbuntuの/tmpフォルダで見れてます。

DockerのUbuntuコンテナにMSSQLコマンドラインツールをインストール

Docker上のUbuntuからSqlServerへ接続させるため、SQLServerコマンドラインツールをインストールする。 途中、いろいろエラーが発生したのでメモっておく。 Ubuntuバージョン確認 $ cat /etc/os-release apt updateエラー apt updateしたらいきなりエラー $ apt-get update ubuntu E: The method driver /usr/lib/apt/methods/https could not be found. # 解決策 $ apt-get install apt-transport-https gnupg エラー やっとGPGキーインポートだー、とcurlしたら E: gnupg, gnupg2 and gnupg1 do not seem to be installed, # 解決策(gnupgがインストールされていないため) $ apt-get update $ apt-get install -y gnupg2 恒久対策としては、Dockerファイルにあらかじめ定義しておくらしい。 ということでDockerファイル更新しておく。 FROM ubuntu:18.04 MAINTAINER Aleksandar Diklic "https://github.com/rastasheep" RUN apt-get update RUN apt-get install -y openssh-server RUN mkdir /var/run/sshd RUN echo 'root:root' |chpasswd RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config RUN mkdir /root/.ssh RUN apt-get install -y sudo RUN apt-get install -y nginx RUN ...

Dockerファイルを使ってUbuntuイメージを作成し、SSHログインする

DockerでUbuntuを起動し、SSHログインする Docker上のUbuntuにSSHログインするためには、Dockerファイルで指示する必要があるようだ。 一番簡単なのは、DockerhubのUbuntuイメージのDockerFileをそのまま流用することだ。 rastasheep/ubuntu-sshd をそのままコピーして少し足して使ってみる。 .\Dockerfile_ubuntu_18_04.txt rootのパスワードを"root"で設定していますね。 FROM ubuntu:12.04 MAINTAINER Aleksandar Diklic "https://github.com/rastasheep" RUN apt-get update RUN apt-get install -y openssh-server RUN mkdir /var/run/sshd RUN echo 'root:root' |chpasswd RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config RUN mkdir /root/.ssh RUN apt-get install -y sudo RUN apt-get install -y nginx RUN apt-get install -y vim RUN apt-get clean && \ rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* EXPOSE 22 EXPOSE 80 EXPOSE 433 CMD ["/usr/sbin/sshd", "-D"] イメージ作成開始 Dockerファイル: Dockerfile_ubuntu_18_04.txt Repository名: ssh_server で作成する。 $ doc...

DockerでSQLServerをインストールする

DockerでSQLServerのイメージを作成する Docker Hub から SQL Server 2019 Linux コンテナー イメージをプルします。 $ sudo docker pull mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04 2019-CU3-ubuntu-18.04: Pulling from mssql/server 5b7339215d1d: Pull complete 14ca88e9f672: Pull complete a31c3b1caad4: Pull complete b054a26005b7: Pull complete 59f979819d9b: Pull complete 29eb18117119: Pull complete 6de3c1cf897f: Pull complete 172ea0155639: Pull complete Digest: sha256:e064843673f08f22192c044ffa6a594b0670a3eb3f9ff7568dd7a65a698fc4d6 Status: Downloaded newer image for mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04 mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04 SQL Server 2019 Developer Edition のコンテナーが作成されます。 $ sudo docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=Passw0rd" \ > -p 1433:1433 --name sql1 \ > -d mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04 f4729a3a2c1f705083a90f426fc76b94a564857d489141c6588dd14e9fb5920e # 状態確認 $ sudo docker ps -a CONTAINER ID IM...

Dockerでubuntuを単純にインストール

Vagrant環境内のDockerでUbuntuをインストールする このコマンドでsudoをしなくてよくなるらしい。 dockerグループにvagrantユーザーを追加する。 $ sudo usermod -aG docker vagrant ubuntuのイメージを探す。 $ docker search ubuntu NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 10978 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 435 [OK] rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 244 [OK] consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 220 [OK] ubuntu-upstart Upstart ...

Vagrant環境下でDockerを動かす

Vagrant(Ubuntu18.04)を作成しつつDockerをインストールしておく まずは、Vagrantファイルの作成。 $ vagrant init ubuntu/bionic64 $ vagrant up 次にVagrantファイルにDockerをインストールしておくよう、provisionを追加。 $ diff Vagrantfile-docker Vagrantfile-org 32,33d31 < config.vm.network "forwarded_port", guest: 80, host: 10800, host_ip: "127.0.0.1" < config.vm.network "forwarded_port", guest: 443, host:10443, host_ip: "127.0.0.1" 37c35 < config.vm.network "private_network", ip: "192.168.33.10" --- > # config.vm.network "private_network", ip: "192.168.33.10" 48c46 < config.vm.synced_folder "./data", "/home/vagrant/data" --- > # config.vm.synced_folder "../data", "/vagrant_data" 54c52 < config.vm.provider "virtualbox" do |vb| --- > # config.vm.provider "virtualbox" do |vb| 59,60c57,58 < vb.memory = "8196" < end --- > # vb.memory = "...

Supervisorをインストールしてdotnetを動かす

■ 環境 Ubuntu20.04(vagrant+virtualBox) dotnet version3.1 すでに nginxでhttpsリバースプロキシを構築し.Net Coreアプリを動かす でnginxでリバースプロキシが構築されていること。 There’s an tool called Supervisor that is good at that so I’ll add it. WebサーバーSupervisorをインストール $ sudo apt-get install supervisor DotnetアプリをRelease Buildする $ sudo mkdir -p /var/aspnetcore $ cd ~/hellomvc $ sudo dotnet publish --configuration Release Microsoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core Copyright (C) Microsoft Corporation. All rights reserved. Determining projects to restore... Restored /home/vagrant/data/hellomvc/hellomvc.csproj (in 2.47 sec). hellomvc -> /home/vagrant/data/hellomvc/bin/Release/netcoreapp3.1/hellomvc.dll hellomvc -> /home/vagrant/data/hellomvc/bin/Release/netcoreapp3.1/publish/ アプリケーションの公開ディレクトリへコピーする $ sudo cp -R ./bin/Release/netcoreapp3.1/publish/* /var/aspnetcore/ $ ls /var/aspnetcore/ appsettings.Development.json appsettings.json hellomvc hellomvc.deps.json hellomvc.dll hellomvc.pdb hel...

nginxでhttpsリバースプロキシを構築し.Net Coreアプリを動かす

■ 前提条件 $dotnet run で http://localhost:5000にアクセスするとdotnetアプリが動作すること。 前回のブログ nginxでリバースプロキシさせてDotnet Coreを動かす で一通りリバースプロキシが構築できていること。 ■ nginxの設定 ngingxのconfigファイルを以下の通り作成する。 httpsでアクセスが来たら、localhost:5000にリバースプロキシする。 ./nginx.conf $ cat nginx.conf user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { include /etc/nginx/proxy_params; ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65;  server_names_hash_bucket_size 128; types_hash_max_size 2048; server_tokens off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /etc/nginx/logs/access.log; error_log /etc/nginx/logs/error.log; upstream helloapp{ server 127.0.0.1:5000; } server { listen 80; server_name localhost; index index.html; root /var/www/...

自己証明書作成

淡々と自己証明書を作成していく。 ディレクトリ作成。 $ mkdir -p /opt/pki $ mkdir -p /opt/pki/configs $ mkdir -p /opt/pki/crl $ mkdir -p /opt/pki/RootCA/newcerts $ mkdir -p /opt/pki/InterCA $ mkdir -p /opt/pki/Server $ mkdir -p /opt/pki/Client CSR( Certificate Signing Request )への署名時に必要なconfigファイルを作成しておく。 /opt/pki/configs/openssl_sign.cnf [ ca ] default_ca = CA_default [ CA_default ] dir = ./ certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt new_certs_dir = $dir/newcerts serial = $dir/serial crlnumber = $dir/crlnumber crl = $dir/crl.pem RANDFILE = $dir/.rand name_opt = ca_default cert_opt = ca_default default_days = 365 default_crl_days= 30 default_bits = 2048 default_md = sha256 preserve = no policy = policy_match [ policy_match ] countryName = match stateOrProvinceName = match organizationName = match organizationalUnitName = optional commonName ...

nginxでリバースプロキシさせてDotnet Coreを動かす

■ 前提条件 $dotnet run で http://localhost:5000にアクセスするとdotnetアプリが動作すること。 nginxを使ってリバースプロキシを構築する。 ■ nginxの設定 ngingxのconfigファイルを以下の通り作成する。 ポート8081でアクセスが来たら、localhost:5000にリバースプロキシする。 $ vim /etc/nginx/conf.d/app01.conf server { listen 8081; server_name localhost 127.0.0.1; charset utf-8; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location / { proxy_pass http://localhost:5000 ; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } configファイルの検証 $ sudo nginx -t 問題なければ、configのリロードで反映。 $ sudo nginx -s reload $ sudo service nginx stop $ sudo service nginx start でもよい。 $ dotnet run host端末から http://192.168.33.10::8081 でアクセスしてみよう。 つながったはず。 参考: nginxでASP.NET Coreをホストする (wiki) Supervisor: A Process Control System Centos 7 .Net...