มีบทช่วยสอนมากมายเกี่ยวกับวิธีการติดตั้ง WordPress การค้นหาโดย Google ด้วยคำว่า "ติดตั้ง WordPress" จะให้ผลลัพธ์ประมาณครึ่งล้านผลลัพธ์ อย่างไรก็ตาม ในความเป็นจริง มีคำแนะนำที่ดีเพียงไม่กี่ข้อ ซึ่งคุณสามารถติดตั้งและกำหนดค่า WordPress และระบบปฏิบัติการพื้นฐานเพื่อให้สามารถรองรับได้เป็นระยะเวลานาน บางทีการตั้งค่าที่ถูกต้องอาจขึ้นอยู่กับความต้องการเฉพาะอย่างมาก หรืออาจเป็นเพราะคำอธิบายโดยละเอียดทำให้บทความอ่านยาก
ในบทความนี้ เราจะพยายามรวมสิ่งที่ดีที่สุดของทั้งสองโลกเข้าด้วยกันโดยจัดเตรียมสคริปต์ทุบตีเพื่อติดตั้ง WordPress บน Ubuntu โดยอัตโนมัติ พร้อมทั้งอธิบายอย่างละเอียด โดยอธิบายว่าแต่ละส่วนทำอะไร รวมถึงการประนีประนอมที่เราทำในการพัฒนามัน . หากคุณเป็นผู้ใช้ขั้นสูงคุณสามารถข้ามข้อความของบทความไปได้
สถาปัตยกรรมที่พัฒนาขึ้นสำหรับการปรับใช้ WordPress โดยใช้หน่วย NGINX มีอธิบายไว้ใน
- เวิร์ดเพรส CLI
- มาเข้ารหัสและใบรับรอง TLSSSL กันดีกว่า
- การต่ออายุใบรับรองอัตโนมัติ
- การแคช NGINX
- การบีบอัด NGINX
- รองรับ HTTPS และ HTTP/2
- กระบวนการอัตโนมัติ
บทความนี้จะอธิบายการติดตั้งบนเซิร์ฟเวอร์เดียว ซึ่งจะโฮสต์เซิร์ฟเวอร์ประมวลผลแบบคงที่ เซิร์ฟเวอร์ประมวลผล PHP และฐานข้อมูลพร้อมกัน การติดตั้งที่รองรับโฮสต์และบริการเสมือนหลายรายการเป็นหัวข้อที่เป็นไปได้สำหรับอนาคต หากคุณต้องการให้เราเขียนเกี่ยวกับสิ่งที่ไม่ได้อยู่ในบทความเหล่านี้ โปรดเขียนในความคิดเห็น
ความต้องการ
- เซิร์ฟเวอร์คอนเทนเนอร์ (
LXC หรือแอลเอ็กซ์ดี ) เครื่องเสมือน หรือเซิร์ฟเวอร์เหล็กทั่วไปที่มี RAM อย่างน้อย 512MB และ Ubuntu 18.04 หรือใหม่กว่าติดตั้งไว้ - พอร์ตที่เข้าถึงอินเทอร์เน็ตได้ 80 และ 443
- ชื่อโดเมนที่เกี่ยวข้องกับที่อยู่ IP สาธารณะของเซิร์ฟเวอร์นี้
- การเข้าถึงรูท (sudo)
ภาพรวมสถาปัตยกรรม
สถาปัตยกรรมเหมือนกับที่อธิบายไว้
หลักการทั่วไป
- คำสั่งการกำหนดค่าจำนวนมากในสคริปต์ถูกรวมไว้ในเงื่อนไขหากเป็นค่าเดิม: สคริปต์สามารถทำงานได้หลายครั้งโดยไม่มีความเสี่ยงในการเปลี่ยนแปลงการตั้งค่าที่มีอยู่แล้ว
- สคริปต์พยายามติดตั้งซอฟต์แวร์จากที่เก็บข้อมูล ดังนั้นคุณสามารถใช้การอัปเดตระบบได้ในคำสั่งเดียว (
apt upgrade
สำหรับอูบุนตู) - คำสั่งพยายามตรวจจับว่ากำลังทำงานอยู่ในคอนเทนเนอร์เพื่อให้สามารถเปลี่ยนการตั้งค่าได้ตามนั้น
- เพื่อกำหนดจำนวนกระบวนการเธรดที่จะเริ่มต้นในการตั้งค่า สคริปต์จะพยายามคาดเดาการตั้งค่าอัตโนมัติสำหรับการทำงานในคอนเทนเนอร์ เครื่องเสมือน และเซิร์ฟเวอร์ฮาร์ดแวร์
- เมื่ออธิบายการตั้งค่า เรามักจะนึกถึงระบบอัตโนมัติเป็นอันดับแรกเสมอ ซึ่งเราหวังว่าจะกลายเป็นพื้นฐานสำหรับการสร้างโครงสร้างพื้นฐานของคุณเองในรูปแบบโค้ด
- คำสั่งทั้งหมดทำงานในฐานะผู้ใช้ รากเนื่องจากพวกเขาเปลี่ยนการตั้งค่าระบบพื้นฐาน แต่ WordPress ทำงานโดยตรงในฐานะผู้ใช้ทั่วไป
การตั้งค่าตัวแปรสภาพแวดล้อม
ตั้งค่าตัวแปรสภาพแวดล้อมต่อไปนี้ก่อนที่จะรันสคริปต์:
WORDPRESS_DB_PASSWORD
- รหัสผ่านฐานข้อมูล WordPressWORDPRESS_ADMIN_USER
- ชื่อผู้ดูแลระบบ WordPressWORDPRESS_ADMIN_PASSWORD
- รหัสผ่านผู้ดูแลระบบ WordPressWORDPRESS_ADMIN_EMAIL
- อีเมลผู้ดูแลระบบ WordPressWORDPRESS_URL
คือ URL แบบเต็มของเว็บไซต์ WordPress เริ่มต้นที่https://
.LETS_ENCRYPT_STAGING
- ว่างเปล่าตามค่าเริ่มต้น แต่เมื่อตั้งค่าเป็น 1 คุณจะใช้เซิร์ฟเวอร์ Let's Encrypt staging ซึ่งจำเป็นสำหรับการขอใบรับรองบ่อยครั้งเมื่อทดสอบการตั้งค่าของคุณ มิฉะนั้น Let's Encrypt อาจบล็อกที่อยู่ IP ของคุณชั่วคราวเนื่องจากมีคำขอจำนวนมาก .
สคริปต์ตรวจสอบว่ามีการตั้งค่าตัวแปรที่เกี่ยวข้องกับ WordPress เหล่านี้และออกหากไม่เป็นเช่นนั้น
บรรทัดสคริปต์ 572-576 ตรวจสอบค่า LETS_ENCRYPT_STAGING
.
การตั้งค่าตัวแปรสภาพแวดล้อมที่ได้รับ
สคริปต์ในบรรทัด 55-61 ตั้งค่าตัวแปรสภาพแวดล้อมต่อไปนี้ เป็นค่าฮาร์ดโค้ดบางส่วนหรือใช้ค่าที่ได้รับจากตัวแปรที่ตั้งค่าไว้ในส่วนก่อนหน้า:
DEBIAN_FRONTEND="noninteractive"
- แจ้งแอปพลิเคชันว่าพวกเขากำลังทำงานในสคริปต์และไม่มีความเป็นไปได้ที่ผู้ใช้จะโต้ตอบกันWORDPRESS_CLI_VERSION="2.4.0"
เป็นเวอร์ชันของแอปพลิเคชัน WordPress CLIWORDPRESS_CLI_MD5= "dedd5a662b80cda66e9e25d44c23b25c"
— การตรวจสอบไฟล์ปฏิบัติการ WordPress CLI 2.4.0 (เวอร์ชันระบุไว้ในตัวแปรWORDPRESS_CLI_VERSION
). สคริปต์ในบรรทัด 162 ใช้ค่านี้เพื่อตรวจสอบว่าดาวน์โหลดไฟล์ WordPress CLI ที่ถูกต้องแล้วUPLOAD_MAX_FILESIZE="16M"
- ขนาดไฟล์สูงสุดที่สามารถอัพโหลดบน WordPress ได้ การตั้งค่านี้ใช้ในหลายแห่ง ดังนั้นจึงง่ายต่อการตั้งค่าในที่เดียวTLS_HOSTNAME= "$(echo ${WORDPRESS_URL} | cut -d'/' -f3)"
- ชื่อโฮสต์ของระบบ ดึงมาจากตัวแปร WORDPRESS_URL ใช้เพื่อรับใบรับรอง TLS/SSL ที่เหมาะสมจาก Let's Encrypt รวมถึงการตรวจสอบ WordPress ภายในNGINX_CONF_DIR="/etc/nginx"
- พาธไปยังไดเร็กทอรีด้วยการตั้งค่า NGINX รวมถึงไฟล์หลักnginx.conf
.CERT_DIR="/etc/letsencrypt/live/${TLS_HOSTNAME}"
— เส้นทางไปยังใบรับรอง Let's Encrypt สำหรับไซต์ WordPress ที่ได้รับจากตัวแปรTLS_HOSTNAME
.
การกำหนดชื่อโฮสต์ให้กับเซิร์ฟเวอร์ WordPress
สคริปต์จะตั้งชื่อโฮสต์ของเซิร์ฟเวอร์ให้ตรงกับชื่อโดเมนของไซต์ ไม่จำเป็น แต่จะสะดวกกว่าในการส่งอีเมลขาออกผ่าน 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
การเพิ่ม
รหัสสคริปต์
# 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 จากนั้นจึงเพิ่มพื้นที่เก็บข้อมูล NGINX โดยเพิ่มคีย์พื้นที่เก็บข้อมูลและไฟล์การกำหนดค่า 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 Unit, PHP MariaDB, Certbot (Let's Encrypt) และการขึ้นต่อกัน
เมื่อเพิ่มที่เก็บข้อมูลทั้งหมดแล้ว ให้อัปเดตข้อมูลเมตาและติดตั้งแอปพลิเคชัน แพ็คเกจที่ติดตั้งโดยสคริปต์ยังรวมถึงส่วนขยาย 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 Unit และ 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 เนื่องจากมีกิจกรรมชุมชนมากกว่าและมีแนวโน้มเช่นกัน
สคริปต์สร้างฐานข้อมูลใหม่และสร้างข้อมูลประจำตัวเพื่อเข้าถึง 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
ในขั้นตอนนี้ สคริปต์จะติดตั้งโปรแกรม
รหัสสคริปต์
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
การติดตั้งและกำหนดค่า WordPress
สคริปต์จะติดตั้ง WordPress เวอร์ชันล่าสุดในไดเร็กทอรี /var/www/wordpress
และยังเปลี่ยนการตั้งค่าด้วย:
- การเชื่อมต่อฐานข้อมูลทำงานบนซ็อกเก็ตโดเมนยูนิกซ์แทน TCP บนลูปแบ็คเพื่อลดการรับส่งข้อมูล TCP
- WordPress เพิ่มคำนำหน้า https:// ไปยัง URL หากไคลเอนต์เชื่อมต่อกับ NGINX ผ่าน HTTPS และยังส่งชื่อโฮสต์ระยะไกล (ตามที่ 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
สคริปต์กำหนดค่าหน่วย NGINX เพื่อเรียกใช้ PHP และประมวลผลเส้นทาง WordPress โดยแยกเนมสเปซกระบวนการ PHP และปรับการตั้งค่าประสิทธิภาพให้เหมาะสม มีคุณสมบัติสามประการที่ต้องระวังที่นี่:
- การรองรับเนมสเปซถูกกำหนดตามเงื่อนไข โดยขึ้นอยู่กับการตรวจสอบว่าสคริปต์ทำงานในคอนเทนเนอร์หรือไม่ นี่เป็นสิ่งจำเป็นเนื่องจากการตั้งค่าคอนเทนเนอร์ส่วนใหญ่ไม่รองรับการเปิดใช้งานคอนเทนเนอร์แบบซ้อน
- หากมีการรองรับเนมสเปซ ให้ปิดการใช้งานเนมสเปซ เครือข่าย. นี่เป็นการอนุญาตให้ WordPress เชื่อมต่อกับปลายทางทั้งสองและพร้อมใช้งานบนเว็บในเวลาเดียวกัน
- จำนวนสูงสุดของกระบวนการถูกกำหนดดังนี้: (หน่วยความจำที่พร้อมใช้งานสำหรับการรัน MariaDB และ NGINX Uniy)/(ขีดจำกัด RAM ใน PHP + 5)
ค่านี้ถูกกำหนดไว้ในการตั้งค่าหน่วย NGINX
ค่านี้ยังบอกเป็นนัยว่ามีกระบวนการ PHP อย่างน้อยสองกระบวนการทำงานอยู่เสมอ ซึ่งเป็นสิ่งสำคัญเนื่องจาก WordPress ส่งคำขอแบบอะซิงโครนัสให้กับตัวเองจำนวนมาก และไม่มีกระบวนการเพิ่มเติม เช่น 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 สำหรับ WordPress
จากนั้นสคริปต์จะสร้างไฟล์กำหนดค่าสำหรับ WordPress default.conf ในแคตตาล็อก conf.d. มีการกำหนดค่าที่นี่:
- การเปิดใช้งานใบรับรอง TLS ที่ได้รับจาก Let's Encrypt ผ่าน Certbot (การตั้งค่าจะอยู่ในหัวข้อถัดไป)
- การกำหนดการตั้งค่าความปลอดภัย TLS ตามคำแนะนำจาก Let's Encrypt
- เปิดใช้งานคำขอข้ามแคชเป็นเวลา 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 และต่ออายุอัตโนมัติ
- หยุด 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 เพื่อให้บริการไซต์ที่พร้อมสำหรับการผลิตโดยเปิดใช้งาน TLSSSL คุณสามารถเพิ่มในอนาคตได้ ขึ้นอยู่กับความต้องการของคุณ:
- สนับสนุน
Brotli ปรับปรุงการบีบอัดข้อมูลได้ทันทีผ่าน HTTPS ModSecurity сกฎเกณฑ์สำหรับเวิร์ดเพรส เพื่อป้องกันการโจมตีอัตโนมัติบนไซต์ของคุณการสำรองข้อมูล สำหรับ WordPress ที่เหมาะกับคุณการป้องกัน ด้วยAppArmor (บนอูบุนตู)- Postfix หรือ msmtp เพื่อให้ WordPress สามารถส่งอีเมลได้
- ตรวจสอบไซต์ของคุณเพื่อให้คุณเข้าใจว่าไซต์สามารถรองรับปริมาณการเข้าชมได้มากเพียงใด
เพื่อประสิทธิภาพไซต์ที่ดียิ่งขึ้น เราขอแนะนำให้อัปเกรดเป็น
NB หากต้องการสนับสนุนไซต์ที่มีการโหลดสูง คุณสามารถติดต่อผู้เชี่ยวชาญได้
Southbridge . เราจะรับประกันการทำงานที่รวดเร็วและเชื่อถือได้ของเว็บไซต์หรือบริการของคุณภายใต้ภาระงานใดๆ
ที่มา: will.com