Archive for the ‘Web Server’ category

Memo: log4j in tomcat (备忘:Tomcat中配置log4j)

March 9th, 2015

In a tomcat project, we can configure log4j to write logs in a file with:
log4j.appender.rollingFile.File=${catalina.home}/logs/services.log.
This will put all the log files in the /log dir under tomcat root dir.

Here is a full version of the sample log4j.properties.

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# log message format: 10:48:40,399 INFO Test:36 - [3, 2, 1]
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct log messages to file ###
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=${catalina.home}/logs/services.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=5
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
# log message format: INFO main wp.newsbag.Test - message ...
#log4j.appender.rollingFile.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.rollingFile.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c:%L - %m%n
log4j.logger.wp.newsbag=ERROR
#log4j.rootLogger=debug, stdout
log4j.rootLogger=debug, rollingFile

Location of tomcat work dir in eclipse (Eclipse中tomcat工作目录)

March 9th, 2015

The default dir for eclipse to publish we apps in tomcat is WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmpX, where X could be 0, 1, 2, depending on how servers you have configured. Of course, the dir can be changed as shown in the Server project, as shown in the pic below:

Configure work dir for tomcat in Eclipse

————————–

备忘:Eclipse中tomcat的默认工作目录为:WORKSPACE\.metadata\.plugins\org.eclipse.wst.server.core\tmpX,X可能为0,1,2….。当然,这个目录可以在Server Project中修改,如上图所示。

Memo: tomcat configurations

December 7th, 2013

Here are some common used configurations of tomcat.

  • Add a new tomcat host. In server.xml, add these lines:
    
    		<Host name="youdomain.com" appBase="/var/www/domain_webapps_dir"
                    unpackWARs="true" autoDeploy="false">
                    <Context path="" docBase="/var/www/domain_webapps_dir/proect_name" reloadable="true" />
                    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                            prefix="xxx_access_log." suffix=".txt"
                            pattern="%h %l %u %t "%r" %s %b" />
                    </Host>
                    

Configure Tomcat 7 & Eclipse on Ubuntu

June 28th, 2013

Install Eclispe

Either via sudo apt-get install eclipse and then install Web Tools Platform for JavaEE, or download Eclipse for JavaEE.

Install tomcat

We can also install tomcat in 2 ways:
1. sudo apt-get tomcat7
2. download tomcat7 and extract it into folder, such as ~/Prog/tomcat

Integrate Eclipse and Tomcat

When I run the project with “Run On Server” and follow the instruction of “Define a new server” with tomcat v7, I always get this error message: “Cannot create a server using the selected type”. Well, here is the solution: http://oramirez.wordpress.com/2012/02/03/eclipse-with-tomcat-v7-0-in-ubuntu-11-10/.

And then somehow I encounter this error: Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config. The configuration may be corrupt or incomplete., and http://www.jusuchyne.com/codingchyne/2010/10/installing-tomcat-7-on-ubuntu/ tells that it’s because eclipse failed to copy the conf files into eclipse tomcat conf. so:
1. first sudo chmod -R 777 ~/Prog/tomcat7/conf;
2. and delete the server in Eclipse Perferences -> Server -> Runtime Environment, re-add it;
3. and then re-define the server at “Run on Server”.

problem with multi fastcgi_finish_request requests in nginx

May 1st, 2013

The php server nginx has a very handy function fastcgi_finish_request(). If fastcgi_finish_request is called in a function, the function will return immidiately but still do the job in background. So it is intuitive to think of using fastcgi_finish_request() to do some jobs in parallel when needed.

However, when a process call the fastcgi_finish_request() many times in a row, the later calls have to wait for the previous call to finish. So it’s not such a big help when you call fastcgi_finish_request() many times in a row, because all these requests will be executed in sequence. Rather you should put all the long calls in a fastcgi_finish_request() call.

For example:

function a() {
     long_work(); // 1st call
     // if we write a log here, we'll find 1st call immediately returns.
     long_work(); // 2nd call
     // if we write a log here, we'll find this 2nd call will have to wait for 10s before it returns.
}

function long_work() {
    sleep(5);
    fastcgi_finish_request();
}

A workaround to mimic multithread with fastcgi_finish_request() is to use curl. We put the long work in a php file, and then use curl to request the php file.

// ------ long_work.php ----------
long_work();
function long_work() {
    sleep(5);
    fastcgi_finish_request();
}


And then in function a(), we can request long_work many times.

function a() {
    // curl( 'http://localhost/long_work.php' ); // 1st call
    // curl( 'http://localhost/long_work.php' ); // 2nd call
}

Memo of using analog to measure website traffic

July 6th, 2012

A sudden heavy load was detected on our server. On this server the only public service is https which serves a few websites. I check the auth logs and didn’t find anything abnormal. So I wonder it’s some sudden heavy requests were thrown on the apache server. I have never used any web log analyzer before, and googled a little and found analog should do the job for me. Here is just a memo of what I did today.

Install the analog package

On a debian/ubuntu server, one can easily install analog by:
#> (sudo) apt-get install analog
On windows, one can simply download the latest binary package, extract it, and then use it.

Basics of analog

Analog is a program that is used to analyze the apache logs and show the statics that may interest you.

To use analog, you need to provide it a config file, telling it where the logs are, and where to output the results, and what things should be in the results. The default config file in debian (if you install it by “apt-get”) will be /etc/analog.cfg. You can edit the file and then run analog without any parameter. Here is an example:

<VirtualHost 212.87.82.24>
# tell analog where the log file is
LOGFILE /var/log/apache2/access.log
# tell analog where and the format to output the result
OUTFILE /home/me/host.stat.html
# the name of the host. this will be part of the title of the report.
HOSTNAME "ONTILES.COM"
# the link of the host.
HOSTURL "http://www.ontiles.com/"
# analog use some simple images to show the bars or to make the report more friendly. If you installed analog by "apt-get", there are under "/usr/share/analog/images/"; if you installed analog by download the binary package, the images directory is under the extracted root directory.
IMAGEDIR "/usr/share/analog/images/"

How to use a different config file and how to analyse more than one website

see this great article HOW-TO Use Analog for Virtual Hosts (Using Separate Logfiles)

How to show different sections or columns

analog can show the report by hourly, daily, monthly or yearly. Each can be a section in the report. And in each section, you can choose what things to show. Refer to http://www.analog.cx/docs/timereps.html & http://www.analog.cx/docs/othreps.html to see the things you can show.

One can find the detail document here: http://www.analog.cx/docs/Readme.html

.htaccess rewrite to redirect non-www to www

August 25th, 2011

htaccess redirect www to non-www


#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

htaccess redirect non-www to www


#redirect non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

htaccess redirect from one site to another


RewriteEngine on
RewriteCond %{HTTP_HOST} ^OLD_DOMAIN\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.OLD_DOMAIN\.com$
RewriteRule ^(.*)$ "http\:\/\/NEW_DOMAIN\.com\/$1" [R=301,L]

How to install MemCache on Windows 7 X64 for PHP with WAMP

August 22nd, 2011

Basics

The memcache sonsists of 2 parts: the server MemCached and the client MemCache.

  • MemCached is the cache server, the same as Mysqld (mysql server), which save and feed cache to clients.
  • MemCache is a client. With php, we need to install it as a mod, just the same as pdo_mysql (php_pdo_mysql.dll on windows).

Install MemCached

  • Download memcached from http://code.jellycan.com/memcached/ (get the win32 binary version). Here we suppose you extract the memcached in d:\memcached\.
  • If you are on Sindows Vista/7, rightclick on memcached.exe and select Properties; click on the Compatibility tab. At the bottom you’ll see Privilege Level, check “Run this program as an administrator”.
  • Open the command line by Win + R (or Start -> All Programs -> Accessories -> Command Prompt), and then run: d:\memcached\memcached.exe -d install to install the service.
  • Still in the command line, run d:\memcached\memcached.exe -d start, or net start "memcached Server" to start the service.
  • Check whether the service is running. Open Windows Task Manager, go to Services tab, you should find status of “memcached server” is “Running”.

Install MemCache php module

Note

  • To change MemCached memory size (default is 64mb), go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to:
    “C:\memcached\memcached.exe” -d runservice -m 512
  • More details see: memcached -help

default icons for apache?

March 11th, 2010

When I make a dir “icons” under the root of a virtual server, for example, http://localhost:8088. It just covers what I put under this folder and goes to a default icon folder seems to be a part of apache. Why?

ways to build session server (cluster)

January 13th, 2010

There are many ways to build centre session server.