Nginxを使用した多機能Webサーバーの構築方法

Webサーバーのセットアップ手順

前提条件の準備

サーバーの初期状態を復元し、セキュリティソフトウェアを無効にしてください。

必要なパッケージのインストール

[root@server ~]# yum install nginx httpd-tools -y

ホストファイルの設定

Windowsの C:\Windows\System32\drivers\etc\hosts ファイルに以下のDNSマッピングを追加:

192.168.48.130 www.webcamp.com

メインWebサイトの作成

[root@server ~]# mkdir -p /var/www/webcamp
 
[root@server ~]# echo 'WebCampへようこそ' > /var/www/webcamp/index.html
 
[root@server ~]# vim /etc/nginx/nginx.conf

以下のサーバーブロックを設定ファイルに追加:

server {    
    listen       80;      
    server_name www.webcamp.com;      
    root         /var/www/webcamp;  
}
[root@server ~]# systemctl start nginx

Windowsブラウザから www.webcamp.com にアクセスしてテスト

教材データサブサイトの作成 (www.webcamp.com/data)

[root@server ~]# mkdir /var/www/webcamp/data
[root@server ~]# echo '教材データ' > /var/www/webcamp/data/index.html

Nginx設定ファイルに以下のロケーションブロックを追加:

location /data {
    alias /var/www/webcamp/data;
    index index.html index.htm;
}
[root@server ~]# systemctl restart nginx

Windowsブラウザから www.webcamp.com/data にアクセスしてテスト

学生情報サブサイトの作成 (www.webcamp.com/student)

[root@server ~]# mkdir /var/www/webcamp/student
[root@server ~]# echo '学生情報' > /var/www/webcamp/student/index.html
[root@server ~]# useradd yamada
[root@server ~]# passwd yamada   # パスワード: student123
[root@server ~]# useradd tanaka
[root@server ~]# passwd tanaka   # パスワード: student456
[root@server ~]# htpasswd -c /etc/nginx/auth_users yamada # パスワード: student123
[root@server ~]# htpasswd /etc/nginx/auth_users tanaka   # パスワード: student456

Nginx設定ファイルに以下のロケーションブロックを追加:

location /student {
    alias /var/www/webcamp/student;
    index index.html index.htm;
    auth_basic "認証が必要です";
    auth_basic_user_file /etc/nginx/auth_users;
}
[root@server ~]# systemctl restart nginx

Windowsブラウザから www.webcamp.com/student にアクセスしてテスト(複数回テストする場合はブラウザのキャッシュをクリア)

支払いサブサイトの作成 (www.webcamp.com/payment)

[root@server ~]# mkdir /var/www/webcamp/payment
[root@server ~]# echo '支払い情報' > /var/www/webcamp/payment/index.html
[root@server ~]# openssl genrsa -aes128 2048 > /etc/nginx/payment.key

秘密鍵のパスフレーズを入力(例: securepass)

[root@server ~]# openssl req -utf8 -new -key /etc/nginx/payment.key -x509 -days 365 -out /etc/nginx/payment.crt

証明書情報を入力:

Country Name (2 letter code) [AU]:JP
State or Province Name (full name) [Some-State]:Tokyo
Locality Name (eg, city) []:Shinjuku
Organization Name (eg, company) [Internet Widgits Pty Ltd]:WebCamp
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:server
Email Address []:admin@webcamp.com
[root@server ~]# cd /etc/nginx
[root@server nginx]# cp payment.key payment.key.org
[root@server nginx]# openssl rsa -in payment.key.org -out payment.key

秘密鍵のパスフレーズを入力

Nginx設定ファイルに以下のサーバーブロックを追加:

server {
    listen       443 ssl http2;
    server_name www.webcamp.com;
    location /payment {
        alias /var/www/webcamp/payment;
        index index.html index.htm;
    }
    ssl_certificate "/etc/nginx/payment.crt";
    ssl_certificate_key "/etc/nginx/payment.key";
}
[root@server nginx]# systemctl restart nginx

Windowsブラウザから https://www.webcamp.com/payment にアクセスしてテスト

タグ: nginx Webサーバー SSL認証 Basic認証 HTTPS

7月28日 00:12 投稿