バーチャルサーバーとは、ひとつのコンピュータで仮想的に複数のWebサイトを同時に運用するためのプログラムの総称である。
//IT用語辞典
バーチャルサーバーは1台のHTTPサーバー上で機能するが、それぞれ異なるHTTPサーバーとして扱うことが可能であり、それぞれ異なるドメイン名やIPアドレスを扱うことができるようになっている。物理的に分離しているサーバーに比べるとレスポンスは低くなるが、運用コストを抑えたり設置を簡易化できるなどのメリットがある。
$ sudo nano /etc/nginx/sites-available/default
#はコメント行を表します
//output
1 ##
2 # You should look at the following URL's in order to grasp a solid understanding
3 # of Nginx configuration files in order to fully unleash the power of Nginx.
4 # https://www.nginx.com/resources/wiki/start/
5 # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
6 # https://wiki.debian.org/Nginx/DirectoryStructure
7 #
8 # In most cases, administrators will remove this file from sites-enabled/ and
9 # leave it as reference inside of sites-available where it will continue to be
10 # updated by the nginx packaging team.
11 #
12 # This file will automatically load configuration files provided by other
13 # applications, such as Drupal or Wordpress. These applications will be made
14 # available underneath a path with that package name, such as /drupal8.
15 #
16 # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
17 ##
18
19 # Default server configuration
20 #
21 server {
22 listen 80 default_server;
23 listen [::]:80 default_server;
24
25 # SSL configuration
26 #
27 # listen 443 ssl default_server;
28 # listen [::]:443 ssl default_server;
29 #
30 # Note: You should disable gzip for SSL traffic.
31 # See: https://bugs.debian.org/773332
32 #
33 # Read up on ssl_ciphers to ensure a secure configuration.
34 # See: https://bugs.debian.org/765782
35 #
36 # Self signed certs generated by the ssl-cert package
37 # Don't use them in a production server!
38 #
39 # include snippets/snakeoil.conf;
40
41 root /var/www/html;
42
43 # Add index.php to the list if you are using PHP
44 index index.html index.htm index.nginx-debian.html;
45
46 server_name _;
47
48 location / {
49 # First attempt to serve request as file, then
50 # as directory, then fall back to displaying a 404.
51 try_files $uri $uri/ =404;
52 }
53
54 # pass PHP scripts to FastCGI server
55 #
56 #location ~ \.php$ {
57 # include snippets/fastcgi-php.conf;
58 #
59 # # With php-fpm (or other unix sockets):
60 # fastcgi_pass unix:/run/php/php7.4-fpm.sock;
61 # # With php-cgi (or other tcp sockets):
62 # fastcgi_pass 127.0.0.1:9000;
63 #}
64
65 # deny access to .htaccess files, if Apache's document root
66 # concurs with nginx's one
67 #
68 #location ~ /\.ht {
69 # deny all;
70 #}
71 }
72
73
74 # Virtual Host configuration for example.com
75 #
76 # You can move that to a different file under sites-available/ and symlink that
77 # to sites-enabled/ to enable it.
78 #
79 #server {
80 # listen 80;
81 # listen [::]:80;
82 #
83 # server_name example.com;
84 #
85 # root /var/www/example.com;
86 # index index.html;
87 #
88 # location / {
89 # try_files $uri $uri/ =404;
90 # }
91 #}
92
19行以降が Default server、74行以降が Virtual serverとなります。殆どがコメントアウトですが…
ここでなぜVirtual serverを使うのかと言うかと、自分はWordpressと一般サイトが欲しいと思いバーチャルサーバーを選びましたが、サイトが1つでも問題有りません。のちのち別サイトを設ける時には元のコードをコピーして追加出来ます。話を戻しますと、
このdefaultファイルは結果的に取り込みません。74行以降の Virtual serverを別ファイルで模して作りそれを読み込む形となります。
下記にそのツリーデレクトリーを示します。
Nginxのディレクトリ構成(バーチャルサーバー含む)
/etc/nginx/
|-- nginx.conf #全般の設定(nginxが起動でconfファイルが読み込まれる)/
|-- uwsgi_params #uWSGI設定/
|-- scgi_params #SCGI設定
|-- fastcgi_params #FastCGI設定
|-- mime.types #MIMEタイプと拡張子を定義
|-- conf.d/
| `-- default.conf #ファイル無し
|-- nginx.conf
|-- sites-available #実態ファイル/
| |-- default #(Default、Virtual server)上記表示ファイル
| |-- webServer80
| `-- webServer443
`-- sites-enabled #シンボリックlink/
|-- webServer80 -> /etc/nginx/sites-available/webServer80
`-- webServer443 -> /etc/nginx/sites-available/webServer443
OSはシンボリックリンクに有るファイルを読み込みます。上記ツリーで実態ファイルのdefaultはそのままで、シンボリックlinkではそれを設けない事で読み込まない様にしています。
//例 webServer80
#シンボリックリンク確認
$ ls -l /etc/nginx/sites-enabled ←確認重要(水色太文字が正:セクションa1-3参照)
#シンボリックリンク作成
$ ln -s /etc/nginx/sites-available/webServer80 /etc/nginx/sites-enabled/
#シンボリックリンクの元ファイル編集
$ sudo nano /etc/nginx/sites-available/webServer80
#シンボリックリンク解除の場合
$ sudo unlink /etc/nginx/sites-enabled/webServer80
#シンボリックリンクの元ファイル削除の場合
$ sudo rm /etc/nginx/sites-available/webServer80
$ sudo chmod -R 777 /etc/nginx/sites-enabled
$ sudo chmod -R 755 /etc/nginx/sites-enabled
#設定ファイルのチェック(セクションd1参照)
$ sudo nginx -t
#設定ファイルの読み込み
$ sudo systemctl reload nginx
#サービスの再起動
$ systemctl restart nginx
下記のコマンドでエラーを特定します。基本的に自力で直すしか有りませんし、分からなければ、エラーをネットで検索です。
$ systemctl status nginx.service
//output 特定エラーの場合
>>>Failed to start A high performance web server and a reverse proxy server.
see 'systemctl status nginx.service' for details
#apache2が原因の場合 その起動停止 <<-- Port Checkより
$ sudo service apache2 stop
#apache2の削除
$ sudo apt-get remove apache2
#現状のポートの利用状態
$ nmap localhost
#ポート 80 を使用しているプロセスを停止
$ sudo fuser -k 80/tcp
#nginx再起動
$ sudo systemctl restart nginx