Centos 下JBoss as7 + jdk+mysql+nginx 网站配置
网站基本框架设计好了,下来就开始搭建服务器框架了 。
服务器组选的是 VPS,为什么选VPS,因为我们除了要使用公共的web服务外,还要定制一些系统的软件和服务,自定义上报端口等。除了自己承包整机服务器外,VPS无疑是最佳选择了。
机器配置
CentOS Linux 6.2
Linux 2.6.18-274.7.1.el5.028stab095.1 on i686
Intel(R) Xeon(R) CPU E5645 @ 2.40GHz, 1 cores
内存2G
硬盘100G
网站架构见我之前的帖子:http://www.cnblogs.com/inteliot/archive/2012/04/17/2454518.html
选用nginx主要做静态、动态内容的分离和负载均衡。jboss完成 jsp和后台的java代码的环境支持。
为什么不选tomcat而是JBoss,上网google了一圈,似乎Jboss要好于tomcat,所以就想试试效果
JBoss AS7 支持 集群管理,有standalone模式和domainserver模式,domainserver模式支持多级分层架构,可以支持大的企业级应用。
我们的生产环境采用standalone。
mysql
查看已安装的包:
rpm -qa | grep mysql
强制性卸载老版本:
rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64
安装新版本:
说明:rpm安装方式默认是没有my.cnf的
1.错误:
解决:
下载 libaio-0.3.107-10.el6.x86_64.rpm
安装:rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm
或者:
# yum install libaio
JDK
下载页面http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u3-download-1501626.html
解压
修改系统环境变量
vi /etc/profile
在末尾加入
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=$JAVA_HOME/lib:$CLASSPATH
立即生效
source /etc/profile
检测是否安装成功
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build
1.7.0_03-b04)
Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode)
Jboss AS7
wget http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.tar.gz
解压完 运行
./bin/standalone.sh
错误一:安装完jdk后,没有配置java环境变量,导致java调用失败,可以运行 java --version,检测是否ok
错误二:jboss7.1 standalone 模式正常,domainserver模式跑步起来,出现错误:
[Host Controller] Error occurred during initialization of VM
[Host Controller] Could not reserve enough space for object heap
[Host Controller] Error: Could not create the Java Virtual Machine.
[Host Controller] Error: A fatal exception has occurred. Program will exit.
原因:Xmx 和 MaxPermSize 过大,预留失败
修改:/usr/local/jboss/bin/domain.conf
JAVA_OPTS="-Xms32m -Xmx64m -XX:MaxPermSize=64m -Djava.net.preferIPv4Stack=true -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000"
同时需要在 /etc/profile 下添加
export _JAVA_OPTIONS="-Xms32m -Xmx64m -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=40 -XX:NewSize=10m -XX:MaxNewSize=10m -XX:SurvivorRatio=6 -XX:TargetSurvivorRatio=80 -XX:+CMSClassUnloadingEnabled"
添加完后 需要 source /etc/profile
修改完后,jboss 初步运行起来了:
最后,开机自启动
在 /etc/rc.d/rc.local 加上启动命令就可以了
NginX 安装
一、准备工作
下载OpenSSL :
wget http://openssl.org/source/openssl-1.0.1.tar.gz
下载pcre
wget http://downloads.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpcre%2Ffiles%2Fpcre%2F8.30%2F&ts=1336739904&use_mirror=cdnetworks-kr-2
wget http://downloads.sourceforge.net/project/libpng/zlib/1.2.6/zlib-1.2.6.tar.gz?r=&ts=1336743664&use_mirror=cdnetworks-kr-1
分别解压到
/usr/openssl-1.0.1/
/usr/zlib-1.2.6/
/usr/pcre-8.30/
二、安装nginx 包
wget http://nginx.org/download/nginx-1.0.14.tar.gz
1. 配置 和编译
./configure --user=www --group=www --prefix=/data/web/nginx --add-module=../ngx_cache_purge-2.0 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre=../pcre-8.30/ --with-openssl=../openssl-1.0.1/ --with-zlib=../zlib-1.2.6/
make && make install
错误:
&& make clean \
&& ./config --prefix=/usr/openssl-1.0.1//.openssl no-shared no-threads \
&& make \
&& make install LIBDIR=lib
make[2]: Entering directory `/usr/openssl-1.0.1'
Makefile is older than Makefile.org, Configure or config.
Reconfigure the source tree (via './config' or 'perl Configure'), please.
make[2]: *** [Makefile] Error 1
make[2]: Leaving directory `/usr/openssl-1.0.1'
make[1]: *** [/usr/openssl-1.0.1//.openssl/include/openssl/ssl.h] Error 2
make[1]: Leaving directory `/home/zcj/nginx/nginx-1.0.14'
make: *** [build] Error 2
解决:
错误提示,进入openssl 目录,执行
./config
回到nginx目录,重新 make
错误
CDPATH="${ZSH_VERSION+.}:" && cd . && /bin/sh /usr/pcre-8.30/missing --run aclocal-1.11 -I m4
aclocal-1.11: couldn't open directory `m4': No such file or directory
make[2]: *** [aclocal.m4] Error 1
make[2]: Leaving directory `/usr/pcre-8.30'
make[1]: *** [/usr/pcre-8.30//.libs/libpcre.a] Error 2
make[1]: Leaving directory `/home/zcj/nginx/nginx-1.0.14'
make: *** [build] Error 2
解决 :在pcre 目录下创建空目录 m4
错误
libtool: and run autoconf again.
make[2]: *** [pcre_byte_order.lo] Error 63
make[2]: Leaving directory `/usr/pcre-8.30'
make[1]: *** [/usr/pcre-8.30//.libs/libpcre.a] Error 2
make[1]: Leaving directory `/home/zcj/nginx/nginx-1.0.14'
make: *** [build] Error 2
解决: 用系统安装的libtool 执行文件替换pcre 下的libtool
cp -f /usr/bin/libtool /usr/pcre-8.30/
OK。成功
2. 添加开机自启动
vi /etc/rc.d/rc.local
添加开机运行命令: /usr/local/webserver/nginx/sbin/nginx
三、运行nginx
运行前,先卸载httpd :rpm -e httpd --nodeps
运行nginx :/usr/local/webserver/nginx/sbin/nginx
1. 错误: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决:
添加链接 ln -s /usr/local/lib/libpcre.so.1 /lib/libpcre.so.1
x64环境下是 ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1
2. 错误:[emerg]: getpwnam(“www”) failed
原因 :配置时用的用户和组不存在
解决:
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www
四、 配置nginx
1. 配置nginx日志目录及网站根目录
2. 修改 conf文件
vi /usr/local/webserver/nginx/conf/nginx.conf
worker_processes 8;
error_log /var/log/nginx/nginx_error.log crit;
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
upstream jboss_svr{
server 127.0.0.1:8080;
}
server
{
listen 0.0.0.0:8088;
index index.html index.htm index.php;
root /var/www/server1;
#limit_conn crawler 20;
location ~ .*\.(jsp|jspx|do)?$
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_pass http://jboss_svr;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
location ~* .*\.sh$ {
return 403;
}
log_format server1_log '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/log/nginx/server1.log server1_log;
}
}
五. 在不停止Nginx服务的情况下平滑变更Nginx配置
1. 修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:
/usr/local/webserver/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
2. 平滑重启:
对于Nginx 0.8.x版本,现在平滑重启Nginx配置非常简单,执行以下命令即可:
/usr/local/webserver/nginx/sbin/nginx -s reload
六、编写每天定时切割Nginx日志的脚本
1、创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
输入以下内容:
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/webserver/nginx/logs/"
mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
2、设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
输入以下内容: