NGINX ඒකකය සහ උබුන්ටු සමඟින් වර්ඩ්ප්‍රෙස් ස්ථාපනය ස්වයංක්‍රීය කිරීම

NGINX ඒකකය සහ උබුන්ටු සමඟින් වර්ඩ්ප්‍රෙස් ස්ථාපනය ස්වයංක්‍රීය කිරීම

වර්ඩ්ප්‍රෙස් ස්ථාපනය කිරීමේදී බොහෝ කරුණු තිබේ; “වර්ඩ්ප්‍රෙස් ස්ථාපනය” සඳහා ගූගල් සෙවුමක් ප්‍රතිඵල මිලියන භාගයක් පමණ ලබා දෙනු ඇත. කෙසේ වෙතත්, වර්ඩ්ප්‍රෙස් සහ යටින් පවතින මෙහෙයුම් පද්ධතිය ස්ථාපනය කිරීමට සහ වින්‍යාස කිරීමට ඔබට උපකාර කළ හැකි ප්‍රයෝජනවත් මාර්ගෝපදේශ ඉතා ස්වල්පයක් ඇත, එවිට ඒවා දිගු කාලයක් සඳහා සහාය විය හැකිය. සමහර විට නිවැරදි සැකසුම් ඔබගේ නිශ්චිත අවශ්‍යතා මත දැඩි ලෙස රඳා පවතී, නැතහොත් සවිස්තරාත්මක පැහැදිලි කිරීම ලිපිය කියවීමට අපහසු වන නිසා විය හැක.

මෙම ලිපියෙන්, අපි උබුන්ටු මත වර්ඩ්ප්‍රෙස් ස්වයංක්‍රීයව ස්ථාපනය කිරීම සඳහා බාෂ් ස්ක්‍රිප්ට් එකක් ලබා දීමෙන් දෙලොවෙහිම හොඳම දේ එකට එකතු කිරීමට උත්සාහ කරන්නෙමු, සහ අපි ඒ හරහා ගමන් කරන්නෙමු, එක් එක් කොටස කරන්නේ කුමක්ද සහ අපි සැලසුම් කිරීමේදී කළ ගනුදෙනු විස්තර කරමු. එය. ඔබ පළපුරුදු පරිශීලකයෙක් නම්, ඔබට ලිපියේ පෙළ මඟ හැරිය හැක පිටපත ගන්න ඔබගේ පරිසරයේ වෙනස් කිරීම සහ භාවිතය සඳහා. ස්ක්‍රිප්ට් ප්‍රතිදානය යනු NGINX ඒකකයේ ක්‍රියාත්මක වන සහ කාර්මික භාවිතය සඳහා යෝග්‍ය වන Lets Encrypt සහාය සහිත අභිරුචි වර්ඩ්ප්‍රෙස් ස්ථාපනයකි.

NGINX ඒකකය භාවිතයෙන් WordPress යෙදවීම සඳහා සංවර්ධිත ගෘහ නිර්මාණ ශිල්පය විස්තර කෙරේ පැරණි ලිපිය, අපි දැන් එහි ආවරණය නොවූ දේවල් (වෙනත් බොහෝ නිබන්ධනවල මෙන්) තවදුරටත් වින්‍යාස කරන්නෙමු:

  • WordPress CLI
  • අපි සංකේතනය කරමු සහ TLSSSL සහතික
  • ස්වයංක්‍රීය සහතික අලුත් කිරීම
  • NGINX හැඹිලිගත කිරීම
  • NGINX සම්පීඩනය
  • HTTPS සහ HTTP/2 සහාය
  • ක්රියාවලි ස්වයංක්රීයකරණය

ලිපිය එක් සේවාදායකයක් මත ස්ථාපනය විස්තර කරනු ඇත, එය ස්ථිතික සැකසුම් සේවාදායකයක්, PHP සැකසුම් සේවාදායකයක් සහ දත්ත සමුදායක් එකවර සත්කාරකත්වය සපයනු ඇත. බහු අතථ්‍ය ධාරක සහ සේවා සඳහා සහය ඇතිව ස්ථාපනය කිරීම අනාගතය සඳහා විභව මාතෘකාවකි. මේ ලිපිවල නැති දෙයක් ගැන අපි ලියන්න කැමති නම් කමෙන්ට් එකේ ලියන්න.

අවශ්යතා

  • සේවාදායක බහාලුම් (එල්එක්ස් හෝ Lxd), අතථ්‍ය යන්ත්‍රයක් හෝ සාමාන්‍ය දෘඪාංග සේවාදායකයක්, අවම වශයෙන් 512MB RAM සහ Ubuntu 18.04 හෝ ඊට වඩා මෑතදී ස්ථාපනය කර ඇත.
  • අන්තර්ජාලයට පිවිසිය හැකි වරායන් 80 සහ 443
  • මෙම සේවාදායකයේ පොදු IP ලිපිනය හා සම්බන්ධ ඩොමේන් නාමය
  • මූල අයිතිවාසිකම් සමඟ ප්රවේශය (sudo).

ගෘහ නිර්මාණ දළ විශ්ලේෂණය

ගෘහ නිර්මාණ ශිල්පය විස්තර කර ඇති ආකාරයටම වේ කලින්, තට්ටු තුනේ වෙබ් යෙදුමකි. එය PHP එන්ජිම මත ක්‍රියාත්මක කරන ලද PHP ස්ක්‍රිප්ට් සහ වෙබ් සේවාදායකය විසින් සකසන ලද ස්ථිතික ගොනු වලින් සමන්විත වේ.

NGINX ඒකකය සහ උබුන්ටු සමඟින් වර්ඩ්ප්‍රෙස් ස්ථාපනය ස්වයංක්‍රීය කිරීම

පොදු මූලධර්ම

  • ස්ක්‍රිප්ට් එකක ඇති බොහෝ වින්‍යාස විධාන IDempotency සඳහා කොන්දේසි තිබේ නම්: දැනටමත් සූදානම්ව ඇති සැකසුම් වෙනස් කිරීමේ අවදානමකින් තොරව ස්ක්‍රිප්ට් කිහිප වතාවක් ධාවනය කළ හැක.
  • ස්ක්‍රිප්ට් නිධි වලින් මෘදුකාංග ස්ථාපනය කිරීමට උත්සාහ කරයි, එබැවින් ඔබට එක් විධානයකින් පද්ධති යාවත්කාලීන යෙදිය හැක (apt upgrade Ubuntu සඳහා).
  • කණ්ඩායම් ඔවුන් කන්ටේනරයක ධාවනය වන බව හඳුනා ගැනීමට උත්සාහ කරන අතර ඒ අනුව ඔවුන්ට ඔවුන්ගේ සැකසුම් වෙනස් කළ හැකිය.
  • සැකසීම් තුළ දියත් කළ යුතු නූල් ක්‍රියාවලි ගණන සැකසීමට, ස්ක්‍රිප්ට් බහාලුම්, අතථ්‍ය යන්ත්‍ර සහ දෘඪාංග සේවාදායකයන්හි වැඩ කිරීම සඳහා ස්වයංක්‍රීය සැකසුම් අනුමාන කිරීමට උත්සාහ කරයි.
  • සැකසුම් විස්තර කරන විට, අපි සෑම විටම ස්වයංක්‍රීයකරණය ගැන මුලින්ම සිතමු, එය ඔබගේම යටිතල පහසුකම් කේතයක් ලෙස නිර්මාණය කිරීමේ පදනම බවට පත්වනු ඇතැයි අපි බලාපොරොත්තු වෙමු.
  • සියලුම විධාන පරිශීලකයාගෙන් ක්‍රියාත්මක වේ මූල, ඔවුන් මූලික පද්ධති සැකසුම් වෙනස් කරන නිසා, නමුත් වර්ඩ්ප්‍රෙස් සාමාන්‍ය පරිශීලකයෙකු ලෙස ක්‍රියාත්මක වේ.

පරිසර විචල්‍යයන් සැකසීම

ස්ක්‍රිප්ට් ධාවනය කිරීමට පෙර පහත පරිසර විචල්‍යයන් සකසන්න:

  • WORDPRESS_DB_PASSWORD — WordPress දත්ත සමුදා මුරපදය
  • WORDPRESS_ADMIN_USER - වර්ඩ්ප්‍රෙස් පරිපාලක පරිශීලක නාමය
  • WORDPRESS_ADMIN_PASSWORD - WordPress පරිපාලක මුරපදය
  • WORDPRESS_ADMIN_EMAIL - වර්ඩ්ප්‍රෙස් පරිපාලක විද්‍යුත් තැපෑල
  • WORDPRESS_URL - වර්ඩ්ප්‍රෙස් අඩවියේ සම්පූර්ණ URL, පටන් ගන්න https://.
  • LETS_ENCRYPT_STAGING — පෙරනිමියෙන් හිස්, නමුත් අගය 1 ට සැකසීමෙන්, ඔබ Let's Encrypt's ස්ටේජින් සර්වර් භාවිතා කරනු ඇත, ඒවා ඔබගේ සිටුවම් පරීක්ෂා කිරීමේදී නිතර සහතික ඉල්ලීමට අවශ්‍ය වේ, එසේ නොමැතිනම් අපි විශාල ඉල්ලීම් සංඛ්‍යාවක් හේතුවෙන් ඔබගේ IP ලිපිනය තාවකාලිකව අවහිර කළ හැක.

මෙම වර්ඩ්ප්‍රෙස් සම්බන්ධ විචල්‍යයන් සකසා ඇති බව ස්ක්‍රිප්ට් පරීක්‍ෂා කර ඒවා නොමැති නම් පිටවෙයි.
ස්ක්‍රිප්ට් රේඛා 572-576 අගය පරීක්ෂා කරන්න LETS_ENCRYPT_STAGING.

ව්යුත්පන්න පරිසර විචල්යයන් සැකසීම

පේළි 55-61 මත ඇති ස්ක්‍රිප්ට් පහත පරිසර විචල්‍යයන් සකසයි, එක්කෝ දෘඪ-කේත කළ අගයකට හෝ පෙර කොටසේ සකසන ලද විචල්‍ය වලින් ලබාගත් අගයක් භාවිතා කරයි:

  • DEBIAN_FRONTEND="noninteractive" — ඒවා ස්ක්‍රිප්ට් එකක ක්‍රියාත්මක වන බවත් පරිශීලක අන්තර්ක්‍රියා කිරීමේ හැකියාවක් නොමැති බවත් යෙදුම් වලට කියයි.
  • WORDPRESS_CLI_VERSION="2.4.0" - යෙදුමේ WordPress CLI අනුවාදය.
  • WORDPRESS_CLI_MD5= "dedd5a662b80cda66e9e25d44c23b25c" — WordPress CLI 2.4.0 ක්‍රියාත්මක කළ හැකි ගොනුවේ චෙක්සම් (අනුවාදය විචල්‍යයේ දක්වා ඇත WORDPRESS_CLI_VERSION) 162 පේළියේ ඇති ස්ක්‍රිප්ටය නිවැරදි වර්ඩ්ප්‍රෙස් CLI ගොනුව බාගත කර ඇති බව තහවුරු කිරීමට මෙම අගය භාවිතා කරයි.
  • UPLOAD_MAX_FILESIZE="16M" — WordPress වෙත උඩුගත කළ හැකි උපරිම ගොනු ප්‍රමාණය. මෙම සැකසුම ස්ථාන කිහිපයක භාවිතා වේ, එබැවින් එය එක් ස්ථානයක තැබීම පහසුය.
  • TLS_HOSTNAME= "$(echo ${WORDPRESS_URL} | cut -d'/' -f3)" — පද්ධති සත්කාරක නාමය, WORDPRESS_URL විචල්‍යයෙන් උපුටා ගන්නා ලදී. Let's Encrypt වෙතින් සුදුසු TLS/SSL සහතික ලබා ගැනීමට මෙන්ම අභ්‍යන්තර වර්ඩ්ප්‍රෙස් සත්‍යාපනය සඳහා භාවිතා කරයි.
  • NGINX_CONF_DIR="/etc/nginx" - ප්‍රධාන ගොනුව ඇතුළුව NGINX සිටුවම් සහිත ඩිරෙක්ටරියට යන මාර්ගය nginx.conf.
  • CERT_DIR="/etc/letsencrypt/live/${TLS_HOSTNAME}" — විචල්‍යයෙන් ලබාගත් WordPress වෙබ් අඩවිය සඳහා සහතික සංකේතනය කරමු 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 ආවර්තිතා කාර්යයන් ක්‍රියාත්මක කිරීමට භාවිතා කරයි, වර්ඩ්ප්‍රෙස් HTTP හරහා ප්‍රවේශ වීමට අවශ්‍ය වේ. WP-Cron සියලු පරිසරයන් තුළ නිවැරදිව ක්‍රියා කරන බවට වග බලා ගැනීම සඳහා, ස්ක්‍රිප්ට් ගොනුවට රේඛාවක් එක් කරයි / Etc / hostsවර්ඩ්ප්‍රෙස් හට loopback අතුරුමුහුණත හරහා ප්‍රවේශ විය හැකි පරිදි:

ස්ක්‍රිප්ට් කේතය

# 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 ගබඩාවලින් NGINX ඒකකය සහ විවෘත මූලාශ්‍ර NGINX ස්ථාපනය කරයි.

ස්ක්‍රිප්ට් එක NGINX Unit repository එක සහ NGINX repository එක එකතු කරනවා, repositories key එක සහ settings files එකතු කරනවා. apt, අන්තර්ජාලය හරහා ගබඩා වෙත ප්රවේශය නිර්වචනය කිරීම.

NGINX ඒකකය සහ 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 ඒකකය, PHP MariaDB, Certbot (අපි සංකේතනය කරමු) සහ ඒවායේ පරායත්තතා ස්ථාපනය කිරීම

සියලුම ගබඩා එකතු කළ පසු, අපි පාරදත්ත යාවත්කාලීන කර යෙදුම් ස්ථාපනය කරමු. ස්ක්‍රිප්ට් මඟින් ස්ථාපනය කර ඇති පැකේජ වල WordPress.org ධාවනය කිරීමේදී නිර්දේශිත PHP දිගුද ඇතුළත් වේ

ස්ක්‍රිප්ට් කේතය

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

NGINX ඒකකය සහ වර්ඩ්ප්‍රෙස් සමඟ භාවිතය සඳහා PHP සැකසීම

ස්ක්‍රිප්ට් ඩිරෙක්ටරියේ සිටුවම් ගොනුවක් සාදයි 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

WordPress සඳහා MariaDB දත්ත සමුදා සැකසුම් සැකසීම

අපි MySQL වලට වඩා MariaDB තෝරා ගත්තේ එයට වැඩි ප්‍රජා ක්‍රියාකාරකම් ඇති නිසා සහ එය කළ හැකි බැවිනි පෙරනිමියෙන් වඩා හොඳ කාර්ය සාධනයක් සපයයි (බොහෝ විට, මෙහි සෑම දෙයක්ම සරලයි: MySQL ස්ථාපනය කිරීමට, ඔබ වෙනත් ගබඩාවක් එකතු කළ යුතුය, දළ වශයෙන්. පරිවර්තක).

ස්ක්‍රිප්ට් නව දත්ත සමුදායක් නිර්මාණය කරන අතර loopback අතුරුමුහුණත හරහා WordPress ප්‍රවේශ අක්තපත්‍ර නිර්මාණය කරයි:

ස්ක්‍රිප්ට් කේතය

# 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;"

WordPress CLI වැඩසටහන ස්ථාපනය කිරීම

මෙම පියවරේදී ස්ක්‍රිප්ට් වැඩසටහන ස්ථාපනය කරයි WP CLI. එය සමඟ, ඔබට අතින් ගොනු සංස්කරණය කිරීම, දත්ත සමුදාය යාවත්කාලීන කිරීම හෝ පාලක පැනලයට පිවිසීමකින් තොරව WordPress සැකසුම් ස්ථාපනය කර කළමනාකරණය කළ හැකිය. එය තේමා සහ ඇඩෝන ස්ථාපනය කිරීමට සහ වර්ඩ්ප්‍රෙස් යාවත්කාලීන කිරීමට ද භාවිතා කළ හැක.

ස්ක්‍රිප්ට් කේතය

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 තදබදය අඩු කිරීම සඳහා TCP on loopback වෙනුවට unix වසම් සොකට් එකක් හරහා ක්‍රියා කරයි.
  • WordPress උපසර්ගයක් එක් කරයි https:// සේවාලාභීන් HTTPS හරහා NGINX වෙත සම්බන්ධ වන්නේ නම් URL වෙත, සහ දුරස්ථ ධාරක නාමය (NGINX විසින් සපයන ලද පරිදි) PHP වෙත යවයි. මෙය සැකසීමට අපි කේතයක් භාවිතා කරමු.
  • WordPress වලට පුරනය වීමට HTTPS අවශ්‍යයි
  • URL ව්‍යුහය නිහඬව සම්පත් පදනම් වේ
  • WordPress නාමාවලිය සඳහා නිවැරදි ගොනු පද්ධති අවසරයන් සකසා ඇත.

ස්ක්‍රිප්ට් කේතය

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 ඒකකය පිහිටුවීම

PHP ක්‍රියාත්මක කිරීමට සහ වර්ඩ්ප්‍රෙස් මාර්ග හැසිරවීමට, PHP ක්‍රියාවලිවල නාම අවකාශය හුදකලා කිරීමට සහ කාර්ය සාධන සැකසුම් ප්‍රශස්ත කිරීමට ස්ක්‍රිප්ට් NGINX ඒකකය වින්‍යාස කරයි. අවධානය යොමු කළ යුතු විශේෂාංග තුනක් තිබේ:

  • නාම අවකාශ සහය කොන්දේසිය අනුව තීරණය කරනු ලැබේ, ස්ක්‍රිප්ට් කන්ටේනරය තුළ ක්‍රියාත්මක වේද යන්න පරීක්ෂා කිරීම මත පදනම්ව. බොහෝ බහාලුම් සැකසුම් බහාලුම්වල කැදලි ධාවනය සඳහා සහාය නොදක්වන බැවින් මෙය අවශ්‍ය වේ.
  • නාම අවකාශ සඳහා සහය තිබේ නම්, නාම අවකාශය අක්‍රීය වේ ජාල. වර්ඩ්ප්‍රෙස් හට එකවර අන්ත ලක්ෂ්‍ය වෙත සම්බන්ධ වීමට සහ අන්තර්ජාලයට ප්‍රවේශ වීමට මෙය අවශ්‍ය වේ.
  • උපරිම ක්‍රියාවලි ගණන පහත පරිදි තීරණය වේ: (MariaDB සහ NGINX Uniy ධාවනය සඳහා පවතින මතකය)/(PHP + 5 හි RAM සීමාව)
    මෙම අගය 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

WordPress සඳහා NGINX පිහිටුවීම

ඊළඟට, ස්ක්‍රිප්ට් එක වර්ඩ්ප්‍රෙස් සඳහා වින්‍යාස ගොනුවක් සාදයි default.conf නාමාවලියෙහි conf.d.. මෙන්න එය වින්‍යාස කර ඇත:

  • Certbot හරහා Let's Encrypt වෙතින් ලැබුණු TLS සහතික සක්‍රිය කිරීම (එය වින්‍යාස කිරීම මීළඟ කොටසේ වනු ඇත)
  • Let's Encrypt වෙතින් නිර්දේශ මත පදනම්ව TLS ආරක්ෂක සැකසුම් වින්‍යාස කරන්න
  • පෙරනිමියෙන් පැය 1ක් සඳහා මඟ හැරුණු ඉල්ලීම් හැඹිලිගත කිරීම සබල කරන්න
  • පොදු ඉල්ලූ ගොනු දෙකක් සඳහා ප්‍රවේශ ලොග් වීම, මෙන්ම ගොනුව සොයාගත නොහැකි නම් දෝෂ ලොග් වීම අක්‍රීය කරන්න: 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 වෙතින් TLS සහතික ලබා ගැනීමට සහ ස්වයංක්‍රීයව අලුත් කිරීමට ඉඩ සලසන ඉලෙක්ට්‍රොනික් ෆ්‍රොන්ටියර් පදනමේ (EFF) නොමිලේ මෙවලමකි. NGINX හි අපි සංකේතනය කරමු වෙතින් සහතික සැකසීමට Certbot වින්‍යාස කිරීමට ස්ක්‍රිප්ට් පහත පියවර සිදු කරයි:

  • NGINX නතර කරයි
  • බාගැනීම් නිර්දේශිත TLS සැකසුම්
  • වෙබ් අඩවිය සඳහා සහතික ලබා ගැනීම සඳහා Certbot ධාවනය කරයි
  • සහතික භාවිතා කිරීමට NGINX නැවත ආරම්භ කරයි
  • සහතික අලුත් කිරීම් සඳහා පරීක්ෂා කිරීමට සහ, අවශ්‍ය නම්, නව සහතික බාගත කර NGINX නැවත ආරම්භ කිරීමට Certbot දිනපතා පෙ.ව. 3:24 ට ධාවනය වන ලෙස වින්‍යාස කරයි.

ස්ක්‍රිප්ට් කේතය

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

ඔබේ වෙබ් අඩවියේ අතිරේක අභිරුචිකරණය

TLSSSL සක්‍රීය කර ඇති නිෂ්පාදන-සුදානම් වෙබ් අඩවියකට සේවය කිරීම සඳහා අපගේ ස්ක්‍රිප්ට් NGINX සහ NGINX ඒකකය වින්‍යාස කරන ආකාරය ගැන අපි ඉහත කතා කළෙමු. ඔබට, ඔබේ අවශ්‍යතා අනුව, අනාගතයේදී එකතු කළ හැක:

  • සහාය බ්‍රොට්ලි, HTTPS හරහා වැඩි දියුණු කරන ලද පියාසර සම්පීඩනය
  • ආරක්ෂක ආරක්‍ෂාව с WordPress සඳහා නීතිඔබේ වෙබ් අඩවියට ස්වයංක්‍රීය ප්‍රහාර වැළැක්වීමට
  • උපස්ථය WordPress සඳහා, ඔබට සුදුසුයි
  • ආරක්ෂාව උදව්වෙන් ඇප්ආර්මර් (උබුන්ටු මත)
  • Postfix හෝ msmtp එසේ වර්ඩ්ප්‍රෙස් තැපැල් යැවිය හැක
  • ඔබේ වෙබ් අඩවිය පරීක්ෂා කිරීම නිසා එයට කොපමණ තදබදයක් හැසිරවිය හැකිද යන්න ඔබට වැටහෙනු ඇත

ඊටත් වඩා හොඳ අඩවි කාර්ය සාධනයක් සඳහා, අපි යාවත්කාලීන කිරීමට නිර්දේශ කරමු NGINX Plus, විවෘත මූලාශ්‍ර NGINX මත පදනම් වූ අපගේ ව්‍යවසාය ශ්‍රේණියේ වාණිජ නිෂ්පාදනය. එහි ග්‍රාහකයින්ට ගතිකව පටවන ලද Brotli මොඩියුලයක් මෙන්ම (අමතර ගාස්තුවක් සඳහා) ලැබෙනු ඇත. NGINX ModSecurity WAF. අපිත් පිරිනමනවා NGINX යෙදුම් ආරක්ෂණය, F5 වෙතින් කර්මාන්තයේ ප්‍රමුඛ ආරක්ෂක තාක්ෂණය මත පදනම් වූ NGINX Plus සඳහා WAF මොඩියුලයකි.

සැ.යු. ඉහළ බරක් සහිත වෙබ් අඩවියක සහාය සඳහා, ඔබට විශේෂඥයින් සම්බන්ධ කර ගත හැකිය සවුත්බ්‍රිජ්. ඕනෑම බරක් යටතේ ඔබේ වෙබ් අඩවියේ හෝ සේවාවේ වේගවත් සහ විශ්වාසනීය ක්‍රියාකාරිත්වය අපි සහතික කරන්නෙමු.

මූලාශ්රය: www.habr.com