
背景
仕事でUbuntu22.04 LTS+Apache+MySQLの組み合わせでRedmineのセットアップを行ったので備忘録をつける
実施した環境
ツール | バージョン | 備考 |
---|---|---|
Ubuntu | 22.04 LTS | OS(Linux) |
Redmine | 5.1.2 | |
Ruby | 3.2.4 | |
Apache | 2.4.52 | |
Phusion Passenger | 6.0.22 | |
MySQL | 8.0.30 |
Ubuntuのセットアップ
Ubuntuのインストール作業自体は省略します。
基本設定
1. ユーザー [redmine]を作成
adduser redmine
2. ユーザー [redmine]に sudo 権限を付与
sudo gpasswd -a redmine sudo
3. rootユーザーのパスワードを設定
sudo passwd root
4. ホスト名設定
hostnamectl set-hostname "変更後のホスト名"
5. ターミナルでの警告音を無効化
sed -i -e "s/# set bell-style none/set bell-style none/g" /etc/inputrc
6. Vimでの警告音を無効化
echo "set belloff=all" >> /etc/vim/vimrc
7. 起動時初期ディレクトリ設定
起動時にルートディレクトリに遷移するshellを作成する
※profile.d内に格納したshellファイルは、起動時に自動的に実行される
sudo nano /etc/profile.d/startdir.sh
#startdir.sh #!/bin/bash cd ~
8. ライブラリのインストールパッケージリストを最新化し、インストール済みのモジュールを更新
sudo apt update sudo apt upgrade -y
9. manコマンドの日本語化
sudo apt update sudo apt install manpages-ja sudo apt install manpages-ja-dev sudo apt install language-pack-ja sudo update-locale LANG=ja_JP.UTF-8 man ls
ネットワーク設定
1. netplan配下のyamlファイルに対して、以下のように設定
sudo nano etc/netplan/00-installer-config.yaml
network: ethernets: enp0s25: addresses: [xxx.xxx.xxx.xxx/24] routes: - to: default via: xxx.xxx.xxx.254 scope: link renderer: networkd version: 2
2. ファイルのパーミッションを変更
sudo chmod 600 /etc/netplan/99_config.yaml
3. 設定の反映
sudo netplan apply
4. IPアドレスの確認
ip addr
プロキシ設定
社内ネットワーク配下で通信したい場合、プロキシ設定が必須となってくる
プロキシ経由ではなく、直接通信している場合、省略しても問題ない
1. proxy.confにプロキシサーバのIPアドレスを指定
sudo nano /etc/apt/apt.conf.d/proxy.conf
Acquire::http::Proxy "http://xxx.xxx.xxx.xxx:<ポート番号>; Acquire::https::Proxy "https://xxx.xxx.xxx.xxx:<ポート番号>;
2. wget/aptコマンドの設定
wgetrcにプロキシサーバのIPアドレスを指定し、wget/aptコマンドを設定
sudo nano /etc/wgetrc http_proxy = http://xxx.xxx.xxx.xxx:<ポート番号> https_proxy = https://xxx.xxx.xxx.xxx:<ポート番号>
3. gitコマンドの設定
以下のコマンドを実行し、gitコマンドのプロキシを設定
git config --global http.proxy http://xxx.xxx.xxx.xxx:<ポート番号>
4. curlコマンドの設定
以下のコマンドを実行し、curlコマンドのプロキシを設定
sudo nano .bashrc export HTTP_PROXY=http://xxx.xxx.xxx.xxx:<ポート番号> export HTTPS_PROXY=https://xxx.xxx.xxx.xxx:<ポート番号>
その他設定
1. NTP設定
timesyncd.confにNTPサーバのIPアドレスを指定
sudo nano /etc/systemd/timesyncd.conf NTP=http://xxx.xxx.xxx.xxx
2. 自動スリープ無効
リモートでの操作が前提である為、
一定時間操作しない場合、自動でスリープモードにならないように設定
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
3. rootのssh接続を許可する
設定することで、WinSCPにおいてrootアカウントでログインできるようになる
sudo nano /etc/ssh/sshd_config PermitRootLogin yes service sshd restart
MySQLのセットアップ
この後の作業の前提となるライブラリをインストール
sudo apt install -y build-essential git vim libffi-dev libyaml-dev
MySQLのインストール
1. 以下のコマンドを実行、MySQLのv8.0.30をインストール
別バージョンをインストールしたい場合、こちらから目的のバージョンを探しダウンロードURLを取得する
cd ~ wget https://dev.mysql.com/get/mysql-apt-config_0.8.30-1_all.deb sudo dpkg -i mysql-apt-config_0.8.30-1_all.deb
インストール対象を聞かれるが、デフォルトのままで問題ないため、そのままOKで進行 rootのパスワードの設定は、 “Use Strong Password Encryption”を選択
sudo apt update sudo apt install -y mysql-server libmysqlclient-dev
2. MySQLが稼働していることを確認
systemctl status mysql
次のような出力が表示され、Active: active
が確認できれば正常に稼働している
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-06-17 06:53:26 UTC; 16s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Main PID: 26378 (mysqld) Status: "Server is operational" Tasks: 38 (limit: 4575) Memory: 359.0M CGroup: /system.slice/mysql.service └─26378 /usr/sbin/mysqld
3. 安全性の低いデフォルトの設定を変更する
sudo mysql_secure_installation
設定例:
redmine@redmine-test:~$ sudo mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: N Using existing password for root. Change the password for root ? ((Press y|Y for Yes, any other key for No) : N ... skipping. By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y Success.
データベースとユーザーの作成
1. MySQLのシェルを起動
mysql -u root -p
2. ユーザーとデータベースを作成する
※my_passwordの変更を忘れずに実行すること
CREATE DATABASE redmine CHARACTERSET utf8mb4; CREATEUSER'redmine'@'localhost'IDENTIFIEDBY'my_password'; GRANTALLPRIVILEGESON redmine.* TO'redmine'@'localhost'; FLUSH PRIVILEGES; EXIT;
Rubyのセットアップ
rbenvのインストール
1. 以下のコマンドを実行し、rbenvをインストール
sudo apt install -y rbenv rbenv init
表示されたメッセージに従い、.bashrc
に以下を追加します。
eval "$(rbenv init -)"
2. 変更内容を反映します。
source .bashrc
ruby-buildのインストール
以下のコマンドを実行し、ruby-buildをインストール
cd ~ mkdir -p "$(rbenv root)"/plugins git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
Rubyのインストール
rbenvを使ってRubyをインストールする準備が完了した為、Rubyのインストールを実行
rbenv install --list rbenv install 3.2.4 rbenv global 3.2.4
インストール後にバージョンを確認
ruby -v
Redmineのセットアップ
Redmineのインストール
1. Redmine.orgから最新版をダウンロードし、ファイルを展開する。
wget https://www.redmine.org/releases/redmine-5.1.2.tar.gz cd /opt sudo tar xvf ~/redmine-5.1.2.tar.gz
2. リンクを作成し、ファイルの所有者を変更する。
sudo ln -s redmine-5.1.2 redmine sudo chown -R redmine:redmine redmine/
Gemのインストール
cd /opt/redmine bundle config set --local without 'development test' bundle install
データベースの設定
1. Session Store Secretの作成
RAILS_ENV=production bundle exec rake generate_secret_token
2. redmine/config/database.yml`をテンプレートからコピーし作成
sudo cp redmine/config/database.yml.example redmine/config/database.yml sudo nano redmine/config/database.yml
production:adapter: mysql2 database: redmine_db host: localhost username: root password:"sgs7104129"encoding: utf8mb4 variables:transaction_isolation:"READ-COMMITTED"
3. データベースの初期化コマンドを実行。日本語で使用する場合は、"ja"を選択する
RAILS_ENV=production bundle exec rake db:migrate RAILS_ENV=production bundle exec rake redmine:load_default_data
Passenger、Apacheのインストール
sudo apt install apache2 apache2-dev
sudo apt-get install -y dirmngr gnupg apt-transport-https ca-certificates curl curl https://oss-binaries.phusionpassenger.com/auto-software-signing-gpg-key.txt | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/phusion.gpg >/dev/null sudo shell -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger jammy main > /etc/apt/sources.list.d/passenger.list' sudo apt-get update sudo apt-get install -y libapache2-mod-passenger sudo /usr/bin/passenger-config validate-install sudo systemctl restart apache2 sudo /usr/sbin/passenger-memory-stats
Passengerの設定
1. Passengerの設定内容出力コマンドを実行
以下のコマンドを実行、Passengerの設定内容を出力する
passenger-install-apache2-module --snippet
redmine.confの設定
1. redmineのpublicフォルダにアクセスできるよう以下のように記述する
また、先ほど実行したPassengerの基本設定コマンドの出力結果をそのまま貼り付ける。
sudo nano /etc/apache2/sites-available/redmine.conf
# Redmineのインストールディレクトリへのアクセスを許可 <Directory "/opt/redmine-5.1.2/public"> Require all granted </Directory> # Passengerの設定 # passenger-install-apache2-module --snippet で表示された設定を記述。 # LoadModule passenger_module /home/redmine/.rbenv/versions/3.2.4/lib/ruby/gems/3.2.0/gems/passenger-6.0.22/buildout/apache2/mod_passenger.so <IfModule mod_passenger.c> PassengerRoot /home/redmine/.rbenv/versions/3.2.4/lib/ruby/gems/3.2.0/gems/passenger-6.0.22 PassengerDefaultRuby /home/redmine/.rbenv/versions/3.2.4/bin/ruby </IfModule> PassengerMaxPoolSize 20 PassengerMaxInstancesPerApp 4 PassengerPoolIdleTime 864000 PassengerStatThrottleRate 10 <Directory /opt/redmine-5.1.2/public> Allow from all Options -MultiViews Require all granted </Directory> Alias /redmine /opt/redmine-5.1.2/public <Location /redmine> PassengerBaseURI /redmine PassengerAppRoot /opt/redmine-5.1.2 </Location>
Apacheの設定
以下のコマンドを実行し、ApacheにRedmineの設定を組み込む。
sudo a2ensite redmine sudo a2ensite default-ssl sudo a2enmod ssl sudo a2enmod headers sudo systemctl restart apache2 sudo apache2ctl configtest
CORSの設定
Rack CORS Middlewareを使用する
cd /opt/redmine sudo nano Gemfile.local
次を追加します。
gem'rack-cors'
Rack CORS Middlewareを利用します。
cd /opt/redmine sudo nano Gemfile.local
次を追加します。
gem'rack-cors'
Gemをインストールします。
bundle install
config/initializers/cors.rb
を作成します。
sudo nano config/initializers/cors.rb
次の内容を書き込みます。
Rails.application.config.middleware.insert_before 0, Rack::Corsdo allow do origins '*' resource '*', headers: :any, methods: [:get, :post, :patch, :put, :delete, :options] endend
Apacheを再起動します。
sudo service apache2 restart
ApacheにCORS設定を組み込み、サーバーを再起動
sudo a2enconf cors sudo a2enmod rewrite sudo systemctl restart apache2
サーバの起動を確認
Webブラウザで以下にアクセス
http://xxx.xxx.xxx.xxx/redmine