Showing posts with label Server. Show all posts
Showing posts with label Server. Show all posts

Friday, January 28, 2011

Tomcat URL 支持中文文件名

在tomcat的默认配置情况下,如果访问一个"中文.html"页面,服务器会抛出文件找不到的错误提示,这需要对tomcat的配置文件略作改动,为Connector设置URIEncoding的参数。
在server.xml文件中找到8080服务器配置的一段设置:

<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/>


加上 URIEncoding="UTF-8" 这句就可以识别中文文件名了,同时如果url中的参数值有中文,服务器端也可以正常解析对应的参数,否则服务器端Java程序中取到的参数是以ISO-8859-1编码的,有中文字符的参数值是乱码的,加了URIEncoding="UTF-8"之后,就可以正确获取到参数值了。
对于这个参数的说明,可以查看tomcat目录中的doc帮助文件,也可以从官网中找到此参数的解释。

References: 官方文档
Configure tomcat's uri encoding

Saturday, November 27, 2010

用maven来运行一个main方法或者启动Server

在maven项目的pom.xml文件的plugins中加入"exec-maven-plugin"这个插件,这个在运行"mvn package"时,会在当前的mvn进程中直接执行指定的class文件的main方法,也可以配置其他的参数,让此main在另一个java进程中启动。如果其中将phase的内容改为"test",就会在运行"mvn test"时执行main方法,也可以在命令行里直接用mvn运行,如下注释说明。
更详细的信息和配置方法,可参考http://mojo.codehaus.org/exec-maven-plugin/usage.html说明。

<!-- commandline: mvn exec:java -Dexec.mainClass="org.phpfirefly.test.Server" -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.phpfirefly.test.Server</mainClass>
</configuration>
</plugin>

maven2 repository server install

以下为安装免费版本的nexus maven2服务器和简单的设置过程:


$> wget http://nexus.sonatype.org/downloads/nexus-oss-webapp-1.8.0-bundle.tar.gz
$> tar zxvf nexus-oss-webapp-1.8.0-bundle.tar.gz
$> mv nexus-webapp-1.8.0-bundle /usr/local/nexus
$> cd /usr/local/nexus
$> ls bin/jsw/
$> bin/jsw/linux-x86-32/nexus start
$> tail -f logs/wrapper.log

启动服务后默认URL为:http://localhost:8081/nexus
默认的登录名和密码:admin/admin123
nexus默认是关闭远程索引下载功能的,主要是担心会造成对服务器的巨大负担,需要我们手工开启。
开启的方式: 点击Administration菜单下面的Repositories,将这4个仓库Apache Snapshots,Google code,Codehaus Snapshots,Maven Central的Configuration - Download Remote Indexes修改为true。然后在这三个仓库上分别右键,选择reIndex,这样Nexus就会去下载远程的索引文件。
部署构件至Nexus: Nexus提供了两种方式来部署构件,你可以从UI直接上传,也可以配置Maven部署构件,在上传一个版本时,可以将jar包和source jar包一起上传。

本地maven仓库配置文件,默认位置在用户根目录下的".m2"目录,文件名为settings.xml,如果没有,则创建一份,更新内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
<pluginGroup>org.mortbay.jetty</pluginGroup>
</pluginGroups>

<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>local nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

References: http://juvenshun.javaeye.com/blog/349534
http://wj98127.javaeye.com/blog/306358

Tuesday, March 09, 2010

一个tomcat启动二个不同端口的web服务

首先在server.xml模仿Service Catalina一样再配一个Service,修改Service/Engine的name,Connector的port

<Service name="Catalina2">
<Connector port="8081" />
<Engine name="Catalina2" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
</Host>
</Engine>
</Service>
其次复制conf下的Catalina整个目录为新的Service名字,注意里面localhost文件夹里的配置因项目不同,进行调整:
$> cp -r conf/Catalina conf/Catalina2

Saturday, September 27, 2008

MySQL Replication server_errno=2020

080927 15:28:42 [Note] Slave: connected to master 'test@localhost:3306',replication resumed in log 'mysql-bin.000003' at position 34699088
080927 15:28:42 [ERROR] Error reading packet from server: Got packet bigger than 'max_allowed_packet' bytes ( server_errno=2020)
080927 15:28:42 [Note] Slave I/O thread: Failed reading log event, reconnecting to retry, log 'mysql-bin.000003' position 34699088
080927 15:28:42 [Note] Slave: connected to master 'test@localhost:3306',replication resumed in log 'mysql-bin.000003' at position 34699088


$> vi /etc/my.cnf
# max_allowed_packet = 1M # modify it to
max_allowed_packet = 10M

Thursday, September 18, 2008

Rsync install and configure

Firstly, download rsync and make install, then:
Rsync Server configure file example:


motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[MODULENAME]
path = /data/backfrom
comment = back data using rsync server
uid = nobody
gid = nobody
read only = no
list = yes
auth users = test
secrets file = /etc/rsyncd.scrt

$> rsync --daemon --port=873

$> vi /etc/rsyncd.scrt
test:testYu@Gmail.com
$> chmod 400 /etc/rsyncd.scrt

==================
Client:

$> vi /home/users/passwordfile
testYu@Gmail.com
$> chmod 400 /etc/rsyncd.scrt

$> rsync -vzrtopg --progress --delete test@192.168.0.1::MODULENAME /data/backto --password-file=/home/users/passwordfile

==================
OR connect rsync server using ssh:

#!/bin/sh
DEST="192.168.0.1"
USER="test"
BACKDIR="/data/backfrom/"
DESTDIR="/data/backto/"
OPTS="-vzrtopg --rsh=ssh --stats --progress"
VAR=`ping -s 1 -c 1 $DEST > /dev/null; echo $?`
if [ $VAR -eq 0 ]; then
rsync $OPTS $BACKDIR $USER@$DEST:$DESTDIR
else
echo "Cannot connect to $DEST."
fi

Wednesday, May 21, 2008

ruby script/server lighttpd

在一个jruby on rails目录下面运行以下命令时报错:
$> ruby script/server lighttpd
.....
Couldn't find any pid file in '/Users/yu/Sites/RubyOnRails/tmp/pids' matching 'dispatch.[0-9]*.pid'
(also looked for processes matching "/Users/yu/Sites/RubyOnRails/public/dispatch.fcgi")
在lighttpd的log中看到如下信息:
2008-05-21 00:23:33: (log.c.75) server started
2008-05-21 00:23:33: (mod_fastcgi.c.1029) the fastcgi-backend /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi failed to start:
2008-05-21 00:23:33: (mod_fastcgi.c.1033) child exited with status 9 /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi
2008-05-21 00:23:33: (mod_fastcgi.c.1036) If you're trying to run PHP as a FastCGI backend, make sure you're using the FastCGI-enabled version.
You can find out if it is the right one by executing 'php -v' and it should display '(cgi-fcgi)' in the output, NOT '(cgi)' NOR '(cli)'.
For more information, check http://trac.lighttpd.net/trac/wiki/Docs%3AModFastCGI#preparing-php-as-a-fastcgi-programIf this is PHP on Gentoo, add 'fastcgi' to the USE flags.
2008-05-21 00:23:33: (mod_fastcgi.c.1340) [ERROR]: spawning fcgi failed.
2008-05-21 00:23:33: (server.c.908) Configuration of plugins failed. Going down.

经google后才发觉script/server lighttpd只能用于ruby on rails的项目上,通过以下命令:
$> /usr/local/lighttpd/bin/spawn-fcgi -f /Users/yu/Sites/RubyOnRails/public/dispatch.fcgi -p 12000 -s /tmp/rails-fcgi.socket
会得到成功的结果如下:
spawn-fcgi.c.197: child spawned successfully: PID: 2275
而在jruby的项目下调用spawn-fcgi则会报脚本错误。

另附dispatch.sh[reference: http://www.javaeye.com/topic/168989]


#!/bin/sh

DISPATCH_PATH=/Users/yu/Sites/cv2/public/dispatch.fcgi
SOCKET_PATH=//Users/yu/Sites/cv2/tmp/sockets
RAILS_ENV=developement
export RAILS_ENV

case "$1" in

start)
for num in 0 1 2
do
/Users/yu/Programs/lighttpd/bin/spawn-fcgi -f $DISPATCH_PATH -s $SOCKET_PATH/rails.socket-$num
done
;;

stop)
killall -9 dispatch.fcgi
;;

restart)
$0 stop
$0 start
;;

*)
echo "Usage: dispatch.sh {start|stop|restart}"
;;

esac

exit 0

Thursday, November 08, 2007

CentOS5.0 VS-NAT config

参考http://zh.linuxvirtualserver.org/node/26,环境如下:
linux centos5.0 server: eth0 192.168.0.199(RIP); eth0:1 192.168.1.199(VIP); gateway 192.168.1.1(连外网)
windowsXP: 192.168.0.109(RIP); gateway: 192.168.0.199(网关设置为Linux CentOS5.0机器的IP)
windowsXP: 192.168.0.112(RIP); gateway: 192.168.0.199(网关设置为Linux CentOS5.0机器的IP)
XP机器都有开80端口WEB服务,linux机器上80端口的服务不一定要开。

1. 在CentOS5.0上搭建NAT
$> vi /etc/sysctl.conf
net.ipv4.ip_forword = 1
$> /sbin/sysctl -p
(需要安装iptables的模块,用这个命令检查: $> modprobe ip_tables)
(也能用以下命令开启NAT: $> echo "1" > /proc/sys/net/ipv4/ip_forword)

2. 关闭iptables service,测试NAT是否启用
$> service iptables stop
将其中一台XP机器的网关设置为此CentOS5.0的VIP地址192.168.1.199,IP改为192.168.1.109(如果real server是linux机器,修改了ip和gateway需要重启网络: $> service network restart),正确的话就可以正常访问局域网内其他192.168.1.*网段的机器和外网。
测试NAT通过后:
($> iptables -t nat -A POSTROUTING -j MASQUERADE -s 192.168.1.0/24)可选
$> iptables -t nat -A POSTROUTING -j MASQUERADE -s 192.168.0.0/24
$> service iptables save(先stop再save会覆盖原来iptables规则,如果不想覆盖则在iptable开启状态进行这二个命令)
$> service iptables start
再测试NAT是否正常,能否从XP机器联外网,如果正常那就把IP和gateway改回原来的设置。如果不能访问外网,需要调整一下iptables reject规则,具体可以看鸟哥的NAT设置相关文章。

3. $> yum install ipvsadm
$> chkconfig ipvsadm on
(linuxcommand info: http://www.linuxcommand.org/man_pages/ipvsadm8.html)
$> vi /etc/sysconfig/ipvsadm
ipvsadm -A -t 192.168.1.199:80 -s rr
ipvsadm -a -t 192.168.1.199:80 -r 192.168.0.109:80 -m -w 1
ipvsadm -a -t 192.168.1.199:80 -r 192.168.0.112:80 -m -w 1
# ipvsadm -a -t 192.168.1.199:80 -r 192.168.0.199:80 -m -w 1
$> service ipvsadm start
$> ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.199:80 rr
-> 192.168.0.112:80 Masq 1 0 1
-> 192.168.0.109:80 Masq 1 0 0

4. $> yum install heartbeat
$> yum install heartbeat-ldirector
$> chkconfig ldirectord on
$> vi /etc/ha.d/ldirectord.cf


# Global Directives
checktimeout=10
checkinterval=2
# fallback=127.0.0.1:80
autoreload=no
# logfile="/var/log/ldirectord.log"
logfile="local0"
quiescent=yes

# Virtual Server for HTTP
virtual=192.168.1.199:80
fallback=127.0.0.1:80
# real=192.168.0.199:80 masq
real=192.168.0.109:80 masq
real=192.168.1.112:80 masq
service=http
request="heartbeat.html"
receive="Test Page"
scheduler=rr
# persistent=600
protocol=tcp
checktype=negotiate

在WEB Server的根目录下要有heartbeat.html这个文件,并且里面内容为Test Page字符串,Ldirector检测到这个文件并接收到的字符串能匹配上这个字符串即认为此Real Server为活跃的,不然会从集群中移除此server

5. 在192.168.1.***网段内找一个client机器访问http://192.168.1.199/index.html,同时在LVS上用以下命令查看连接数:
$> ipvsadm -L -c
IPVS connection entries
pro expire state source virtual destination
可在上面的list中看到NAT将source发起的http请求调度到不同的destination上,正常的话应该能看到index.html页面内容。
需要注意的一点是请求的Client机器不能和集群的Real Server在同一个子网内,也不能在RealServer上发过Client请求,这样无法经NAT调度就不能正常显示index.html页面。

Monday, October 22, 2007

ntpdate error: No Server suitable for synchronization found

主要是ntpd server的iptables把udp的123端口关了,局域网内其他机器无法访问到Server,所以才会报No Server suitable for synchronization found这个错误。

打开此udp端口重启iptables后用/usr/sbin/ntpdate ntp-server-address; /sbin/hwclock -w命令即可,当然client端最好是做个Crontab定时跑一下这个命令,同步时间。

Thursday, May 24, 2007

mysql server and client config file on windows

MySQL客户端和实用程序,例如mysql命令行客户端和mysqldump不能将my.ini文件放到服务器安装目录中。要想配置客户端和实用工具,根据Windows 版本,在C:\Windows或C:\WINNT目录下创建一个新的my.ini/my.cnf文件。

对于Windows MySQL服务器,根据下面规则确定服务器使用的服务名和选项文件:

· 如果在服务安装命令中,没有在--install选项后面指定了服务名或使用默认服务名(MySQL),服务器则使用MySQL服务名并从标准选项文件的[mysqld]组读取选项。

· 如果在服务安装命令中,在--install选项后面指定的服务名不是默认服务名(MySQL)。则从具有相同服务名的组中读取选项,并从标准选项文件读取选项。

服务器还从标准选项文件的[mysqld]组读取选项。你可以使用[mysqld]组中的选项用于所有MySQL 服务,还可以使用具有相同服务名的组,用于该服务名所对应的服务器。

· 如果在服务安装命令中,在服务名后面指定了--defaults-file选项,服务器只从命名文件的[mysqld]组中读取选项,忽略标准选项文件。

对于更复杂的例子可以考虑使用命令:

C:\> C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqld --install MySQL --defaults-file=C:\my-opts.cnf
该命令中,--install选项后面给出了默认服务名(MySQL)。如果未给出--defaults-file选项,该命令可以让服务器从标准选项文件的[mysqld]组中读数。由于提供了--defaults-file选项,服务器只从命名文件的[mysqld]组读取选项。

还可以在启动MySQL服务之前,在Windows Services工具中指定启动参数选项。

MySQL服务器安装为服务后,Windows启动时自动启动服务。还可以从Services工具直接启动服务,或使用命令NET START MySQL。NET命令忽略大小写。

Tuesday, March 20, 2007

winddows 2003 server 无法启动mongrel_rails service

mongrel_rails 在windows 2003 server上安装好service之后,不能正常启动,而在cmd下可以正常启动,后检查mongrel_service.log发现因为ruby,rails,mongrel_rails都是装在Program Files里,因为文件夹里空格造成service启动时找不到程序路径才启动失败,将ruby移到C盘根目录下,修改一下rails及mongrel_rails等文件里的路径,即将C:\Program Files\Ruby\bin\rails改为C:\Ruby\bin\rails,其他几个文件雷同处理即可。