Day: September 23, 2021

TODO: Change wordpress TCP port

Change wordpress TCP port

After change port in settings, also redeployed dockers, the website is unreachable.

Change port

Update in setting of wordpress

Update docker-compose.yml file

Destory and recreate dockers

docker-compose down
docker-compose up -d

Note: Failed

Change back port

Change port back using by update option values database.

Access mariadb docker

docker exec -it wp_db_1 bash

Login to mariadb

mysql -u wordpress -p

Search option value

MariaDB [wordpress]> select * from wp_options where option_value like '%192.168.1.14%';
+-----------+-------------+------------------------+----------+
| option_id | option_name | option_value           | autoload |
+-----------+-------------+------------------------+----------+
|         1 | siteurl     | http://192.168.1.14:80 | yes      |
|         2 | home        | http://192.168.1.14:80 | yes      |
+-----------+-------------+------------------------+----------+
2 rows in set (0.058 sec)

Update value back

MariaDB [wordpress]> update wp_options set option_value='http://192.168.1.14:8080' where option_value='http://192.168.1.14:80';
Query OK, 2 rows affected (0.008 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [wordpress]> select * from wp_options where option_value like '%192.168.1.14%';
+-----------+-------------+--------------------------+----------+
| option_id | option_name | option_value             | autoload |
+-----------+-------------+--------------------------+----------+
|         1 | siteurl     | http://192.168.1.14:8080 | yes      |
|         2 | home        | http://192.168.1.14:8080 | yes      |
+-----------+-------------+--------------------------+----------+
2 rows in set (0.058 sec)

MariaDB [wordpress]> quit

TODO: Change docker container mapping port

Change docker container mapping port

To change the running container mapping port with or without recreating container.

By recreating container

Stop and commit running container, then run new container using new image.

This requires changing image name and knowing the docker run command parameters.

docker stop test01
docker commit test01 test02
docker run -p 8080:8080 -td test02

Modify configuration file

Stop the container and docker service, then change the docker container configuration file hostconfig.json. After that, start docker service and container.

This requires updating docker run command document.

  1. Stop docker.
docker stop test01
systemctl stop docker
  1. Edit hostconfig.json file
vi /var/lib/docker/containers/[hash_of_the_container]/hostconfig.json

or following file when using snap

/var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.json
  1. Start docker
systemctl start docker
docker start test01

WordPress URLs point actual server behind NGINX

WordPress URLs point actual server behind NGINX

NGINX did not translate URL correctly

When accessing server via NGINX, it didn't translate URLs in page to the URL accessing.

For example, the URL is https://example.com/sample.html, the server behind is https://192.168.1.11/sample.html, the NGINX is still leaving it without translating.

I have done following, but not useful

  1. Updated both options siteurl and home in database
update wp_options set option_value='http://newhost:8080' where option_name='siteurl';
update wp_options set option_value='http://newhost:8080' where option_name='home';
  1. Added host and port in wp_config.php
if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
      $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
}

if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
      $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_PORT'];
}

Someone suggested following lines as well, but I didn't do it.

define('WP_HOME','https://test.com/blog/');
define('WP_SITEURL','https://test.com/blog/');
  1. Updated NGINX configuration.
proxy_pass http://newhost

Solution

Modify NGINX configuration

Configure https

server {
    server_name  blog.bianxi.com;

    # SSL configuration
    listen 443 ssl;

    ssl_certificate     conf.d/www.bianxi.com.crt;
    ssl_certificate_key conf.d/www.bianxi.com.key;

    location / {
        proxy_pass      'http://192.168.1.14:8080';
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_read_timeout    90;
        proxy_connect_timeout 90;
        proxy_redirect        off;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header Proxy "";
    }
}

Configure http redirect

server {
    listen       80;
    server_name  blog.bianxi.com;

    return 301 https://$host$request_uri;
}

Config wp-config.php

Add following lines in wp-config.php

//$_SERVER['REQUEST_URI'] = str_replace("/wp-admin/", "/blog/wp-admin/",  $_SERVER['REQUEST_URI']);

if($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){

    $_SERVER['HTTPS'] = 'on';
    $_SERVER['SERVER_PORT'] = 443;

    define('WP_HOME','https://blog.bianxi.com/');
    define('WP_SITEURL','https://blog.bianxi.com/');
}

If change URL is required

Change location in NGINX configuration file, and the URL replace were used if requires changing of URL as well in wp-config.php file.

Replace Certificate in Synology NAS

Replace Certificate in Synology NAS

Fill up info

Following steps can be used to replace certificate (not renew) in Synology NAS user interface.

  • Go to Control Panel -> Security -> Certificate
  • Select Add -> Add
  • Select Replace an existing certificate
  • Choose the certificate to be replaced
  • Select Get a certificate from Let's Encrypt
  • Fill up info, includes domain, email, alias (seperated by semi-colons)

Change port forwarding

Now, make sure Synology NAS can be accessed from internet via port forwarding at port 80 and 443 if required.

Suggest using A * record in DNS entry to avoid DNS change. Use NGINX to redirect traffic to this host.

Generate

Then generate certificate.

Docker Compose – wordpress

Docker Compose - wordpress

Simple steps to start using docker compose to create wordpress dockers.

Installation

Install docker-compose package

Run following command on ubuntu and armbian servers.

apt install docker-compose

Create dockers

Create folder as project name wp

The project name will be used as a part of docker container name.

mkdir -p /app/wp

Create docker compose file

Using vi to create file docker-compose.yml in directory /app/wp

version: "3.3"

services:
  db:
    image: mariadb:latest
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "80:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

Run docker compose command

docker-compose up -d

Destroy dockers

Run docker compose command

docker-compose down

Destroy dockers and their volumes

docker-compose down --volumes