Automating WordPress fametrahana miaraka amin'ny NGINX Unit sy Ubuntu
Betsaka ny fitaovana any amin'ny fametrahana WordPress; ny fikarohana Google momba ny "WordPress install" dia hamerina valiny antsasaka tapitrisa. Na izany aza, tena vitsy dia vitsy ny mpitari-dalana mahasoa izay afaka manampy anao hametraka sy hanitsy ny WordPress sy ny rafitra fiasa fototra mba ho tohanana mandritra ny fotoana maharitra. Miankina be amin'ny filanao manokana angamba ny fanovana marina, na mety ho sarotra vakina ilay lahatsoratra noho ny fanazavana amin'ny antsipiriany.
Ato amin'ity lahatsoratra ity, hiezaka ny hanambatra ny tsara indrindra amin'ny tontolo roa isika amin'ny fanomezana script bash hametrahana WordPress ho azy amin'ny Ubuntu, ary handeha amin'izany isika, hanazava ny zavatra ataon'ny ampahany tsirairay sy ny varotra nataontsika tamin'ny famolavolana. izany. Raha mpampiasa za-draharaha ianao, dia azonao atao ny mandingana ny lahatsoratry ny lahatsoratra ary fotsiny alao ny script ho an'ny fanovana sy fampiasana amin'ny tontolo iainanao. Ny famoahana ny script dia fametrahana WordPress mahazatra miaraka amin'ny fanohanana Lets Encrypt, mandeha amin'ny NGINX Unit ary mety amin'ny fampiasana indostrialy.
Ny maritrano novolavolaina amin'ny fametrahana WordPress amin'ny alΓ lan'ny NGINX Unit dia voafaritra ao lahatsoratra taloha, dia hanamboatra zavatra tsy voaresaka ao koa isika izao (toy ny amin'ny fampianarana maro hafa):
WordPress CLI
Aleo Encrypt sy fanamarinana TLSSSL
Fanavaozana taratasy fanamarinana ho azy
NGINX Caching
NGINX compression
HTTPS sy HTTP/2 fanohanana
Fanaovana automation
Ny lahatsoratra dia hamaritra ny fametrahana amin'ny mpizara iray, izay hampiantrano mpizara fanodinana static, mpizara fanodinana PHP ary angon-drakitra. Ny fametrahana miaraka amin'ny fanohanana ireo mpampiantrano sy serivisy virtoaly maro dia lohahevitra mety ho an'ny ho avy. Raha tianao ny hanoratra momba ny zavatra tsy ao anatin'ireo lahatsoratra ireo ianao dia manorata ao amin'ny fanehoan-kevitra.
fepetra takiana
fitoeran-server (LXC na LXD), milina virtoaly, na mpizara hardware mahazatra, miaraka amin'ny RAM 512MB fara-fahakeliny sy Ubuntu 18.04 na vao haingana kokoa.
Ny seranana azo idirana amin'ny Internet 80 sy 443
Ny anaran-tsehatra mifandraika amin'ny adiresy IP-bahoaka an'ity mpizara ity
Fidirana miaraka amin'ny zo fototra (sudo).
Hevitra momba ny Architecture
Ny maritrano dia mitovy amin'ny voalaza aloha, fampiharana tranonkala misy ambaratonga telo. Izy io dia misy script PHP natao tamin'ny motera PHP sy rakitra static nokarakarain'ny mpizara tranonkala.
Fitsipika ankapobeny
Fibaikoana maromaro amin'ny script no fonosina raha misy fepetra ho an'ny tsy fahampiana: azo atao im-betsaka ny script nefa tsy misy atahorana hanova ny fanovana efa vonona.
Ny script dia manandrana mametraka rindrambaiko avy amin'ny repository, mba hahafahanao mampihatra ny fanavaozana ny rafitra amin'ny baiko iray (apt upgrade ho an'ny Ubuntu).
Miezaka mamantatra ny ekipa fa mihazakazaka ao anaty kaontenera izy ireo mba hahafahan'izy ireo manova ny firafiny mifanaraka amin'izany.
Mba hametrahana ny isan'ny fizotry ny kofehy hatomboka ao amin'ny toe-javatra, ny script dia manandrana maminavina ny fandrindrana mandeha ho azy amin'ny fiasana ao anaty kaontenera, milina virtoaly ary mpizara hardware.
Rehefa mamaritra ny toe-javatra dia mieritreritra foana momba ny automatique foana izahay, izay antenainay fa ho fototry ny famoronana fotodrafitrasa ho anao manokana ho code.
Ny baiko rehetra dia mihazakazaka avy amin'ny mpampiasa faka, satria izy ireo dia manova ny rafitra fototra, fa ny WordPress mihitsy no mandeha ho mpampiasa mahazatra.
LETS_ENCRYPT_STAGING β banga amin'ny alΓ lan'ny default, fa amin'ny fametrahana ny sanda amin'ny 1, dia hampiasa ny mpizara fandaharana Let's Encrypt ianao, izay ilaina amin'ny fangatahana mari-pankasitrahana matetika rehefa manandrana ny fanovanao, raha tsy izany, Let's Encrypt dia mety hanakana vonjimaika ny adiresy IP anao noho ny habetsahan'ny fangatahana.
Ny script dia manamarina fa napetraka ireo variables mifandraika amin'ny WordPress ireo ary mivoaka raha tsy izany.
Tsidiho ny sandany ny andalana 572-576 LETS_ENCRYPT_STAGING.
Fametrahana fari-piainan'ny tontolo iainana
Ny script eo amin'ny andalana 55-61 dia mametraka ireto fari-piainan'ny tontolo iainana manaraka ireto, na amin'ny sanda misy kaody sarotra na mampiasa sanda azo avy amin'ny fari-piainana napetraka ao amin'ny fizarana teo aloha:
DEBIAN_FRONTEND="noninteractive" - milaza amin'ny fampiharana fa mandeha amin'ny script izy ireo ary tsy misy ny mety hisian'ny fifaneraserana amin'ny mpampiasa.
WORDPRESS_CLI_MD5= "dedd5a662b80cda66e9e25d44c23b25c" - checksum an'ny WordPress CLI 2.4.0 executable file (ny dikan-teny dia aseho amin'ny variable WORDPRESS_CLI_VERSION). Ny script amin'ny andalana 162 dia mampiasa io sanda io mba hanamarinana fa ny rakitra WordPress CLI marina dia nalaina.
UPLOAD_MAX_FILESIZE="16M" β ny haben'ny rakitra ambony indrindra azo alaina ao amin'ny WordPress. Ampiasaina amin'ny toerana maromaro io fandrindrana io, ka mora kokoa ny mametraka azy amin'ny toerana iray.
Ny script dia mametraka ny anaran'ny mpampiantrano ny mpizara mba hifanaraka amin'ny anaran'ny sehatra ny sandany. Tsy ilaina izany, fa mora kokoa ny mandefa mailaka mivoaka amin'ny alΓ lan'ny SMTP rehefa manangana lohamilina tokana, araka ny namboarin'ny script.
code script
# 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
Manampy ny anaran'ny mpampiantrano amin'ny /etc/hosts
koa WPβCron ampiasaina amin'ny fampandehanana asa tsindraindray, mitaky ny WordPress ho afaka miditra amin'ny alΓ lan'ny HTTP. Mba hahazoana antoka fa miasa tsara amin'ny tontolo rehetra ny WP-Cron, dia manampy tsipika amin'ny rakitra ny script / etc / hostsmba hahafahan'ny WordPress miditra amin'ny tenany amin'ny alΓ lan'ny interface loopback:
code script
# 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
Fametrahana ny fitaovana ilaina amin'ny dingana manaraka
Ny ambiny amin'ny script dia mitaky programa sasantsasany ary mihevitra fa ny trano fitehirizam-bokatra dia havaozina. Manavao ny lisitry ny repository izahay, ary mametraka ny fitaovana ilaina:
code script
# 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
Manampy ny tahiry NGINX Unit sy NGINX
Ny script dia mametraka ny NGINX Unit sy NGINX loharano misokatra avy amin'ny tranokala NGINX ofisialy mba hahazoana antoka fa ny dikan-teny misy ny fanavaozana farany momba ny fiarovana sy ny fanamboarana bug dia ampiasaina.
Ny script dia manampy ny tahiry NGINX Unit ary avy eo ny tahiry NGINX, manampy ny fanalahidin'ny tahiry sy ny rakitra apt, mamaritra ny fidirana amin'ny repository amin'ny alΓ lan'ny Internet.
Ny tena fametrahana ny NGINX Unit sy NGINX dia miseho amin'ny fizarana manaraka. Manampia tahiry mialoha izahay mba hialana amin'ny fanavaozana metadata imbetsaka, manamora ny fametrahana.
code script
# 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
Fametrahana NGINX, NGINX Unit, PHP MariaDB, Certbot (Let's Encrypt) sy ny fiankinany
Raha vao ampiana ny tahiry rehetra dia manavao ny metadata izahay ary mametraka ny fampiharana. Ny fonosana apetraka amin'ny script dia misy ihany koa ny fanitarana PHP atolotra rehefa mihazakazaka WordPress.org
code script
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
Mametraka PHP hampiasaina amin'ny NGINX Unit sy WordPress
Ny script dia mamorona fisie fika ao amin'ny lahatahiry conf.d. Io no mametraka ny haben'ny fampiakarana rakitra ambony indrindra ho an'ny PHP, ahafahan'ny PHP lesoka mivoaka amin'ny STDERR ka ho tafiditra ao amin'ny NGINX Unit izy ireo, ary hamerina ny NGINX Unit.
code script
# 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
Mametraka MariaDB Database Settings ho an'ny WordPress
Nisafidy ny MariaDB izahay noho ny MySQL satria manana hetsika fiaraha-monina bebe kokoa izy ary afaka ihany koa manome fampisehoana tsara kokoa amin'ny alΓ lan'ny default (Angamba, tsotra kokoa ny zava-drehetra eto: ny fametrahana MySQL dia mila manampy tahiry hafa ianao, eo ho eo. mpandika teny).
Ny script dia mamorona tahiry vaovao ary mamorona fahazoan-dΓ lana fidirana amin'ny WordPress amin'ny alΓ lan'ny interface loopback:
code script
# 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;"
Fametrahana ny programa WordPress CLI
Amin'ity dingana ity dia mametraka ny programa ny script WP-CLI. Miaraka amin'izany, afaka mametraka sy mitantana ny fikandrana WordPress ianao nefa tsy mila manova ny rakitra amin'ny tanana, manavao ny angon-drakitra, na miditra ao amin'ny tontonana fanaraha-maso. Azo ampiasaina amin'ny fametrahana lohahevitra sy add-ons ary fanavaozana WordPress.
code script
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
Fametrahana sy fanamboarana WordPress
Ny script dia mametraka ny kinova farany an'ny WordPress ao amin'ny lahatahiry /var/www/wordpress, ary koa manova ny fika:
Ny fampifandraisana angon-drakitra dia miasa amin'ny socket domain unix fa tsy TCP amin'ny loopback mba hampihenana ny fifamoivoizana TCP.
WordPress dia manampy prefix https:// mankany amin'ny URL raha mifandray amin'ny NGINX amin'ny HTTPS ny mpanjifa, ary mandefa ny anaran'ny mpampiantrano lavitra (araka ny nomen'ny NGINX) amin'ny PHP. Mampiasa kaody iray izahay mba hametrahana izany.
Mila HTTPS ny WordPress raha te hidirana
Ny rafitra URL dia mifototra amin'ny loharano mangina
Ny fahazoan-dΓ lana amin'ny rafi-drakitra marina dia napetraka ho an'ny lahatahiry WordPress.
code script
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
Fametrahana Unit NGINX
Ny script dia manamboatra ny NGINX Unit mba hampandehanana PHP sy hifehezana ny lalan'ny WordPress, mitoka-monina ny anaran'ny fizotran'ny PHP ary manatsara ny toe-javatra. Misy lafin-javatra telo tokony hojerena:
Ny fanohanan'ny namespace dia voafaritra amin'ny fepetra, mifototra amin'ny fanamarinana fa mandeha ao anaty fitoeran-javatra ny script. Ilaina izany satria ny ankamaroan'ny fametrahana kaontenera dia tsy mahazaka ny fampandehanana ny kaontenera.
Raha toa ka misy fanohanana ny namespaces, dia kilemaina ny namespace Network. Ilaina izany mba ahafahan'ny WordPress mifandray amin'ny teboka farany ary ho azo idirana amin'ny Internet.
Ny isan'ny dingana ambony indrindra dia voafaritra toy izao manaraka izao: (Fahatsiarovana azo ampiasaina amin'ny fampandehanana ny MariaDB sy NGINX Uniy)/(famerana RAM amin'ny PHP + 5)
Ity sanda ity dia napetraka ao amin'ny firafitry ny Unit NGINX.
Ity sanda ity dia midika ihany koa fa misy dingana PHP roa farafahakeliny mandeha, izay zava-dehibe satria ny WordPress dia manao fangatahana asynchronous be dia be ho azy, ary raha tsy misy dingana fanampiny mandeha, ohatra, dia ho tapaka ny WP-Cron. Azonao atao ny mampitombo na mampihena ireo fetra ireo arakaraka ny firafitry ny toerana misy anao, satria mpandala ny nentin-drazana ny fandrindrana noforonina eto. Amin'ny ankamaroan'ny rafitra famokarana dia eo anelanelan'ny 10 ka hatramin'ny 100 ny toe-javatra.
code script
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
Fametrahana NGINX
Fametrahana ny Settings NGINX fototra
Ny script dia mamorona lahatahiry ho an'ny cache NGINX ary avy eo dia mamorona ny rakitra fanamafisana lehibe nginx.conf. Tandremo ny isan'ny fizotry ny mpitantana sy ny haben'ny rakitra ambony indrindra azo alaina. Misy ihany koa ny tsipika izay mampifandray ny fisie famandrihan-toerana, voafaritra ao amin'ny fizarana manaraka, ary arahin'ny filaharan'ny caching.
Ny fanerena votoaty amin'ny lalitra alohan'ny handefasana azy amin'ny mpanjifa dia fomba tsara hanatsarana ny fahombiazan'ny tranokala, fa raha toa ka voarindra tsara ny famatrarana. Ity fizarana amin'ny script ity dia mifototra amin'ny toe-javatra avy eto.
code script
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
Fametrahana NGINX ho an'ny WordPress
Manaraka, mamorona rakitra fanamafisana ho an'ny WordPress ny script default.conf ao amin'ny katalaogy conf.d. Eto dia voarindra:
Ny fampandehanana ny mari-pankasitrahana TLS azo avy amin'ny Let's Encrypt amin'ny alΓ lan'ny Certbot (ny fametrahana azy dia ho ao amin'ny fizarana manaraka)
Alefaso ny caching fangatahana voatsipaka mandritra ny adiny 1 ho default
Atsaharo ny fidirana fidirana, ary koa ny firaketana diso raha tsy hita ilay rakitra, ho an'ny rakitra roa nangatahana matetika: favicon.ico sy robots.txt
MandΓ ny fidirana amin'ny rakitra miafina sy ny rakitra sasany .phpmba hisorohana ny fidirana tsy ara-dalΓ na na ny fandefasana tsy nahy
Atsaharo ny fidirana fidirana ho an'ny rakitra static sy font
Manampy lalana ho an'ny index.php sy statika hafa.
code script
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
Ny fanamafisana ny Certbot ho an'ny mari-pankasitrahana Let's Encrypt ary manavao azy ireo ho azy
certbot dia fitaovana maimaim-poana avy amin'ny Electronic Frontier Foundation (EFF) izay ahafahanao mahazo sy manavao ho azy ny mari-pankasitrahana TLS avy amin'ny Let's Encrypt. Ny script dia manao ireto dingana manaraka ireto mba handrindrana ny Certbot amin'ny fanodinana taratasy fanamarinana avy amin'ny Let's Encrypt amin'ny NGINX:
Atsaharo ny NGINX
Ampidino ny kiran'ny TLS naroso
Mandeha Certbot mba hahazoana mari-pankasitrahana ho an'ny tranokala
Mamerina indray ny NGINX hampiasa certificat
Ampifanaraho ny Certbot mba handeha isan'andro amin'ny 3:24 maraina hanamarina ny fanavaozana ny taratasy fanamarinana ary, raha ilaina, misintona mari-pankasitrahana vaovao ary avereno indray ny NGINX.
code script
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
Fanamboarana fanampiny amin'ny tranokalanao
Niresaka teo ambony momba ny fomba nanamboaranay ny sora-tananay ny NGINX sy NGINX Unit mba hanompoana tranokala efa vonona amin'ny famokarana miaraka amin'ny TLSSSL. Azonao atao koa ny manampy amin'ny ho avy, arakaraka ny filanao:
MANAMPY Brotli, nanatsara ny famatrarana amin'ny sidina amin'ny HTTPS
Postfix na msmtp mba ahafahan'ny WordPress mandefa mailaka
Fanamarinana ny tranokalanao mba hahafantaranao ny habetsahan'ny fifamoivoizana azony
Ho an'ny fampandehanana tranokala tsara kokoa dia manoro hevitra izahay ny hanavao ny NGINX Plus, vokatra ara-barotra amin'ny orinasa mifototra amin'ny loharano misokatra NGINX. Ny mpanjifany dia hahazo mody Brotli feno dynamically, ary koa (amin'ny sara fanampiny) NGINX ModSecurity WAF. Manolotra koa izahay NGINX App Protect, maody WAF ho an'ny NGINX Plus mifototra amin'ny teknolojia fiarovana amin'ny indostrian'ny F5.
NB Mba hanohanana tranokala be entana dia afaka mifandray amin'ny manam-pahaizana manokana ianao tetezana atsimo. Hiantoka ny fampandehanana haingana sy azo ianteherana ny tranokalanao na serivisy izahay amin'ny entana rehetra.