アプリケーション開発ポータルサイト
ServerNote.NET
カテゴリー【ApacheDebian
DebianにウェブサーバーApache2をセットアップ
POSTED BY
2024-07-23

インストール&設定開始

  1. apt install apache2
  2. cd /etc/apache2

外に出す名前を最低限に

  1. conf-available/security.conf
  2. ServerTokens Prod
  3. ServerSignature Off

ApacheプロセスおよびCGIの実行ユーザー
自分を設定するとCGIでのファイル操作が楽

  1. envvars
  2. export APACHE_RUN_USER=hogeuser
  3. export APACHE_RUN_GROUP=hogegroup

.cgiをCGIプログラムとして実行する

  1. mods-available/mime.conf
  2. AddHandler cgi-script .cgi

mods-enabledでmods-availableへ使うモジュールのリンクが置かれている。
デフォルトに追加で、よく以下のようなモジュールを開放する。

  1. cd mods-enabled
  2. ln -s ../mods-available/cgi.load
  3. ln -s ../mods-available/expires.load
  4. ln -s ../mods-available/headers.load
  5. ln -s ../mods-available/proxy.load
  6. ln -s ../mods-available/proxy_http.load
  7. ln -s ../mods-available/proxy_http2.load
  8. ln -s ../mods-available/rewrite.load
  9. ln -s ../mods-available/socache_shmcb.load
  10. ln -s ../mods-available/ssl.conf
  11. ln -s ../mods-available/ssl.load

最後に、自分のサイトのオリジナル設定ファイルをsites-enabledに置く
自分サイトの構成が以下のような感じとすると

  1. /home/www
  2. --- apache2-my.conf # global config
  3. --- site1 # for site1.example1155.jp
  4. --- objs # contents dir
  5. --- logs # log dir
  6. --- site2 # for site2.example1155.jp
  7. --- objs # contents dir
  8. --- logs # log dir

  1. cd sites-enabled
  2. デフォルトを消す
  3. rm 000-default.conf
  4. ln -s /home/www/apache2-my.conf
  5. としてリンクを貼って、一般ユーザでapache2-my.confを編集する。

apache2-my.conf 例

Shellapache2-my.confGitHub Source
  1. SetEnv LD_LIBRARY_PATH "/usr/local/lib"
  2. AddType application/x-httpd-php .php
  3.  
  4. <Directory "/home/www/*/objs">
  5. Options ExecCGI FollowSymLinks
  6. AllowOverride All
  7. Require all granted
  8. </Directory>
  9.  
  10. <VirtualHost *:80>
  11. ServerAdmin webmaster@example1155.jp
  12. ServerName site1.example1155.jp
  13. CustomLog "/home/www/site1/logs/access_log" combined
  14. ErrorLog "/home/www/site1/logs/error_log"
  15. DocumentRoot "/home/www/site1/objs"
  16. </VirtualHost>
  17.  
  18. <VirtualHost *:443>
  19. SSLEngine on
  20. SSLProxyEngine on
  21. SSLCertificateFile "/etc/letsencrypt/live/site1.example1155.jp/cert.pem"
  22. SSLCertificateChainFile "/etc/letsencrypt/live/site1.example1155.jp/chain.pem"
  23. SSLCertificateKeyFile "/etc/letsencrypt/live/site1.example1155.jp/privkey.pem"
  24. ServerAdmin webmaster@example1155.jp
  25. ServerName site1.example1155.jp
  26. CustomLog "/home/www/site1/logs/ssl_access_log" combined
  27. ErrorLog "/home/www/site1/logs/ssl_error_log"
  28. DocumentRoot "/home/www/site1/objs"
  29. </VirtualHost>
  30.  
  31. <VirtualHost *:80>
  32. ServerAdmin webmaster@example1155.jp
  33. ServerName site2.example1155.jp
  34. CustomLog "/home/www/site2/logs/access_log" combined
  35. ErrorLog "/home/www/site2/logs/error_log"
  36. DocumentRoot "/home/www/site2/objs"
  37. RewriteEngine on
  38. RewriteCond %{HTTPS} off
  39. RewriteRule ^/(.*)$ https://site2.example1155.jp/$1 [R=301,L]
  40. </VirtualHost>
  41.  
  42. <VirtualHost *:443>
  43. SSLEngine on
  44. SSLProxyEngine on
  45. SSLCertificateFile "/etc/letsencrypt/live/site2.example1155.jp/cert.pem"
  46. SSLCertificateChainFile "/etc/letsencrypt/live/site2.example1155.jp/chain.pem"
  47. SSLCertificateKeyFile "/etc/letsencrypt/live/site2.example1155.jp/privkey.pem"
  48. ServerAdmin webmaster@example1155.jp
  49. ServerName site2.example1155.jp
  50. CustomLog "/home/www/site2/logs/ssl_access_log" combined
  51. ErrorLog "/home/www/site2/logs/ssl_error_log"
  52. DocumentRoot "/home/www/site2/objs"
  53. </VirtualHost>

site1.example1155.jp、site2.example1155.jp 2つのサイトを運用する例。

site1はhttpでもhttpsでも許容し、同じobjsコンテンツを表示する。

site2はhttpでのアクセスが来たら、httpsへリダイレクトする設定をしている。

SSLサーバー証明書設定Let's Encryptはこちら

設定のチェック

  1. sudo apache2ctl configtest

サーバー起動・停止・再起動

  1. sudo /etc/init.d/apache2 start
  2. sudo /etc/init.d/apache2 stop
  3. sudo /etc/init.d/apache2 restart

※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

【キーワード検索】