Web development and stuff

Latest Posts

Use Windows 10 home folder as WSL Ubuntu shell home directory

WSL-Ubuntu

As I moved on professionally and started to develop dotnet applications for Windows I had to switch my OS from MacOSX to Windows (I still use MacOSX for other stuff). So this change forced me to adapt to a new environment – or better, to go back to a previous one – anyway, I still miss my shell and all it’s features.

Fortunately, Windows 10 can run a WSL and I can use the Ubuntu shell as my main one. If you use a shell regularly you probably often use the commands: cd or the shortcut ~/Downloads for example, and that is a problem because the WSL Shell home is located, in my case, at /home/walter and my user (windows) home is at C:/Users/Walter (/mnt/c/Users/Walter) so: how can I change my default home shell so I can use the home shell directory as the same as the OS home directory?

Quite easy, open a new shell and type:

1
sudo vim /etc/passwd

Search for your username, must look something like that:

1
walter:x:1000:1000:,,,:/home/walter:/bin/bash

and change your home directory /home/walter to the one that you want to use, in my case I changed it to /mnt/c/Users/Walter

1
walter:x:1000:1000:,,,:/mnt/c/Users/Walter:/bin/bash

save and exit vim.

You can copy your user definition

1
cp /home/walter/.bash* /home/walter/.profile /mnt/c/Users/Walter/

Close and re-open the shell and you’re good to go.

Windows 10 connect to NAS with smb1 (Samba, the old one)

Since Windows 10, microsoft remove the support for Samba 1, the problem is that I still have that old NAS who only support SMB1, so here’s the error shown:

You can't connect to the file share because it's not secure. This share requires the obsolete SMB1 protocol, which is unsafe and could expose your system to attack.
Your system requires SMB2 or higher. For more info on resolving this issue, see: https://go.microsoft.com/fwlink/?linkid=852747

And the solution is realy easy.

  1. Open control panel
  2. Search for Turn Windows features on or off
  3. Scroll down to SMB 1.0/CIFS File Sharing Support and activate the checkbox to activate que option
  4. Restart and you’re done

Android FX Explorer can’t connect to Raspberry pi with OSMC via SSH

Since the last update of OSMC in my Raspberry PI I was unable to connect from my smartphone using this awesome app called FX Explorer on the other and I still can use Juiced SSH.

So I had to dig on that problem!

After I little search I finally manage to find the problem, the last version of OSMC had a different configuration

1
$ sudo vim /etc/ssh/ssh_config

That was a missing algorithm in that line:

1
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256

Just add

1
diffie-hellman-group-exchange-sha1

So the line looks like:

1
KexAlgorithms curve25519-sha256@libssh.org,ecdh-sha2-nistp521,ecdh-sha2-nistp384,ecdh-sha2-nistp256,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1

Now save, exit and restart sshd service

1
$ sudo systemctl restart sshd

And you’re done!

Inject a custom Controller in Sonata

I had this problem, I needed to inject a Storage to my controller so I can create a custom action and connect to my db using a storage service.

All I needed was configure the services as following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
services:
  app.admin.document
:
    class
: AppBundle\Admin\DocumentAdmin
    arguments
: [~, AppBundle\Entity\Document, 'app.controller.document']
    tags
:
      - { name
: sonata.admin, manager_type: orm, group: Documentation, label: Documents }

app.controller.document
:
    class
: AppBundle\Controller\DocumentAdminController
    calls
:
      - [ setContainer,[ "@service_container" ] ]
    arguments
:
      - "@security.token_storage"
      - "@app.storage.document"

app.repository.document
:
    class
: AppBundle\Repository\DocumentRepository
    factory
: ["@doctrine.orm.entity_manager", getRepository]
    arguments
:
      - AppBundle\Entity\Document

app.storage.document
:
    class
: AppBundle\Storage\DocumentStorage
    arguments
:
      - "@app.repository.document"

Ignore modified file from commit

Sometimes you need to change some files, just for your development environment and you don’t want to commit those changes. With Git there a way to fix your problem.

First of all, made all the change you need in the file(s) to ignore then run this command:

1
$ git update-index --assume-unchanged path/to/the/file/to/ignore

and it’s done, now your changes are ignored.

Sure, you can roolback those changes by running the command

1
$ git update-index --no-assume-unchanged path/to/the/file/to/version/again

At any point, if you feel that you lost the count of the untracked files, you can list all the files versioned with the files the that starts with a lowercase letter are ignored from versioning.

1
git ls-files -v

To simplify the output run this command, it will only show the ignored files:

1
git ls-files -v | grep '^[[:lower:]]'

append and prepend script with apache

Sometimes we need to run a script in our localhost (or maybe in production) but we don’t want to edit our application to test the current environment. To solve this problem we can just add the file via apache virtual host configuration.

So go on and edit your virtual host, and add this line to the <Directory> directive.

To execute at the end:

1
php_value auto_append_file "/Users/walter/workspace/append.php"

To execute before everything:

1
php_value auto_prepend_file "/Users/walter/workspace/prepend.php"

Don’t forget to restart apache before reloading the app.

Composer update creates a lack of memory

While using Symfony 2 it’s common that you have to run the command composer update. Sometimes we’re graced with the error:

1
2
3
4
5
6
7
8
9
10
The following exception is caused by a lack of memory and not having swap configured

PHP Fatal error:  Uncaught exception 'ErrorException' with message 'proc_open(): fork failed - Cannot allocate memory' in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php:954
Stack trace:
#0 [internal function]: Composer\Util\ErrorHandler::handle(2, 'proc_open(): fo...', 'phar:///usr/loc...', 954, Array)
#1 phar:///usr/local/bin/composer/vendor/symfony/console/Application.php(954): proc_open('stty -a | grep ...', Array, NULL, NULL, NULL, Array)
#2 phar:///usr/local/bin/composer/vendor/symfony/console/Application.php(754): Symfony\Component\Console\Application->getSttyColumns()
#3 phar:///usr/local/bin/composer/vendor/symfony/console/Application.php(715): Symfony\Component\Console\Application->getTerminalDimensions()
#4 phar:///usr/local/bin/composer/vendor/symfony/console/Application.php(648): Symfony\Component\Console\Application->getTerminalWidth()
#5 phar:///usr/local/bin/composer/vendor/symfony/console/Application.php(130): Symfony\Component\Console\Application->renderException(Object(ErrorException),  in phar:///usr/local/bin/composer/vendor/symfony/console/Application.php on line 954

The easiest way to fix it it’s by creating a virtual swap partition using the following commands:

1
2
3
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1

WordPress configurations for Heroku

Starting…

Download the last version of wordpress, then create a directory called “wordpress” (or anything you want else to call it) and extract the content of the zip to the directory.

WordPress Configuration

Jump to your directory and create the Procfile with this line

1
web: vendor/bin/heroku-php-apache2 /

Add a file for composer, named composer.json

1
2
3
4
5
6
7
{
  "name" : "Wordpress",
  "require" : {
    "php" : ">=5.3.3",
    "ext-mbstring" : "*"
  }
}

Edit the wp-config.php file and replace the first lines with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$db = parse_url(getenv('CLEARDB_DATABASE_URL'));

define('DB_NAME', str_replace('/', '', $db['path']));
define('DB_USER', $db['user']);
define('DB_PASSWORD', $db['pass']);
define('DB_HOST', $db['host']);

define('AUTH_KEY',         '{%AUTH_KEY%}');
define('SECURE_AUTH_KEY',  '{%SECURE_AUTH_KEY%}');
define('LOGGED_IN_KEY',    '{%LOGGED_IN_KEY%}');
define('NONCE_KEY',        '{%NONCE_KEY%}');
define('AUTH_SALT',        '{%AUTH_SALT%}');
define('SECURE_AUTH_SALT', '{%SECURE_AUTH_SALT%}');
define('LOGGED_IN_SALT',   '{%LOGGED_IN_SALT%}');
define('NONCE_SALT',       '{%NONCE_SALT%}');

Git repository

1
2
3
4
5
6
7
8
9
10
11
$ git init
Initialized empty Git repository
$ echo "/wp-content/plugins/" >> .gitignore
$ echo "/wp-content/themes/" >> .gitignore
$ git add .
$ git commit -am 'First commit'
[master (root-commit) 83b5792] First commit
 1014 files changed, 384288 insertions(+)
 create mode 100644 .gitignore
....
 create mode 100644 xmlrpc.php

Configure Heroku

Create an App with heroku toolbelt setting up the app name my-wordpress-app and the region eu

1
$ heroku apps:create --region eu my-wordpress-app

Database

Creating the database, this will create a free cleardb on heroku and add the system variable CLEARDB_DATABASE_URL:

1
2
3
4
5
$ heroku addons:create cleardb:ignite
Creating giving-gently-8505... done, (free)
Adding giving-gently-8505 to my-wordpress-app... done
Setting CLEARDB_DATABASE_URL and restarting my-wordpress-app... done, v3
Use <code>heroku addons:docs cleardb</code> to view documentation.

The Deploy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
git push heroku master
Counting objects: 1101, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (1079/1079), done.
Writing objects: 100% (1101/1101), 4.61 MiB | 282.00 KiB/s, done.
Total 1101 (delta 63), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> PHP app detected
remote: -----> Resolved composer.json requirement for PHP to version 5.6.11.
remote: -----> Installing system packages...
remote:        - PHP 5.6.11
remote:        - Apache 2.4.10
remote:        - Nginx 1.6.0
remote: -----> Installing PHP extensions...
remote:        - mbstring (composer.json; bundled)
remote:        - zend-opcache (automatic; bundled)
remote: -----> Installing dependencies...
remote:        Composer version 1.0.0-alpha10 2015-04-14 21:18:51
remote:        Loading composer repositories with package information
remote:        Installing dependencies
remote:        Nothing to install or update
remote:        Writing lock file
remote:        Generating optimized autoload files
remote: -----> Preparing runtime environment...
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing... done, 77.0MB
remote: -----> Launching... done, v4
remote:        https://my-wordpress-app.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/my-wordpress-app.git
   83b2946..4274dc2  master -> master

Finally, load your website and finish the wordpress configurations.
Screen Shot 2015-07-30 at 16.43.29

I hope it was usefull.