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.

自分のサーバー周りの設定備忘録

Last updated at Posted at 2016-12-06

未完成です

環境

PuTTYJP Version 0.67-jp20160306
Tera Term v4.93(16/11/30)
TeraTermの設定
WinSCP 5.9.3
ConoHa VPS630

名称 設定値
タイプ VPS
リージョン 東京
メモリ 512M
イメージタイプ OS
OS種類 その他(ArchLinux)
自動バックアップ 無効
ディスク容量 SSD 50GB
接続許可ポートIPv4 全て許可
接続許可ポートIPv6 全て許可
SSH Key 使用しない

手順

コンソールを開いて

LOGIN:root
Password:入力

はじめにやること

# pacman -Syu
# pacman -Sy archlinux-keyring && pacman -Syyu ※もしsyuでエラーが出た場合

インストールされている全てのパッケージを最新の状態にします。

# pacman -S vim php php-fpm nginx certbot ruby fcgi spawn-fcgi jdk8-openjdk tomcat8 tomcat-native
# systemctl start nginx.service
# systemctl enable nginx.service
# systemctl start tomcat8.service
# systemctl enable tomcat8.service

rootで色々操作するのは好ましくないっぽいのでsudoを入れます。
~~共通鍵認証をするためにopensshを入れます。~~ここでは元から入ってます
zshとかvimとかは好みで入れてください。私はvimだけ入れます。
nginxというWebサーバーを使っていくので入れます。
phpも入れます。
php-fpmはnginxでphpを扱う際に必要なので入れます。
certbotはSSLで暗号化通信する時に必要なので入れます。
そして予め全て起動しておきます。

ユーザーの追加

useradd -m -g wheel [user]      # ユーザを追加して wheel グループに入れる
passwd [user]                   # ユーザのパスワードを設定
visudo                          # 下記参照
44 Defaults env_keep +="HOME"
82 %wheel ALL=(ALL) ALL

visudo では # Defaults env_keep += "HOME" 行と # %wheel ALL=(ALL) ALL 行のコメントアウトを解除して保存。 Defaults 行は、 sudo したときユーザの $HOME を参照できるようにする設定。これをしないと sudo vim したとき .vimrc が読み込まれなくて悲しいことになる。 %wheel 行は wheel グループのユーザに sudo する権限を与える。

管理者権限のないユーザにしたいなら staff グループあたりに入れておけばいい。あらかじめ groupadd staff を実行してグループを作っておく。
ここまで終えたらいったん exit して、今作ったユーザでログインし直す。

su
パスワード入力

でrootに切り替えができる。

SSHログインが出来るようにする

SSH鍵生成はここを参照

wheelユーザーでログインした後

mkdir .ssh
chmod 700.ssh
vim authorized_keys
id_rsa.pubの中身をこの中にペースト
chmod 600 authorized_keys

その後は属性を変えてちゃんとssh認証出来るようにする

SSH接続の設定変更

# vim /etc/ssh/sshd_config 

設定すること

13 Port 22→任意の数字
19 Protocol 2
43 PermitRootLogin no
45 MaxAuthTries 6
71 PasswordAuthentication no
72PermitEmptyPasswords no
101 X11Forwarding no
AllowUsers wheelユーザー名

1:鍵の認証に使うポート番号を変更します
2:プロトコル2を明示的に使用します。
3:ルートでログインできません。
4:パスワードが連続で認証出来る回数は6回です。(公開鍵認証は無関係)
5:パスワード認証ができません。
6:空パスワードでログインできません。
7:X11転送を許可しません。
8:wheelユーザー名ユーザーのみ公開鍵認証が使用出来ます。
最後は書いてないので最終行に追加しましょう。

設定しておいたほうがいいけどどっちがいいかわからない

私はデフォルトのyesにしてます。

AllowAgentForwarding no
AllowTcpForwarding no

1:エージェントフォワーディングが出来ません。
2:ポートフォワーディングが出来ません。

WinSCPでsudo出来るように

sudo visudo
%Wheel   ALL=(ALL) NOPASSWD: ALLの下に追加
ユーザー名 ALL=NOPASSWD: /usr/lib/ssh/sftp-server 

WinSCP側はまず、セッション→転送プロトコルをSFTPに設定する。
その後、設定→環境→SFTP→プロトコル オプション→SFTPサーバ の中に以下を書き込む。

sudo /usr/lib/ssh/sftp-server

Nginx設定

nginxを制御するユーザーを作成

groupadd nginx
useradd -m -g nginx nginx
vim /etc/nginx/nginx.conf
  1. nginxを制御するユーザーを明示的に指定する
  2. 19行で個別に作ったserverブロックなどを全て読み込ませる
nginx.conf
2 user nginx;
3 worker_processes 1;(CPUコア数によって変わる)

17 http{
18 include ほにゃらら
19 include /etc/nginx/conf.d/*.conf;(conf.dディレクトリ下全てのconfファイルを読み込む)
21 default_type ほにゃらら

コンフィグディレクトリを作成した後コンフィグファイルを作成

mkdir /etc/nginx/conf.d/
vim /etc/nginx/conf.d/hoge.conf
mkdir /home/nginx/htmlディレクトリへのパス/ -p
hoge.conf
server{
  listen 80;
  root /htmlディレクトリへのパス/;
  index index.html index.htm;
}
vim /htmlディレクトリへのパス/index.html
index.html
it works!
systemctl restart nginx

これで自分のサイトにアクセスしてit works!と表示されたら設定完了

PHP設定

vim /etc/php/php.ini
php.ini
294 open_basedir = phpを動かす必要のある/(ルート)から一番遠いディレクトリを指定

難しいこと言ってるようでよくわからないならとりあえず/home/nginx/しておけば動く

mkdir /etc/nginx/cgiconf.d
vim /etc/nginx/cgiconf.d/php.conf
php.conf
location ~ \.php$ {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;

  fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
  fastcgi_index  index.php;
  include        fastcgi.conf;
}

もしhtml等もphpとして読み込ませたいなら.php$の部分を\.(php|html|htm)$$に書き換える

/etc/php/php-fpm.conf
security.limit_extensions = .php .html .htm

いずれにせよphpを導入したいserverブロックにこのconfをインクルードさせる
例えば

hoge.conf
server{
  listen 80;
  root /htmlディレクトリへのパス/;
  index index.html index.htm index.php;
  include  /etc/nginx/cgiconf.d/*.conf;
}

注意
include ./cgiconf.d/*.conf;
こういう感じの相対パスで指定するとうまくいきません。

vim /etc/php/php-fpm.d/www.conf
www.conf
23 user = nginx
24 group = nginx
47 listen.owner = nginx
48 listen.group = nginx
systemctl start php-fpm.service
systemctl enable php-fpm.service

ruby設定そんなものはなかった

ruby.conf
  location ~ \.rb$ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.html;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

SSL設定

certbot certonly --email email@example.com --webroot -w /path/to/html/ -d your.domain
/etc/nginx/conf.d/hoge.conf
server{
  listen 80;
  return 301 https://自分のドメイン$request_uri;
}
server{
  listen 443 ssl http2;
  root /htmlディレクトリへのパス/;
  index index.html index.htm index.php;
  ssl on
  ssl_certificate /fullchain.pemへのパス;
  ssl_certificate_key /privkey.pemへのパス;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#POODLE回避
  ssl_prefer_server_ciphers on;#サーバー側の暗号スイートを優先(TLSv1~1.2が使われるようにする)
  ssl_ciphers ECDHE+AES128+AESGCM:ECDHE+AESGCM+ECDHE+AES128:ECDHE+AES:RSA+AES128+AESGCM:RSA+AESGCM:RSA+AES128:RSA+AES:RSA+3DES;
  client_max_body_size 128M;
  access_log /home/nginx/dodontof.example.com/logs/access.log combined;
  error_log  /home/nginx/dodontof.example.com/logs/error.log;
  include  /etc/nginx/cgiconf.d/*.conf;
}

証明書は三ヶ月ごとに更新が必要になりますが、以下の .service ファイルを作成することで完全に自動化することが可能です。

/etc/systemd/system/certbot.service
[Unit]
Description=Let's Encrypt renewal

[Service]
Type=oneshot
ExecStart=/usr/bin/certbot renew --quiet --agree-tos

Tomcat導入

vim /etc/nginx/conf.d/tomcat.conf
tomcat.conf
server {
  listen 好きなポート番号(デフォルト80);
  server_name servlet.example.com;

  root /home/web/tomcat;

  access_log /var/log/nginx/tomcat_access.log;
  error_log /var/log/nginx/tomcat_error.log;

  location / {
    proxy_pass http://127.0.0.1:8080/;
  }

}

JDK1.8導入

cd
vim Hello.java
Hello.java
class Hello
{
  public static void main(String[]args)
  {
    System.out.println("Hello World");
  }
}
javac Hello.java
java Hello

Hello World

Servletの動作確認

こんな感じの設定で
/var/lib/tomcat8/webapps/ROOT/WEB-INF内にclassesディレクトリを作成、その中にServletを作成します。

mkdir /var/lib/tomcat8/webapps/ROOT/WEB-INF/classes
cd /var/lib/tomcat8/webapps/ROOT/WEB-INF/
chown tomcat8 classes
chgrp tomcat8 classes
cd classes
vi daytime.java
daytime.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Calendar;

public class daytime extends HttpServlet {
    public void doGet(HttpServletRequest request
    ,HttpServletResponse response)

    throws IOException, ServletException{
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Calendar cal = Calendar.getInstance();
        out.println("<html>\n<head>\n<title>DayTime</title>\n</head>\n<body>");
        out.println("<div style=\"font-size: 40px; text-align: center; font-weight: bold\">");
        out.println(cal.get(Calendar.YEAR) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + 
        cal.get(Calendar.DATE) + " " + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE));
        out.println("</div>\n</body>\n</html>");
    }
javac -classpath /usr/share/java/tomcat8/servlet-api.jar daytime.java 
vim ../web.xml

以下を - の間の適当なところへ追記

web.xml

  <servlet>
     <servlet-name>daytime</servlet-name>
     <servlet-class>daytime</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>daytime</servlet-name>
     <url-pattern>/daytime</url-pattern>
  </servlet-mapping>

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?