ืื•ื˜ื•ืžืฆื™ื” ืฉืœ ื”ืชืงื ืช ื•ื•ืจื“ืคืจืก ืขื NGINX Unit ื•ืื•ื‘ื•ื ื˜ื•

ืื•ื˜ื•ืžืฆื™ื” ืฉืœ ื”ืชืงื ืช ื•ื•ืจื“ืคืจืก ืขื NGINX Unit ื•ืื•ื‘ื•ื ื˜ื•

ื™ืฉ ื”ืจื‘ื” ืžื“ืจื™ื›ื™ื ื›ื™ืฆื“ ืœื”ืชืงื™ืŸ ื•ื•ืจื“ืคืจืก, ื—ื™ืคื•ืฉ ื‘ื’ื•ื’ืœ ืฉืœ "ื”ืชืงื ืช ื•ื•ืจื“ืคืจืก" ื™ื‘ื™ื ื›ื—ืฆื™ ืžื™ืœื™ื•ืŸ ืชื•ืฆืื•ืช. ืขื ื–ืืช, ืœืžืขืฉื”, ื™ืฉ ื‘ื™ื ื™ื”ื ืžืขื˜ ืžืื•ื“ ืžื“ืจื™ื›ื™ื ื˜ื•ื‘ื™ื, ืœืคื™ื”ื ื ื™ืชืŸ ืœื”ืชืงื™ืŸ ื•ืœื”ื’ื“ื™ืจ ืืช ื•ื•ืจื“ืคืจืก ื•ืืช ืžืขืจื›ืช ื”ื”ืคืขืœื” ื”ื‘ืกื™ืกื™ืช ื›ืš ืฉื™ื”ื™ื• ืžืกื•ื’ืœื™ื ืœืชืžื•ืš ืœืื•ืจืš ื–ืžืŸ. ืื•ืœื™ ื”ื”ื’ื“ืจื•ืช ื”ื ื›ื•ื ื•ืช ืชืœื•ื™ื•ืช ืžืื•ื“ ื‘ืฆืจื›ื™ื ืกืคืฆื™ืคื™ื™ื, ืื• ืฉื–ื” ื ื•ื‘ืข ืžื”ืขื•ื‘ื“ื” ืฉื”ืกื‘ืจ ืžืคื•ืจื˜ ืžืงืฉื” ืขืœ ื”ืงืจื™ืื” ืฉืœ ื”ืžืืžืจ.

ื‘ืžืืžืจ ื–ื”, ื ื ืกื” ืœืฉืœื‘ ืืช ื”ื˜ื•ื‘ ืžืฉื ื™ ื”ืขื•ืœืžื•ืช ืขืœ ื™ื“ื™ ืืกืคืงืช ืกืงืจื™ืคื˜ bash ืœื”ืชืงื ื” ืื•ื˜ื•ืžื˜ื™ืช ืฉืœ ื•ื•ืจื“ืคืจืก ืขืœ ืื•ื‘ื•ื ื˜ื•, ื›ืžื• ื’ื ืœืขื‘ื•ืจ ืขืœื™ื•, ืœื”ืกื‘ื™ืจ ืžื” ื›ืœ ื—ืœืง ืขื•ืฉื”, ื›ืžื• ื’ื ืืช ื”ืคืฉืจื•ืช ืฉืขืฉื™ื ื• ื‘ืคื™ืชื•ื—ื• . ืื ืืชื” ืžืฉืชืžืฉ ืžืชืงื“ื, ืืชื” ื™ื›ื•ืœ ืœื“ืœื’ ืขืœ ื˜ืงืกื˜ ื”ืžืืžืจ ื•ืคืฉื•ื˜ ืœืงื—ืช ืืช ื”ืชืกืจื™ื˜ ืœืฉื™ื ื•ื™ ื•ืฉื™ืžื•ืฉ ื‘ืกื‘ื™ื‘ื•ืช ืฉืœืš. ื”ืคืœื˜ ืฉืœ ื”ืกืงืจื™ืคื˜ ื”ื•ื ื”ืชืงื ืช ื•ื•ืจื“ืคืจืก ืžื•ืชืืžืช ืื™ืฉื™ืช ืขื ืชืžื™ื›ื” ืฉืœ Lets Encrypt, ื”ืคื•ืขืœืช ืขืœ NGINX Unit ื•ืžืชืื™ืžื” ืœืฉื™ืžื•ืฉ ืชืขืฉื™ื™ืชื™.

ื”ืืจื›ื™ื˜ืงื˜ื•ืจื” ืฉืคื•ืชื—ื” ืœืคืจื™ืกืช ื•ื•ืจื“ืคืจืก ื‘ืืžืฆืขื•ืช ื™ื—ื™ื“ืช NGINX ืžืชื•ืืจืช ื‘ ืžืืžืจ ื™ืฉืŸ ื™ื•ืชืจ, ื›ืขืช ืื ื• ื’ื ื ื’ื“ื™ืจ ืขื•ื“ ื“ื‘ืจื™ื ืฉืœื ื›ื•ืกื• ืฉื (ื›ืžื• ื‘ืžื“ืจื™ื›ื™ื ืจื‘ื™ื ืื—ืจื™ื):

  • ื•ื•ืจื“ืคืจืก CLI
  • ื‘ื•ืื• ืœื”ืฆืคื™ืŸ ืื™ืฉื•ืจื™ TLSSSL
  • ื—ื™ื“ื•ืฉ ืื•ื˜ื•ืžื˜ื™ ืฉืœ ืชืขื•ื“ื•ืช
  • ืžื˜ืžื•ืŸ NGINX
  • ื“ื—ื™ืกื” ืฉืœ NGINX
  • ืชืžื™ื›ื” ื‘-HTTPS ื•-HTTP/2
  • ืื•ื˜ื•ืžืฆื™ื” ืฉืœ ืชื”ืœื™ื›ื™ื

ื”ืžืืžืจ ื™ืชืืจ ื”ืชืงื ื” ื‘ืฉืจืช ืื—ื“, ืฉื™ืืจื— ื‘ื• ื–ืžื ื™ืช ืฉืจืช ืขื™ื‘ื•ื“ ืกื˜ื˜ื™, ืฉืจืช ืขื™ื‘ื•ื“ PHP ื•ืžืกื“ ื ืชื•ื ื™ื. ื”ืชืงื ื” ืขื ืชืžื™ื›ื” ื‘ืžืกืคืจ ืžืืจื—ื™ื ื•ืฉื™ืจื•ืชื™ื ื•ื™ืจื˜ื•ืืœื™ื™ื ื”ื™ื ื ื•ืฉื ืคื•ื˜ื ืฆื™ืืœื™ ืœืขืชื™ื“. ืื ืืชื” ืจื•ืฆื” ืฉื ื›ืชื•ื‘ ืขืœ ืžืฉื”ื• ืฉืœื ืžื•ืคื™ืข ื‘ืžืืžืจื™ื ื”ืืœื”, ื›ืชื•ื‘ ื‘ืชื’ื•ื‘ื•ืช.

ื“ืจื™ืฉื•ืช

  • ืžื™ื›ืœ ืฉืจืช (LXC ืื• Lxd), ืžื›ื•ื ื” ื•ื™ืจื˜ื•ืืœื™ืช, ืื• ืฉืจืช ื—ื•ืžืจื” ืจื’ื™ืœ, ืขื ืœืคื—ื•ืช 512MB ืฉืœ ื–ื™ื›ืจื•ืŸ RAM ื•-Ubuntu 18.04 ืื• ื™ื•ืชืจ ืžื•ืชืงืŸ.
  • ื™ืฆื™ืื•ืช 80 ื•-443 ื ื’ื™ืฉื•ืช ืœืื™ื ื˜ืจื ื˜
  • ืฉื ื“ื•ืžื™ื™ืŸ ื”ืžืฉื•ื™ืš ืœื›ืชื•ื‘ืช ื”-IP ื”ืฆื™ื‘ื•ืจื™ืช ืฉืœ ืฉืจืช ื–ื”
  • ื’ื™ืฉื” ืขื ื–ื›ื•ื™ื•ืช ืฉื•ืจืฉ (sudo).

ืกืงื™ืจื” ื›ืœืœื™ืช ืฉืœ ืื“ืจื™ื›ืœื•ืช

ื”ืืจื›ื™ื˜ืงื˜ื•ืจื” ื–ื”ื” ืœืชื™ืื•ืจ ืžื•ืงื“ื ื™ื•ืชืจ, ืืคืœื™ืงืฆื™ื™ืช ืื™ื ื˜ืจื ื˜ ืชืœืช-ืฉื›ื‘ืชื™ืช. ื”ื•ื ืžื•ืจื›ื‘ ืžืกืงืจื™ืคื˜ื™ื ืฉืœ PHP ื”ืžื‘ื•ืฆืขื™ื ื‘ืžื ื•ืข PHP ื•ืงื‘ืฆื™ื ืกื˜ื˜ื™ื™ื ื”ืžืขื•ื‘ื“ื™ื ืขืœ ื™ื“ื™ ืฉืจืช ื”ืื™ื ื˜ืจื ื˜.

ืื•ื˜ื•ืžืฆื™ื” ืฉืœ ื”ืชืงื ืช ื•ื•ืจื“ืคืจืก ืขื NGINX Unit ื•ืื•ื‘ื•ื ื˜ื•

ืขืงืจื•ื ื•ืช ื›ืœืœื™ื™ื

  • ืคืงื•ื“ื•ืช ืชืฆื•ืจื” ืจื‘ื•ืช ื‘ืกืงืจื™ืคื˜ ืขื˜ื•ืคื™ื ื‘ืชื ืื™ื ืœืื™ืžืคื•ื˜ื ื˜ื™ื•ืช: ื ื™ืชืŸ ืœื”ืจื™ืฅ ืืช ื”ืกืงืจื™ืคื˜ ืžืกืคืจ ืคืขืžื™ื ืœืœื ืกื™ื›ื•ืŸ ืฉืœ ืฉื™ื ื•ื™ ื”ื’ื“ืจื•ืช ืฉื›ื‘ืจ ืงื™ื™ืžื•ืช.
  • ื”ืกืงืจื™ืคื˜ ืžื ืกื” ืœื”ืชืงื™ืŸ ืชื•ื›ื ื” ืžืžืื’ืจื™ื, ื›ืš ืฉืชื•ื›ืœ ืœื”ื—ื™ืœ ืขื“ื›ื•ื ื™ ืžืขืจื›ืช ื‘ืคืงื•ื“ื” ืื—ืช (apt upgrade ืขื‘ื•ืจ ืื•ื‘ื•ื ื˜ื•).
  • ืฆื•ื•ืชื™ื ืžื ืกื™ื ืœื–ื”ื•ืช ืฉื”ื ืคื•ืขืœื™ื ื‘ืงื•ื ื˜ื™ื™ื ืจ ื›ื“ื™ ืฉื™ื•ื›ืœื• ืœืฉื ื•ืช ืืช ื”ื”ื’ื“ืจื•ืช ืฉืœื”ื ื‘ื”ืชืื.
  • ืขืœ ืžื ืช ืœื”ื’ื“ื™ืจ ืืช ืžืกืคืจ ืชื”ืœื™ื›ื™ ื”ืฉืจืฉื•ืจ ืœื”ืชื—ื™ืœ ื‘ื”ื’ื“ืจื•ืช, ื”ืกืงืจื™ืคื˜ ืžื ืกื” ืœื ื—ืฉ ืืช ื”ื”ื’ื“ืจื•ืช ื”ืื•ื˜ื•ืžื˜ื™ื•ืช ืœืขื‘ื•ื“ื” ื‘ืงื•ื ื˜ื™ื™ื ืจื™ื, ืžื›ื•ื ื•ืช ื•ื™ืจื˜ื•ืืœื™ื•ืช ื•ืฉืจืชื™ ื—ื•ืžืจื”.
  • ื›ืืฉืจ ืื ื• ืžืชืืจื™ื ื”ื’ื“ืจื•ืช, ืื ื• ืชืžื™ื“ ื—ื•ืฉื‘ื™ื ืงื•ื“ื ื›ืœ ืขืœ ืื•ื˜ื•ืžืฆื™ื”, ืฉืื ื• ืžืงื•ื•ื™ื ืฉืชื”ืคื•ืš ืœื‘ืกื™ืก ืœื™ืฆื™ืจืช ืชืฉืชื™ืช ืžืฉืœืš ื›ืงื•ื“.
  • ื›ืœ ื”ืคืงื•ื“ื•ืช ืžื•ืคืขืœื•ืช ื›ืžืฉืชืžืฉ ืฉื•ืจืฉ, ื›ื™ ื”ื ืžืฉื ื™ื ืืช ื”ื’ื“ืจื•ืช ื”ืžืขืจื›ืช ื”ื‘ืกื™ืกื™ื•ืช, ืื‘ืœ ื™ืฉื™ืจื•ืช ื•ื•ืจื“ืคืจืก ืคื•ืขืœืช ื›ืžืฉืชืžืฉ ืจื’ื™ืœ.

ื”ื’ื“ืจืช ืžืฉืชื ื™ ืกื‘ื™ื‘ื”

ื”ื’ื“ืจ ืืช ืžืฉืชื ื™ ื”ืกื‘ื™ื‘ื” ื”ื‘ืื™ื ืœืคื ื™ ื”ืคืขืœืช ื”ืกืงืจื™ืคื˜:

  • WORDPRESS_DB_PASSWORD - ืกื™ืกืžืช ืžืกื“ ื”ื ืชื•ื ื™ื ืฉืœ ื•ื•ืจื“ืคืจืก
  • WORDPRESS_ADMIN_USER - ืฉื ืžืฉืชืžืฉ ืฉืœ ืื“ืžื™ืŸ ื•ื•ืจื“ืคืจืก
  • WORDPRESS_ADMIN_PASSWORD - ืกื™ืกืžืช ืžื ื”ืœ ื•ื•ืจื“ืคืจืก
  • WORDPRESS_ADMIN_EMAIL - ื“ื•ื"ืœ ืœื ื™ื”ื•ืœ ื•ื•ืจื“ืคืจืก
  • WORDPRESS_URL ื”ื•ื ื›ืชื•ื‘ืช ื”ืืชืจ ื”ืžืœืื” ืฉืœ ืืชืจ ื•ื•ืจื“ืคืจืก, ื”ื—ืœ ืž- https://.
  • LETS_ENCRYPT_STAGING โ€” ืจื™ืง ื›ื‘ืจื™ืจืช ืžื—ื“ืœ, ืืš ืขืœ ื™ื“ื™ ื”ื’ื“ืจืช ื”ืขืจืš ืœ-1, ืชืฉืชืžืฉ ื‘ืฉืจืชื™ ื”-Staging ืฉืœ Let's Encrypt, ื”ื ื—ื•ืฆื™ื ื›ื“ื™ ืœื‘ืงืฉ ืชืขื•ื“ื•ืช ืชื›ื•ืคื•ืช ื‘ืขืช ื‘ื“ื™ืงืช ื”ื”ื’ื“ืจื•ืช ืฉืœืš, ืื—ืจืช Let's Encrypt ืขืฉื•ื™ื” ืœื—ืกื•ื ื–ืžื ื™ืช ืืช ื›ืชื•ื‘ืช ื”-IP ืฉืœืš ื‘ื’ืœืœ ืžืกืคืจ ื”ื‘ืงืฉื•ืช ื”ื’ื“ื•ืœ.

ื”ืกืงืจื™ืคื˜ ื‘ื•ื“ืง ืฉื”ืžืฉืชื ื™ื ื”ืงืฉื•ืจื™ื ืœื•ื•ืจื“ืคืจืก ืžื•ื’ื“ืจื™ื ื•ื™ื•ืฆื ืื ืœื.
ืฉื•ืจื•ืช ืกืงืจื™ืคื˜ 572-576 ื‘ื•ื“ืงื•ืช ืืช ื”ืขืจืš LETS_ENCRYPT_STAGING.

ื”ื’ื“ืจืช ืžืฉืชื ื™ ืกื‘ื™ื‘ื” ื ื’ื–ืจื™ื

ื”ืกืงืจื™ืคื˜ ื‘ืฉื•ืจื•ืช 55-61 ืžื’ื“ื™ืจ ืืช ืžืฉืชื ื™ ื”ืกื‘ื™ื‘ื” ื”ื‘ืื™ื, ืื• ืœืขืจืš ืžืงื•ื“ื“ ื›ืœืฉื”ื• ืื• ื‘ืืžืฆืขื•ืช ืขืจืš ื”ืžืชืงื‘ืœ ืžื”ืžืฉืชื ื™ื ืฉื”ื•ื’ื“ืจื• ื‘ืกืขื™ืฃ ื”ืงื•ื“ื:

  • DEBIAN_FRONTEND="noninteractive" - ืื•ืžืจ ืœืืคืœื™ืงืฆื™ื•ืช ืฉื”ืŸ ืคื•ืขืœื•ืช ื‘ืกืงืจื™ืคื˜ ื•ืฉืื™ืŸ ืืคืฉืจื•ืช ืœืื™ื ื˜ืจืืงืฆื™ื” ืขื ื”ืžืฉืชืžืฉ.
  • WORDPRESS_CLI_VERSION="2.4.0" ื”ื™ื ื”ื’ืจืกื” ืฉืœ ืืคืœื™ืงืฆื™ื™ืช ื•ื•ืจื“ืคืจืก CLI.
  • WORDPRESS_CLI_MD5= "dedd5a662b80cda66e9e25d44c23b25c" โ€” ืกื›ื•ื ื‘ื“ื™ืงื” ืฉืœ ืงื•ื‘ืฅ ื”ื”ืคืขืœื” ืฉืœ WordPress CLI 2.4.0 (ื”ื’ืจืกื” ืžืฆื•ื™ื ืช ื‘ืžืฉืชื ื” WORDPRESS_CLI_VERSION). ื”ืกืงืจื™ืคื˜ ื‘ืฉื•ืจื” 162 ืžืฉืชืžืฉ ื‘ืขืจืš ื–ื” ื›ื“ื™ ืœื‘ื“ื•ืง ืฉื”ื•ืจื“ื” ื”ืงื•ื‘ืฅ ื”ื ื›ื•ืŸ ืฉืœ WordPress CLI.
  • UPLOAD_MAX_FILESIZE="16M" - ื’ื•ื“ืœ ื”ืงื•ื‘ืฅ ื”ืžืจื‘ื™ ืฉื ื™ืชืŸ ืœื”ืขืœื•ืช ืœื•ื•ืจื“ืคืจืก. ื”ื’ื“ืจื” ื–ื• ืžืฉืžืฉืช ื‘ืžืกืคืจ ืžืงื•ืžื•ืช, ื›ืš ืฉืงืœ ื™ื•ืชืจ ืœื”ื’ื“ื™ืจ ืื•ืชื” ื‘ืžืงื•ื ืื—ื“.
  • TLS_HOSTNAME= "$(echo ${WORDPRESS_URL} | cut -d'/' -f3)" โ€” ืฉื ืžืืจื— ืžืขืจื›ืช, ื—ื•ืœืฅ ืžื”ืžืฉืชื ื” WORDPRESS_URL. ืžืฉืžืฉ ืœื”ืฉื’ืช ืชืขื•ื“ื•ืช TLS/SSL ืžืชืื™ืžื•ืช ืž-Let's Encrypt, ื›ืžื• ื’ื ืœืื™ืžื•ืช ื•ื•ืจื“ืคืจืก ืคื ื™ืžื™.
  • NGINX_CONF_DIR="/etc/nginx" - ื ืชื™ื‘ ืœืกืคืจื™ื™ื” ืขื ื”ื’ื“ืจื•ืช NGINX, ื›ื•ืœืœ ื”ืงื•ื‘ืฅ ื”ืจืืฉื™ nginx.conf.
  • CERT_DIR="/etc/letsencrypt/live/${TLS_HOSTNAME}" โ€” ื ืชื™ื‘ ืœืื™ืฉื•ืจื™ Let's Encrypt ืขื‘ื•ืจ ืืชืจ ื•ื•ืจื“ืคืจืก, ื”ืžืชืงื‘ืœ ืžื”ืžืฉืชื ื” TLS_HOSTNAME.

ื”ืงืฆืืช ืฉื ืžืืจื— ืœืฉืจืช ื•ื•ืจื“ืคืจืก

ื”ืกืงืจื™ืคื˜ ืžื’ื“ื™ืจ ืืช ืฉื ื”ืžืืจื— ืฉืœ ื”ืฉืจืช ื›ืš ืฉื”ืขืจืš ื™ืชืื™ื ืœืฉื ื”ื“ื•ืžื™ื™ืŸ ืฉืœ ื”ืืชืจ. ื–ื” ืœื ื”ื›ืจื—ื™, ืื‘ืœ ื–ื” ื™ื•ืชืจ ื ื•ื— ืœืฉืœื•ื— ื“ื•ืืจ ื™ื•ืฆื ื“ืจืš SMTP ื‘ืขืช ื”ื’ื“ืจืช ืฉืจืช ื‘ื•ื“ื“, ื›ืคื™ ืฉื”ื•ื’ื“ืจ ืขืœ ื™ื“ื™ ื”ืกืงืจื™ืคื˜.

ืงื•ื“ ืกืงืจื™ืคื˜

# Change the hostname to be the same as the WordPress hostname
if [ ! "$(hostname)" == "${TLS_HOSTNAME}" ]; then
  echo " Changing hostname to ${TLS_HOSTNAME}"
  hostnamectl set-hostname "${TLS_HOSTNAME}"
fi

ื”ื•ืกืคืช ืฉื ืžืืจื— ืœ-/etc/hosts

ืชื•ืกืคืช WP-Cron ืžืฉืžืฉ ืœื”ืคืขืœืช ืžืฉื™ืžื•ืช ืชืงื•ืคืชื™ื•ืช, ื“ื•ืจืฉ ืฉ-WordPress ืชื”ื™ื” ืžืกื•ื’ืœืช ืœื’ืฉืช ืœืขืฆืžื” ื‘ืืžืฆืขื•ืช HTTP. ื›ื“ื™ ืœื•ื•ื“ื ืฉ-WP-Cron ืคื•ืขืœ ื›ื”ืœื›ื” ื‘ื›ืœ ื”ืกื‘ื™ื‘ื•ืช, ื”ืกืงืจื™ืคื˜ ืžื•ืกื™ืฃ ืฉื•ืจื” ืœืงื•ื‘ืฅ / Etc / hostsื›ืš ืฉ-WordPress ื™ื›ื•ืœื” ืœื’ืฉืช ืœืขืฆืžื” ื“ืจืš ืžืžืฉืง ื”ืœื•ืœืื”:

ืงื•ื“ ืกืงืจื™ืคื˜

# Add the hostname to /etc/hosts
if [ "$(grep -m1 "${TLS_HOSTNAME}" /etc/hosts)" = "" ]; then
  echo " Adding hostname ${TLS_HOSTNAME} to /etc/hosts so that WordPress can ping itself"
  printf "::1 %sn127.0.0.1 %sn" "${TLS_HOSTNAME}" "${TLS_HOSTNAME}" >> /etc/hosts
fi

ื”ืชืงื ืช ื”ื›ืœื™ื ื”ื“ืจื•ืฉื™ื ืœืฉืœื‘ื™ื ื”ื‘ืื™ื

ืฉืืจ ื”ืชืกืจื™ื˜ ืฆืจื™ืš ื›ืžื” ืชื•ื›ื ื™ื•ืช ื•ืžื ื™ื— ืฉื”ืžืื’ืจื™ื ืžืขื•ื“ื›ื ื™ื. ืื ื• ืžืขื“ื›ื ื™ื ืืช ืจืฉื™ืžืช ื”ืžืื’ืจื™ื, ื•ืœืื—ืจ ืžื›ืŸ ืื ื• ืžืชืงื™ื ื™ื ืืช ื”ื›ืœื™ื ื”ื“ืจื•ืฉื™ื:

ืงื•ื“ ืกืงืจื™ืคื˜

# Make sure tools needed for install are present
echo " Installing prerequisite tools"
apt-get -qq update
apt-get -qq install -y 
  bc 
  ca-certificates 
  coreutils 
  curl 
  gnupg2 
  lsb-release

ื”ื•ืกืคืช ื™ื—ื™ื“ืช NGINX ื•ืžืื’ืจื™ NGINX

ื”ืกืงืจื™ืคื˜ ืžืชืงื™ืŸ ืืช NGINX Unit ื•-NGINX ื‘ืงื•ื“ ืคืชื•ื— ืžืžืื’ืจื™ NGINX ื”ืจืฉืžื™ื™ื ื›ื“ื™ ืœื•ื•ื“ื ืฉื”ื’ื™ืจืกืื•ืช ืขื ืชื™ืงื•ื ื™ ื”ืื‘ื˜ื—ื” ื•ืชื™ืงื•ื ื™ ื”ื‘ืื’ื™ื ื”ืขื“ื›ื ื™ื™ื ื‘ื™ื•ืชืจ ื ืžืฆืื•ืช ื‘ืฉื™ืžื•ืฉ.

ื”ืกืงืจื™ืคื˜ ืžื•ืกื™ืฃ ืืช ืžืื’ืจ NGINX Unit ื•ืœืื—ืจ ืžื›ืŸ ืืช ืžืื’ืจ NGINX, ืžื•ืกื™ืฃ ืืช ืžืคืชื— ื”ืžืื’ืจื™ื ื•ืงื•ื‘ืฆื™ ื”ืชืฆื•ืจื” apt, ื”ื’ื“ืจืช ื’ื™ืฉื” ืœืžืื’ืจื™ื ื“ืจืš ื”ืื™ื ื˜ืจื ื˜.

ื”ื”ืชืงื ื” ื‘ืคื•ืขืœ ืฉืœ NGINX Unit ื•-NGINX ืžืชืจื—ืฉืช ื‘ืกืขื™ืฃ ื”ื‘ื. ืื ื• ืžื•ืกื™ืคื™ื ืžืจืืฉ ืืช ื”ืžืื’ืจื™ื ื›ื“ื™ ืฉืœื ื ืฆื˜ืจืš ืœืขื“ื›ืŸ ืืช ื”ืžื˜ื ื ืชื•ื ื™ื ืžืกืคืจ ืคืขืžื™ื, ืžื” ืฉื”ื•ืคืš ืืช ื”ื”ืชืงื ื” ืœืžื”ื™ืจื” ื™ื•ืชืจ.

ืงื•ื“ ืกืงืจื™ืคื˜

# Install the NGINX Unit repository
if [ ! -f /etc/apt/sources.list.d/unit.list ]; then
  echo " Installing NGINX Unit repository"
  curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add -
  echo "deb https://packages.nginx.org/unit/ubuntu/ $(lsb_release -cs) unit" > /etc/apt/sources.list.d/unit.list
fi

# Install the NGINX repository
if [ ! -f /etc/apt/sources.list.d/nginx.list ]; then
  echo " Installing NGINX repository"
  curl -fsSL https://nginx.org/keys/nginx_signing.key | apt-key add -
  echo "deb https://nginx.org/packages/mainline/ubuntu $(lsb_release -cs) nginx" > /etc/apt/sources.list.d/nginx.list
fi

ื”ืชืงื ืช NGINX, NGINX Unit, PHP MariaDB, Certbot (ื‘ื•ืื• ื ืฆืคื™ืŸ) ื•ื”ืชืœื•ืช ืฉืœื”ื

ืœืื—ืจ ื”ื•ืกืคืช ื›ืœ ื”ืžืื’ืจื™ื, ืขื“ื›ืŸ ืืช ื”ืžื˜ื ื ืชื•ื ื™ื ื•ื”ืชืงืŸ ืืช ื”ื™ื™ืฉื•ืžื™ื. ื”ื—ื‘ื™ืœื•ืช ื”ืžื•ืชืงื ื•ืช ืขืœ ื™ื“ื™ ื”ืกืงืจื™ืคื˜ ื›ื•ืœืœื•ืช ื’ื ืืช ื”ืจื—ื‘ื•ืช PHP ื”ืžื•ืžืœืฆื•ืช ื‘ืขืช ื”ืคืขืœืช WordPress.org

ืงื•ื“ ืกืงืจื™ืคื˜

echo " Updating repository metadata"
apt-get -qq update

# Install PHP with dependencies and NGINX Unit
echo " Installing PHP, NGINX Unit, NGINX, Certbot, and MariaDB"
apt-get -qq install -y --no-install-recommends 
  certbot 
  python3-certbot-nginx 
  php-cli 
  php-common 
  php-bcmath 
  php-curl 
  php-gd 
  php-imagick 
  php-mbstring 
  php-mysql 
  php-opcache 
  php-xml 
  php-zip 
  ghostscript 
  nginx 
  unit 
  unit-php 
  mariadb-server

ื”ื’ื“ืจืช PHP ืœืฉื™ืžื•ืฉ ืขื ื™ื—ื™ื“ืช NGINX ื•-WordPress

ื”ืกืงืจื™ืคื˜ ื™ื•ืฆืจ ืงื•ื‘ืฅ ื”ื’ื“ืจื•ืช ื‘ืกืคืจื™ื™ื” conf.d. ื–ื” ืžื’ื“ื™ืจ ืืช ื’ื•ื“ืœ ื”ืขืœืืช ื”ืงื‘ืฆื™ื ื”ืžืงืกื™ืžืœื™ ืขื‘ื•ืจ PHP, ืžืืคืฉืจ ืคืœื˜ ืฉืœ ืฉื’ื™ืื•ืช PHP ืœ-STDERR ื›ืš ืฉื”ืŸ ื™ื™ืจืฉืžื• ืœื™ื—ื™ื“ืช NGINX, ื•ืžืคืขื™ืœ ืžื—ื“ืฉ ืืช ื™ื—ื™ื“ืช NGINX.

ืงื•ื“ ืกืงืจื™ืคื˜

# Find the major and minor PHP version so that we can write to its conf.d directory
PHP_MAJOR_MINOR_VERSION="$(php -v | head -n1 | cut -d' ' -f2 | cut -d'.' -f1,2)"

if [ ! -f "/etc/php/${PHP_MAJOR_MINOR_VERSION}/embed/conf.d/30-wordpress-overrides.ini" ]; then
  echo " Configuring PHP for use with NGINX Unit and WordPress"
  # Add PHP configuration overrides
  cat > "/etc/php/${PHP_MAJOR_MINOR_VERSION}/embed/conf.d/30-wordpress-overrides.ini" << EOM
; Set a larger maximum upload size so that WordPress can handle
; bigger media files.
upload_max_filesize=${UPLOAD_MAX_FILESIZE}
post_max_size=${UPLOAD_MAX_FILESIZE}
; Write error log to STDERR so that error messages show up in the NGINX Unit log
error_log=/dev/stderr
EOM
fi

# Restart NGINX Unit because we have reconfigured PHP
echo " Restarting NGINX Unit"
service unit restart

ืฆื™ื•ืŸ ื”ื’ื“ืจื•ืช ืžืกื“ ื”ื ืชื•ื ื™ื ืฉืœ MariaDB ืขื‘ื•ืจ WordPress

ื‘ื—ืจื ื• ื‘-MariaDB ืขืœ ืคื ื™ MySQL ืžื›ื™ื•ื•ืŸ ืฉื™ืฉ ืœื• ื™ื•ืชืจ ืคืขื™ืœื•ืช ืงื”ื™ืœืชื™ืช ื•ืกื‘ื™ืจ ืœื”ื ื™ื— ืฉื’ื ื›ืŸ ืžืกืคืง ื‘ื™ืฆื•ืขื™ื ื˜ื•ื‘ื™ื ื™ื•ืชืจ ื›ื‘ืจื™ืจืช ืžื—ื“ืœ (ื›ื ืจืื”, ื”ื›ืœ ืคืฉื•ื˜ ื™ื•ืชืจ ื›ืืŸ: ื›ื“ื™ ืœื”ืชืงื™ืŸ MySQL, ืืชื” ืฆืจื™ืš ืœื”ื•ืกื™ืฃ ืžืื’ืจ ื ื•ืกืฃ, ืžืฉื•ืขืจ. ืžึฐืชื•ึผืจื’ึฐืžึธืŸ).

ื”ืกืงืจื™ืคื˜ ื™ื•ืฆืจ ืžืกื“ ื ืชื•ื ื™ื ื—ื“ืฉ ื•ื™ื•ืฆืจ ืื™ืฉื•ืจื™ ื’ื™ืฉื” ืœ-WordPress ื“ืจืš ืžืžืฉืง ื”-loopback:

ืงื•ื“ ืกืงืจื™ืคื˜

# Set up the WordPress database
echo " Configuring MariaDB for WordPress"
mysqladmin create wordpress || echo "Ignoring above error because database may already exist"
mysql -e "GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"localhost" IDENTIFIED BY "$WORDPRESS_DB_PASSWORD"; FLUSH PRIVILEGES;"

ื”ืชืงื ืช ืชื•ื›ื ื™ืช ื•ื•ืจื“ืคืจืก CLI

ื‘ืฉืœื‘ ื–ื” ื”ืกืงืจื™ืคื˜ ืžืชืงื™ืŸ ืืช ื”ืชื•ื›ื ื™ืช WP-CLI. ื‘ืขื–ืจืชื• ืชื•ื›ืœื• ืœื”ืชืงื™ืŸ ื•ืœื ื”ืœ ืืช ื”ื’ื“ืจื•ืช ื•ื•ืจื“ืคืจืก ืœืœื ืฆื•ืจืš ืœืขืจื•ืš ืงื‘ืฆื™ื ื‘ืื•ืคืŸ ื™ื“ื ื™, ืœืขื“ื›ืŸ ืืช ืžืกื“ ื”ื ืชื•ื ื™ื ืื• ืœื”ื™ื›ื ืก ืœืœื•ื— ื”ื‘ืงืจื”. ื ื™ืชืŸ ืœื”ืฉืชืžืฉ ื‘ื• ื’ื ืœื”ืชืงื ืช ืขืจื›ื•ืช ื ื•ืฉื ื•ืชื•ืกืคื•ืช ื•ืœืขื“ื›ื•ืŸ ื•ื•ืจื“ืคืจืก.

ืงื•ื“ ืกืงืจื™ืคื˜

if [ ! -f /usr/local/bin/wp ]; then
  # Install the WordPress CLI
  echo " Installing the WordPress CLI tool"
  curl --retry 6 -Ls "https://github.com/wp-cli/wp-cli/releases/download/v${WORDPRESS_CLI_VERSION}/wp-cli-${WORDPRESS_CLI_VERSION}.phar" > /usr/local/bin/wp
  echo "$WORDPRESS_CLI_MD5 /usr/local/bin/wp" | md5sum -c -
  chmod +x /usr/local/bin/wp
fi

ื”ืชืงื ื” ื•ื”ื’ื“ืจื” ืฉืœ ื•ื•ืจื“ืคืจืก

ื”ืกืงืจื™ืคื˜ ืžืชืงื™ืŸ ืืช ื”ื’ืจืกื” ื”ืื—ืจื•ื ื” ืฉืœ ื•ื•ืจื“ืคืจืก ื‘ืกืคืจื™ื™ื” /var/www/wordpressื•ื’ื ืžืฉื ื” ืืช ื”ื”ื’ื“ืจื•ืช:

  • ื—ื™ื‘ื•ืจ ืžืกื“ ื”ื ืชื•ื ื™ื ืขื•ื‘ื“ ืขืœ ืฉืงืข ืชื—ื•ื ืฉืœ ื™ื•ื ื™ืงืก ื‘ืžืงื•ื TCP ื‘-loopback ื›ื“ื™ ืœืฆืžืฆื ืืช ืชืขื‘ื•ืจืช TCP.
  • ื•ื•ืจื“ืคืจืก ืžื•ืกื™ืคื” ืงื™ื“ื•ืžืช https:// ืœื›ืชื•ื‘ืช ื”-URL ืื ืœืงื•ื—ื•ืช ืžืชื—ื‘ืจื™ื ืœ-NGINX ื“ืจืš HTTPS, ื•ื›ืŸ ืฉื•ืœื—ื™ื ืืช ืฉื ื”ืžืืจื— ื”ืžืจื•ื—ืง (ื›ืคื™ ืฉืกื•ืคืง ืขืœ ื™ื“ื™ NGINX) ืœ-PHP. ืื ื• ืžืฉืชืžืฉื™ื ื‘ืงื˜ืข ืงื•ื“ ื›ื“ื™ ืœื”ื’ื“ื™ืจ ื–ืืช.
  • ื•ื•ืจื“ืคืจืก ื–ืงื•ืงื” ืœ-HTTPS ื›ื“ื™ ืœื”ืชื—ื‘ืจ
  • ืžื‘ื ื” ื›ืชื•ื‘ืช ื”ืืชืจ ื”ืžื•ื’ื“ืจ ื›ื‘ืจื™ืจืช ืžื—ื“ืœ ืžื‘ื•ืกืก ืขืœ ืžืฉืื‘ื™ื
  • ืžื’ื“ื™ืจ ืืช ื”ื”ืจืฉืื•ืช ื”ื ื›ื•ื ื•ืช ื‘ืžืขืจื›ืช ื”ืงื‘ืฆื™ื ืขื‘ื•ืจ ืกืคืจื™ื™ืช ื•ื•ืจื“ืคืจืก.

ืงื•ื“ ืกืงืจื™ืคื˜

if [ ! -d /var/www/wordpress ]; then
  # Create WordPress directories
  mkdir -p /var/www/wordpress
  chown -R www-data:www-data /var/www

  # Download WordPress using the WordPress CLI
  echo " Installing WordPress"
  su -s /bin/sh -c 'wp --path=/var/www/wordpress core download' www-data

  WP_CONFIG_CREATE_CMD="wp --path=/var/www/wordpress config create --extra-php --dbname=wordpress --dbuser=wordpress --dbhost="localhost:/var/run/mysqld/mysqld.sock" --dbpass="${WORDPRESS_DB_PASSWORD}""

  # This snippet is injected into the wp-config.php file when it is created;
  # it informs WordPress that we are behind a reverse proxy and as such
  # allows it to generate links using HTTPS
  cat > /tmp/wp_forwarded_for.php << 'EOM'
/* Turn HTTPS 'on' if HTTP_X_FORWARDED_PROTO matches 'https' */
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strpos($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') !== false) {
    $_SERVER['HTTPS'] = 'on';
}
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
    $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}
EOM

  # Create WordPress configuration
  su -s /bin/sh -p -c "cat /tmp/wp_forwarded_for.php | ${WP_CONFIG_CREATE_CMD}" www-data
  rm /tmp/wp_forwarded_for.php
  su -s /bin/sh -p -c "wp --path=/var/www/wordpress config set 'FORCE_SSL_ADMIN' 'true'" www-data

  # Install WordPress
  WP_SITE_INSTALL_CMD="wp --path=/var/www/wordpress core install --url="${WORDPRESS_URL}" --title="${WORDPRESS_SITE_TITLE}" --admin_user="${WORDPRESS_ADMIN_USER}" --admin_password="${WORDPRESS_ADMIN_PASSWORD}" --admin_email="${WORDPRESS_ADMIN_EMAIL}" --skip-email"
  su -s /bin/sh -p -c "${WP_SITE_INSTALL_CMD}" www-data

  # Set permalink structure to a sensible default that isn't in the UI
  su -s /bin/sh -p -c "wp --path=/var/www/wordpress option update permalink_structure '/%year%/%monthnum%/%postname%/'" www-data

  # Remove sample file because it is cruft and could be a security problem
  rm /var/www/wordpress/wp-config-sample.php

  # Ensure that WordPress permissions are correct
  find /var/www/wordpress -type d -exec chmod g+s {} ;
  chmod g+w /var/www/wordpress/wp-content
  chmod -R g+w /var/www/wordpress/wp-content/themes
  chmod -R g+w /var/www/wordpress/wp-content/plugins
fi

ื”ื’ื“ืจืช ื™ื—ื™ื“ืช NGINX

ื”ืกืงืจื™ืคื˜ ืžื’ื“ื™ืจ ืืช ื™ื—ื™ื“ืช NGINX ืœื”ืจื™ืฅ PHP ื•ืœืขื‘ื“ ื ืชื™ื‘ื™ ื•ื•ืจื“ืคืจืก, ืžื‘ื•ื“ื“ ืืช ืžืจื—ื‘ ื”ืฉืžื•ืช ืฉืœ ืชื”ืœื™ืš PHP ื•ืื•ืคื˜ื™ืžื™ื–ืฆื™ื” ืฉืœ ื”ื’ื“ืจื•ืช ื”ื‘ื™ืฆื•ืขื™ื. ื™ืฉื ืŸ ืฉืœื•ืฉ ืชื›ื•ื ื•ืช ืฉื›ื“ืื™ ืœื”ืงืคื™ื“ ืขืœื™ื”ืŸ ื›ืืŸ:

  • ื”ืชืžื™ื›ื” ื‘ืžืจื—ื‘ื™ ืฉืžื•ืช ื ืงื‘ืขืช ืœืคื™ ืชื ืื™, ืขืœ ืกืžืš ื‘ื“ื™ืงื” ืฉื”ืกืงืจื™ืคื˜ ืคื•ืขืœ ื‘ืžื™ื›ืœ. ื–ื” ื”ื›ืจื—ื™ ืžื›ื™ื•ื•ืŸ ืฉืจื•ื‘ ื”ื’ื“ืจื•ืช ื”ืžื›ื•ืœื” ืื™ื ืŸ ืชื•ืžื›ื•ืช ื‘ื”ืฉืงื” ืžืงื•ื ื ืช ืฉืœ ืงื•ื ื˜ื™ื™ื ืจื™ื.
  • ืื ื™ืฉ ืชืžื™ื›ื” ื‘ืžืจื—ื‘ื™ ืฉืžื•ืช, ื”ืฉื‘ืช ืืช ืžืจื—ื‘ ื”ืฉืžื•ืช ืจืฉืช. ื–ืืช ื›ื“ื™ ืœืืคืฉืจ ืœื•ื•ืจื“ืคืจืก ืœื”ืชื—ื‘ืจ ืœืฉืชื™ ื ืงื•ื“ื•ืช ื”ืงืฆื” ื•ืœื”ื™ื•ืช ื–ืžื™ื ื” ื‘ืื™ื ื˜ืจื ื˜ ื‘ื• ื–ืžื ื™ืช.
  • ื”ืžืกืคืจ ื”ืžืจื‘ื™ ืฉืœ ืชื”ืœื™ื›ื™ื ื ืงื‘ืข ื‘ืื•ืคืŸ ื”ื‘ื: (ื–ื™ื›ืจื•ืŸ ื–ืžื™ืŸ ืœื”ืคืขืœืช MariaDB ื•-NGINX Uniy)/(ื”ื’ื‘ืœืช RAM ื‘-PHP + 5)
    ืขืจืš ื–ื” ืžื•ื’ื“ืจ ื‘ื”ื’ื“ืจื•ืช ื™ื—ื™ื“ืช NGINX.

ื”ืขืจืš ื”ื–ื” ื’ื ืžืจืžื– ืฉืชืžื™ื“ ืคื•ืขืœื™ื ืœืคื—ื•ืช ืฉื ื™ ืชื”ืœื™ื›ื™ PHP, ื•ื–ื” ื—ืฉื•ื‘ ื›ื™ ื•ื•ืจื“ืคืจืก ืขื•ืฉื” ื”ืจื‘ื” ื‘ืงืฉื•ืช ืืกื™ื ื›ืจื•ื ื™ื•ืช ืœืขืฆืžื”, ื•ืœืœื ืชื”ืœื™ื›ื™ื ื ื•ืกืคื™ื ืฉืจืฆื™ื, ืœืžืฉืœ, WP-Cron ื™ื™ืฉื‘ืจ. ื™ื™ืชื›ืŸ ืฉืชืจืฆื” ืœื”ื’ื“ื™ืœ ืื• ืœื”ืงื˜ื™ืŸ ืžื’ื‘ืœื•ืช ืืœื” ืขืœ ืกืžืš ื”ื”ื’ื“ืจื•ืช ื”ืžืงื•ืžื™ื•ืช ืฉืœืš, ืžื›ื™ื•ื•ืŸ ืฉื”ื”ื’ื“ืจื•ืช ืฉื ื•ืฆืจื• ื›ืืŸ ื”ืŸ ืฉืžืจื ื™ื•ืช. ื‘ืจื•ื‘ ืžืขืจื›ื•ืช ื”ื™ื™ืฆื•ืจ ื”ื”ื’ื“ืจื•ืช ื”ืŸ ื‘ื™ืŸ 10 ืœ-100.

ืงื•ื“ ืกืงืจื™ืคื˜

if [ "${container:-unknown}" != "lxc" ] && [ "$(grep -m1 -a container=lxc /proc/1/environ | tr -d '')" == "" ]; then
  NAMESPACES='"namespaces": {
        "cgroup": true,
        "credential": true,
        "mount": true,
        "network": false,
        "pid": true,
        "uname": true
    }'
else
  NAMESPACES='"namespaces": {}'
fi

PHP_MEM_LIMIT="$(grep 'memory_limit' /etc/php/7.4/embed/php.ini | tr -d ' ' | cut -f2 -d= | numfmt --from=iec)"
AVAIL_MEM="$(grep MemAvailable /proc/meminfo | tr -d ' kB' | cut -f2 -d: | numfmt --from-unit=K)"
MAX_PHP_PROCESSES="$(echo "${AVAIL_MEM}/${PHP_MEM_LIMIT}+5" | bc)"
echo " Calculated the maximum number of PHP processes as ${MAX_PHP_PROCESSES}. You may want to tune this value due to variations in your configuration. It is not unusual to see values between 10-100 in production configurations."

echo " Configuring NGINX Unit to use PHP and WordPress"
cat > /tmp/wordpress.json << EOM
{
  "settings": {
    "http": {
      "header_read_timeout": 30,
      "body_read_timeout": 30,
      "send_timeout": 30,
      "idle_timeout": 180,
      "max_body_size": $(numfmt --from=iec ${UPLOAD_MAX_FILESIZE})
    }
  },
  "listeners": {
    "127.0.0.1:8080": {
      "pass": "routes/wordpress"
    }
  },
  "routes": {
    "wordpress": [
      {
        "match": {
          "uri": [
            "*.php",
            "*.php/*",
            "/wp-admin/"
          ]
        },
        "action": {
          "pass": "applications/wordpress/direct"
        }
      },
      {
        "action": {
          "share": "/var/www/wordpress",
          "fallback": {
            "pass": "applications/wordpress/index"
          }
        }
      }
    ]
  },
  "applications": {
    "wordpress": {
      "type": "php",
      "user": "www-data",
      "group": "www-data",
      "processes": {
        "max": ${MAX_PHP_PROCESSES},
        "spare": 1
      },
      "isolation": {
        ${NAMESPACES}
      },
      "targets": {
        "direct": {
          "root": "/var/www/wordpress/"
        },
        "index": {
          "root": "/var/www/wordpress/",
          "script": "index.php"
        }
      }
    }
  }
}
EOM

curl -X PUT --data-binary @/tmp/wordpress.json --unix-socket /run/control.unit.sock http://localhost/config

ื”ื’ื“ืจืช NGINX

ื”ื’ื“ืจืช ื”ื’ื“ืจื•ืช NGINX ื‘ืกื™ืกื™ื•ืช

ื”ืกืงืจื™ืคื˜ ื™ื•ืฆืจ ืกืคืจื™ื™ื” ืขื‘ื•ืจ ืžื˜ืžื•ืŸ NGINX ื•ืœืื—ืจ ืžื›ืŸ ื™ื•ืฆืจ ืืช ืงื•ื‘ืฅ ื”ืชืฆื•ืจื” ื”ืจืืฉื™ nginx.conf. ืฉื™ืžื• ืœื‘ ืœืžืกืคืจ ืชื”ืœื™ื›ื™ ื”ืžื˜ืคืœ ื•ืœื”ื’ื“ืจืช ื’ื•ื“ืœ ื”ืงื•ื‘ืฅ ื”ืžืจื‘ื™ ืœื”ืขืœืื”. ื™ืฉื ื” ื’ื ืฉื•ืจื” ื”ื›ื•ืœืœืช ืืช ืงื•ื‘ืฅ ื”ื’ื“ืจื•ืช ื”ื“ื—ื™ืกื” ืฉื”ื•ื’ื“ืจ ื‘ืกืขื™ืฃ ื”ื‘ื, ื•ืื—ืจื™ื• ืืช ื”ื’ื“ืจื•ืช ื”ืžื˜ืžื•ืŸ.

ืงื•ื“ ืกืงืจื™ืคื˜

# Make directory for NGINX cache
mkdir -p /var/cache/nginx/proxy

echo " Configuring NGINX"
cat > ${NGINX_CONF_DIR}/nginx.conf << EOM
user nginx;
worker_processes auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       ${NGINX_CONF_DIR}/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    client_max_body_size ${UPLOAD_MAX_FILESIZE};
    keepalive_timeout  65;
    # gzip settings
    include ${NGINX_CONF_DIR}/gzip_compression.conf;
    # Cache settings
    proxy_cache_path /var/cache/nginx/proxy
        levels=1:2
        keys_zone=wp_cache:10m
        max_size=10g
        inactive=60m
        use_temp_path=off;
    include ${NGINX_CONF_DIR}/conf.d/*.conf;
}
EOM

ื”ื’ื“ืจืช ื“ื—ื™ืกืช NGINX

ื“ื—ื™ืกืช ืชื•ื›ืŸ ืชื•ืš ื›ื“ื™ ืชื ื•ืขื” ืœืคื ื™ ืฉืœื™ื—ืชื• ืœืœืงื•ื—ื•ืช ื”ื™ื ื“ืจืš ืžืฆื•ื™ื ืช ืœืฉืคืจ ืืช ื‘ื™ืฆื•ืขื™ ื”ืืชืจ, ืืš ืจืง ืื ื”ื“ื—ื™ืกื” ืžื•ื’ื“ืจืช ื›ื”ืœื›ื”. ื—ืœืง ื–ื” ืฉืœ ื”ืกืงืจื™ืคื˜ ืžื‘ื•ืกืก ืขืœ ื”ื’ื“ืจื•ืช ืžื›ืืŸ.

ืงื•ื“ ืกืงืจื™ืคื˜

cat > ${NGINX_CONF_DIR}/gzip_compression.conf << 'EOM'
# Credit: https://github.com/h5bp/server-configs-nginx/
# ----------------------------------------------------------------------
# | Compression                                                        |
# ----------------------------------------------------------------------
# https://nginx.org/en/docs/http/ngx_http_gzip_module.html
# Enable gzip compression.
# Default: off
gzip on;
# Compression level (1-9).
# 5 is a perfect compromise between size and CPU usage, offering about 75%
# reduction for most ASCII files (almost identical to level 9).
# Default: 1
gzip_comp_level 6;
# Don't compress anything that's already small and unlikely to shrink much if at
# all (the default is 20 bytes, which is bad as that usually leads to larger
# files after gzipping).
# Default: 20
gzip_min_length 256;
# Compress data even for clients that are connecting to us via proxies,
# identified by the "Via" header (required for CloudFront).
# Default: off
gzip_proxied any;
# Tell proxies to cache both the gzipped and regular version of a resource
# whenever the client's Accept-Encoding capabilities header varies;
# Avoids the issue where a non-gzip capable client (which is extremely rare
# today) would display gibberish if their proxy gave them the gzipped version.
# Default: off
gzip_vary on;
# Compress all output labeled with one of the following MIME-types.
# `text/html` is always compressed by gzip module.
# Default: text/html
gzip_types
  application/atom+xml
  application/geo+json
  application/javascript
  application/x-javascript
  application/json
  application/ld+json
  application/manifest+json
  application/rdf+xml
  application/rss+xml
  application/vnd.ms-fontobject
  application/wasm
  application/x-web-app-manifest+json
  application/xhtml+xml
  application/xml
  font/eot
  font/otf
  font/ttf
  image/bmp
  image/svg+xml
  text/cache-manifest
  text/calendar
  text/css
  text/javascript
  text/markdown
  text/plain
  text/xml
  text/vcard
  text/vnd.rim.location.xloc
  text/vtt
  text/x-component
  text/x-cross-domain-policy;
EOM

ื”ื’ื“ืจืช NGINX ืขื‘ื•ืจ ื•ื•ืจื“ืคืจืก

ืœืื—ืจ ืžื›ืŸ, ื”ืกืงืจื™ืคื˜ ื™ื•ืฆืจ ืงื•ื‘ืฅ ืชืฆื•ืจื” ืขื‘ื•ืจ ื•ื•ืจื“ืคืจืก default.conf ื‘ืงื˜ืœื•ื’ conf.d. ื–ื” ืžื•ื’ื“ืจ ื›ืืŸ:

  • ื”ืคืขืœืช ืชืขื•ื“ื•ืช TLS ืฉื”ืชืงื‘ืœื• ืž-Let's Encrypt ื‘ืืžืฆืขื•ืช Certbot (ื”ื’ื“ืจืช ื”ืชืฆื•ืจื” ืชื”ื™ื” ื‘ืกืขื™ืฃ ื”ื‘ื)
  • ื”ื’ื“ืจืช ื”ื’ื“ืจื•ืช ืื‘ื˜ื—ื” ืฉืœ TLS ืขืœ ืกืžืš ื”ืžืœืฆื•ืช ืฉืœ Let's Encrypt
  • ืืคืฉืจ ื›ื‘ืจื™ืจืช ืžื—ื“ืœ ื‘ืงืฉื•ืช ื“ื™ืœื•ื’ ืœืื—ืกื•ืŸ ื‘ืžื˜ืžื•ืŸ ืœืžืฉืš ืฉืขื” ืื—ืช
  • ื”ืฉื‘ืช ืืช ืจื™ืฉื•ื ื”ื’ื™ืฉื”, ื›ืžื• ื’ื ืจื™ืฉื•ื ืฉื’ื™ืื•ืช ืื ื”ืงื•ื‘ืฅ ืœื ื ืžืฆื, ืขื‘ื•ืจ ืฉื ื™ ืงื‘ืฆื™ื ืžื‘ื•ืงืฉื™ื ื ืคื•ืฆื™ื: favicon.ico ื•-robots.txt
  • ื“ื—ื™ื™ืช ื’ื™ืฉื” ืœืงื‘ืฆื™ื ืžื•ืกืชืจื™ื ื•ืœื—ืœืง ืžื”ืงื‘ืฆื™ื . Phpื›ื“ื™ ืœืžื ื•ืข ื’ื™ืฉื” ืœื ื—ื•ืงื™ืช ืื• ื”ืชื—ืœื” ืœื ืžื›ื•ื•ื ืช
  • ื”ืฉื‘ืช ืืช ืจื™ืฉื•ื ื”ื’ื™ืฉื” ืขื‘ื•ืจ ืงื‘ืฆื™ ืกื˜ื˜ื™ ื•ื’ื•ืคืŸ
  • ื”ื’ื“ืจืช ื›ื•ืชืจืช ื‘ืงืจืช ื’ื™ืฉื”-ืืคืฉืจ-ืžืงื•ืจ ืขื‘ื•ืจ ืงื‘ืฆื™ ื’ื•ืคื ื™ื
  • ื”ื•ืกืคืช ื ื™ืชื•ื‘ ืขื‘ื•ืจ index.php ื•ืกื˜ื˜ื™ื ืื—ืจื™ื.

ืงื•ื“ ืกืงืจื™ืคื˜

cat > ${NGINX_CONF_DIR}/conf.d/default.conf << EOM
upstream unit_php_upstream {
    server 127.0.0.1:8080;
    keepalive 32;
}
server {
    listen 80;
    listen [::]:80;
    # ACME-challenge used by Certbot for Let's Encrypt
    location ^~ /.well-known/acme-challenge/ {
      root /var/www/certbot;
    }
    location / {
      return 301 https://${TLS_HOSTNAME}$request_uri;
    }
}
server {
    listen      443 ssl http2;
    listen [::]:443 ssl http2;
    server_name ${TLS_HOSTNAME};
    root        /var/www/wordpress/;
    # Let's Encrypt configuration
    ssl_certificate         ${CERT_DIR}/fullchain.pem;
    ssl_certificate_key     ${CERT_DIR}/privkey.pem;
    ssl_trusted_certificate ${CERT_DIR}/chain.pem;
    include ${NGINX_CONF_DIR}/options-ssl-nginx.conf;
    ssl_dhparam ${NGINX_CONF_DIR}/ssl-dhparams.pem;
    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    # Proxy caching
    proxy_cache wp_cache;
    proxy_cache_valid 200 302 1h;
    proxy_cache_valid 404 1m;
    proxy_cache_revalidate on;
    proxy_cache_background_update on;
    proxy_cache_lock on;
    proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    # Deny all attempts to access hidden files such as .htaccess, .htpasswd,
    # .DS_Store (Mac)
    # Keep logging the requests to parse later (or to pass to firewall utilities
    # such as fail2ban)
    location ~ /. {
        deny all;
    }
    # Deny access to any files with a .php extension in the uploads directory;
    # works in subdirectory installs and also in multi-site network.
    # Keep logging the requests to parse later (or to pass to firewall utilities
    # such as fail2ban).
    location ~* /(?:uploads|files)/.*.php$ {
        deny all;
    }
    # WordPress: deny access to wp-content, wp-includes PHP files
    location ~* ^/(?:wp-content|wp-includes)/.*.php$ {
        deny all;
    }
    # Deny public access to wp-config.php
    location ~* wp-config.php {
        deny all;
    }
    # Do not log access for static assets, media
    location ~* .(?:css(.map)?|js(.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
        access_log off;
    }
    location ~* .(?:svgz?|ttf|ttc|otf|eot|woff2?)$ {
        add_header Access-Control-Allow-Origin "*";
        access_log off;
    }
    location / {
        try_files $uri @index_php;
    }
    location @index_php {
        proxy_socket_keepalive on;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_pass       http://unit_php_upstream;
    }
    location ~* .php$ {
        proxy_socket_keepalive on;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        try_files        $uri =404;
        proxy_pass       http://unit_php_upstream;
    }
}
EOM

ื”ื’ื“ืจืช Certbot ืœืื™ืฉื•ืจื™ื ืž-Let's Encrypt ื•ื—ื™ื“ื•ืฉ ืื•ื˜ื•ืžื˜ื™ ืฉืœื”ื

ืกืจื‘ื•ื˜ ื”ื•ื ื›ืœื™ ื—ื™ื ืžื™ ืฉืœ Electronic Frontier Foundation (EFF) ื”ืžืืคืฉืจ ืœืš ืœื”ืฉื™ื’ ื•ืœื—ื“ืฉ ืื•ื˜ื•ืžื˜ื™ืช ืชืขื•ื“ื•ืช TLS ืž-Let's Encrypt. ื”ืกืงืจื™ืคื˜ ืขื•ืฉื” ืืช ื”ืคืขื•ืœื•ืช ื”ื‘ืื•ืช ื›ื“ื™ ืœื”ื’ื“ื™ืจ ืืช Certbot ืœืขื‘ื“ ืื™ืฉื•ืจื™ื ืž-Let's Encrypt ื‘-NGINX:

  • ืขื•ืฆืจ ืืช NGINX
  • ื”ื•ืจื“ื” ืฉืœ ื”ื’ื“ืจื•ืช TLS ืžื•ืžืœืฆื•ืช
  • ืžืคืขื™ืœ ืืช Certbot ื›ื“ื™ ืœืงื‘ืœ ืื™ืฉื•ืจื™ื ืœืืชืจ
  • ืžืคืขื™ืœ ืžื—ื“ืฉ ืืช NGINX ื›ื“ื™ ืœื”ืฉืชืžืฉ ื‘ืื™ืฉื•ืจื™ื
  • ืžื’ื“ื™ืจ ืืช Certbot ืœืคืขื•ืœ ืžื“ื™ ื™ื•ื ื‘ืฉืขื” 3:24 ืœืคื ื•ืช ื‘ื•ืงืจ ื›ื“ื™ ืœื‘ื“ื•ืง ืื ื™ืฉ ื—ื™ื“ื•ืฉื™ ืื™ืฉื•ืจื™ื ื•ื‘ืžื™ื“ืช ื”ืฆื•ืจืš, ืœื”ื•ืจื™ื“ ืื™ืฉื•ืจื™ื ื—ื“ืฉื™ื ื•ืœื”ืคืขื™ืœ ืžื—ื“ืฉ ืืช NGINX.

ืงื•ื“ ืกืงืจื™ืคื˜

echo " Stopping NGINX in order to set up Let's Encrypt"
service nginx stop

mkdir -p /var/www/certbot
chown www-data:www-data /var/www/certbot
chmod g+s /var/www/certbot

if [ ! -f ${NGINX_CONF_DIR}/options-ssl-nginx.conf ]; then
  echo " Downloading recommended TLS parameters"
  curl --retry 6 -Ls -z "Tue, 14 Apr 2020 16:36:07 GMT" 
    -o "${NGINX_CONF_DIR}/options-ssl-nginx.conf" 
    "https://raw.githubusercontent.com/certbot/certbot/master/certbot-nginx/certbot_nginx/_internal/tls_configs/options-ssl-nginx.conf" 
    || echo "Couldn't download latest options-ssl-nginx.conf"
fi

if [ ! -f ${NGINX_CONF_DIR}/ssl-dhparams.pem ]; then
  echo " Downloading recommended TLS DH parameters"
  curl --retry 6 -Ls -z "Tue, 14 Apr 2020 16:49:18 GMT" 
    -o "${NGINX_CONF_DIR}/ssl-dhparams.pem" 
    "https://raw.githubusercontent.com/certbot/certbot/master/certbot/certbot/ssl-dhparams.pem" 
    || echo "Couldn't download latest ssl-dhparams.pem"
fi

# If tls_certs_init.sh hasn't been run before, remove the self-signed certs
if [ ! -d "/etc/letsencrypt/accounts" ]; then
  echo " Removing self-signed certificates"
  rm -rf "${CERT_DIR}"
fi

if [ "" = "${LETS_ENCRYPT_STAGING:-}" ] || [ "0" = "${LETS_ENCRYPT_STAGING}" ]; then
  CERTBOT_STAGING_FLAG=""
else
  CERTBOT_STAGING_FLAG="--staging"
fi

if [ ! -f "${CERT_DIR}/fullchain.pem" ]; then
  echo " Generating certificates with Let's Encrypt"
  certbot certonly --standalone 
         -m "${WORDPRESS_ADMIN_EMAIL}" 
         ${CERTBOT_STAGING_FLAG} 
         --agree-tos --force-renewal --non-interactive 
         -d "${TLS_HOSTNAME}"
fi

echo " Starting NGINX in order to use new configuration"
service nginx start

# Write crontab for periodic Let's Encrypt cert renewal
if [ "$(crontab -l | grep -m1 'certbot renew')" == "" ]; then
  echo " Adding certbot to crontab for automatic Let's Encrypt renewal"
  (crontab -l 2>/dev/null; echo "24 3 * * * certbot renew --nginx --post-hook 'service nginx reload'") | crontab -
fi

ื”ืชืืžื” ืื™ืฉื™ืช ื ื•ืกืคืช ืฉืœ ื”ืืชืจ ืฉืœืš

ื“ื™ื‘ืจื ื• ืœืžืขืœื” ืขืœ ื”ืื•ืคืŸ ืฉื‘ื• ื”ืกืงืจื™ืคื˜ ืฉืœื ื• ืžื’ื“ื™ืจ ืืช NGINX ื•-NGINX Unit ืœืฉืจืช ืืชืจ ืžื•ื›ืŸ ืœื™ื™ืฆื•ืจ ืขื TLSSSL ืžื•ืคืขืœ. ืชื•ื›ืœ ื’ื, ื‘ื”ืชืื ืœืฆืจื›ื™ื ืฉืœืš, ืœื”ื•ืกื™ืฃ ื‘ืขืชื™ื“:

ืœื‘ื™ืฆื•ืขื™ ื”ืืชืจ ื˜ื•ื‘ื™ื ืขื•ื“ ื™ื•ืชืจ, ืื ื• ืžืžืœื™ืฆื™ื ืœืฉื“ืจื’ ืœ NGINX Plus, ื”ืžื•ืฆืจ ื”ืžืกื—ืจื™ ืฉืœื ื• ื‘ืจืžื” ื”ืืจื’ื•ื ื™ืช ื”ืžื‘ื•ืกืก ืขืœ ืงื•ื“ ืคืชื•ื— NGINX. ื”ืžื ื•ื™ื™ื ืฉืœื• ื™ืงื‘ืœื• ืžื•ื“ื•ืœ Brotli ื ื˜ืขืŸ ื‘ืื•ืคืŸ ื“ื™ื ืžื™, ื•ื›ืŸ (ื‘ืชืฉืœื•ื ื ื•ืกืฃ) NGINX ModSecurity WAF. ืื ื—ื ื• ืžืฆื™ืขื™ื ื’ื NGINX App Protect, ืžื•ื“ื•ืœ WAF ืขื‘ื•ืจ NGINX Plus ื”ืžื‘ื•ืกืก ืขืœ ื˜ื›ื ื•ืœื•ื’ื™ื™ืช ืื‘ื˜ื—ื” ืžื•ื‘ื™ืœื” ื‘ืชืขืฉื™ื™ื” ืžื‘ื™ืช F5.

ื .ื‘. ืœืชืžื™ื›ื” ื‘ืืชืจ ื˜ืขื•ืŸ ืžืื•ื“, ืืชื” ื™ื›ื•ืœ ืœื™ืฆื•ืจ ืงืฉืจ ืขื ื”ืžื•ืžื—ื™ื ืกืื•ืช'ื‘ืจื™ื“ื’ '. ืื ื• ื ื‘ื˜ื™ื— ืชืคืขื•ืœ ืžื”ื™ืจ ื•ืืžื™ืŸ ืฉืœ ื”ืืชืจ ืื• ื”ืฉื™ืจื•ืช ืฉืœืš ื‘ื›ืœ ืขื•ืžืก.

ืžืงื•ืจ: www.habr.com