How to install Imagick in PHP Docker?

The PHP docker image is build from the source and hence the usual apt-get install php-imagick doesn’t work at all. This might be frustrating for you as most of the solutions have a version attached to it. However, I found a way to always have it updated. Here is how –

Dockerfile for Imagick

This is how docker file should look like:

FROM php:7.3-apache
RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN printf "\n" | pecl install imagick
RUN docker-php-ext-enable imagick

Build Output

....

configuration option "php_ini" is not set to php.ini location
You should add "extension=imagick.so" to php.ini
Removing intermediate container 1c032fba6d45
 ---> 517795f672ac
Step 4/4 : RUN docker-php-ext-enable imagick
 ---> Running in 976ecd4a9627
Removing intermediate container 976ecd4a9627
 ---> 4b978dd49756
Successfully built 4b978dd49756

Git Repositor and Docker

Worth Sharing?

How to reset MySQL password in Ubuntu 18.04 LTS?

Unable to reset MySQL Password in Ubuntu 18.04 LTS?

First I want to discuss in which case the password will not change. I waste quite a lot of time setting a password which had “@” in between. For example “Password@123” – It simply didn’t work. Only when I removed “@” making “Password123” then it worked. So – If you are using @ in the password, please don’t think there is a problem with the approach, problem is with the password.

Stop the MySQL service

sudo service mysql stop

Create directories and set permissions

As soon as you stop the MySQL service, somehow, directories are deleted. You can re-create it as follows:

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld

Run the MySQL without grant table

This will allow you to login without the need of input password.

sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
[1] 2613

Confirm that MySQL is running (OPTIONAL)

jobs
[1]+  Running     sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &

Connect to MySQL

mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Flush Privileges

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Change Password

Assuming that your new password is “your_password_without_at_sign” here is how you can change it.

mysql> USE mysql; 
Database changed
mysql> UPDATE user SET authentication_string=PASSWORD("your_password_without_at_sign") WHERE User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> UPDATE user SET plugin="mysql_native_password" WHERE User='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

Now you have reset the password, you can quick mysql then kill the job running.

mysql> quit                                                                                                                                                                                    
Bye
sudo pkill mysqld                                                                                                                                                        
jobs                                                                                                                                                                     
[1]+  Done       sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking

Start MySQL in Normal Mode

sudo service mysql start
mysql -u root --password=your_password_without_at_sign                                                                                                                                 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.16 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql>

This is it! Let me know in comments if you find any problem.

Worth Sharing?

SpaceBox – Docker Development Box on SpaceVim

I have created SVIM which is a dockerized version of VIM. However, some of the most imporant plugins I was using is no longer maintained for VIM but are compatible and focussed on NVIM. Thus, I switched to SpaceVim. Despite there is an official Docker of SpaceVim, it is lacking the support of languages as required tools are missing in the docker particularly Go lang hints. Hence, I created my own version of SpaceVim and I ensured that all the stuff I use is working properly. If you are PHP developer, JavaScript developer, Golang Coder or even if you create simple HTMLs – you have everything you need. I haven’t focused on Java or alike line languages like scala or for that matter Objective C. This is ideally for the Web Developer and not for the Mobile App Developer. Though, feel free to send a PR. I am calling it SpaceBox – a dockerized SpaceVim

Why should you use SpaceBox?

  • PHP AutoComplete
  • TypeScript AutoComplete
  • Python AutoComplete
  • Go AutoComplete
  • JavaScript AutoComplete
  • Git

How to use it?

alias vbox='docker run -ti -e TZ=Asia/Kolkata -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -e TMPDIR="/tmp/" -e TERM=xterm -e GIT_USERNAME="Your Name" -e GIT_EMAIL="YourEmail@Stealer.com"  --rm -v /etc/timezone:/etc/timezone:ro -v $HOME/tmp/:/tmp/ -v ~/.ssh:/home/spacevim/.ssh -v $(pwd):/home/spacevim/src varunbatrait/spacebox'

Worth Sharing?

Enable CORS with Credentials in Nginx

You must have noticed that when enable cors with “*”, it doesn’t allow credential to pass. Solution to this is pretty simply, you just need to list all of your domains in configuration. My approach is to have a separate file for each domain.

Directory Structure:

./conf/site-enabled/<site-name>
./conf/cors/<site-name>

Configuration

Assuming that site-name is webapplicationconsultant.com and I want to enable credentials for varunbatra.com along it itself – This is how it goes:

#./conf/cors/webapplicationconsultant.com


set $cors '';
if ($http_origin ~ '^https?://(varunbatra\.com|webapplicationconsultant\.com)') {
        set $cors 'true';
}

if ($cors = 'true') {
        add_header 'Access-Control-Allow-Origin' "$http_origin" always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
        # required to be able to read Authorization header in frontend
        #add_header 'Access-Control-Expose-Headers' 'Authorization' always;
}

if ($request_method = 'OPTIONS') {
        # Tell client that this pre-flight info is valid for 20 days
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
}
#./conf/site-enabled/webapplicationconsultant.com

server {
	listen 443 ssl;
	server_name webapplicationconsultant.com;
....	
        location / {
                include "./../cors/webapplicationconsultant.com";
                try_files $uri @app;
	}
	location @app {
...
	}
}

Worth Sharing?

Important Visuals Hacks in VIM

1. Numbering

VIM is not improved without custom configuration. It varies from a developer to developer but everyone will agree on having line-numbers.

set number

But do you know that you can move between lines by doing following:

: +2 ---> Two lines Below
: -2 ---> Two lines Above

Because of this and because of the fact that Y/D J/K all goes with numbers, you must be interested in showing numbers relative to the cursor.

set relativenumber
VIM Relative numbers!

2. Highlight Current Line and Column

If you are working in tabs/windows, it is a bit frustrating to mover your cursor every time just to find where are you typing! Following two lines will make your life a bit easier.

set cursorline        "highlight current line                                                                                                                            
set cursorcolumn      "highlight current column

3. Highlight Searches

Can you spot the difference between two searches? – Of course “set” is highlighted in the second screen. Here is how I did it:

set hlsearch
set incsearch

Worth Sharing?

Docker Services For Development Infrastructure

Most of the web developers need at least three services running in their machine namely redis, mongo and mysql.

I have composed a simply docker-compose.yml file to run MySQL, MongoDB and Redis with Docker that too with a standard ports and authentication.

version: '3.2'
services:
    redis:
        image: redis:5.0.3
        restart: always
        network_mode: host
        ports:
            - 6379:6379
        command: redis-server --requirepass a_password
    mysql:
        image: mysql:5.7
        restart: always
        network_mode: host
        ports:
            - 3306:3306
        volumes:
            - type: bind
              source: ./mysql
              target: /var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=a_password
        command: mysqld --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    mongodb:
        image: mongo:4.1.7-xenial
        restart: always
        environment:
          - MONGODB_USER="root"
          - MONGODB_DATABASE="some_db"
          - MONGODB_PASS="a_password"
          - MONGO_DATA_DIR=/data/db
          - MONGO_LOG_DIR=/dev/null
        volumes:
          - ./data/db:/data/db
        ports:
            - 27017:27017

Run

To run it, use following command:

docker-compose up -d

Worth Sharing?

Why you weren’t hired!

Warning! Strong language, whole idea was to be heard!

Should it even be in your resume?

In the past three years, I am taken a lot of interviews. I realized that most of the developers who weren’t hired were not because they were less talented – but because our expectations from their resume didn’t match the level of knowledge they had. For instance, they will write “GO” and “GIT” in their resume but in an interview, I am told, they have worked on just a REST API and CAPP (Commit, Add, Pull and Push) in Go and Git respectively.

The Desire to be Hired!

They want to impress the interviewer and hence, they end up adding a lot of stuff. See, for me, I want knowledge! and I am sure it is for every interviewer. When I receive a resume, my expectations are set and I am generally very eager to speak to candidates. I am all excited and dreaming of the good happy ending to have a guy who knows what we are looking for. The desire here is to hire – I really want to hire you, but these high expectations are shattered into pieces right at the beginning of the interview when you just described what you have built. It is like saying I know Maths once you have learned how to divide! or English when you can barely form a word!

The Company Cup VS The World Cup

We have lived in technical problems – we have faced probably more fire than you might have seen in Game of Thrones. We have once paralyzed by the choice of our tech-stack. We have faced the wrath of hits. We have seen our database dying which wouldn’t revive after continuous CPR. This is World! And it Sucks! Now you come along and is full of confidence just because you are better than five developers in your company. You are forgetting that there is a world out there and it requires more than a REST API and CAPP!

The Reality Check!

After a couple of questions which is probably the toughest one – I have seen some candidates are still confident about a tech. After fifteen minutes, I am told that the interview is ‘tough’. Now you have forgotten the fact that you rated yourself 8-9 in MySQL and PHP. As long as it is PHP I think an interview is fine – It isn’t tough. Perhaps you don’t know your level. Ah ha – Your level was computed with a limited dataset – Your Current Company.

“I hate you” – you just said it

Unfortunately the tech can be quantified. It can be measured! There are multiple right answers but one is the best solution? No – only best solution is the only solution. We can measure the memory and cpu usage of your codes. We can measure number of queries you ran – fuck queries, we can even judge on your typing speed. Interview is a mirror! I can tell you where you stand but can you take it? Most of the interviewee couldn’t. As soon as I tell you “You are wrong” and You argued, it is over. It isn’t that we have a really nasty culture out here. But we know that sooner you are told you are wrong, sooner you can fix your codes! If you are arguing, we are loosing probably a couple of customers every minute! So attitude is a key. You are not hired probably because you live more than you should in denial! We can’t afford that.

Farewell!

So, please write what you really know in the resume. Ensure that level you are telling is measured with a bigger dataset. If you have attitude problem, I think there isn’t much you can do. Stay in your current company – consider yourself lucky!


Worth Sharing?

Scheduler for AdonisJS without Command

AdonisJS is Laravel-like framework written for a Node. If you want to run a task like cron, there is a package called: adonis-scheduler

Assumptions / Requirements

I am assuming that you have already followed the installation steps of adonis-scheduler. Once you are done, following steps is what it takes to make ‘cron-less’ scheduler for your Adonis App

Running Scheduler without Command

Step 1 – Set up Scheduler

Create a file called scheduler.js in start folder with following content:

// start/scheduler.js

'use strict'

const Scheduler = use('Scheduler')
Scheduler.run()

Step 2 – Load with Server

Open server.js file and load the above file with preLoad(‘start/start-scheduler’)

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .preLoad('start/scheduler')
  .fireHttpServer()
  .catch(console.error)

This way, you don’t need pm2 setup and scheduler will not load with ace commands.

Cron syntax for JavaScript

Cron for JavaScript supports second as well as described following:

 # ┌────────────── second (optional)
 # │ ┌──────────── minute
 # │ │ ┌────────── hour
 # │ │ │ ┌──────── day of month
 # │ │ │ │ ┌────── month
 # │ │ │ │ │ ┌──── day of week
 # │ │ │ │ │ │
 # │ │ │ │ │ │
 # * * * * * *

Worth Sharing?

Laravel Alternative in Node – AdonisJS

AdonisJS can be said to be a complete transformation of a Laravel framework in PHP to Node. Consider it in this way – it is a Laravel re-written in Node.

We have used Laravel extensively to create various Shopify Application. The fundamental problem with Laravel is database connections which goes out of hand when you use multiple databases. If you come to node and write some stuff which isn’t AdonisJS, then you will fall under the trap of callbacks and promises. So we needed something which is not only in node – but a lot like Laravel.

AdonisJS has a Query Builder, ORM (LUCID), Scheduler and of course, it is MVC. Adonis follows almost same naming conventions of Laravel – if that be a Controller, Route or anything. The API is so consistent, you can guess it. If you are coming from Laravel directly, you already know! Give it a try and watch screencasts.

AdonisJS an alternative to Laravel

Worth Sharing?