Snipe-ITのインストールをShellスクリプトで半自動化する

 少しだけ時間ができたので、全然今の仕事と関係ないけどIT資産管理とかでいいやつないかなと探してたら出てきたのがこのSnipe-ITとかいうやつ。

なんかいろいろできそうで、挙句にPC構成情報まで無料で集められるとかいうなかなかの優れものらしい。とりあえず環境作るのに色々とあれらしいのでひとまず、簡単に試せるようにしとこうとインストールについてスクリプトで動かせるようにしておいた。

◆Shellスクリプト

#!/bin/bash
HOST_NAME=snipeit-srv
DATABASE=snipeit
DB_INITIAL_PASSWORD=password
DB_ROOT_PASSWORD=password
DB_USER=snipeit
DB_PASSWORD=snipeit
DB_HOST=localhost
MAIL_HOST=YOURSMTP_SERVER
MAIL_USERNAME=YOUR_USER
MAIL_PASSWORD=YOUR_PASSWORD
MAIL_FROM_ADDR=snipe-it@example.com
MAIL_FROM_NAME=system-admin
MAIL_REPLYTO_ADDR=snipe-it@example.com
MAIL_REPLYTO_NAME=system-admin
PHP_VERSION=8.3
WEB_USER=www-data
SERVER_NAME=snipeit.example.com
SITE_URL=https://snipeit.example.com
WEB_DIR=/var/www/
DOCUMENT_ROOT=${WEB_DIR}/snipe-it
ADMIN_MAIL=yourmail@example.com


sudo hostnamectl set-hostname ${HOST_NAME}


# ---------------------------------------------
# 1.PHPとComposerインストール
# ---------------------------------------------
read -p "## Do you want to install php? (y/n) [y]: " -e -i "y" phpResponseinput
if [ "$phpResponseinput" == "y" ] || [ "$phpResponseinput" == "Y" ]; then
    echo "# install php process..."
    sudo apt -y install software-properties-common
    sudo add-apt-repository ppa:ondrej/php
    sudo apt -y update
    sudo apt install -y php${PHP_VERSION}-{bcmath,common,ctype,curl,fileinfo,fpm,gd,iconv,intl,mbstring,mysql,soap,xml,xsl,zip,cli}
    curl -sS https://getcomposer.org/installer -o composer-setup.php
    sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
    sudo sed -i 's/;extension=zip/extension=zip/' /etc/php/${PHP_VERSION}/cli/php.ini
    echo "# Install php done!"
fi


# ---------------------------------------------
# 2.Apacheインストール
# ---------------------------------------------
read -p "## Do you want to install Apache2? (y/n) [y]: " -e -i "y" apacheResponseinput
if [ "$apacheResponseinput" == "y" ] || [ "$apacheResponseinput" == "Y" ]; then
    echo "# install apache process..."
    sudo add-apt-repository ppa:ondrej/apache2
    sudo apt-get update
    sudo apt-get install -y apache2 libapache2-mod-php
    sudo cp /etc/apache2/conf-enabled/security.conf /etc/apache2/conf-enabled/security.conf.org
    # ---------------------------------------------
    # security.conf設定
    # ---------------------------------------------
    sudo tee /etc/apache2/conf-enabled/security.conf <<_EOF_
# バージョン情報の隠蔽
ServerTokens Prod
Header unset "X-Powered-By"
# httpoxy 対策
RequestHeader unset Proxy
# クリックジャッキング対策
Header append X-Frame-Options SAMEORIGIN
# XSS対策
Header set X-XSS-Protection "1; mode=block"
Header set X-Content-Type-Options nosniff
# XST対策
TraceEnable Off
_EOF_
    sudo sed -i 's#DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm#DirectoryIndex index.php index.html index.htm#' /etc/apache2/mods-enabled/dir.conf
    sudo sed -i 's#Options Indexes FollowSymLinks#Options FollowSymLinks#' /etc/apache2/apache2.conf
    sudo a2enmod headers
    sudo a2enmod proxy_fcgi setenvif
    sudo a2enconf php${PHP_VERSION}-fpm
    sudo systemctl restart php${PHP_VERSION}-fpm apache2
    echo "# Install Apache2 done!"
fi


# ---------------------------------------------
# 3.Snipe-ITインストール
# ---------------------------------------------
read -p "## Do you want to install snipe-IT? (y/n) [y]: " -e -i "y" snipeResponseinput
if [ "$snipeResponseinput" == "y" ] || [ "$snipeResponseinput" == "Y" ]; then
    echo "# install snipe-IT process..."
    sudo chown -R www-data:www-data ${WEB_DIR}
    # Settings file replace
    sudo git clone https://github.com/snipe/snipe-it ${DOCUMENT_ROOT}
    sudo chown -R www-data:www-data ${DOCUMENT_ROOT}
    sudo cp -pi ${DOCUMENT_ROOT}/.env.example ${DOCUMENT_ROOT}/.env
    sudo sed -i "s#UTC#Asia/Tokyo#g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/en-US/ja-JP/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/DB_HOST=127.0.0.1/DB_HOST=${DB_HOST}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/DB_DATABASE=null/DB_DATABASE=${DATABASE}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/DB_USERNAME=null/DB_USERNAME=${DB_USER}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/DB_PASSWORD=null/DB_PASSWORD=${DB_PASSWORD}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s#APP_URL=null#APP_URL=${SITE_URL}#g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_HOST=email-smtp.us-west-2.amazonaws.com/MAIL_HOST=${MAIL_HOST}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_USERNAME=YOURUSERNAME/MAIL_USERNAME=${MAIL_USERNAME}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_PASSWORD=YOURPASSWORD/MAIL_PASSWORD=${MAIL_PASSWORD}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_FROM_ADDR=you@example.com/MAIL_FROM_ADDR=${MAIL_FROM_ADDR}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_FROM_NAME='Snipe-IT'/MAIL_FROM_NAME='${MAIL_FROM_NAME}'/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_REPLYTO_ADDR=you@example.com/MAIL_REPLYTO_ADDR=${MAIL_REPLYTO_ADDR}/g" ${DOCUMENT_ROOT}/.env
    sudo sed -i "s/MAIL_REPLYTO_NAME='Snipe-IT'/MAIL_REPLYTO_NAME='${MAIL_REPLYTO_NAME}'/g" ${DOCUMENT_ROOT}/.env
    # Install snipe-it
    cd ${DOCUMENT_ROOT}
    sudo apt install -y curl unzip php-curl php-xml
    sudo -u www-data composer update --no-plugins --no-scripts --ignore-platform-reqs
    sudo -u www-data composer install --no-dev --prefer-source --no-plugins --no-scripts --ignore-platform-reqs
    sudo -u www-data php artisan key:generate
fi


# ---------------------------------------------
# 4.MariaDBインストール
# ---------------------------------------------
read -p "## Do you want to install MariaDB? (y/n) [y]: " -e -i "y" MariaDBResponseinput
if [ "$MariaDBResponseinput" == "y" ] || [ "$MariaDBResponseinput" == "Y" ]; then
    read -p "## Do you want to Setting localDataBase? (y/n) [y]: " -e -i "y" localDBResponseinput
    if [ "$localDBResponseinput" == "y" ] || [ "$localDBResponseinput" == "Y" ]; then
        echo "# please password input ${DB_PASSWORD} "
        sudo apt-get -y install apt-transport-https curl
        sudo mkdir -p /etc/apt/keyrings
        sudo curl -o /etc/apt/keyrings/mariadb-keyring.pgp 'https://mariadb.org/mariadb_release_signing_key.pgp'
        sudo apt-get -y update
        sudo apt-get -y install mariadb-server
        sudo mysql -u root -p'${DB_INITIAL_PASSWORD}' -h localhost<<_EOF_
ALTER USER root@localhost IDENTIFIED BY '${DB_ROOT_PASSWORD}';
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE IF EXISTS test;
FLUSH PRIVILEGES;
_EOF_
    #
    # データベース作成
    #
    sudo mysql -u root -p"${DB_ROOT_PASSWORD}" -h "${DB_HOST}"<<_EOF_
CREATE DATABASE ${DATABASE};
create user '${DB_USER}'@'${DB_HOST}' identified by '${DB_PASSWORD}';
grant all privileges on ${DATABASE}.* to '${DB_USER}'@'${DB_HOST}';
FLUSH PRIVILEGES;
_EOF_
    else
      #
      # ローカルデータベース以外にインストールする場合
      #
      sudo apt install -y mariadb-client
      #
      # データベース作成
      #
      echo "please input password:${DB_ROOT_PASSWORD}"
      sudo mysql -u root -h ${DB_HOST} -p<<_EOF_
CREATE DATABASE ${DATABASE};
create user '${DB_USER}'@'%' identified by '${DB_PASSWORD}';
grant all privileges on ${DATABASE}.* to '${DB_USER}'@'%';
FLUSH PRIVILEGES;
_EOF_
    fi
fi


# ---------------------------------------------
# 5.SSL設定 Let'sEncrypt使用しない場合はn
# ---------------------------------------------
read -p "## Do you want to install Let's Encrypt? (y/n) [y]: " -e -i "y" sslResponseinput
if [ "$sslResponseinput" == "y" ] || [ "$sslResponseinput" == "Y" ]; then
    echo "# install Let's Encrypt process..."
    sudo apt -y install certbot
    sudo certbot certonly --webroot -w /var/www/html -d ${SERVER_NAME} --agree-tos --email ${ADMIN_MAIL} --non-interactive
    echo "# Install Let'sEncrypt done!"
fi


# ---------------------------------------------
# 6.バーチャルホスト設定
# ---------------------------------------------
read -p "## Do you want to Setting Vitualhost? (y/n) [y]: " -e -i "y" vhostResponseinput
if [ "$vhostResponseinput" == "y" ] || [ "$vhostResponseinput" == "Y" ]; then
    echo "# Vitualhost setting process..."
    sudo tee /etc/apache2/sites-available/000-default.conf <<_EOT_
<VirtualHost *:443>
    ServerName ${SERVER_NAME}
    ServerAdmin ${ADMIN_MAIL}
    DocumentRoot ${DOCUMENT_ROOT}/public
    <FilesMatch \.php$>
      SetHandler application/x-httpd-php
    </FilesMatch>
    <Directory ${DOCUMENT_ROOT}/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    ErrorLog  \${APACHE_LOG_DIR}/error.log
    LogLevel warn
    CustomLog  \${APACHE_LOG_DIR}/access.log combined
    ServerSignature On
    SSLEngine on
    SSLCertificateFile      /etc/ssl/certs/ssl-cert-snakeoil.pem
    SSLCertificateKeyFile   /etc/ssl/private/ssl-cert-snakeoil.key
    #SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt
</VirtualHost>
_EOT_

	# Let'sEncrypt
	if [ "$sslResponseinput" == "y" ] || [ "$sslResponseinput" == "Y" ]; then
	    sudo sed -i 's#/etc/ssl/certs/ssl-cert-snakeoil.pem#/etc/letsencrypt/live/'"${SERVER_NAME}"'/cert.pem#' /etc/apache2/sites-available/000-default.conf
	    sudo sed -i 's#/etc/ssl/private/ssl-cert-snakeoil.key#/etc/letsencrypt/live/'"${SERVER_NAME}"'/privkey.pem#' /etc/apache2/sites-available/000-default.conf
	    sudo sed -i 's|#SSLCertificateChainFile /etc/apache2/ssl.crt/server-ca.crt|SSLCertificateChainFile /etc/letsencrypt/live/'"${SERVER_NAME}"'/chain.pem|' /etc/apache2/sites-available/000-default.conf
	fi

    sudo a2ensite default-ssl
    sudo sed -i 's|/var/www/html|${DOCUMENT_ROOT}|' /etc/apache2/sites-available/default-ssl.conf
    sudo a2enmod ssl
    sudo a2enmod rewrite
    sudo a2dismod mpm_event
    sudo a2enmod mpm_prefork
    sudo a2enmod php${PHP_VERSION}
    sudo a2dissite default-ssl
    sudo rm /var/www/html/index.html
    sudo systemctl restart php${PHP_VERSION}-fpm apache2
    echo "# Vitualhost setting done!"
fi

echo "### snipe-IT Setting Done!"

◆参考サイト

・マニュアル

・Install


・composerエラー


・CloudSQL



まぁ今までObserviumとかZabbixとかも似たようなことやっているのでその延長線上でやってみたところあります。想定はUbuntu22.04LTSだけど、ひとまずUbuntu24.04LTSでうごいてます。あとAWS環境とGoogleCloud環境でも試して動いたのでどっちでも好きな方で試せそうです。Ubuntu24.04LTSでのShellスクリプトインストールはZabbixとかObserviumも動くか試しておいた方がいいかな。
Snipe-ITについてはLDAP連携もできるらしいので時間ができたらDocker使ってAD連携も試してみたい。
今やってるマルチクラウドVPNが落ち着いたらかなぁ。
他にもWindows11の22H2のサポート期限とかUbuntu24.04LTS切替で試しておくのとか色々とあるし、やりたいことも多い中で時間があまりくて大変だなぁと思った。

コメント

このブログの人気の投稿

GASでGoogleDriveのサブフォルダとファイル一覧を出力する

証券外務員1種勉強(計算式暗記用メモ)

マクロ経済学(IS-LM分析)