> For the complete documentation index, see [llms.txt](https://yutakatay.gitbook.io/katapedia/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yutakatay.gitbook.io/katapedia/doc/sba.md).

# サーバ環境構築

サーバ環境を初期構築するためのメモ（あまり整理していない）

## 移行前作業

### イメージの移動

docker save -o &#x20;

sudo docker images | awk 'NR>1 {print $1":"$2}' | tr / \_ > aaa # スラッシュを加工

sudo docker images | awk 'NR>1 {print $1":"$2}' > bbb

paste -d " " aaa bbb | awk '{print "sudo docker save -o " $0}' > ccc

./ccc

docker load -i&#x20;

ls -1 | xargs -i sudo docker load -i {}

## アクセス制限

```
sudo vi  /etc/hosts.deny
ALL : ALL
```

```
sudo vi /etc/hosts.allow
ALL : network_address/24 network_address2/24
```

### proxyの設定

### sudoersの設定

```
Defaults    env_keep =  "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS"
Defaults    env_keep += "MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE"
Defaults    env_keep += "LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES"
Defaults    env_keep += "LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE"
Defaults    env_keep += "LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY"
Defaults    env_keep += "http_proxy https_proxy"
```

### プロキシの例外設定

```
sudo apt-get install dconf-tools
```

dconf-tools起動

system->proxy->ignore-hosts

## インストール

### 基本モジュールのインストール

```
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install openssh-server dconf-tools zsh vim git gnome-shell  tmux gcc make ruby rails ubuntu-gnome-desktop
sudo apt-get install aptitude python-keyring ttf-ubuntu-font-family hddtemp curl lm-sensors conky-all
sudo apt-get install git-all git-core
sudo apt-get install fcitx fcitx-mozc fcitx-libs-qt5 fcitx-frontend-qt5
sudo apt-get install gpointing-device-settings
sudo apt-get install php-pear php5-cgi php5-fpm
sudo apt-get install xrdp rdesktop
sudo apt-get install autokey-gtk
```

### redmine用

### conky

```
./conky-colors --cpu=4 --cputemp --swap --hd=default --hdtemp1=sda --calendar --network --weather=JAXX0099 --proc=5 --updates --theme=elementary --ubuntu
sudo sensors-detect
```

### ssh時システム情報表示

ubuntuなどでターミナルログインしたときにCPU、メモリなどを表示する。

```
sudo aptitude install landscape-common
```

### Gitのインストール

```
sudo apt-get install git-all

sudo mkdir /var/gitrepo
sudo chmod 777 /var/gitrepo
sudo rmdir /var/cache/git
sudo ln -s /var/gitrepo /var/cache/git
```

/usr/lib/git-core/git-daemon --verbose --reuseaddr --base-path=/var/cache/git --export-all --enable=receive-pack

### nginxのインストール

```
sudo apt-get install nginx
```

### MySQLのインストール

```
sudo apt-get install mysql-server
```

### phpMyAdminのインストール

```
sudo apt-get install phpmyadmin
sudo apt-get install php-pear php5-cgi php5-fpm
```

/etc/nginx/sites-available/defaultに

```
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+.php)$ {
try_files $uri =404;
fastcgi_pass   unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
```

### Redmineのインストール

```
sudo apt-get install redmine-mysql
sudo apt-get install imagemagick libmagickwand-dev
sudo apt-get install libxslt-dev libxml2-dev
sudo apt-get install libmysqlclient-dev

# redmineをダウンロードしてtarを展開する。

# files, pluginをインストールする

echo "gem 'unicorn'" >> Gemfile.local
chmod -R 755 files log tmp plugins
bundle install
# pluginを入れてたりすると色々エラーが発生するが適宜対応する
bundle exec rake generate_secret_token
```

<http://t.nzk.me/entry/2012/12/28/144730>

<http://redmine.jp/faq/system_management/production-loga-secret-is-required-redmine/>

<http://kujira16.hateblo.jp/entry/2012/09/04/144953>

* redmineのデータ移行

mysql -u root -padminadmin < dump\_utf8.sql

```
sudo rake db:migrate_plugins RAILS_ENV=production
sudo rake redmine:plugins:migrate RAILS_ENV=production
sudo rake tmp:cache:clear
sudo rake tmp:sessions:clear
update-rc.d mongrel_cluster_redmine defaults
```

unicornでサーバを立てnginxからリバースプロキシでアクセスするようにする

#### redmineをサブディレクトリで動作するようにする

```
environment.rb

# 全て/redmine以下になる。末尾に追加
ActionController::Base.relative_url_root = "/redmine"

config.ru

#run RedmineApp::Application
map ActionController::Base.relative_url_root || "/" do
run RedmineApp::Application
end
```

## gitlab構築

今回は、/opt/gitlabに設定したが、設定ファイルがすべて/home/gitになっているため、編集がめんどくさい。

次は、/home/gitのままがいいかと。

### gitlabのインストール

```
sudo apt-get install libmysqld-dev
sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev postfix python nginx imagemagick  libmagickcore-dev libmagickwand-dev mysql-server mysql-client libmysqlclient-dev

sudo gem install bundler

sudo addgroup git
sudo adduser --system --disabled-login --home /opt/gitlab --shell /bin/bash --gecos 'GitLab' --ingroup git git
sudo chown -R git:git /var/gitrepo
```

### Git Shellのインストール

```
# Login as git
sudo su git

# Go to home directory
cd /opt/gitlab

# Clone gitlab shell
git clone https://github.com/gitlabhq/gitlab-shell.git

cd gitlab-shell

# switch to right version
git tag -l
git checkout -b v1.9.3 refs/tags/v1.9.3

cp -v config.yml.example config.yml

# Edit config and replace gitlab_url
# gitlab_url: "http://localhost/" ⇒ "[利用FQDN]"
# vim config.yml
# OR
# sed -i 's/localhost/[利用FQDN]/' config.yml
sed -i "s/localhost/`hostname -f`/" config.yml
# repos_path: /var/gitrepo
# auth_file: /opt/gitlab/.ssh/authorized_keys

# Do setup
./bin/install
```

### Mysqlの設定

```
## DBをdumpする際にユーザが必要なのでredmineインストール時にやったがいいかも
# MySQL の最低限の調整
sudo mysql_secure_installation

# MySQLにログイン
$ mysql -u root -p

# gitlabユーザーを作成（$passwordにパスワード）
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY '$password';

# プロダクション用のデータベースを作成
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_production`  DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;

# gitlabユーザーに権限を付与
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX,   ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost';

mysql> flush privileges;
mysql> quit
```

### gitlabのインストール

```
cd /opt/gitlab

# Clone GitLab repository
sudo -u git -H git clone https://github.com/gitlabhq/gitlabhq.git   gitlab

# Go to gitlab dir
cd /opt/gitlab/gitlab

# Checkout to stable release
git checkout -b v6.8.0.rc1 refs/tags/v6.8.0.rc1

cd /opt/gitlab/gitlab

# Copy the example GitLab config
cp -v config/gitlab.yml.example config/gitlab.yml

# "localhost" を "利用のFQDN" に
# ちなみにここで設定する "ホスト名" が画面で出てくる、「gitのURL」になります。
# sudo -u git -H vim config/gitlab.yml
# OR
# sudo -u git -H sed -i 's/localhost/[利用FQDN]/' config/gitlab.yml
sudo -u git -H sed -i "s/localhost/`hostname -f`/" config/gitlab.yml

# Make sure GitLab can write to the log/ and tmp/ directories
chmod -R u+rwX  log/
chmod -R u+rwX  tmp/

# Create directory for satellites
mkdir /opt/gitlab/gitlab-satellites

# Create directories for sockets/pids and make sure GitLab can  write to them
chmod -R u+rwX  tmp/pids/
chmod -R u+rwX  tmp/sockets/

# DBの設定(MySQL)
cp -v config/database.yml.mysql config/database.yml

# DB 情報設定
vim config/database.yml

# 設定情報設定
vim config/gitlab.yml
# repos_path: /var/gitrepo

# install
cd /opt/gitlab/gitlab

gem install charlock_holmes --version '0.6.9'

# gitlab のインストール
bundle install --deployment --without development test postgres

# インストール
bundle exec rake gitlab:setup RAILS_ENV=production

# gitlab の起動
sudo /etc/init.d/gitlab restart

# スクリプトの設置
sudo curl --output /etc/init.d/gitlab https://raw.github.com/gitlabhq/gitlab-recipes/5-1-stable/init.d/gitlab
sudo chmod +x /etc/init.d/gitlab

# 自動起動設定
sudo update-rc.d gitlab defaults 21

# gitlab の起動
sudo /etc/init.d/gitlab restart
```

### その他

* 参考ページ

  <http://snickerjp.blogspot.jp/2013/05/setup-gitlab-ubuntu.html>
* 初期パスワード

  <login.........admin@local.host>

password......5iveL!fe

サブディレクトリ（/gitlab）とする場合

config.yml の gitlab\_url を外から見えるURLに変更する。

gitlab\_url: "https\://＜ドメイン＞/gitlab/"

下記のとおり、gitlab.yml を変更。

```
production: &base
gitlab:
host: ＜ドメイン＞
port: 443
https: true
relative_url_root: /gitlab
```

Nginx の設定

```
location /gitlab {
root /home/git/gitlab/public;

if ( -f $request_filename ) { break; }

proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect     off;

proxy_set_header   X-Forwarded-Proto $scheme;
proxy_set_header   Host              $http_host;
proxy_set_header   X-Real-IP         $remote_addr;

proxy_pass http://gitlab;
}
```

* サブディレクトリに設定するために

```
# Uncomment and customize the last line to run in a non-root path
# WARNING: This feature is no longer supported
# Note that three settings need to be changed for this to work.
# 1) In your application.rb file: config.relative_url_root = "/gitlab"
# 2) In your gitlab.yml file: relative_url_root: /gitlab
# 3) In your unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT']
# 4) In ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab    "
# To update the path, run: sudo -u git -H bundle exec rake assets:precom    pile RAILS_ENV=production
```

<http://d.hatena.ne.jp/tilfin/20130619/1371648472>

* リポジトリパスの変更

```
cd gitlab-shell
vi config.yml
```

repos\_path: "xxx"

```
cd gitlab
vi gitlab.yml
```

repos\_path: "xxx"

* ssh key の設定

ssh key はWeb画面から入れる必要がある。ターミナルからssh-copy-idするとパラメータが不足して、clone/pushの際にエラーとなる。

### FAQ

* アイコンが表示されない

  ->サブディレクトリ環境であることが問題

```
RAILS_RELATIVE_URL_ROOT="/gitlab" bundle exec rake assets:precompile RAILS_ENV=production
```

## Jenkins

### jenkinsのインストール

```
sudo apt-get install jenkins
```

### jenkins ポート変更

sudo vi /etc/default/jenkins

HTTP\_PORT=8081

### gerritのインストール

@TODO

## git-daemon

gitプロトコル(git://)でアクセスできるようにするために

**/etc/sv/git-daemon/run**

```
#!/bin/sh
exec 2>&1
echo 'git-daemon starting.'
exec chpst -ugit   "$(git --exec-path)"/git-daemon --verbose --reuseaddr --export-all     --base-path=/var/gitrepo
```
