Linux Bash: Force writing console output to a file

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:34

Sometimes this command just does not write content to a file by the ">" operator:

myprogram do --something > my-file-with-the-output.txt

Then you can you a small Linux default program called "tee". Example usage:

myprogram do --something | tee my-file-with-the-output.txt

Tee allows you also to use some option as "-a" to extend the file's content's instead of replacing them.

Tags

Apache2 RewriteRule: Map images folder from remote system to your local virtual host

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:33

With the following directive in your Apache2 virtual host file (at /etc/apache2/sites-available) you must not download the images from products etc. on your local system for development. You just map your local folder location to the remote one. The following example is for a Drupal site (files are under /sites/default/files):

RewriteEngine On
RewriteOptions Inherit
RewriteRule /sites/default/files/(.*)\.(jpeg|jpg|gif|png)$ http://www.my-website.com/sites/default/files/$1.$2 [L,R=301]

Addition

This 1 line does also work:

RedirectMatch ^/media/(.*)\.(jpeg|jpg|gif|png)$ https://www.my-website.com/static/media/$1.$2

Tags

SFTP: Upload and download files from command line

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:32

Download one file:

sftp user@host:/path/to/file.txt localfile.txt

Upload one file:

scp localfile.zip user@host:/my/folder/

Upload entire folder:

scp -r myFancyFolder user@host:/my/folder/

Download entire remote folder into current folder on host machine:

scp -r user@host:/var/www/mysite/themes/mytheme/ .
Tags

Creating a patch by Git

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:25

Firstly create a new Git branch: 

git checkout -b BRANCH-WITH-CHANGES

Do your changes and commit:

git commit -am "My changes."

Now create the patch file by a diff from your new branch and the master branch to extract the changes:

git diff master BRANCH-WITH-CHANGES > EXTRACTED-CHANGES.patch

To test your patch, you can switch back to your master branch and apply the patch (the changes will not be commited, until you commit them in another Git command):

git apply EXTRACTED-CHANGES.patch
Tags

Debugging PHP CLI scripts in PhpStorm which are running inside a remote server

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:18

Please notice: This article is for debugging with Xdebug. Not any "old-fashioned" debugging with var_dump(); or print_r() or any other "90ies webmastering style".

Remote debugging by SSH tunnel and the PhpStorm debugger

Additional settings in /etc/php5/cli/php.ini:

xdebug.remote_enable=true
xdebug.remote_autostart=true
xdebug.remote_port=9000
xdebug.remote_connect_back=1

 Then connect to your server via SSH tunnel (I have used a Vagrant box as an example, but you can tunnel into any server):

ssh -R 9000:localhost:9000 username_goes_here@hostname_goes_here

Start a script via PHP from the command line and PhpStorm will react (debugging button in PhpStorm must be turned on and the mapping for the directories must be set).

Remote debugging by the PhpStorm tools

In case that the debugger window will be opened, but the execution point will not be shown, you can run the script from PhpStorm itself.
Firstly you have to set up the remote debugging (Preferences > Languages & Framework > PHP). Choose here a remote interpreter.

PhpStorm Settings

If you have not already done so, set it up. The "SSH Credentials" settings have worked for me. While I'm using Vagrant and PhpStorm has an extra Vagrant checkbox (which has not worked for me):

PhpStorm Settings

In your PHP command line programs you need to specify often arguments. That can be done also:

PhpStorm Settings
Tags

SSH Troubleshooting

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:14

Fix folder and file permissions for ~/.ssh folder

SSH is checking the folder and files permissions at ~/.ssh. If there're any problems with reading or writing into that folder, the following command helps out.

chmod -R 700 ~/.ssh/ && chmod 600 ~/.ssh/*

Copy your public SSH key to the server

After submitting the following command, you will be asked for a password. Afterwards you will not be asked for the password anymore. The public SSH key will be used.

ssh-copy-id user@remote-host
Tags

Git: Working with Git on different file systems

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:09

Git works also on Windows by the Git bash program, which integrates into certain terminal clients, like the one from PhpStorm IDE. To use Git on Windows, you should be aware of a few issues, if you work with other file systems, like the ones from the Unix world (Linux/Mac).

Case insensitive file system

Windows has a case-insensitive filesystem. That means, that the filenames "facebook.jpg" and "Facebook.jpg" are the same file for Windows. If you have such files, you cannot regularly commit. You have to use a special command, to let Git assume this files are unchanged:

git update-index --assume-unchanged FILENAME

Afterwards you can commit and Git will not bother you with that file.

Unix file and folder permissions

Windows does not know about Unix file and folder permissions. Even you have not visibly changes inside your code, Git will mark all files as changed after clone from a Unix file system. To let Windows ignore the file mode use:

git config core.fileMode false

Line endings

Windows has a pair of CR and LF characters to terminate lines. Linux and Mac have a single LF character. To not have all files marked as changed after cloning on a Windows system, use the following setting:

git config --global core.autocrlf true

sed: Another way of fixing wrong line endings on linux

sed is a stream editor which is pre-installed on linux systems like Ubuntu. It can fix line endings by the following command.

sed -i -e 's/\r$//' scriptname.sh
Tags