Article : How to Keep Your Zend Studio Project Synchronized with a Linux Server

How to Keep Your Zend Studio Project Synchronized with a Linux Server

Overview

Many web developers prefer to keep their projects consistently synchronized with a staging server. This enables previewing how the code works and debugging it on the server right away. Currently, Zend Studio has very limited support for such a scenario. However, the functionality of Zend Studio can sometimes easily be extended using the Eclipse platform features.

This article suggests a way to synchronize your projects with a remote Linux server using 'rsync'.

These are the main steps you need to perform:

  • Setting up passwordless SSH authentication with the remote server.
  • Configuring a synchronization builder for your Zend Studio project(s).
  • Testing the configuration.

Instructions

Prerequisites and Conventions

The procedures in this article are based on the following assumptions:

  1. On the Windows workstation cwRsync a Windows port of rsync is used.
  2. On Windows the program is installed in the default location – C:\Program Files\cwRsync. Most Linux and MacOS workstation users will find rsync and ssh already installed in their distributions.
  3. The remote Linux server's name is staging-server.
  4. On the workstation the user logs in as John.
  5. The user has the account developer on staging-server.
  6. Synchronization of the Zend Studio project will be performed with the directory /home/developer/public_html on staging-server.

Setting Up SSH Authentication

Rsync in the simplest case uses SSH for communication with remote hosts. To be able to synchronize your work seamlessly with the server, you need to configure passwordless SSH authentication on the Linux server. The synchronization will take place at each Save action and you don't want to be asked for SSH password every time.

First, you need to create an authentication key pair. But before you do this, make sure that you don't have the keys already in:

  • Windows – %USERPROFILE%\.ssh
  • Linux and MacOS – ~/.ssh

The key files are usually called 'id_rsa' and 'id_rsa.pub' or 'id_dsa' and 'id_dsa.pub'. If you have these files, you probably want to use the existing keys, thus skipping step 5.

  1. In Zend Studio go to Window | Preferences (in MacOS – Zend Studio | Preferences) and switch to the page General | Network Connections | SSH2.
  2. Make sure that the SSH home field points to the '.ssh' sub-directory in your home (by default the sub-directory may be 'ssh' – without the dot in the beginning). Click Apply after correcting the default location.

Note:

It is important to put the key files in '.ssh' because the generic SSH client (used by rsync) expects to find them in this location. Although the SSH home setting can be overridden when saving the generated keys, I believe it's a good idea to have both Zend Studio and the generic SSH client use the same directory.

  1. Switch to the Key Management tab.
  2. Click one of the buttons: Generate DSA Key, Generate RSA Key or, if you already have the keys, Load Existing Key.
    • The Generate DSA Key and Generate RSA Key buttons will generate a new key pair using either DSA or RSA algorithm (I used RSA in this example) and display the public part of it in the text box.
    • If you have chosen to use an existing key, load it into this dialog by clicking Load Existing Key. You will receive a prompt to choose a file containing the private key.

Note:

Leave the Passphrase field empty. As explained earlier, we want to have fully automated authentication.

  1. If you are using an existing key, skip this step. Click Save Private Key and make sure that you save the key in the '.ssh' sub-directory in your user's home directory.
  2. Click Export Via SFTP. This will add your key to the list of keys accepted by the SSH server.
  1. You will receive a series of prompts where you have to specify the SSH connection credentials. Upon successful completion of the process, you will receive a confirmation:
  1. Verify that the authentication is working by connecting with the SSH client that will be used by rsync. You should receive no password or passphrase prompt.
  • Windows users:

C:\> set HOME=%USERPROFILE%

C:\> cd C:\Program Files\cwRsync\bin
C:\Program Files\cwRsync\bin> ssh developer@staging-server
Welcome to the staging server!
developer@staging-server:~$ pwd
/home/developer

  • Linux and MacOS users:

$ ssh developer@staging-server
Welcome to the staging server!
developer@staging-server:~$ pwd
/home/developer

Configuring the Rsync Synchronization Builder

Eclipse provides the interface for creating custom action-sets that will be performed on a project during the build process. These actions can use external programs or scripts. We will utilize this functionality of Eclipse to synchronize a PHP Project with a remote Linux machine.

  1. Create a new PHP Project or use an existing one.
  2. In most cases some files are excluded from the synchronization. To achieve this, create a plain text file in the project (in Zend Studio go to File | New | File). This file should contain the list of all files and folders to be excluded – one per line. For example, I created a file called 'rsyncExclude.txt' that contains the following lines:

Listing of rsyncExclude.txt:

.project
.buildpath
.settings
.cache
.svn
.externalToolBuilders
rsyncExclude.txt
*.gif

  1. In Zend Studio go to Project | Properties. This will open the Project Properties dialog.
  2. Open the Builders page and click New to add your custom builder.
  3. In the Choose configuration type dialog that appears select Program and click OK.


  1. In the Edit configuration dialog that appears, fill in the Name field with a meaningful name for your new builder.
  2. In the Main tab of the Edit configuration dialog enter the following values:
  • In the Location field enter C:\Program Files\cwRsync\bin\rsync.exe, if you are using Windows, and /usr/bin/rsync otherwise.

Note:

/usr/bin/rsync is the correct path for MacOS and most Linux flavors. However, it is recommended to verify this by issuing the command which rsync in a console window.

  • In the Working Directory field enter ${build_project}. This Eclipse variable stands for the full file system path of the PHP Project the builder is triggered from.
  • In the Arguments text box enter the following lines:

-vcrz
–delete-after
–exclude-from=rsyncExclude.txt
.
developer@staging-server:public_html

 


  1. This step is required for Windows users only. Switch to the Environment tab and click New to add the modified %PATH% environment variable for the rsync launch. In the Edit Environment Variable dialog that opens, enter the following values and click OK:
    • In the Name field enter PATH.
    • In the Value field enter C:\Program Files\cwRsync\bin;${env_var:PATH}.

      Click New again to add a new environment viarible %HOME% which is required for proper operation of SSH:

    • In the Name field enter HOME.
    • In the Value field enter ${env_var:USERPROFILE}.


  1. Switch to the Build Options tab and select the following checkboxes:
    • Launch in background,
    • During auto builds,
    • During a "Clean".
  2. Click OK.
    The synchronization should start immediately.


Result

Testing the Synchronization

To test the new builder, perform the following operations in the project and verify that they are reflected on the server. Each action should produce output in the Console view of Zend Studio:

  1. Create a PHP file.
  2. Make some changes in the file and save it.
  3. Rename the file.
  4. Delete the file.
  5. Create a folder and create a file in this folder.
  6. Delete the folder.

Also make sure that no files or directories listed in the excludes file (rsyncExclude.txt) have been transferred to the server.

Explanation of the Rsync Parameters Used in the Builder

-v Verbose. Output basic information about the operation in progress. To have more detailed output use -vv or -vvv.
-c Checksum. Use checksum verification rather than timestamp and size comparison to decide about the list of files to be updated.
-r Recursive. Scan directories recursively to synchronize all changes in the tree.
-z Compression. Instructs rsync to compress the file contents when transferring it to the server.
–delete-after Instructs rsync to delete files and directories on the server, if they don't exist in the project. Basically, this parameter replicates the deletion of a project resource to the server.
–exclude-from=rsyncExclude.txt Specifies the file that contains the list of resources to be ignored during the synchronization. Wildcards are allowed in this file.
. Synchronization source. '.' means current working directory.
developer@staging-server:public_html Synchronization target. In this example the path on the server is relative to the 'developer' user home directory. However, an absolute path can be specified, for example – developer@staging-server:/var/www