User Tools

Site Tools


servers:nginx:wordpress

Information

Prerequisites

Notes

  • :!: Do not initiate any updates from the Admin/wp-admin web page as this may cause local file changes and break the automatic update service

Download Source

sudo git clone --branch 'master' --depth '1' --recurse-submodules 'https://github.com/WordPress/WordPress.git' '/var/www/blog' && sudo chown -R 'nginx':'nginx' '/var/www/blog' && sync

SELinux

sudo semanage fcontext --deleteall "/var/www/blog(/.*)?"
sudo semanage fcontext --add --type 'httpd_sys_rw_content_t' "/var/www/blog(/.*)?"
sudo restorecon -R -v '/var/www/blog' && sync

Database

mysql --user='root' --password
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'localhost' IDENTIFIED BY 'x';
FLUSH PRIVILEGES;

nginx + PHP-FPM Configuration

PHP-FPM Socket

sudo -e '/etc/php-fpm.d/blog.conf' && sudo systemctl restart 'php-fpm'
[blog]
user = nginx
group = nginx

listen = /run/php-fpm/blog.sock
listen.owner = nginx
listen.group = nginx
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 8
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 4

php_value[date.timezone] = "America/New_York"
php_value[upload_tmp_dir] = "/tmp"

php_value[max_execution_time] = "200"
php_value[memory_limit] = "512M"
php_value[post_max_size] = "10M"
php_value[upload_max_filesize] = "10M"
php_value[max_file_uploads] = "100"

php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
php_value[opcache.file_cache] = /var/lib/php/opcache

FastCGI

sudo -e '/etc/nginx/default.d/blog.conf'
# PHP-FPM
location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_pass unix:/run/php-fpm/blog.sock;
}

Server Block

  • :!: style-src in the CSP blocks a Google font URL from being loaded 2) 3)
sudo -e '/etc/nginx/conf.d/blog.conf' && sudo systemctl reload 'nginx'
server {
    listen '443' 'ssl' 'http2';
    server_name 'blog.realmofespionage.xyz';
    root '/var/www/blog';
    index 'index.php';

    include '/etc/nginx/default.d/blog.conf';
    include '/etc/nginx/default.d/headers.conf';

    client_max_body_size '10M';

    add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; font-src 'self' data:; img-src 'self' data: s.w.org" always;

#    access_log  /var/log/nginx/blog-access.log;
#    error_log  /var/log/nginx/blog-error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
          access_log off; log_not_found off; expires max;
    }
}

Initial Setup

Settings

  • Date Format: Y/m/d

Services

Updater

Service

sudo -e '/etc/systemd/system/blog-up.service'
[Service]
User=nginx
Group=nginx
Type=oneshot
ExecStart='/usr/bin/git' -C '/var/www/blog' pull origin 'master'
ExecStartPost='/usr/bin/sync'

Timer

  • Every day at 04:00:00
sudo -e '/etc/systemd/system/blog-up.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'blog-up.timer' --now && sudo systemctl start 'blog-up' && sudo systemctl status 'blog-up' -l
[Unit]
Description=WordPress Git Updater
After=network-online.target
Wants=network-online.target

[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true

[Install]
WantedBy=timers.target

Maintenance

Service

sudo -e '/etc/systemd/system/blog-m.service'
[Service]
User=nginx
Group=nginx
Type=oneshot
ExecStart='/usr/bin/git' -C '/var/www/blog' gc --aggressive --prune='all'
ExecStart='/usr/bin/git' -C '/var/www/blog' fsck --full --strict
ExecStartPost='/usr/bin/sync'

Timer

  • 01 day of every month at 04:20:00
sudo -e '/etc/systemd/system/blog-m.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'blog-m.timer' --now
[Unit]
Description=WordPress Maintenance
After=network-online.target
Wants=network-online.target

[Timer]
OnCalendar=*-*-01 04:20:00
Persistent=true

[Install]
WantedBy=timers.target

Backup

Files

Service

mkdir -p ~/'backups' && sudo -e '/etc/systemd/system/blog-fb.service' && sudo sed -i 's/CHANGEME/'$USER'/g' '/etc/systemd/system/blog-fb.service'
[Service]
Type=oneshot
WorkingDirectory=/var/www
ExecStart='/usr/bin/bash' -c '"/usr/bin/tar" -cvzf "/home/CHANGEME/backups/wordpress-files-auto-"$$(date +%%Y-%%m-%%d)".tar.gz" "blog"'
ExecStartPost='/usr/bin/sync'

Timer

  • 01 day of every month at 04:50:00
sudo -e '/etc/systemd/system/blog-fb.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'blog-fb.timer' --now && sudo systemctl start 'blog-fb' && sudo systemctl status 'blog-fb' -l
[Unit]
Description=WordPress Files Backup

[Timer]
OnCalendar=*-*-01 04:50:00
Persistent=true

[Install]
WantedBy=timers.target

Database

Database Auth

sudo -u 'mysql' -e '/var/lib/mysql/auth/wordpress' && sudo chmod '600' '/var/lib/mysql/auth/wordpress'
[mysqldump]
user=wordpress
password=x

Service

mkdir -p ~/'backups' && sudo -u 'mysql' mkdir -p '/var/lib/mysql/tmp' && sudo -e '/etc/systemd/system/blog-db.service' && sudo sed -i 's/'CHANGEME'/'$USER'/g' '/etc/systemd/system/blog-db.service'
[Service]
Type=oneshot
WorkingDirectory=/var/lib/mysql/tmp
ExecStartPre='/usr/bin/mysqldump' --defaults-extra-file='/var/lib/mysql/auth/wordpress' --single-transaction 'wordpress' -r '/var/lib/mysql/tmp/wordpress.sql'
ExecStart='/usr/bin/gzip' -f '/var/lib/mysql/tmp/wordpress.sql'
ExecStart='/usr/bin/bash' -c '"/usr/bin/mv" "/var/lib/mysql/tmp/wordpress.sql.gz" "/home/CHANGEME/backups/wordpress-database-auto-"$$(date +%%Y-%%m-%%d)".sql.gz"'
ExecStartPost='/usr/bin/sync'

Timer

  • Every day at 04:15:00
sudo -e '/etc/systemd/system/blog-db.timer' && sudo systemctl daemon-reload && sudo systemctl enable 'blog-db.timer' --now && sudo systemctl start 'blog-db' && sudo systemctl status 'blog-db' -l
[Unit]
Description=WordPress Database Backup
After=mariadb.service

[Timer]
OnCalendar=*-*-* 04:15:00
Persistent=true

[Install]
WantedBy=timers.target

Backup

  • Create backup archive on server and transfer to client computer

Server

Stop Services

sudo systemctl stop nginx php-fpm

Backup Folder

cd '/var/www' && sudo tar -cvzf ~/'wordpress-files-manual-'$(date +%Y-%m-%d)'.tar.gz' 'blog' && cd ~ && sync

Backup Database

sudo -u 'mysql' mysqldump --defaults-extra-file='/var/lib/mysql/auth/wordpress' --single-transaction 'wordpress' -r ~/'wordpress-database-manual-'$(date +%Y-%m-%d)'.sql' && sync

Start Services

sudo systemctl start nginx php-fpm

Client

Transfer Files To Client

scp espionage724@192.168.1.153:~/'wordpress-files-'*'.tar.gz' espionage724@192.168.1.153:~/'wordpress-database-'*'.sql' ~/'Downloads' && sync

Restore

Client

Uncompress Database

  • This is only needed if restoring an automated database backup 4)
gunzip ~/'Downloads/wordpress-database-'*'.sql.gz'

Transfer Files To Server

scp ~/'Downloads/wordpress-files-'*'.tar.gz' ~/'Downloads/wordpress-database-'*'.sql' espionage724@192.168.1.153:~

Remove Files

rm -f ~/'Downloads/wordpress-files-'*'.tar.gz' ~/'Downloads/wordpress-database-'*'.sql.gz' && sync

Server

Stop Services

sudo systemctl stop nginx php-fpm

Remove Previous Folder

sudo rm -Rf '/var/www/blog'

Restore WordPress Folder

cd '/var/www' && sudo tar -xvzf ~/'wordpress-files-'*'.tar.gz' 'blog' && sudo chown -R 'nginx':'nginx' '/var/www/blog' && cd ~ && sync

SELinux

sudo semanage fcontext --deleteall "/var/www/blog(/.*)?"
sudo semanage fcontext --add --type 'httpd_sys_rw_content_t' "/var/www/blog(/.*)?"
sudo restorecon -R -v '/var/www/blog' && sync

Drop Previous Database

mysql --user='root' --password
DROP DATABASE wordpress;
FLUSH TABLES;

Re-create Databases

mysql --user='root' --password
CREATE DATABASE wordpress;

Restore Database

mysql --user='root' --password 'wordpress' < ~/'wordpress-database-'*'.sql' && sync

Reapply Permissions

mysql --user='root' --password
GRANT ALL PRIVILEGES ON wordpress.* to 'wordpress'@'localhost' IDENTIFIED BY 'x';
FLUSH PRIVILEGES;

Start Services

sudo systemctl start nginx php-fpm

Remove Backups

  • Verify that WordPress works before running
rm ~/'wordpress-files-'*'.tar.gz' ~/'wordpress-database-'*'.sql' && sync
2)
at least with the Twenty Seventeen theme; nothing appears broken, so looks good to me
3)
add fonts.googleapis.com after unsafe-inline in style-src to fix this
4)
manual doesn't gzip
servers/nginx/wordpress.txt · Last modified: 2019/06/28 22:48 by Sean Rhone