TOP Web Server nginx
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
/cgi-bin/ は URL上のフォルダ名: http://localhost/cgi-bin/hello_cgi.cgi
/usr/lib/cgi-bin/ は実際のファイルの場所
CGI:Common Gateway Interface
CGI による動的コンテンツ
CGI による動的コンテンツ
Tomcat JSP
ref:UbuntuとApacheでのWebサーバー構築方法【初心者向け】
smnb@Dig23:~$ apachectl -v # version check
Server version: Apache/2.4.52 (Ubuntu)
Server built: 2023-05-03T20:02:51
smnb@Dig23:~$ firefox http://localhost & # デフォルトページ表示
smnb@Dig23:~$ sudo apt install apache2
smnb@Dig23:~$ sudo systemctl status apache2 # server status check
Server version: Apache/2.4.52 (Ubuntu)
Server built: 2023-05-03T20:02:51
smnb@Dig23:~$ firefox http://localhost & # デフォルトページ表示
smnb@Dig23:~$ sudo su
root@Dig23:/home/smnb# cd /etc/ssl/private
root@Dig23:/etc/ssl/private# ls
ssl-cert-snakeoil.key
root@Dig23:/etc/ssl/private# openssl genrsa -aes128 -out testssl.key 2048
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
root@Dig23:/etc/ssl/private# openssl req -new -days 365 -key testssl.key -out testssl.csr
root@Dig23:/etc/ssl/private# openssl x509 -in testssl.csr -out testssl.crt -req -signkey testssl.key -days 365
root@Dig23:/etc/ssl/private# ls -lrt
total 16
root@Dig23:/home/smnb# cd /etc/ssl/private
root@Dig23:/etc/ssl/private# ls
ssl-cert-snakeoil.key
root@Dig23:/etc/ssl/private# openssl genrsa -aes128 -out testssl.key 2048
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
root@Dig23:/etc/ssl/private# openssl req -new -days 365 -key testssl.key -out testssl.csr
root@Dig23:/etc/ssl/private# openssl x509 -in testssl.csr -out testssl.crt -req -signkey testssl.key -days 365
root@Dig23:/etc/ssl/private# ls -lrt
total 16
- rw-r----- 1 root ssl-cert 1704 8月 12 15:39 ssl-cert-snakeoil.key
- rw------- 1 root root 1874 8月 14 15:53 testssl.key
- rw-r--r-- 1 root root 1029 8月 14 16:00 testssl.csr
- rw-r--r-- 1 root root 1265 8月 14 16:04 testssl.crt
# vi /etc/apache2/sites-available/default-ssl.conf
(修正前) | (修正後) | |
SSLCertificateFile | /etc/ssl/certs/ssl-cert-snakeoil.pem | /etc/ssl/private/testssl.crt |
SSLCertificateKeyFile | /etc/ssl/private/ssl-cert-snakeoil.key | /etc/ssl/private/testssl.key |
# systemctl restart apache2
🔐 Enter passphrase for SSL/TLS keys for 127.0.1.1:443 (RSA): (press TAB for no (no echo)
# exit
$ firefox https://localhost & # 自己証明書であるため、警告表示され、承知すれば、Apacheのデフォルトページ表示
🔐 Enter passphrase for SSL/TLS keys for 127.0.1.1:443 (RSA): (press TAB for no (no echo)
# exit
$ firefox https://localhost & # 自己証明書であるため、警告表示され、承知すれば、Apacheのデフォルトページ表示
smnb@Dig23:~$ sudo ufw enable
smnb@Dig23:~$ sudo ufw status
Port80(HTTP) 用は /etc/apache2/sites-available/000-default.conf
Port443(HTTPS) 用は /etc/apache2/sites-available/default-ssl.conf
$ cd /etc/apache2/sites-available/
$ sudo cp 000-default.conf smnb.conf
$ sudo cp default-ssl.conf smnb-ssl.conf
# ServerNameのエラーが出たら、/etc/apache2/apache2.conf に ServerName指定
$ grep ServerName /etc/apache2/apache2.conf
ServerName localhost
$ systemctl reload apache2
Port443(HTTPS) 用は /etc/apache2/sites-available/default-ssl.conf
$ cd /etc/apache2/sites-available/
$ sudo cp 000-default.conf smnb.conf
$ sudo cp default-ssl.conf smnb-ssl.conf
# ServerNameのエラーが出たら、/etc/apache2/apache2.conf に ServerName指定
$ grep ServerName /etc/apache2/apache2.conf
ServerName localhost
$ systemctl reload apache2
smnb@Dig23:~$ firefox http://localhost & # forbidden
smnb@Dig23:~$ firefox https://localhost & # OK
In case 403 Error occurs
$ sudo chmod 755 /home/smnb # chechk r_x is available for others to every path /home/smnb/www
smnb@Dig23:~$ firefox https://localhost & # OK
In case 403 Error occurs
$ sudo chmod 755 /home/smnb # chechk r_x is available for others to every path /home/smnb/www
「http://〜」にアクセスされても、 「https://〜」に強制的にリダイレクト
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
仮想ホストの設定ファイル | /etc/apache2/sites-available/ |
モジュールの設定ファイル | /etc/apache2/mods-available/ |
その他上記以外の細かい設定 | conf.d/ |
通信ポート設定
Listen 80 <IfModule ssl_module> Listen 443 </IfModule> <IfModule mod_gnutls.c> Listen 443 </IfModule>
$ grep -r LoadModule /etc/apache2/* | grep cgi_module /etc/apache2/mods-available/proxy_scgi.load:LoadModule proxy_scgi_module /usr/lib/apache2/modules/mod_proxy_scgi.so /etc/apache2/mods-available/cgi.load:LoadModule cgi_module /usr/lib/apache2/modules/mod_cgi.so /etc/apache2/mods-available/proxy_fcgi.load:LoadModule proxy_fcgi_module /usr/lib/apache2/modules/mod_proxy_fcgi.so
- /etc/apache2/mods-available/cgi.load が必要
$ grep -r cgi-bin /etc/apache2/* /etc/apache2/conf-available/serve-cgi-bin.conf: ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ /etc/apache2/conf-available/serve-cgi-bin.conf: <Directory "/usr/lib/cgi-bin"> /etc/apache2/conf-available/localized-error-pages.conf:#ErrorDocument 404 "/cgi-bin/missing_handler.pl" /etc/apache2/sites-available/default-ssl.conf: #Include conf-available/serve-cgi-bin.conf /etc/apache2/sites-available/default-ssl.conf: <Directory /usr/lib/cgi-bin> /etc/apache2/sites-available/000-default.conf: #Include conf-available/serve-cgi-bin.conf /etc/apache2/sites-enabled/000-default.conf.org: #Include conf-available/serve-cgi-bin.conf /etc/apache2/sites-enabled/000-default.conf~: #Include conf-available/serve-cgi-bin.conf
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
/cgi-bin/ は URL上のフォルダ名: http://localhost/cgi-bin/hello_cgi.cgi
/usr/lib/cgi-bin/ は実際のファイルの場所
$ grep -r -A 4 "<Directory \"/usr/lib/cgi-bin" /etc/apache2/* /etc/apache2/conf-available/serve-cgi-bin.conf: <Directory "/usr/lib/cgi-bin"> /etc/apache2/conf-available/serve-cgi-bin.conf- AllowOverride None /etc/apache2/conf-available/serve-cgi-bin.conf- Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch /etc/apache2/conf-available/serve-cgi-bin.conf- Require all granted /etc/apache2/conf-available/serve-cgi-bin.conf- </Directory>
- +ExecCGI が必要
#include <stdio.h> int main(int argc,char ** argv){ /* http header */ /* content type : HTML*/ printf("Content-type: text/html; charset=utf-8\n"); /* inhibit browser to cache */ printf("Pragma: no-cache\n"); printf("Cache-Control: no-cache\n"); /* end of header */ printf("\n"); /* contents */ printf("<html><head><title>テスト</title><head>"); printf("<body>テスト出力でんがな。</body></html>"); return EXIT_SUCCESS; }
sudo cp hello_cgi /usr/lib/cgi-bin/hello_cgi.cgi xdg-open http://localhost/cgi-bin/hello_cgi.cgi
次のような形式でCGIスクリプトに渡す。
http://...../cgi-bin/xxx.cgi?名前1=値1&名前2=値2
http://...../cgi-bin/xxx.cgi?名前1=値1&名前2=値2
xdg-open "http://localhost/cgi-bin/&a.cgi?m=10&n=12"CGIスクリプトは、これを環境変数の QUERY_STRING から読み出す。
char *p = getenv("QUERY_STRING"); if (p!=NULL){ long m,n; if(sscanf(s_dt,"m=%ld&n=%ld",&m,&n)==2){ printf("mxn=%ld),m*n); } }
stdioからEOFまで読み出し。
Formで
Formで
<FORM METHOD="POST" ACTION="cgi_post.cgi">していすれば、各パラメータを&区切りの NAME=VALUE 文字列で得られる
CGIスクリプトが参照可能な環境変数
#include <stdlib.h>
char *getenv(const char *varname);
char *secure_getenv(const char *name);
#include <stdlib.h>
char *getenv(const char *varname);
char *secure_getenv(const char *name);
変数名 | 意味 |
AUTH_TYPE | 認証方式(例えば MD5, Basicなど) |
CONTENT_LENGTH | 標準入力から読み込み可能なデータのバイト数(METHOD=POSTの時) |
CONTENT_TYPE | クライアントから送られてきたデータのタイプ。フォームからMETHOD=POSTで送信した場合は、application/x-www-form-urlencoded となる。 |
GATEWAY_INTERFACE | ゲートウェイプロトコル名称(例えばCGI/1.1) |
HTTP_ACCEPT | ブラウザがサポートする Content-type: のリスト。すべてを許可する場合、*/* となる。 |
HTTP_FORWARDED | この要求をフォワードしたプロキシサーバーの情報。送信されない場合もある。 |
HTTP_REFERER | そのCGIを呼び出したページのURL。送信されない場合や、たまに、全く別のURLを差していることもある。 |
HTTP_USER_AGENT | ブラウザに関する情報(Mozilla/4.01 [ja] (Win95; I) など) |
HTTP_X_FORWARDED_FOR | この要求をフォワードしたプロキシサーバーのIPアドレス。 |
PATH_INFO | パス情報。たとえば、「cgi-bin/xxx.cgi/taro/xxx.htm」というURLでCGIスクリプトを呼び出した場合、PATH_INFOには「/taro/xxx.htm」が格納される。 |
PATH_TRANSLATED | PATH_INFOで指定したファイルの、サーバー上の絶対パス名。 |
QUERY_STRING | 「http://サーバー名/CGIスクリプト名?データ」というURLを要求した場合のデータ部分。 |
REMOTE_ADDR | クライアントのIPアドレス(例えば10.0.0.1) |
REMOTE_HOST | クライアントのホスト名(例えば abc32.abc.co.jp) |
REMOTE_IDENT | クライアント側のユーザーID |
REMOTE_USER | クライアント側のユーザー名 |
REQUEST_METHOD | METHODで指定したデータ取得手段。GET, POST, HEAD, PUT, DELETE, LINK, UNLINKなどの種類がある。 |
SCRIPT_NAME | CGIスクリプトの名前。 |
SERVER_NAME | サーバー名 |
SERVER_PORT | サーバーのポート番号(例えば80) |
SERVER_PROTOCOL | サーバーのプロトコル名(例えばHTTP/1.0) |
SERVER_SOFTWARE | サーバーのソフトウェア名(例えば NCSA/1.3) |
最新コメント