setup lonely mastodon(4/5)

投稿日:

nginx と Let's Encrypt

Let's Encrypt

$ sudo apt-get install certbot python-certbot-apache -t jessie-backports
$ sudo letsencrypt certonly --standalone --standalone-supported-challenges tls-sni-01 -d ドメイン名
$ sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

nginx

まず、apacheを削除。

$ sudo apt-get remove apache2

パッケージは古いので、最新をcompile。

$ sudo apt-get install libpcre3-dev build-essential libssl-dev
$ cd /opt
$ sudo wget http://nginx.org/download/nginx-1.9.9.tar.gz
$ sudo tar -zxvf nginx-1.*.tar.gz
$ cd nginx-1.*
$ sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6
$ sudo make 
$ sudo make install 
$ sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx

自動起動のために、systemファイルを作成。

/lib/systemd/system/nginx.service

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target
 
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /opt/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
 
[Install]
WantedBy=multi-user.target

/opt/nginx/conf.d/mastodon_proxy.conf

map $http_upgrade $connection_upgrade {
  default upgrade;
  ''      close;
}
 
server {
  listen 80;
  listen [::]:80;
  server_name ドメイン名;
  # Useful for Let's Encrypt
  location /.well-known/acme-challenge/ { allow all; }
  location / { return 301 https://$host$request_uri; }
}
 
server {
  listen 443 ssl;
  listen [::]:443 ssl;
  server_name mstdn.poppo-ya.com;
 
  ssl_protocols TLSv1.2;
  ssl_ciphers EECDH+AESGCM:EECDH+AES;
  ssl_ecdh_curve prime256v1;
  ssl_prefer_server_ciphers on;
  ssl_session_cache shared:SSL:10m;
 
  ssl_certificate     /etc/letsencrypt/live/ドメイン名/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/ドメイン名/privkey.pem;
  ssl_dhparam         /etc/ssl/certs/dhparam.pem;
 
  keepalive_timeout    70;
  sendfile             on;
  client_max_body_size 0;
 
  # assets 配下のjsやcssはnginxで面倒を見る。
  root /home/ユーザ名/mastodon/mastodon/public/;
 
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 
  add_header Strict-Transport-Security "max-age=31536000";
 
  location / {
    try_files $uri @proxy;
  }
 
  location /assets {
    add_header Cache-Control "public, max-age=31536000, immutable";
  }
 
  location @proxy {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Proxy "";
    proxy_pass_header Server;
 
    proxy_pass http://127.0.0.1:3000;
    proxy_buffering off;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
 
    tcp_nodelay on;
  }
 
  location /api/v1/streaming {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Proxy "";
 
    proxy_pass http://localhost:4000;
    proxy_buffering off;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
 
    tcp_nodelay on;
  }
 
  error_page 500 501 502 503 504 /500.html;
}

nginx の起動。

$ sudo chmod +x /lib/systemd/system/nginx.service
$ sudo systemctl unmask nginx
$ sudo systemctl start nginx
$ sudo systemctl enable nginx