Thursday, 8 March 2012

Cấu hình Php,Drupal trong Eclipse



Install the LAMP stack

https://help.ubuntu.com/community/ApacheMySQLPHP
The easies way is to do this through the terminal. Go ahead and click on Applications -> Accessories -> Terminal and paste in the following command:
sudo apt-get install apache2 php5 libapache-mod-php5 mysql-server libapache2-mod-auth-mysql php5-mysql phpmyadmin
This will install Apache, MySQL, PHP, and phpmyadmin (a web-based database administration tool). I could go into details on how to configure each one of these, but that would be a subject for another day. For now, visit the URL above to read up on that.

Install Xdebug

 

http://www.xdebug.org
Same here. Most of the commands for installing anything will be using apt-get, since it's the easiest way. Run this command:
sudo apt-get install php5-xdebug
Now, to configure it, open the xdebug configuration file (nano /etc/php5/conf.d/xdebug.ini) and paste the following:
[debug]
; Remote settings
xdebug.remote_autostart=off
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

; General
xdebug.auto_trace=off
xdebug.collect_includes=on
xdebug.collect_params=off
xdebug.collect_return=off
xdebug.default_enable=on
xdebug.extended_info=1
xdebug.manual_url=http://www.php.net
xdebug.show_local_vars=0
xdebug.show_mem_delta=0
xdebug.max_nesting_level=100
;xdebug.idekey=

; Trace options
xdebug.trace_format=0
xdebug.trace_output_dir=/tmp
xdebug.trace_options=0
xdebug.trace_output_name=crc32

; Profiling
xdebug.profiler_append=0
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=0
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=crc32
I originally got this code from http://www.apaddedcell.com/easy-php-debugging-ubuntu-using-xdebug-and-vim, which explains how to setup xdebug for vi. For all practical purposes, the configuration is the same.
Let us leave Xdebug for now and install other things. We'll go back to it later.

Install Drush

http://drupal.org/project/drush
Drush will allow you to perform Drupal maintenance tasks from the command line. Pretty nifty utility. If you use Ubuntu, you're in luck! Run this command:
sudo apt-get install drush
If not, you will have to make sure you have CVS installed:
sudo apt-get install cvs
Then, you have to run the following set of commands to install Drush:
cd /usr/local/share
sudo cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal-contrib checkout -d drush contributions/modules/drush/
cd /usr/local/bin
sudo ln -s /usr/local/share/drush/drush .
That will install Drush on your /usr/local/share/drush folder and create a symbolic link on /usr/local/bin so that you can run Drush from wherever you are in the terminal.

Install jQuery libraries

http://jquery.com
This is not strictly necessary, but Drupal uses jQuery, so it could be nice to link the libraries into Eclipse, independent from any Drupal installation, so that they are available in your project.
sudo apt-get install libjs-jquery libjs-jquery-ui

Downgrade PHP to 5.2

Now, this part is tricky. PHP 5.3 changed a few things in the language construct that Drupal modules are not necessarily aware of. I won't go on about the details of these changes. Suffice it to say that not all Drupal modules out there are fully compatible with PHP 5.3. This is not strictly necessary either, but if you see that your web server starts complaining, you can use this to downgrade to PHP 5.2.
I found that this script works better when you run it after installing all your PHP stuff. If you ever need to install anything else that depends on PHP, you should re-run the script to make sure it's all part of the same version.
#!/bin/bash
# store list of installed PHP packages
php_installed=`dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
# install Aptitude
sudo apt-get install aptitude
# remove all PHP packages
sudo aptitude purge $php_installed
# use PHP packages from Karmic
# pin-params: a (archive), c (components), v (version), o (origin) and l (label).
echo -e "Package: php5\nPin: release a=karmic\nPin-Priority: 991\n" | sudo tee /etc/apt/preferences.d/php > /dev/null
apt-cache search php5-|grep php5-|awk '{print "Package:", $1,"\nPin: release a=karmic\nPin-Priority: 991\n"}'|sudo tee -a /etc/apt/preferences.d/php > /dev/null
apt-cache search -n libapache2-mod-php5 |awk '{print "Package:", $1,"\nPin: release a=karmic\nPin-Priority: 991\n"}'| sudo tee -a /etc/apt/preferences.d/php > /dev/null
echo -e "Package: php-pear\nPin: release a=karmic\nPin-Priority: 991\n" | sudo tee -a /etc/apt/preferences.d/php > /dev/null
# add Karmic to source list
egrep '(main restricted|universe|multiverse)' /etc/apt/sources.list | grep -v "#" | sed s/lucid/karmic/g | sed s/maverick/karmic/g | sudo tee /etc/apt/sources.list.d/karmic.list > /dev/null
# update package database
sudo apt-get update
# install original PHP packages
sudo apt-get install $php_installed
# make sure they don't get updated
sudo aptitude hold `dpkg -l | grep php5| awk '{print $2}' |tr "\n" " "`

Copy-paste that code into a text editor, then save as php-downgrade.sh and run the following command to make it executable:
chmod +x php-downgrade.sh
Then run the file with: ./php-downgrade.sh

Install Eclipse

I know, the whole purpose of this post is to install Eclipse, yet we're a few pages down and now is when I'm actually starting to do that.
Up until now, we were just satisfying some dependencies and setting up the entire development environment. This is the last actual program to install. The rest are plugins for Eclipse, as well as configuration.
Let's start!
You will need to install several components of Eclipse:
sudo apt-get install eclipse-platform eclipse-pde eclipse-plugin-cvs


  • eclipse-platform


    This the base platform for Eclipse. Nothing more.


  • eclipse-pde


    This is the Plug-in Development Environment. It's not strictly necessary for Drupal, but you will need it to install other Eclipse plug-ins. Also, you might be interested in Apache ANT for building, deployment, or packaging. That is also here.


  • eclipse-plugin-cvs


    This is just one of the many plug-ins for Eclipse. This one just happens to be part of the Ubuntu repositories. So go ahead and install it. It provides CVS integration with Eclipse.
Apart from the CVS plug-in, there are several others that we will need. Eclipse is by default a Java IDE, but its extensibility has allowed the community to make it into pretty much anything with the proper plug-ins.
Here's the entire list:
To install them, open Eclipse and go to Help -> Install New Software and click on the Available Software Sites link to add new URLs. Make sure that the following URLs are there:
Then select All Available Sites from the drop-down list and select the previous plug-in list from the window. Follow the prompts to finally install the plug-ins.
Assuming the plug-in installation went well, all you have left is to configure Eclipse with the proper Drupal-friendly settings. Onwards!

Configure Eclipse

Open the Preferences windows and you will be able to browse and change the following settings. Match them as closely as you can.
  • General > Editors > Text Editors:
    • Displayed tab width: 2
    • Insert spaces for tabs: Yes
  • General > Web Browser: New...
    • Name: Firefox
    • Location: /usr/bin/firefox
    • Parameters: --new-window %URL%
  • PHP > Code Style > Formatter:
    • Tab policy: Spaces
    • Indentation size: 2
  • PHP > Debug
    • PHP Debugger: XDebug
  • Team > CVS: Files and Folders
    • Convert text files to use platform line ending: No
    • Default text mode: ASCII with keyword expansion (-kkv)
  • Web > CSS Files > Editor
    • Indent using spaces
    • Indentation size: 2
    • Capitalization style: Lowercase
  • Web > HTML Files > Editor
    • Indent using spaces
    • Indentation size: 2
  • Web > JavaScript > Code Style > Clean Up: Edit...
    • Use blocks in if/while/for/do statements: Always
  • Web > JavaScript > Code Style > Formatter: Edit...
    • Tab policy: Spaces only
    • Indentation size: 2
    • Tab size: 2
    • Statements within 'switch' body: Yes
  • Web > JavaScript > Include Path > User Libraries: New...
    • User library name: jQuery
    • Add .js file: /usr/share/javascript/jquery/jquery.js
    • Add .js file: /usr/share/javascript/jquery-ui/jquery-ui.js

Configure Debugging

  • PHP > PHP Servers
    • Name: Apache
    • URL: http://localhost
    • Path Mapping: Add...
    • Path on Server: /
    • Path in Workspace: /%PROJECT_NAME%
  • PHP > PHP Executables: Add...
    • Name: PHP
    • Executable path: /usr/bin/php
    • PHP ini file: /etc/php5/apache2/php.ini
    • SAPI Type: CLI
    • PHP debugger: XDebug
This configuration now should be done on the Run window, instead of the Preferences. Where it says %PROJECT_ROOT%, substitute for the path to your Drupal website (e.g. /var/www/example/index.php).
  • Run > Debug Configurations...
    • PHP Web Page: New
    • Name: Drupal
    • Server Debugger: XDebug
    • PHP Server: Apache
    • File: /%PROJECT_ROOT%/index.php
    • Common: Display in favorites menu: Debug
Yay! You're done with all the configuration! Now, to actually use your system...

Use your new development environment

Debugging

  1. Set your breakpoints
  2. Click on the bug tool: Drupal
  3. PHP Debug perspective should open
  4. Click on the Resume button (F8) every time you perform an action, so that the debugger runs through the entire page load.

Checking out CVS modules

http://drupal.org/node/37615
  1. Right-click on /%PROJECT_ROOT%/sites/all/modules: Import... (substitute %PROJECT_ROOT% for your web root)
    • CVS > Project from CVS
    • Host: cvs.drupal.org
    • Repository path: /cvs/drupal-contrib
    • User: %USERNAME% (substitute %USERNAME% for your CVS account username)
    • Password: %PASSWORD% (substitute %PASSWORD% for your CVS account passowrd)
    • Connection type: pserver
  2. Browse to /contributions/modules/%MODULE_NAME% (substitute %MODULE_NAME% for your module name)
    • Check out into an existing project
    • Checkout subfolders: Yes
  3. Select /%PROJECT_ROOT%/sites/all/modules (substitute %PROJECT_ROOT% for your web root)
  4. Select Tag: Refresh Tags
    • Branches > DRUPAL-X--Y

Using Remote File System

  1. Open Remote System Explorer perspective
  2. Right-click > New Connection...
  3. Select Linux and enter the Host name
  4. From the following prompts, select ssh.files, processes.shell.linux, ssh.shells, ssh.terminals
  5. From the newly created connection, browse to your webroot or project folder
  6. Right-click > Create Remote Project
  7. Go back to the PHP perspective
Play around and have fun! Debugging can be a very satisfying experience, if done well and with purpose. If you managed to read all the way down to the end of this article: wow! I'm impressed! If not, it's okay, someone, somewhere, will find it useful.

No comments:

Post a Comment