Wednesday, September 21, 2011

FastCGI with a PHP APC Opcode Cache

 
 

Sent to you by Danny via Google Reader:

 
 

via Brandon's Blog by Brandon on 7/6/09

Hosting PHP web applications in a shared environment usually involves a choice between two exclusive options: host a fast application by using a persistent opcode cache, or host an application that your shared neighbors can't snoop around or destroy. In this post I discuss a way to get the best of both worlds, by combining FastCGI with a single opcode cache per user.

This is a long post, ready to jump right in? Skip the history!

The evolution of mod_php to FastCGI

In the early days of all-you-can eat shared hosting, administrators served PHP via mod_php. mod_php loads the PHP interpreter into every web server process during server startup, thus alleviating the expense of starting an interpreter each time a script executes. This allowed executing PHP scripts relatively fast.

mod_php came with a few drawbacks:

  • Every server process, even those serving static files such as images and CSS scripts, contained the PHP interpreter. This caused a lot of bloat in the web server's memory footprint. It also eliminated the ability to use mutil-threaded web servers as many PHP extensions are not thread safe.
  • Every PHP script ran as the same user as the web server. While web servers typically run as a non-privileged user such as nobody, multiple mutually untrusting shared accounts could easily access, disrupt or destroy each other by executing a PHP script.

FastCGI loads the PHP interpreter into a separate process. This process is still persistent across connections, but, using a mechanism such as suEXEC, can run as a different user. Static files can be served by a lightweight multi-threaded web server process while PHP scripts are served by a single-threaded FastCGI process. What's more, if PHP crashes, it doesn't bring down the entire web server.

In the shared hosting context, each user's PHP scripts are executed with the user's credentials. This leads to a more secure environment for both the host and the shared user.

The opcode cache

One of the easiest and most effective things you can do to speed up your PHP scripts is to enable an opcode cache such as APC, XCache or eAccelerator. An opcode cache caches the compiled state of PHP scripts in shared memory. Thus each time a PHP script is run, the server doesn't have to waste time compiling the source code. Opcode caches can speed up execution of scripts by up to 5 times and decrease server load.

In my opinion running PHP on a webserver without an opcode cache is like restarting your car's engine at every stop sign. You can still get where you're going but it's going to take longer and put a lot more wear and tear on your engine. An opcode cache is so important that APC is going to be included in the core of PHP 6.

An opcode cache requires that the PHP interpreter process persist between connections. Both mod_php and FastCGI satisfy this requirement. An opcode cache requires RAM, a precious commodity on a shared hosting server. By default, each cache allocates 30MB of shared memory. This can be easily configured up or down depending on the scripts you are running.

Combining FastCGI with an opcode cache

So if we agree that FastCGI and opcode caches are good (a must IMHO), why do most shared hosting providers only enable one? The answer is two-fold:

  1. RAM. Each opcode cache is typically 30MB. Each PHP process gets its own opcode cache. Each user must run its own PHP process for security. Thus each user requires at least 30MB of RAM on top of the RAM required for the PHP interpreter (a lot). All you can eat shared hosting companies typically oversell their servers. Overselling usually works when it comes to bandwidth, I/O and CPU time, however overselling RAM is harder. Remember the PHP processes stay in memory between connections. So a small site only getting a 100 hits a day still hogs the same amount of RAM as a busy site. This breaks the overselling model.
  2. FastCGI. In a typical configuration, FastCGI spawns many separate PHP processes per user. Each PHP process needs its own opcode cache. Instead of maintaining one opcode cache (per user), the server maintains multiple caches. This reduces the effectiveness of the cache and increases the strain on server resources.

Solving problem #1 is hard. Some have suggested a single cache that can be shared across multiple processes and users and still provide assurance that different users cannot mess with each other. This blog post is not about #1. There are many reasons to use unlimited shared hosting providers. Opcode caches are not one.

This blog post is about how to solve problem #2. The goal is to have a reasonable system that utilizes suEXEC, FastCGI and the APC opcode cache. Each user should have one and only one opcode cache. The administrator should be able to adjust the size of the cache for each individual user based on their needs (and monthly fee). Finally, the solution should decrease script load time and increase server performance while maintaining security and privacy between accounts.

mod_fastcgi vs mod_fcgid

I run Apache on my server. Many people suggest running a more lightweight server such as lighttpd. One day I may switch, but for now I've tuned my Apache server to be as fast as I need.

There are two modules to implement FastCGI on Apache – mod_fastcgi and the newer mod_fcgid. Both are binary compatible with each other and do basically the same thing. mod_fcgid sports better control over spawning processes and error detection. mod_fastcgi has been around longer. Both support suEXEC, and both separate PHP from Apache, thus allowing Apache to run threaded workers if desired.

As I mentioned in the combining FastCGI with an opcode cache section, the typical behavior of FastCGI is to spawn multiple PHP interpreters. The FastCGI process monitors each child process, kicking out processes with errors, restarting failed processes and sending incoming requests to the least busy child. This is usually the preferred behavior, and mod_fcgid implements it particularly well.

Opcode caches throw a wrench in this however, because of their inability to share the cache across FastCGI processes. Hopefully one day this will be remedied. Luckily, in the meantime, PHP is capable of playing "process manager" and a single PHP process can spawn several children to handle requests. This way the parent PHP process can instantiate the opcode cache and its children can share it. You'll see this later when we set the PHP_FCGI_CHILDREN environment variable.

Both mod_fcgid and mod_fastcgi can be told to limit the number of PHP processes to 1 per user. The PHP process can then be told how many children to spawn. Unfortunately mod_fcgid will only send one request per child process. The fact that PHP spawns its own children is ignored by mod_fcgid. If we use mod_fcgid with our setup, we can only handle one concurrent PHP request. This is not good. A long running request could easily block multiple smaller requests.

mod_fastcgi will send multiple simultaneous requests to a single PHP process if the PHP process has children that can handle it. This is the reason we must use mod_fastcgi to achieve our goal of one cache per user.

Implementation

This section describes the steps I took to enable suEXEC FastCGI with a single APC opcode cache per user on Apache 2.2. These instructions may vary by Linux distribution and are not intended to be a cut-and-paste howto. I use Gentoo, so most steps will be geared towards a Gentoo install but the general idea should work on any distribution.

1. Install php-cgi and disable mod_php

The PHP interpreter can run in three different modes: as an Apache module, as a CGI binary or as a command line command. Typically, separate binaries are built for the CGI and CLI modes, php-cgi and php respectively. On Gentoo, each mode is associated with a USE flag: apache2 for mod_php, cgi for a CGI binary, and cli for command-line PHP. The cgi USE flag must be enabled. If it isn't, add it to /etc/make.conf or /etc/portage/package.use and recompile PHP. On other distributions, search for a php-cgi binary.

You will want to disable mod_php (if it was enabled) before implementing FastCGI. This can be done by commenting out the appropriate LoadModule line in your Apache configuration file:

# LoadModule php5_module modules/libphp5.so

On Gentoo, this can be easily done by removing PHP5 from the APACHE2_OPTS variable in /etc/conf.d/apache2.

2. Install and enable mod_fastcgi Apache module

We already discussed why we must use mod_fastcgi instead of mod_fcgid. On Gentoo, installing mod_fastcgi can easily be done by running:

$ sudo emerge mod_fastcgi

For other distributions, try installing a mod_fastcgi package or see the FastCGI Installation Notes.

Make sure your Apache conf file contains the line:

LoadModule fastcgi_module modules/mod_fastcgi.so

On Gentoo, this line is found in /etc/apache/modules.d/20_mod_fastcgi.conf. mod_fastcgi is enabled by adding FASTCGI to the APACHE2_OPTS variable in /etc/conf.d/apache2.

3. Install and configure the APC Opcode Cache

To install APC on Gentoo, simply run:

$ sudo emerge pecl-apc

For other distributions, see the Alternative PHP Cache installation instructions.

Once installed, look for the apc.ini file in your php extension configuration directory (e.g. /etc/php/cgi-php5/ext-active). The default apc.ini works with one exception. You need to comment out apc.shm_size="30" (line 5 below). Commenting this line will enable us to set it per user later.

My apc.ini file looks like:

 extension=apc.so apc.enabled="1" apc.shm_segments="1" ;commenting this out allows you to set it in each fastcgi process ;apc.shm_size="30" apc.num_files_hint="1024" apc.ttl="7200" apc.user_ttl="7200" apc.gc_ttl="3600" apc.cache_by_default="1" ;apc.filters="" apc.mmap_file_mask="/tmp/apcphp5.XXXXXX" apc.slam_defense="0" apc.file_update_protection="2" apc.enable_cli="0" apc.max_file_size="1M" apc.stat="1" apc.write_lock="1" apc.report_autofilter="0" apc.include_once_override="0" apc.rfc1867="0" apc.rfc1867_prefix="upload_" apc.rfc1867_name="APC_UPLOAD_PROGRESS" apc.rfc1867_freq="0" apc.localcache="0" apc.localcache.size="512" apc.coredump_unmap="0" 

4. Install/enable Apache suEXEC

Apache 2.2 contains built-in support for executing CGI programs as a different user id and group id than the webserver. This support must be compiled into Apache. On Gentoo, use the suexec USE flag and recompile apache. On other distributions, see Configuring & Installing suEXEC.

5. Create wrapper scripts

The Apache suEXEC security model requires that the CGI binary meet some pretty stringent requirements concerning file ownership and permissions. Rather than copying the php-cgi binary for each user, we create multiple wrapper scripts around the php-cgi binary. These wrapper scripts allow us to set options on a per-user basis.

I keep my wrapper scripts in /var/www/bin, though you may keep yours wherever you want. Each user has a directory in /var/www/bin, for example:

$ ls -l /var/www/bin
dr-xr-xr-x 2 bob bob 104 Jun 24 13:56 bob/
dr-xr-xr-x 2 sue sue 104 Jun 24 13:56 sue/
dr-xr-xr-x 2 joe joe 104 Jun 24 13:53 joe/

Inside each user's bin directory is a single wrapper script, php-fastcgi:

$ ls -l /var/www/bin/bob/
-r-xr-x‐‐‐ 1 bob bob 145 Jun 24 13:56 php-fastcgi

I've shown the ls -l output to show the file and directory ownership and permissions. These are important, and Apache suEXEC will not work correctly if the owner and permissions are not correct.

The contents of the php-fastcgi file in each user's bin directory (see below for an explanation):

 #!/bin/sh  PHP_FCGI_CHILDREN=5 export PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=500 export PHP_FCGI_MAX_REQUESTS  umask 0022 exec /usr/bin/php-cgi -d apc.shm_size=25 

PHP_FCGI_CHILDREN
This variable tells PHP how many child processes it should spawn. As we discussed earlier, our PHP process will act as "process manager" and pass incoming requests to its children. The parent will maintain a single opcode cache which each child will share. The PHP_FCGI_CHILDREN variable tells PHP how many children to spawn. Another way to think of this is the number of concurrent PHP requests that can be handled per user.

PHP_FCGI_MAX_REQUESTS
PHP is known for memory leaks in long running processes. This variable causes each child process to be restarted once it has served a given number of requests (e.g. 500). Only the child process is restarted, the parent process remains. Since the parent process maintains the opcode cache, the opcode cache persists.

umask 0022
This sets the umask the PHP binary will run under. Some people may prefer a stricter umask such as 0077, however I've found 0022 works best as it allows the Apache server running as nobody to read static files written earlier by a suEXEC'd PHP process. Some PHP applications (WordPress plugins) do not do a good job with permissions, and a strict umask can cause applications to fail.

exec /usr/bin/php-cgi -d apc.shm_size=25
This line calls the php-cgi binary and modifies the APC cache size. It is possible to configure this to use a separate php.ini file instead of setting configuration parameters on the command line, however I like the ability to share a single php.ini file.

6. Edit global Apache settings

There are two sets of settings you must configure in Apache: those that affect all users and those that affect a specific user. This section describes global settings that affect all users.

I like to keep my global settings in my /etc/apache/modules.d/20_mod_fastcgi.conf file, but these can go in any part of your http.conf file. Most of the time you do not want this in a VirtualHost section. My global mod_fastcgi settings look like this (see below for an explanation):

 <IfDefine FASTCGI> LoadModule fastcgi_module modules/mod_fastcgi.so  FastCgiConfig -idle-timeout 20 -maxClassProcesses 1 FastCgiWrapper On  AddHandler php5-fcgi .php Action php5-fcgi /cgi-bin/php-fastcgi  <Location "/cgi-bin/php-fastcgi">    Order Deny,Allow    Deny from All    Allow from env=REDIRECT_STATUS    Options ExecCGI    SetHandler fastcgi-script </Location>  </IfDefine> 

FastCgiConfig
The FastCgiConfig configuration directive sets parameters for all dynamic FastCGI processes. The idle-timeout causes FastCGI to abort a request if there is no activity for more than 20 seconds. The maxClassProcesses option is very important: it tells FastCGI to only spawn one php-cgi process regardless of how many requests are pending. Remember that our PHP process will spawn its own children, so FastCGI only needs to spawn one. Until this APC bug is fixed, this is necessary to allow sharing the APC cache among children.

FastCgiWrapper
The FastCgiWrapper configuration directive is needed to allow suEXEC to work.

AddHandler / Action
The AddHandler and Action configuration directives tell Apache to handle all files ending in .php with the php-fastcgi script in cgi-bin. In the next step, you'll see how we alias this cgi-bin directory for each individual user.

Location
The Location directive tells Apache how to handle requests to /cgi-bin/php-fastcgi. The Allow from env=REDIRECT_STATUS on line 13 prevents users from executing this script directly. With this line, the only way to execute php-fastcgi is by requesting a file ending in .php.

7. Edit per-user Apache settings

On my host, every virtual host is associated with one user. And every user has exactly one opcode cache. A single user can have multiple virtual hosts, but these virtual hosts share the same opcode cache.

For each virtual host, I add the following lines, customized for the user associated with that virtual host:

 <VirtualHost *:80> ServerName www.sue.bltweb.net ... <IfModule mod_fastcgi.c>    SuexecUserGroup sue sue    Alias /cgi-bin/ /var/www/bin/sue/ </IfModule> ... </VirtualHost> 

When combined with the global apache settings and the wrapper scripts, this will launch the php-cgi binary using suEXEC to execute as the appropriate user and group whenever a .php file is requested.

There are several different ways to call the FastCGI binary. On my hosts, users don't have access to their cgi-bin directory. The /var/www/bin directory is not accessible by ordinary users. This doesn't have to be the case, the cgi-bin directory could be stored in the user's directory. It is important to note that allowing the user to modify php.ini values allows them to modify their opcode cache size, which could have severe repercussions on RAM usage.

Pros and Cons

The implementation described above is only one of many ways to implement FastCGI and APC. In my opinion, it is the best way to meet my goals, but in this section I'll try to outline some of the advantages and disadvantages of my setup.

Advantages
  • Different users can have different APC cache sizes
  • Multiple concurrent PHP requests can be handled simultaneously
  • RAM usage is predictable as a product of the number of users on the host
  • Server is better secured against attacks from the inside since PHP processes run as the user who owns the script
  • Resource usage can be monitored since each user has a separate PHP process
  • A PHP crash doesn't mean an Apache crash. If a PHP process crashes it is restarted automatically.
Disadvantages
  • The process manager built into mod_fastcgi isn't used. One of the motivations behind mod_fcgid was to improve upon the process manager in mod_fastcgi.
  • The newer mod_fcgid cannot be used as it will only send one request at a time to the PHP process, thus multiple requests can't be handled simultaneously
  • Maintaining separate opcode caches per user uses a considerably larger amount of RAM than a single opcode cache used with mod_php
  • Users cannot alter php.ini files
  • If a PHP script crashes it has potential to take down all of the PHP requests currently being processed for that user

Performance

In my next post I'll try to cover RAM usage, performance, benchmarks, compatibility and best practices. This post is already way too long; I'm surprised you are even still reading it!

Stay tuned for more information on using FastCGI with a PHP APC opcode cache. In the meantime let me know what you think of this approach. Have you tried it? Know of a better way? Found any bugs or problems? Leave a comment below!


 
 

Things you can do from here:

 
 

FreeBSD csh / tcsh: Export Shell Variable

FreeBSD csh / tcsh: Export Shell Variable
by VIVEK GITE on SEPTEMBER 2, 2008 · 2 COMMENTS

Q. I'm using FreeBSD 7 with csh (tcsh) shell. How do I export shell variable under FreeBSD operating systems?

A. tcsh is an enhanced but completely compatible version of the Berkeley UNIX C shell, csh. It is a command language interpreter usable both as an interactive login shell and a shell script command processor. It includes a command-line editor and many other features.

FreeBSD display current environment variables

Type the following command to print current names and values of environment variables:


# setenv

Sample output:

SHELL=/usr/local/bin/bash
TERM=xterm
SSH_CLIENT=10.10.29.66 37484 22
SSH_TTY=/dev/ttyp2
USER=root
PAGER=more
FTP_PASSIVE_MODE=YES
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
MAIL=/var/mail/root
BLOCKSIZE=K
PWD=/root
SHLVL=2
HOME=/root
LOGNAME=root
SSH_CONNECTION=10.10.29.66 37484 10.24.116.2 22
_=/bin/csh
HOSTTYPE=FreeBSD
VENDOR=unknown
OSTYPE=FreeBSD
MACHTYPE=unknown
GROUP=wheel
HOST=vps.nixcraft.in
REMOTEHOST=10.10.29.66
EDITOR=vim
Export shell variable

To export and set new environment variables, enter:
setenv name value
setenv EDITOR /usr/bin/vim

You need to add all your enviorment variables to ~/.cshrc file - csh resource script, read at beginning of execution by each shell. Here is my sample .cshrc file:

alias h history 25
alias j jobs -l
alias la ls -a
alias lf ls -FA
alias ll ls -lA

# A righteous umask
umask 22

set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)

setenv EDITOR vim
setenv PAGER less
setenv BLOCKSIZE M

if ($?prompt) then
# An interactive shell -- set some stuff up
set prompt = "`/bin/hostname -s`# "
set filec
set history = 100
set savehist = 100
set mail = (/var/mail/$USER)
if ( $?tcsh ) then
bindkey "^W" backward-delete-word
bindkey -k up history-search-backward
bindkey -k down history-search-forward
endif
endif
A list of commonly used environment variables

An array of strings called the environment is made available by execve() call when a process begins. By convention these strings have the form name=value. The following names are used by various commands

BLOCKSIZE : The size of the block units used by several commands, most notably df, du and ls. BLOCKSIZE may be specified in units of a byte by specifying a number, in units of a kilobyte by specifying a number followed by K or k, its of a megabyte by specifying a number followed by M or m etc.
COLUMNS : The user's preferred width in column positions for the terminal. Utilities such as ls and who use this to format output into columns.
EDITOR : Default editor name.
EXINIT : A startup list of commands read by ex and vi.
HOME : A user's login directory, set by login from the password file /etc/passwd.
LANG : This variable configures all programs which use setlocale to use the specified locale unless the LC_* variables are set.
MAIL : The location of the user's mailbox instead of the default in /var/mail, used by mail, sh, and many other mail clients.
PAGER : Default paginator program. The program specified by this variable is used by mail, man, ftp, etc, to display information which is longer than the current display.
PATH : The sequence of directories, separated by colons, searched by csh, sh, system, execvp, etc, when looking for an executable file. PATH is set to /usr/bin:/bin initially by login.
PRINTER : The name of the default printer to be used by lpr, lpq, and lprm.
PWD : The current directory pathname.
SHELL : The full pathname of the user's login shell.
TERM : The kind of terminal for which output is to be prepared. This information is used by commands, such as nroff or plot which may exploit special terminal capabilities.
TMPDIR : The directory in which to store temporary files. Most applications use either /tmp or var/tmp. Setting this variable will make them use another directory.
TZ : The timezone to use when displaying dates.
USER : The login name of the user.
Further readings:

man page - csh, tcsh

Reference:
http://www.cyberciti.biz/faq/freebsd-how-to-export-shell-variable/

Tuesday, September 20, 2011

有沒有試過 GNU Screen + dvtm ?或另一套替換 screen 的工具: tmux

有沒有試過 GNU Screen + dvtm ?
http://hsian-studio.blogspot.com/2010/04/screen-dvtm-tiling-window.html

或另一套替換 screen 的工具: tmux

Tmux 教學 + Screen 到 Tmux 的無痛轉換
http://blog.longwin.com.tw/2011/04/tmux-learn-screen-config-2011/

tmux: 超好用terminal multiplexer,把screen冷凍吧!
http://petonline.dyndns.org/node/104

Wednesday, September 14, 2011

Drupal SVN Deployment Strategy

Drupal SVN Deployment Strategy

Submitted by Jay on Mon, 08/23/2010 - 13:15
In the past year, I have started using SVN to deploy my Drupal installations. In the following post I will discuss my own personal SVN deployment strategy. This strategy allows me to work locally on a WAMP installation, and use SVN to deploy my sites to their remote installations.

Disclaimer: By no means am I claiming this is the only way, or the best way to use SVN in conjunction with Drupal. But, it is a method that works well for me, and I thought I would share it with others. If anyone has any comments on how they deploy Drupal themselves, or possible ways I could improve my own deployment strategy, I would love to hear them.

I work on a number of different Drupal installations. I maintain my own site, as well as a number of client sites. In deciding my deployment strategy, I considered the following points:

Drupal will be the same across all installations
The modules and themes (for the most part) will be different in each installation.
I don't want to use SVN for the sites/default/files folder, as the contents of this folder are generally user generated, and should be backed up using other methods
I need a way to deploy database backups and keep them for future reference
There are some situations where I have altered Drupal core. Most people think that this is an absolute no-no, but rather, the rule of thumb should be 'Don't alter Drupal core unless you know what you are doing'
In considering the above points, I decided on the following strategy. First, I need one central repository for Drupal core. This repository has to contain Drupal core, along with any alterations I have made to Drupal core (such as allowing for unlimited uploads per user by role in the upload module - a feature currently available). Every time I create a new site, I simply check out my core files into the new installation directory on the server where the new site will reside.

DRUPAL CORE DEPLOYMENT SCHEMA



In putting together this strategy, I did the following:

I created a Drupal installation on my local PC.
I then made my core alterations, and imported it into the central repository, which is on my own server that I maintain.
I then checked it back out of that respository onto my local PC. This allows me to make future alterations and just check them in if necessary. Note: to preserve my core alterations, I use a technique called Vendor Branching. Explaining how this works is more than I can go into in this posting, but it is explained very well, with a screencast, on gotdrupal.com. You can see the screencast here: Drupal Vendor Branching
When creating a new site, be it local on my machine or on a remote server (usually both, since I initially do my development locally and then deploy the site to the remote server), I simply check do an SVN checkout, which automatically checks out the files into the new directory. The advantage of this is that anytime a new version of Drupal comes out, all I need to do is update the local repository, then navigate to the root directory of each of my Drupal installations and run svn update. This automatically updates the relevant files on each installation, and only takes a few seconds per installation. If necessary I run the update.php script, and the site is updated. I can literally update multiple sites in minutes, whereas it used to take hours, between downloading core, going through and checking all my core modifications in the new files, logging into each installation separately and uploading the entire core through FTP
So that takes care of the common file system (Drupal core) through all of my installations. Next is my deployment strategy for the 'all' folder (modules and themes) in each installation. For the most part, this will be different between each installation. There are some modules that are common to all installations, but I generally just add them to each installation as it is not really a big deal. My 'all' folder looks like this:

'ALL' FOLDER DEPLOYMENT SCHEMA



In working with this, I do the following:

Log into my server and create a new repository for the site in question
Start building the site on my local PC
After doing some work, I import my local 'all' folder into the respository
Check out the repository into my local 'all folder'
Log into the remote Drupal installation (the remote site), and create an 'all' folder in the 'sites' directory
Check out the repository into the remote all folder
Do more work on my local installation. At intervals, I commit my work to the central repository.
Log into the remote installation, navigate to the 'all' folder, and run svn udpate. This grabs all the newest code and updates the folder to the most current version of the modules and themes
Through the above two steps, I have now created a remote Drupal installation and a remote 'all' folder. This covers core, non-core modules, and non-core themes. All are versioned, and all are easily updatable after work done on my local installation. However, there are two more areas which need to be considered, the database, and the sites/default/files directory. As I build individual pages on my local installation through the admin interface, these are only stored in the database. The same goes for any other changes made through the admin interface. As such, I need a way to sync my databases between the installations. I have found the Backup and Migrate module to be excellent for this. It is made specifically for backing up Drupal installations and is one of the modules I install on every single site I create. I also create a respository for my database backups made through this module.

DATABASE BACKUP DEPLOYMENT SCHEMA



To manage database backups, I do the following:

Log into my server and create a new 'db_backups' repository for the site in question
Enable the backup and migrate module on my local PC.
Navigate to Administer -> Content management -> Backup and Migrate -> Destinations (admin/content/backup_migrate/destination), and create a new destination called 'remote'. This destination won't actually be used on the local PC, but since the database is synced between the local installation and remote installation, I need to create the destination here or it will be overwritten each time I re-sync the databases. Note: all destinations, 'manual', 'scheduled' and 'remote', should ideally be placed outside your webroot so as to increase security
Navigate to Administer -> Content management -> Backup and Migrate (admin/content/backup_migrate), and do a quick backup to the manual backups directory
Do an SVN import of the contents of the Backup and Migrate 'manual' directory to the 'db_backups' repository on my server
Do an SVN checkout of the 'db_backups' respository into the Backup and Migrate 'manual' directory
Log into the remote Drupal installation (and install it if I haven't already)
Enable the Backup and Migrate module
Navigate to Administer -> Content management -> Backup and Migrate -> Destinations (admin/content/backup_migrate/destination), and create a new destination called 'remote'. This remote directory WILL be used on the remote installation. This destination should also be placed outside the web root so as to increase security
Do an SVN checkout of the 'db_backups' repository to the 'remote' directory on the remote installation
On the remote installation, Navigate to Administer -> Content management -> Backup and Migrate -> Destinations (admin/content/backup_migrate/destination), and click on 'list files' for the 'remote' destionation
The quick backup that was taken on my local installation should now be visible on this page, as it was checked into the respository and then checked out into the remote folder. Click 'restore' for this backup. This will take you to another page where you click the 'restore' button. This will install the database backup into the remote installation, syncing it with the database on the local installation
As necessary, take a quick backup of the local installation, commit it to the repository, update the contents of the remote folder, and restore the new backup
Once the site goes live, the db_backups repository can be deleted, as the local installation will never contain the same code as the remote installation. However, this repository *could* be used as an external backup for any database backups taken on the remote installation.
And that just leaves the contents of the sites/default/files folder. I generally just upload these through FTP, as there is no real need for versioning of these files, and generally when I am creating a site, this folder is barely even used, as it is mainly files created through the admin interface.

So that is my Drupal SVN deployment strategy. I'm interested in hearing any ideas on how this can be improved, as by no means am I a pro, and/or hearing how other people use SVN with Drupal in order to facilitate a smooth workflow.


Reference:
http://www.jaypan.com/blog/drupal-svn-deployment-strategy

HTML5, canvas, Flotr, SVG, WebGL

HTML5, canvas, Flotr

SVG

Flash

Java

HTML5 - HTML5 is a language for structuring and presenting content for the World Wide Web, a core technology of the Internet. It is the fifth revision of the HTML standard (originally created in 1990 and most recently standardized as HTML4 in 1997[1]) and as of August 2011 is still under development. Its core aims have been to improve the language with support for the latest multimedia while keeping it easily readable by humans and consistently understood by computers and devices (web browsers, parsers etc.). HTML5 is intended to subsume not only HTML4, but XHTML1 and DOM2HTML (particularly JavaScript) as well.[1]

http://en.wikipedia.org/wiki/HTML5

WebGL (Web-based Graphics Library) - is a software library that extends the capability of the JavaScript programming language to allow it to generate interactive 3D graphics within any compatible web browser. WebGL code executes on a computer display card's Graphics Processing Unit (GPU).
WebGL is a context of the canvas HTML element that provides a 3D computer graphics API without the use of plug-ins.[2] The specification was released as version 1.0 on March 3, 2011.[1] WebGL is managed by the non-profit Khronos Group.

http://en.wikipedia.org/wiki/WebGL

canvas - The canvas element is part of HTML5 and allows for dynamic, scriptable rendering of 2D shapes and bitmap images. It is a low level, procedural model that updates a bitmap and does not have a built-in scene graph.

http://en.wikipedia.org/wiki/Canvas_element

Flotr - Flotr is a javascript plotting library based on the Prototype Javascript Framework 1.6+.

http://code.google.com/p/flotr/

======
Code Library

http://code.google.com/apis/ajax/playground/?type=visualization#annotated_time_line
http://code.google.com/p/time-series-graph/
http://www.humblesoftware.com/finance/index
http://solutoire.com/flotr/
http://www.zingchart.com/labs/html5/html5_stock_chart.php
http://www.amcharts.com/download
http://teethgrinder.co.uk/open-flash-chart-2/
http://www.highcharts.com/
http://www.rgraph.net/
http://www.jointjs.com
====
Links

http://www.quora.com/Which-is-better-for-developing-stock-charts-HTML5-Canvas-or-SVG
======

GDB常用指令

在Linux中寫了一個程式,但往往執行的結果都是出乎你的意料,因為可能是某個部份邏輯錯誤,所以可能會造成程式當掉了,因此我們使用GNU gdb這個除錯程式來除bug,而常使用的基本指令和基本用法,介紹給大家。

※ 編譯時加入-g的參數

為了讓gdb可以debug你的程式,所以在你編譯的期間,必須加入-g的參數,如此才可以讓gdb來除錯,所以編譯時的語法。

gcc ex.c -g -o ex

基本上編不過就是你程式語法或是語義的錯誤,而除錯主要是用在,程式可以正確的執行,但是執行出來的結果卻不是你要的,如果才需要除蟲。開始執行gdb除錯程式。

gdb ./your_programe

※ gdb的指令

進入gdb之後會類似像shell一樣有個提示字元,所以也和shell一樣要輸入你要執行的指令。

1. 環境的設定

* file ./your_programe 開始別的程式除錯
* set args xxx xxx 將xxx xxx當成參數執行你的程式
* set listsize n 設定使用list指令會一次顯示n行

2. 中斷點的設定

* break(b) n | function_name 設定中斷點在行或函式
* break(b) filename:n 如果是多個c檔案時指定filename和第n行
* clear n 清除第n行的中斷點
* delete index 清除第index號的中斷點
* disable index 暫時使第index號中斷無作用
* enable index 使第index號中斷再作用

3. 開始執行debug

* run 當都設定好你要給此程式的參數後開始執行
* quit 離開gdb除錯程式

4. 開始追蹤程式

* continue(c) 繼續執行直到下一個中斷點或結束
* next(n) 執行一行程式碼,不會跳進函式去執行
* step(s) 執行一行程式碼,如果碰到函式會跳進函式內部去執行
* until(u) 跳離一個while for迴圈

5. 監看你想知道的變數值

* print(p) 印出某個變數的資料
* display 會每次step, next時都會印出值來,print只印一次
* list(l) 向下列出程式碼,和listsize行數有關
* list(l) - 向上列出程式碼,和listsize行數有關
* list(l) n 列出第n行的程式碼

6. 其它觀看設定

* info (i) break (b) 看目前有建立的中斷點列表
* show args 顯示目前給此程式的參數
* show listsize 顯示目前使用list指令會一次顯示多少行

Reference:
http://hsian-studio.blogspot.com/2008/09/gdb.html

GCC常用編譯參數

gcc是在unix like system上最常用的編譯程式,而它的參數實在非常的多,功能非常的強大,不過常常會用到的也只有那幾個而已,列出幾個代表性的編譯選項。

※ 使用方式

gcc [option] filename

※ 選項

* -c : 只做編譯(不做連結)
* -S : 輸出組譯碼
* -E : 將預處理結果顯示
* -o filename : 指定輸出檔名
* -ansi : 程式要求依據ansi c標準
* -Dmacro : 使定義巨集(marco)為有效
* -Dmarco=defn : 使定義巨集(marco)為defn
* -Wa,option : 將選項(option)傳給組譯器
* -wl,option : 將選項(option)傳給連結器
* -I : 追加include檔案的搜尋路徑
* -L : 追加library檔案的搜尋路徑
* -l : 指定連結的函式庫
* -Wall : 顯示所有的警告訊息
* -g : 編入除錯資訊(要使用GDB除錯一定要加)
* -O2 : 做最佳化

※ 使用範例

Example:

gcc -o file a.c b.c c.c
gcc -Wall -g -o test test.c
gcc -Iinclude -Llibrary -lmy_lib -o test1 test1.c
gcc -DDEBUG_ON -o test2 test2.c
gcc -c -o test3 test.c

Reference:
http://hsian-studio.blogspot.com/2008/09/gcc.html