SSH

Phing: Sync a complete website to remote server quickly

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

The famous Phing build tool (written in PHP) offers automation for repetitive tasks, which need to be executed by the Linux terminal. So you can sync an entire website (files and database) to the remote server. Here is an example of a Phing target to do the job:

    <target name="syncWebsiteToLive"
            description="Sync folder and files via Rsync to live server.">
        <echo msg="Syncing data to live server." />
        <exec command="rsync -a --progress --delete /home/peter/Websites/example.com/ --exclude=.git/ --exclude=.idea/ --exclude=sites/default/settings.php user@example.com:/var/www/example.com/"
              passthru="true"
              checkreturn="true" />
        <echo msg="Delete old database" />
        <exec command="ssh example.com 'mysql -uroot -pPASSWORD -e &quot;drop database example_com;&quot;'"
              passthru="true"
              checkreturn="true" />
        <echo msg="Create new empty database" />
        <exec command="ssh example.com 'mysql -uroot -pPASSWORD -e &quot;create database example_com;&quot;'"
              passthru="true"
              checkreturn="true" />
        <echo msg="Import database dump" />
        <exec command="ssh example.com 'mysql -uroot -pPASSWORD example_com &lt; /var/www/example.com/db_dumps/latest.sql'"
              passthru="true"
              checkreturn="true" />
    </target>

This target is later contained in your build.xml file. Afterwards you can execute the following command:

phing syncWebsiteToLive

The output will be the following:

Buildfile: /home/peter/build.xml
DevOps > syncWebsiteToLive:
     [echo] Syncing data to live server.
sending incremental file list
db_dumps/latest.sql
      7,515,285 100%  111.94MB/s    0:00:00 (xfr#1, ir-chk=1009/1040)
     [echo] Delete old database
     [echo] Create new empty database
     [echo] Import database dump
BUILD FINISHED
Total time: 5.7706 seconds

The website has been single by one single console command in an absolute minimum of time. This approach is good for a website in development stadium or a website which database can be just replaced. Then this way can save a lot of time compared to other approaches like ftp and manual database dump import. 

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

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
Subscribe to SSH