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]


This 1 line does also work:

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

This setting can be also made in the .htaccess file in your project folder. It's important that this setting is made in the <IfModule mod_rewrite.c> section.


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/ .

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:


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

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:


 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

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

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

git clean - Cleaning the Git repository to be able to merge with remote

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

Sometimes you get Git messages like

The following untracked working tree files would be overwritten by checkout

 The following command can be helpful in that case.

git clean -d -fx ""

See http://www.kernel.org/pub/software/scm/git/docs/git-clean.html

-x means ignored files are also removed as well as files unknown to git.
-d means remove untracked directories in addition to untracked files.
-f is required to force it to run.

 I've learned this by a discussion on Stackoverflow: http://goo.gl/I9NMxO.


Rsync: Sync Two Folders via SSH in Both Directions

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

After started writing about rsync from local to live. Here's a post about syncing in two directions.

Helpful in rsync is, that also the permissions- and owner information is transfered ("-a" parameter). The "--progress" parameter shows the progress and "--exclude" allows to exclude directories. Be carefully about "--delete". Mind hidden folders like ".git" and ".vagrant" etc.

SSH session on local system (developer desktop) to sync files from (stage/live) server to local system

Here we assume you are in a ssh session on your local development environment and you want to sync from the stage/live server to your local development environment.

rsync -a --progress --delete source/ destination-host:/destination-directory/


rsync -a --progress --delete www/ --exclude=sites/default/files/ --exclude=.idea/ --exclude=.vagrant/ --exclude=provisioning/ --exclude=.git ssh-username@host.com:/srv/www/destination-folder/

From (stage/live) server to local system (developer desktop)

Here we assume you are in a ssh session on your stage/live server and you want to sync from your local development environment to stage/live server.


rsync -chavzP --stats --progress --delete username@host.com:/srv/www/some-folder/ /var/www/some-folder/


Detailed explanation for the commands can be found at explainshell.com. The rsync command above is explained here.

Happy syncing.

Rsync without changing permissions

The following command will sync the files and folders. It will also delete. But it will not change the permissions. It can be helpful, if you want to ensure, that your webserver is still able to read and execute the files. Like in a case, where I needed this command.

rsync -a --no-perms --progress --delete ./ user@host:/var/www/some-directory/

Just simulate, without real sync

Use the --dry-run option, to execute the command without any real file transfer. Then this command shows you, what "would" be transferred.