0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

debian install 直後の設定メモ

Last updated at Posted at 2018-03-27

debian インストール時のメモ

はじめに

このメモは,今運用している鯖が,どっかのCPUベンダの脆弱性対応してくれなくてOSのメジャーバージョン上げないといけないとか,そういうシチュエーションで,毎回調べながらやると時間かかるので,作業内容をメモに残しておくためのものである.

前提

リファレンスになる元サーバがある.
インストールオプションは,基本最小でやったものとする.(sshだけ入れておくと早いかも.EC2ならコンソールエミュレータがないから必須)
クラウド上のVPSにisoイメージからインストールした想定.

やることリスト

ssh login できるようにする

  1. パスワードログインできるようにする.
    1. ポート開放
      1. 22番は内部で使うのはいいけど,外向きには別のポートを使う方がいいよ.ここでは40002とする
        この時,FWは40002のみ開ける
      2. opensshも40002を見るように設定はできるが,次のNAPTを使った方が,後でインスタンスを増やした時とかに内部の設定が楽なので鯖は22をlistenするようにしたままにする.
    2. NAPT系の設定
      1. グローバルIPの40002へのアクセスをインストールしたホスト(ここではdebsvrとする)の22番に飛ばすよう設定する.
    3. opensshが入ってなかったらインストール
  2. 公開鍵認証でログインできるようにする.
    1. 公開鍵をscpでコピーする.scpはポート指定の-Pを後から思い出して最後につけても通らないので注意.

      scp -P 40002 .ssh/id_ed25519.pub user@ipaddr:
      
    2. ついでにクライアントPCからsshを簡単に飛ばせるよう設定しておく

      --- .ssh/config.old
      +++ .ssh/config
      @@ -0,0 +1,4 @@
      + Host debsvr
      +     HostName ipaddr
      +     User user
      +     Port 40002
      

sources.list 更新

  1. sources.list generatorで作る

  2. 以下のパッケージを入れろと言われるので,盲目的に入れる

    apt-get install curl wget apt-transport-https dirmngr
    

不要ソフトの削除

奉仕精神のないダメ人間なので,レポート系は全部消す.
まずパッケージ確認.

dpkg --get-selections | grep port

installation-reportとかレポート系の機能を見付けて消す.ディストリによっては,popularity-contestの場合もある.apport系とかも入ってたら消す.(ubuntuの場合だけかも)

sudo apt-get purge installation-report

必要ソフトのインストール

人に依るけど,私の場合は

sudo apt-get install tmux vim zsh build-essential htop heirloom-mailx 

一部はbackportsから入れる.

sudo apt-get install -t jessie-backports certbot openssl

一部はソースコードから入れる
nginx news

古いサーバがあれば,そのリストと差分を取る方が早い.

dpkg --get-selections>newhost.list; diff oldhost.list newhost.list | less

gitbucket

  1. JDKのインストール

    /etc/apt/sources.list.d/java.list

deb http://ppa.launchpad.net/webupd8team/java/ubuntu precise main
deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu precise main

    ```shell
sudo apt-get install oracle-java8-installer
  1. gitbucketの起動
    インストールは不要.内蔵httpdを使う場合,httpdもいらない.

wget https://github.com/gitbucket/gitbucket/releases/download/4.22.0/gitbucket.war
java -jar gitbucket.war


なおクライアント側はgitのremoteに新しい鯖を登録するだけ.お手軽.

```shell
git remote remove origin
git remote add origin http://IPADDR:8080/path/to/repository.git
git push origin master

nginx

パッケージのnginxが古いのでhttp2が使えなかったりして困るんで自分でソースから入れる.

  1. opensslを1.0.2系にする.あとついでに,いくつかパッチを入れたいので,gitもいれておく.

sudo apt-get install -t jessie-backports openssl
sudo apt-get install git


1. nginxのソースを落としてくる.私の場合はstableにするけど,mainlineでも良いと思う.

    ```shell
wget https://nginx.org/download/nginx-X.XX.X.tar.gz
  1. ソースやらパッチの展開.パッチは欲しいものを探すと良い.
    標準でついてるやつで済む場合 (nginx documentation) は,それに留めておいた方が良い.

mkdir tmp; cd tmp
tar xzf ../nginx-X.XX.X.tar.gz
cd nginx-X.XX.X
git clone https://github.com/openresty/headers-more-nginx-module.git
git clone https://github.com/arut/nginx-dav-ext-module.git


1. buildの準備
   セキュリティアップデートだったりでbuildし直しは必ずあるので,configureのオプションを書いたシェルスクリプトを作っておいた方が良い.

    ```shell:configure.sh
./configure \
    --prefix=/usr/local/nginx-1.12.2 \
    --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' \
    --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' \
    --lock-path=/var/lock/nginx.lock \
    --pid-path=/run/nginx.pid \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_gzip_static_module \
    --with-http_v2_module \
    --with-http_dav_module \
    --add-module=./headers-more-nginx-module \
    --add-module=./nginx-dav-ext-module
  1. 足りないと怒られたものを入れるとか,足りないものを入れるとか.

sudo apt-get install libpcre3-dev
sudo apt-get install -t jessie-backports libssl-dev
sudo apt-get install php5 php5-fpm
sudo apt-get install -t jessie-backports certbot
sudo apt-get install mariadb
mysql_secure_installation
sudo apt-get install -t jessie-backports postgresql


    postgresqlは,私はたまたまpostgisの2.3以降が欲しくて,そちらに合わせてbackportsを使うと9.6になるだけで,普通はstableの9.4でも良いんじゃないかと.  
あと普通はmariadbだけで済む.posgresql使うパッケージって少ないよね.

1. build
   makeに,かなり時間かかるから注意.

    ```shell
make
多分,buildに失敗するので必要なパッケージを入れる.

```shell

sudo apt-get install libexpat1-dev


    makeだけやり直してもいいし,気になるならconfigure.shからやり直してもいい.
    ただしconfigure.shからやり直すとMakefileの日付が新しくなって,compileも全部やり直しなんで注意.
    (コマンド略)

1. install

    ```shell
sudo make install
バージョン番号のないディレクトリ構成を作る.

```shell

cd /usr/local/
sudo ln -s nginx-X.XX.X nginx
cd /etc
sudo ln -s /usr/local/nginx/conf nginx
sudo -s
cd /var/log
sudo ln -s /usr/local/nginx/logs nginx


1. 起動スクリプトの準備

    ```shell
git clone https://github.com/Fleshgrinder/nginx-sysvinit-script.git
cd nginx-sysvinit-script
sudo make
  1. サービス定義の準備

    /lib/systemd/system/nginx.service
    [Unit]
    Description=The NGINX HTTP and reverse proxy server
    After=syslog.target network.target remote-fs.target nss-lookup.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecStartPost=/bin/sleep 1
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    

    ```shell
sudo systemctl unmask nginx.service
sudo systemctl enable nginx.service
  1. テスト

sudo /usr/local/nginx/sbin/nginx -V
sudo /usr/local/nginx/sbin/nginx -t
sudo systemctl list-unit-files --type=service
sudo systemctl status nginx.service


1. phpとかの設定

    ```shell
sudo apt-get install php5-mysqlnd php5-pgsql php5-curl
```diff
--- php.ini.old
+++ php.ini
@@ -390,7 +390,7 @@

 ; Maximum amount of memory a script may consume (128MB)
 ; http://php.net/memory-limit
-memory_limit = 128M
+memory_limit = 512M

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ; Error handling and logging ;
@@ -657,7 +657,7 @@
 ; Its value may be 0 to disable the limit. It is ignored if POST data reading
 ; is disabled through enable_post_data_reading.
 ; http://php.net/post-max-size
-post_max_size = 8M
+post_max_size = 0

 ; Automatically add files before PHP document.
 ; http://php.net/auto-prepend-file
@@ -817,7 +817,7 @@

 ; Maximum allowed size for uploaded files.
 ; http://php.net/upload-max-filesize
-upload_max_filesize = 2M
+upload_max_filesize = 0

 ; Maximum number of files that can be uploaded via a single request
 max_file_uploads = 20

    ```diff:/etc/php5/fpm/pool.d/www.conf
--- www.conf.old
+++ www.conf
    @@ -106,7 +106,7 @@
     ; forget to tweak pm.* to fit your needs.
     ; Note: Used when pm is set to 'static', 'dynamic' or 'ondemand'
     ; Note: This value is mandatory.
    -pm.max_children = 5
    +pm.max_children = 10

     ; The number of child processes created on startup.
     ; Note: Used only when pm is set to 'dynamic'
    @@ -366,7 +366,7 @@
     ; Setting to "no" will make all environment variables available to PHP code
     ; via getenv(), $_ENV and $_SERVER.
     ; Default Value: yes
    -;clear_env = no
    +clear_env = no

     ; Limits the extensions of the main script FPM will allow to parse. This can
     ; prevent configuration mistakes on the web server side. You should only limit
  1. 起動
    問題なさそうなら,起動する.次のどれでも起動するはず.

sudo /etc/init.d/nginx start

sudo service nginx start

sudo systemctl start nginx.service


1. dummyパッケージ
   owncloudとか入れようとすると,依存関係でパッケージ管理のhttpd系サービスを入れようとしてくるので,nginxが入ってるよって認識させるためにダミーのパッケージを作ってインストールする.  
   まずはequivsを入れる.

    ```shell
sudo apt-get install equivs
mkdir ~/tmp/nginx-dummy
cd ~/tmp/nginx-dummy
equivs-control nginx-dummy
デフォのnginx-dummyを編集する.

```diff:nginx-dummy
--- nginx-dummy.old
+++ nginx-dummy
@@ -6,14 +6,14 @@
 # Homepage: <enter URL here; no default>
 Standards-Version: 3.9.2

-Package: <package name; defaults to equivs-dummy>
+Package: nginx-dummy
 # Version: <enter version here; defaults to 1.0>
 # Maintainer: Your Name <yourname@example.com>
 # Pre-Depends: <comma-separated list of packages>
 # Depends: <comma-separated list of packages>
 # Recommends: <comma-separated list of packages>
 # Suggests: <comma-separated list of packages>
-# Provides: <comma-separated list of packages>
+Provides: httpd
 # Replaces: <comma-separated list of packages>
 # Architecture: all
 # Copyright: <copyright file; defaults to GPL2>
@@ -22,7 +22,7 @@
 # Extra-Files: <comma-separated list of additional files for the doc directory>
 # Files: <pair of space-separated paths; First is file to include, second is destination>
 #  <more pairs, if there's more than one file to include. Notice the starting space>
-Description: <short description; defaults to some wise words>
- long description and info
+Description: dummy package for nginx
+ dummy package for nginx installed manually.
  .
- second paragraph
+ nginx provides httpd

    ダミーパッケージのbuild/install.

    ```shell
equivs-build nginx-dummy
sudo dpkg -i nginx-dummy_1.0_all.deb

nginxの設定

基本,旧鯖からもってくる.
gitbucketもSNIで振り分けするとhttp2対応が簡単にできる.

SSLまわりは,Generate Mozilla Security Recommended Web Server Configuration Filesを使うと楽チン.

/etc/nginx/sites-available/gitbucket
server {
    listen 80;
    server_name DOMAINNAME;

    return 301 https://DOMAINNAME$request_uri;
}


server {
    listen 443 ssl http2;
    #listen [::]:443 ssl http2;

    # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
    ssl_dhparam /path/to/dhparam;

    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (15768000 seconds = 6 months)
    #add_header Strict-Transport-Security max-age=15768000;

    # OCSP Stapling ---
    # fetch OCSP records from URL in ssl_certificate and cache them
    ssl_stapling on;
    ssl_stapling_verify on;

    ## verify chain of trust of OCSP response using Root CA and Intermediate certs
    ssl_trusted_certificate /path/to/chain.pem;

    #resolver DNSIPADDR;  # リバプロをホスト名で指定する場合DNSを指定

    access_log logs/access.log;
    error_log  logs/error.log;

    server_name DOMAINNAME;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_max_temp_file_size 0;

    # set max upload size
    client_max_body_size 512M; # 画像とかバイナリとか置く場合,ここをでかくしておかないとpushできないので注意

    location / {
        proxy_pass http://127.0.0.1:8080/; # 同じホストでデフォ設定でjavaでgitbucket.warを実行している場合
    }
}

certbotで証明書取得

自動化する方法を別記事にかいたので,こちらに修正.
owncloudとかgitbucketとか動かしているサーバでcertbot - Qiita

opensslを使ったCSRの作成など一つずつ自分でやる手順は割愛した.(ここにもともと書いてた)

mariadbの設定

/etc/mysql/conf.d/mariadb.cnf
--- a/conf.d/mariadb.cnf
+++ b/conf.d/mariadb.cnf
@@ -11,7 +11,16 @@
 #
 # Default is Latin1, if you need UTF-8 set all this (also in client section)
 #
-#character-set-server  = utf8
-#collation-server      = utf8_general_ci
-#character_set_server   = utf8
-#collation_server       = utf8_general_ci
+character-set-server  = utf8
+collation-server      = utf8_general_ci
+character_set_server   = utf8
+collation_server       = utf8_general_ci
+default-storage-engine = innodb
+default_storage_engine = innodb
+innodb-file-format     = Barracuda
+innodb_file_format     = Barracuda
+innodb-file-per-table  = 1
+innodb_file_per_table  = 1
+innodb-use-sys-malloc  = 0
+innodb_use_sys_malloc  = 0
+skip-character-set-client-handshake
sudo systemctl restart mysql.service

取り込み.

createuser.sql
GRANT ALL PRIVILEGES ON database.* TO username@localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
mysql -u root -p < createuser.sql
export MYSQL_PWD=password
mysql -e 'CREATE DATABASe database'
mysql database < backup.sql

wordpressみたいなDB依存ソフトの書き換えなど行なって,一部www-dataが書き込みする場所のパーミッション修正.

owncloud

頑張ってみたけど,jessieに10.0.7を入れるのはうまくできなかった.
9.1.8を落としてきて入れたらすんなり入るし,9.1.8を入れた後に10.0.7にアップデートすれば動くんだけど.

今回はソースから入れてみた.

  1. 公式サイトからtarballを落とす
  2. 解答して設置したい場所に配置する
  3. 設定
    1. nginxでSNIか何かで設置した場所をrootとするよう設定し,php-fpmに渡すように設定する.
      基本的には公式サイトのマニュアル(Configuration — ownCloud 10.0.7 Server Administration Manual 10.0.7 documentation)
      を参考にした方が良いが,最低限はroot,とfast_cgiまわり.

    2. owncloud用のDB設定(MariaDBの場合)
      こんな感じで実行.ユーザ名は,飽くまでDBのユーザ名なので注意.

      setup.sql
      CREATE DATABASE owncloudDBname;
      GRANT ALL PRIVILEGES ON owncloudDBname.* to owncloudUSERname@localhost identified by 'PASSWD';
      FLUSH PRIVILEGES;
      
    3. パーミッション設定は,Installation Wizard — ownCloud 9.0 Server Administration Manual 9.0 documentationのスクリプトのパスなどを修正して実行.

  4. セットアップ
    1. ブラウザでアクセスして,DB設定とか投入してできあがり.

以下,予定.書かないかも.

個人設定

個人の好みなのでどーでもいいけど,この辺りをやる.

.zsh
.zshenv
.vimrc
.vim/bundle
git clone https://github.com/Shougo/neobundle.vim
.dir_colors
.tmux.conf
chsh

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?