Apache


Overview:

Apache is Apache.

Version:
        初版        最新版
  V1.3  1998-06-06  2010-02-03 (1.3.42)
  V2.0  2002-04-06  2013-07-10 (2.0.65)
  V2.2  2005-12-01  2017-07-11 (2.2.34)
  V2.4  2012-02-21  2017-10-23 (2.4.29)

Install:

$ sudo yum -y install httpd
$ sudo systemctl start httpd
$ sudo systemctl enable httpd

$ /etc/init.d/httpd restart
$ systemctl restart httpd
$ systemctl status httpd
$ apachectl -k start
$ apachectl -k restart
$ apachectl -k stop
/var/log/httpd/

$ apachectl -V

MPM (マルチ プロセッシング モジュール):

prefork:
  - マルチプロセスモデル。
  - 1つのリクエストに対してあらかじめコピー(fork)された子プロセスが通信を受け持ち、他のプロセスに影響なく安定した通信が可能(スレッドセーフ)。多数のクライアントを裁くには多数のメモリ・CPUを使用
worker:
  - マルチスレッドモデル。
  - 複数の子プロセスがマルチスレッドで起動しスレッドがクライアントを受け持つ。
  - プロセスの起動を抑えられCPUやメモリの使用率を抑えることが可能。プロセス内での影響の可能性。
event:
  - イベント駆動マルチスレッドモデル。
  - 1つのプロセスが複数のリクエストを処理。
  - preforkやworkerはKeep-Alive(持続的接続)の際に同じプロセスやスレッドを保持し利用するが、workerは別のスレッドに割り振る

Virtual Host:

IPベース:   各ウェブサイトに違うIP
NAMEベース: それぞれのIPに複数の名前
PORTベース: PORT別

<VirtualHost ADDR[:PORT]>
    ServerName    # Host Name
    DocumentRoot  # コンテンツディレクトリ
    ServerAdmin   # 管理者メールアドレス(省略するとデフォルト値)
    ServerPath    # 非互換のブラウザ用
</VirtualHost>


NAMEベース:
  DNS:
   CNAME www.aaa.tld
   CNAME www.bbb.tld

  file: httpd.conf
  edit: |
        Listen 80
        NameVirtualHost *:80

        <VirtualHost *:80>  # default host
            ServerName      www.aaa.tld
            ServerAlias     aaa.tld *.aaa.tld
            DocumentRoot    /www/aaa
        </VirtualHost>

        <VirtualHost *:80>
            ServerName      www.bbb.tld
            DocumentRoot    /www/bbb
        </VirtualHost>

SSL:
  file: httpd.conf
  edit: |
        <IfModule mod_ssl.c>
            Listen 443
            NameVirtualHost *:443
        </IfModule>

        <VirtualHost *:443>
            ServerName      www.aaa.tld
            DocumentRoot    /var/www/aaa
            SSLEngine on
            SSLCertificateFile    /etc/ssl/aaa.pem
            SSLCertificateKeyFile /etc/ssl/aaa.key
        </VirtualHost>

        <VirtualHost *:443>
            ServerName      www.bbb.tld
            DocumentRoot    /var/www/bbb
            SSLEngine on
            SSLCertificateFile    /etc/ssl/bbb.pem
            SSLCertificateKeyFile /etc/ssl/bbb.key
        </VirtualHost>

IPベース:
  file: httpd.conf
  edit: |
        Listen 80

        <VirtualHost 172.20.30.40>  # default host
            ServerName      www.aaa.tld
            ServerAlias     aaa.tld *.aaa.tld
            DocumentRoot    /www/aaa
        </VirtualHost>

        <VirtualHost 172.20.30.50>
            ServerName      www.bbb.tld
            DocumentRoot    /www/bbb
        </VirtualHost>

SSL:
  http://www.aaa.tld   192.168.0.1:80
  http://www.bbb.tld   192.168.0.2:80
  https://www.bbb.tld  192.168.0.2:443
  http://www.ccc.tld   192.168.0.2:80

  file: httpd.conf
  edit: |
        <VirtualHost 192.168.0.1:80>
          ServerName www.aaa.tld
          DocumentRoot /var/www/aaa
        </VirtualHost>

        NameVirtualHost 192.168.0.2:80

        <VirtualHost 192.168.0.2:80>
          ServerName www.bbb.tld
          DocumentRoot /var/www/bbb
        </VirtualHost>

        <VirtualHost 192.168.0.2:80>
          ServerName www.ccc.tld
          DocumentRoot /var/www/ccc
        </VirtualHost>

        <VirtualHost 192.168.0.2:443>
          ServerName www.bbb.tld
          DocumentRoot /var/www/bbb-ssl
          SSLEngine on
          SSLCertificateFile /etc/apache2/ssl/cacert.crt
          SSLCertificateKeyFile /etc/apache2/ssl/private/cakey.pem
        </VirtualHost>

.htaccess Redirect:

file: httpd.conf
edit: |
      LoadModule rewrite_module modules/mod_rewrite.so

      <Directory "/usr/local/apache2/htdocs">
          AllowOverride FileInfo
      </Directory>

  AllowOverride:
    - None        # .htaccess」ファイルの存在自体を無視します。「.htaccess」を無効にします
    - ALL         # .htaccess」で指定した全ディレクティブの使用を許可します。
    - AuthConfig  # 認証に関するディレクティブの使用を許可します。
    - Limit       # ホスト名やIPアドレスによるアクセス制御の上許可します
    - FileInfo    # ディレクトリ表示の設定について許可します。SetEnvIfErrorDocument mod_rewrite 関連の設定などのこのキーワードになります
    - Indexes     # ディレクトリインデックスに関する以下のディレクティブの使用を許可します。 DirectoryIndex などこのキーワードになります
    - Options     # Options指定子で設定する機能について許可します

Python CGI:

file: httpd.conf
  edit: |
      LoadModule cgid_module modules/mod_cgid.so

      <Directory "/usr/local/apache2/cgi-bin">
        Options ExecCGI
        AddHandler cgi-script .py .pyc
      </Directory>

file: sample.py
  edit: |
      #!/usr/bin/python3
      import sys
      import io
      sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
      print("Content-Type: text/html;")
      print("")
      print("<!DOCTYPE html>")
      print("<html lang='ja'>")
      print("<head>")
      print("    <meta charset='utf-8'>")
      print("    <title>hello world.</title>")
      print("</head>")
      print("<body>")
      print("    <h1>hello world.</h1>")
      print("</body>")
      print("</html>")

$ sudo chmod 755 ./cgi-bin/sample.py
http://[IP Address]/cgi-bin/sample.py

apachectl:

comm: apachectl
Usage: /usr/local/apache2/bin/httpd [-D name] [-d directory] [-f file]
                                    [-C "directive"] [-c "directive"]
                                    [-k start|restart|graceful|graceful-stop|stop]
                                    [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
  -t -D DUMP_RUN_CFG : show parsed run settings
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules
  -M                 : a synonym for -t -D DUMP_MODULES
  -t -D DUMP_INCLUDES: show all included configuration files
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)

WSGI (ウィズギー):

Mode:
  - Embedded Mode
  - Daemon Mode


python
    >>> import sys
    >>> sys.path


python3-dev
gcc
libc-dev

Module List:
  httpd -M

$ find /var/www/python/venv -name 'mod_*.so'

python Install prefix:
$ python
   >>> import sysconfig
   >>> sysconfig.get_config_vars('CONFIG_ARGS')


$ tar zxvf mod_wsgi.tar.gz
$ cd mod_wsgi
$ ./configure --with-python=/home/username/.pyenv/versions/3.5.0/bin/python
$ make
$ sudo make install

mod_wsgi (Embedded Mode):

file: httpd.conf
edit: |
      LoadModule wsgi_module "/usr/local/apache2/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

      WSGIPythonHome /usr                               # or /usr/local
      WSGIPythonPath /usr/lib/python3.6/site-packages   # or /home/django

      WSGIScriptAlias / /home/django/mysite/wsgi.py
      <Directory "/home/django/mysite">
          <Files wsgi.py>
              Require all granted
          </Files>
      </Directory>

      Alias /static/ /home/django/myapp/static/
      <Directory /home/django/myapp/static>
          Require all granted
      </Directory>

mod_wsgi (Daemon Mode):

file: httpd.conf
edit: |
      LoadModule wsgi_module "/usr/local/apache2/modules/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"

      WSGIDaemonProcess django user=daemon group=daemon processes=2 threads=15 python-home=/usr/local python-path=/home/django
      WSGIProcessGroup django
      WSGISocketPrefix /var/run/wsgi

      WSGIScriptAlias / /home/django/mysite/wsgi.py
      <Directory "/home/django/mysite">
          <Files wsgi.py>
              Require all granted
          </Files>
      </Directory>

      Alias /static/ /home/django/myapp/static/
      <Directory /home/django/myapp/static>
          Require all granted
      </Directory>

Core Dump:

httpd:
  httpd.conf
    CoreDumpDirectory /tmp

process:
$ ulimit -c unlimited
$ ulimit -a
      core file size      (blocks, -c) unlimited

/etc/profile    #  or (.bashrc/.bash_profile)
  ulimit -c unlimited > /dev/null 2>&1

system: # ?
$ sysctl -a
$ sysctl kern.sugid_coredump=1
$ sysctl fs.suid_dumpable=1     # ??
  kernel.ftrace_dump_on_oops=1  # ??

debug:
$ gdb <command> -c <core file>
$ gdb bin/httpd -c core         # at /usr/local/apache2


apache dump io:
  httpd.conf
    LoadModule dumpio_module modules/mod_dumpio.so
    DumpIOInput On
    DumpIOOutput On

Core Dump USAGE:

Segmentation fault!

file: docker-compose
      tty: true
      command: /bin/bash

$ docker-compose up
$ ulimit -c unlimited
$ httpd-foreground
  Segmentation fault!!
$ gdb bin/httpd -c core