学習記録 (2/25 -3/1 )
NGINXプラクティス等 (2/25 -3/1)
nginx
- サーバ兼リバースプロキシのソフトウェア
- VirtualBoxのDebianにインストール
- 静的なコンテンツを提供するwebサーバで、Webアプリケーションとの連携で動的コンテンツの提供が可能
- リバースプロキシ (キャッシュサーバ)として利用可能
- 基本コマンド
# 起動 sudo /etc/init.d/nginx start # 終了 sudo /etc/init.d/nginx stop
- ローカルホストに接続できない...
taku-no-MacBook-Pro:~ takunaka$ telnet localhost 80 Trying ::1... telnet: connect to address ::1: Connection refused Trying fe80::1... telnet: connect to address fe80::1: Connection refused Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host taku-no-MacBook-Pro:~ takunaka$
デフォルトのapacheではターミナルの
sudo apachectl start
から URLlocalhost
でアクセスできた。sudo apachectl stop
で停止。接続可能。。。というかnginxを起動しているのはローカルホストではなく、virtualbox上のリモートホスト。
- nginx主なファイル構成
/etc/init.d/nginx nginx の実行ファイル 管理者権限で末尾にオプションを指定して起動・終了等を行う ※ デーモンとしてOS起動時に自動で立ち上がるみたい /etc/nginx/nginx.conf 全てのバーチャルホストに対する設定 Webサーバー全体に対して一括で変更を加えたい場合はこちらを編集する /etc/nginx/sites-available/ 全てのバーチャルホスト毎の個別設定ファイルを配置する /etc/nginx/sites-enabled/ /etc/nginx/sites-available/に配置した設定ファイルのシンボリックリンクを配置する
nginx.conf
ファイル
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # 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; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
/etc/nginx/sites-enabled/default
caminotak@debian:/etc/nginx/sites-enabled$ less default ## # You should look at the following URL's in order to grasp a solid understanding # of Nginx configuration files in order to fully unleash the power of Nginx. # https://www.nginx.com/resources/wiki/start/ # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ # https://wiki.debian.org/Nginx/DirectoryStructure # # # In most cases, administrators will remove this file from sites-enabled/ and # leave it as reference inside of sites-available where it will continue to be # updated by the nginx packaging team. # # Note: You should disable gzip for SSL traffic. # This file will automatically load configuration files provided by other # applications, such as Drupal or Wordpress. These applications will be made # available underneath a path with that package name, such as /drupal8. # # See: https://bugs.debian.org/765782 # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples. ## # Default server configuration # server { listen 80 default_server; listen [::]:80 default_server; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs.debian.org/773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs.debian.org/765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html; server_name _; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; } # pass PHP scripts to FastCGI server # #location ~ \.php$ { # include snippets/fastcgi-php.conf; # # # With php-fpm (or other unix sockets): # fastcgi_pass unix:/run/php/php7.4-fpm.sock; # # With php-cgi (or other tcp sockets): # fastcgi_pass 127.0.0.1:9000; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # #server { # listen 80; # listen [::]:80; # # server_name example.com; # # root /var/www/example.com; # index index.html; # # location / { # try_files $uri $uri/ =404; # } #} }
仮装サーバの構築
serverを復数記述することで、仮想サーバとして動作する http内に記述できるディレクティブは、だいたいserver内にも記述できる様子
http{ server{ server_name alpha.example.com; } server{ server_name bravo.example.com; } }
IPベース listenで別個のIPを指定することで、IPベースの仮想サーバを構築できる
http{ server{ listen 192.168.0.1:80; server_name alpha.example.com; } server{ listen 192.168.0.2:80; server_name bravo.example.com; } }
http{ listen 443 ssl; ssl on; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/cert.key; }
テキスト .txt text/plain HTML文書 .htm .html text/html XML文書 .xml text/xml JavaScript .js text/javascript VBScript .vbs text/vbscript CSS .css text/css GIF画像 .gif image/gif JPEG画像 .jpg .jpeg image/jpeg PNG画像 .png image/png CGIスクリプト .cgi application/x-httpd-cgi Word文書 .doc application/msword PDF文書 .pdf application/pdf
VirutualHost
一つのサーバの中で複数のドメインを運用するための技術 サーバの設定の中に、ドメインごとに見せるサイトを切り替える設定をする。 サイト管理者のメールアドレスやエラーログ、アクセスログの設定も可能 ポート443のVirtualHost設定を追加して SSL証明書関連の設定を指定することでhttpsでのアクセスにも対応可能
home/demo/public_html
の下で複数のバーチャルホストをフォルダ(フォルダ名'domain1.com'等)に分け管理/etc/nginx/sites-available
の下にバーチャルホスト(ドメイン)の設定ファイルを設置
サーバ名など:(例では'www.domain1.com''domain1.com'のふたつの名前を定めている。またロケーションも定められいる。listenはポートを示している。
server { listen 80; server_name www.domain1.com; access_log /home/demo/public_html/domain1.com/log/access.log; error_log /home/demo/public_html/domain1.com/log/error.log; location / { root /home/demo/public_html/domain1.com/public/; index index.html index.php; } }
- sites-enabledにシンボリックリンクを作成する(サイトが参照可能になる)
- nginxの起動ポートの変更: etc/nginx/sites-enabled/defaultのポート番号を変更する
- sites-enabled domain1.comのポートを8888にするとnginxは起動せず、88だと起動する。
- nginxは、起動時に、ディレクトリ/etc/nginx/sites-enabledに入っている設定ファイルを読み込みます。
ここから先に出てくるNginxの設定ファイル内の「server」や「listen」、「location」などの項目を「ディレクティブ」といいます。
Nginxの設定ファイルはデフォルトで2つ、以下の順番で読み込まれます。
- /etc/nginx/nginx.conf
- /etc/nginx/conf.d/default.conf
そのほかにもconf.ファイルで以下のファイルが読み込まれている。
- include /etc/nginx/modules-enabled/*.conf;
- include /etc/nginx/mime.types;
- include /etc/nginx/sites-enabled/*;
ドキュメントルートやHTMLファイルについては、「/etc/nginx/conf.d/default.conf」を開くことで確認できます。
と書いてあるが、/etc/nginx/conf.d/というディレクトリの中には何もない。。。
- etc/nginx/の構成
drwxr-xr-x 2 root root 4096 May 29 2021 conf.d -rw-r--r-- 1 root root 1125 May 29 2021 fastcgi.conf -rw-r--r-- 1 root root 1055 May 29 2021 fastcgi_params -rw-r--r-- 1 root root 2837 May 29 2021 koi-utf -rw-r--r-- 1 root root 2223 May 29 2021 koi-win -rw-r--r-- 1 root root 3957 May 29 2021 mime.types drwxr-xr-x 2 root root 4096 May 29 2021 modules-available drwxr-xr-x 2 root root 4096 Feb 25 22:02 modules-enabled -rw-r--r-- 1 root root 1447 May 29 2021 nginx.conf -rw-r--r-- 1 root root 180 May 29 2021 proxy_params -rw-r--r-- 1 root root 636 May 29 2021 scgi_params drwxr-xr-x 2 root root 4096 Feb 26 21:28 sites-available drwxr-xr-x 2 root root 4096 Feb 26 21:08 sites-enabled drwxr-xr-x 2 root root 4096 Feb 25 22:02 snippets -rw-r--r-- 1 root root 664 May 29 2021 uwsgi_params -rw-r--r-- 1 root root 3071 May 29 2021 win-utf
- 仮装ホストのon/off ソース
また、仮想ホストwww.foo.comを(一時的に)無効にするには、ステップ2で作成したシンボリックリックを削除して、Nginxをリスタートしてやります。 つまり、シンボリックリンクを作成すれば仮想ホストは有効になり、シンボリックリンクを削除すれば、仮想ホストは無効になります。 余談ですが、Ubuntuでは、apacheでも、2つのディレクトリsites-availableとsites-enabledを使って、仮想ホストを管理します。さらに、apacheでは、コマンドa2ensite(仮想ホストを有効する)とa2dissite(仮想ホストを無効にする)が用意されています。 しかし、Nginxでは、このようなコマンドは用意されていないので、手作業でsites-enabledからsites-availableへのシンボリックを作成・削除する必要があります。
さくらVPS
【 Aレコード 】 ドメインをIPアドレスに置き換えるレコード。一番基本的なレコードになります。 【 MXレコード 】 メールエクスチェンジの略で、メールサーバのホスト名を記載するレコードです。 【 CNAMEレコード 】 キャノニカルネームの略でドメインを別のドメインに置き換えるレコードです。特定のドメインを別のドメインに転送する場合に使用します。
- sslファイルの設定は以下を参考に設定
"listen 80" と "listen 443"をわけるのではなく、一つのディレクティブに混ぜ込む
server { listen 80; #SSL listen 443 ssl; ssl_certificate /etc/ssl/bar.fjord.com/ssl.crt/server.crt; ssl_certificate_key /etc/ssl/bar.fjord.com/ssl.key/server.key; server_name bar.fjord.com; access_log /home/ymmtd0x0b/public_html/bar.fjord.com/log/access.log; error_log /home/ymmtd0x0b/public_html/bar.fjord.com/log/error.log; location / { root /home/ymmtd0x0b/public_html/bar.fjord.com/public/; index index.html; } }
nginxサーバを起動するとエラー、journalctl -xe
を指定すると以下のようなログが得られる
Mar 01 17:54:16 ik1-107-60499 nginx[4511]: nginx: [emerg] cannot load certificate "/etc/ssl/server.crt": PEM_read_bio_X509_AUX() faile>
パーミッションを設定(制限)
$ sudo chmod 700 /etc/ssl/dharmaskop.com/ssl.key $ sudo chmod 400 /etc/ssl/dharmaskop.com/ssl.key/server.key
サーバが立ち上がった
<順序>
- サーバ管理者が秘密鍵ファイル(server.key)を作成する。
- サーバ管理者が秘密鍵ファイルから、公開鍵(Public Key)と、秘密鍵のハッシュを組み合わせて、証明書署名要求ファイル(server.csr)を作成する。サーバ管理者はこの証明書署名要求ファイルを認証局に送付する。
server.key/server.crt/cerver.csrはコピーして他のサイトでも流用可能
sshでの接続
ssh caminotak@192.168.0.126 -p 80 ssh_exchange_identification: Connection closed by remote host
以前のログから見直してできました。助かった。
taku-no-MacBook-Pro:~ takunaka$ ssh -p 8888 caminotak@192.168.0.126 Linux debian 5.10.0-10-amd64 #1 SMP Debian 5.10.84-1 (2021-12-08) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat Feb 26 11:33:10 2022 caminotak@debian:~$
diffコマンド(比較したい二つのファイル) ファイルの違いを表示
- sshdのファイル構文をチェック :
# sshd -t
- sshd : の設定を反映
# service sshd restart
変更しただけではssh接続できず、おそらく鍵の再設定が必要(ポートを8888に戻して再接続)
/etc 設定ファイルをおくディレクトリ
/lib 共有ライブラリ
etc/hostsでローカルで接続する場合のipアドレスを設定する。
TOPICS
/var/log/auth.log
にログが載っているsudo less
で表示
Feb 26 11:53:49 debian systemd-logind[352]: New session 9 of user caminotak. <#Debianから直接操作> Feb 26 12:06:59 debian sudo: caminotak : TTY=tty2 ; PWD=/home/caminotak ; USER=root ; COMMAND=/etc/init.d/nginx stop Feb 26 12:06:59 debian sudo: pam_unix(sudo:session): session opened for user root(uid=0) by caminotak(uid=1000) Feb 26 12:06:59 debian sudo: pam_unix(sudo:session): session closed for user root Feb 26 12:07:28 debian sudo: caminotak : TTY=pts/0 ; PWD=/home/caminotak ; USER=root ; COMMAND=/etc/init.d/nginx start <#クライアントから遠隔操作>
ttyとは端末デバイス。tty
コマンドを入力すると自分のコマンド名が表示される。pts
はSSHやtelnetで接続してる仮装端末、ターミナル。
ttyはteletypewriter(テレタイプライター) ptsは擬似端末 (pseudo-terminal)
caminotak@debian:/var/log$ tty /dev/pts/0
caminotak@debian:/var/log$ tty /dev/tty2
- ログイン中のユーザを表示 : $ w
- プロセスログを確認 : $ ps
- tty同士でコマンドのやりとりができるソース
ファイルディスクリプタ
DNS(domain name system)サーバ = ネームサーバ : ドメインを受け取りipアドレスを返すサーバ ドメイン名を利用するときにドメイン名情報の中に[ネームサーバ情報]を登録する
- VPSサーバ:仮装専用サーバ/Virutual Private Server
サーバーを共有するも、サーバの中に仮想化したサーバを設置し専用サーバのような使い方ができる
共有サーバとの違いはOSの選択やアプリの構築ができる。
コマンド操作が必要になる。
- バイナリとソースパッケージ
- ソースファイル:人間が書いたプログラムのソースコードが書いてあるファイル
- TARファイル : Tape ARchive / ディレクトリツリーを単一のファイルとしてする。tarでパッケージしてGNU zipしたものをtar.gzという
- クラスメソッドは「クラスオブジェクトの特異メソッド」である
(クラス名)::(メソッド名)/File::exist?表現する
- Rubyではクラス名も定数の一つなので、エラーも
uninitialized constant
で表示される - Minitestではsetupメソッドを定義するとテストメソッドに実行前に毎回呼び出される。
def setup @umeda = Gate.new(:umeda) @juso = Gate.new(:juso)