Categories
Symfony

Scaffold Symfony app from existing database

Prepare for a new run

rm -r src/Entity/*
rm -r src/Form/*
rm -r src/Controller/*
rm -r src/Repository/*
rm -r templates/*

Copy in a backup of base twig template

cp base.html.twig templates/

Generate entities from an existing database

bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity
bin/console make:entity --regenerate App

Make CRUD for each new entity

bin/console make:crud MyEntity
bin/console make:crud AnotherEntity
...

Adjust file permissions

sudo chown -R chris:chris templates/
sudo chown -R chris:chris src/Entity/
sudo chown -R chris:chris src/Controller/
sudo chown -R chris:chris src/Form/

Now, sql insert data fixtures

...
Categories
webserver

Apache & Nginx reverse proxy debugging

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{Host}i\" \"%{User-agent}i\"" chrisdebug
CustomLog ${APACHE_LOG_DIR}/access.log chrisdebug

Logs at /var/log/apache2

Categories
Linux tips

Query DNS data

dig

Query for information on all DNS records

dig your_domain_name.com ANY

Query for A records

dig your_domain_name.com A

host, nslookup and dig share similar functionality.

whois

Return information about a domain name registration, including the name servers it is configured to work with

whois your_domain_name.com
Categories
Linux tips

Ubuntu select and paste with Mouse Keys

On Linux I like to make use of the select and paste feature i.e. selecting text copies it and the middle mouse button pastes it. When a laptop touchpad has no buttons a middle mouse click can be simulated with a three-finger tap, but this is awkward.

Here is my workaround (for keyboards with a numeric keypad only):

  1. From the Accessibility menu turn on Mouse Keys.
  2. Ensure the Number Lock is off.
  3. Select text.
  4. Paste text by pressing 5 on the numeric keypad.

Furthermore, in a terminal and some other applications SHIFT + INSERT will paste text which has been previously selected.

I am using Ubuntu 20.04.1 and GNOME 3.36.8

Categories
WordPress

WordPress debugging

To turn on debugging add the following to wp-config.php

// Turn debugging on
define('WP_DEBUG', true);

// Tell WordPress to log everything to /wp-content/debug.log
define('WP_DEBUG_LOG', true);

// Display error messages
define('WP_DEBUG_DISPLAY', true);

Write to /wp-content/debug.log

error_log('logging message here...');
Categories
Backing up

Backup mysql database via socks tunnel

The following bash script will open a socks tunnel with a remote host and use a local mysqldump cli tool to backup a mysql database.

#!/bin/sh
DT=$(date '+%Y-%m-%dT%H:%M:%S')
SUFFIX=
FILENAME=$DT$SUFFIX
BACKUPSPATH=
USER=
IP=
PORT=
DBUSER=
DBPASS=
DBNAME=
echo "Opening socks tunnel to $IP"
echo "Writing $FILENAME"
ssh -f -L $PORT:127.0.0.1:$PORT $USER@$IP sleep 10; \
mysqldump -u $DBUSER -h 127.0.0.1 -p$DBPASS -P $PORT --routines $DBNAME > $BACKUPSPATH/$FILENAME
Categories
Developer notes

Dockerized phpmyadmin

Phpmyadmin is available as a Docker image. The mysql server connection parameters are provided when the container is instantiated.

docker run -d --name pma_local \
--network=docker_network \
-e PMA_HOST=db \
-e PMA_PORT=3306 \
-e PMA_USER=root \
-e PMA_PASSWORD=password \
-p 8080:80 phpmyadmin/phpmyadmin
  • --network The docker network the phpmyadmin container will join.
  • PMA_HOST, PMA_PORT Name and port of the database server host on the docker network.

Browse to phpmyadmin at http://localhost:8080

Categories
Uncategorised

Import Barclays Bank statement

Create a table for the bank statement entries

CREATE TABLE `bank_statement_entries` (
  `id` int(11) NOT NULL,
  `cheque_id` varchar(50) DEFAULT NULL,
  `date` date DEFAULT NULL,
  `amount` decimal(13,4) DEFAULT NULL,
  `subcategory` varchar(50) DEFAULT NULL,
  `memo` varchar(100) DEFAULT NULL,
  `my_notes` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

SQL script for importing batch of csv statement entries into table `bank_statement_entries`

DELIMITER $$

/* remove any tables created by a previous run */
DROP TABLE IF EXISTS `bank_statement_entries_raw`$$
DROP TABLE IF EXISTS `bank_statement_entries_batch`$$

/* make an empty copy of the master table to put the batch of statements into */
/* but get rid of the id column */
CREATE TABLE `bank_statement_entries_batch` LIKE `bank_statement_entries`$$
ALTER TABLE `bank_statement_entries_batch` DROP `id`$$

/* prepare a table ready for the raw csv import */
CREATE TABLE `bank_statement_entries_raw` (
  `Number` varchar(50) DEFAULT NULL,
  `Date` varchar(100) DEFAULT NULL,
  `Account` varchar(100) DEFAULT NULL,
  `Amount` decimal(13,4) DEFAULT NULL,
  `Subcategory` varchar(50) DEFAULT NULL,
  `Memo` varchar(100) DEFAULT NULL,
  `Count` int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (Count)
)$$

/* upload the local csv file */
LOAD DATA LOCAL INFILE '/home/chris/2018-07-31_to_2018-09-03.csv'                        
INTO TABLE `bank_statement_entries_raw`
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS$$

/* populate the batch table */
/* do any transformations here */
INSERT INTO bank_statement_entries_batch (
  cheque_id,
  date,
  amount,
  subcategory,
  memo) 
SELECT 
   `Number`
	,DATE_FORMAT(STR_TO_DATE(Date, '%d/%m/%Y'), '%Y-%m-%d')
	,Amount
	,Subcategory
	,Memo
FROM `bank_statement_entries_raw`
ORDER BY `Count` DESC$$

/* now insert the newly prepared batch of statements in to the master table */
INSERT INTO `bank_statement_entries` (
  cheque_id,
  date,
  amount,
  subcategory,
  memo,
  my_notes
)
SELECT 
  cheque_id,
  date,
  amount,
  subcategory,
  memo,
  my_notes
FROM `bank_statement_entries_batch`$$

DELIMITER ;
Categories
Uncategorised

Configure Laradock, XDebug, VSCode and Selenium Chrome

Getting the basics working first i.e. a simple index.php file inside laravel/public. Install Laravel framework after this setup.

Directory structure:

.vscode/
    └── launch.json
laradock/
laravel/
    └── xdebug.log
    └── public/
            └── index.php

With a fresh install of Laradock
cp env-example .env

In .env
APP_CODE_PATH_HOST=../laravel/
WORKSPACE_INSTALL_XDEBUG=true
PHP_FPM_INSTALL_XDEBUG=true

In laradock/selenium/Dockerfile, use the latest chrome debug image and expose port 5900 for vnc.

FROM selenium/standalone-chrome-debug

LABEL maintainer="Edmund Luong <edmundvmluong@gmail.com>"

EXPOSE 4444
EXPOSE 5900

In laradock/docker-compose.yml add vnc port mapping for selenium service

selenium:
    build: ./selenium
    ports:
    - "${SELENIUM_PORT}:4444"
    - "5900:5900"
    volumes:
    - /dev/shm:/dev/shm
    networks:
    - frontend

laradock/nginx/default.conf needs to be:

server {

    listen 80;
    listen [::]:80;

    # For https
    listen 443 ssl;
    listen [::]:443 ssl ipv6only=on;
    ssl_certificate /etc/nginx/ssl/default.crt;
    ssl_certificate_key /etc/nginx/ssl/default.key;

    server_name localhost;
    root /var/www/public;
    index index.php index.html index.htm;

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

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass php-upstream;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fixes timeouts
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }

    location /.well-known/acme-challenge/ {
        root /var/www/letsencrypt/;
        log_not_found off;
    }

    error_log /var/log/nginx/laravel_error.log;
    access_log /var/log/nginx/laravel_access.log;
}

Change permissions on the ssl files
cd laradock/nginx
sudo chown -R chris:root ssl/

.vscode/launch.json


    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/var/www": "/home/chris/laravel",
            }
        }
    ]
}

Edit BOTH workspace/xdebug.ini and php-fpm/xdebug.ini to

xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_log=/var/www/xdebug.log
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.idekey=VSCODE

Run the docker project (will also start php-fpm and workspace)
docker-compose up -d nginx mysql

Workspace container might error "…maybe run apt-get update". If so try adding apt-get update && \ to workspace Dockerfile e.g.

RUN if [ ${INSTALL_XDEBUG} = true ]; then \
    apt-get update && \
    # Load the xdebug extension only with phpunit commands
    ...
    ...

Re-build the containers as required
docker-compose build workspace php-fpm

Enter workspace container, for example
docker-compose exec workspace bash

Test http://localhost in a browser and check vscode debugger will stop on a breakpoint in index.php

Install Laravel and bring up all required containers
docker-compose up -d nginx mysql phpmyadmin redis selenium