因公司运营环境需求,需要nginx、resin整合,nginx负责处理静态部份,resin负责处理动态部份
系统环境:CentOS 5.6 X64
#安装常用组件
- yum -y install gcc gcc-c++ bison patch unzip mlocate flex wget automake autoconf gd cpp gettext readline-devel libjpeg \
- libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 \
- glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openldap \
- openldap-devel openldap-clients openldap-servers nss_ldap expat-devel libtool libtool-ltdl-devel bison
#---------------------------- 使用cmake编译安装mysql ----------------------------------
#使用Tcmalloc 优化nginx、mysql
#64位操作系统请先安装 libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API
/opt
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99.tar.gz
tar -zxvf libunwind-0.99.tar.gz
./configure
make
make install
cd /opt
tar -zxvf google-perftools-1.7.tar.gz
cd google-perftools-1.7/
./configure
make;make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
cd ../
tar -zxvf cmake-2.8.4.tar.gz
cd cmake-2.8.4
./bootstrap
gmake
gmake install
cd ../
/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql -s /sbin/nologin
mkdir -p /data/mysql/{data,binlog,relaylog,mysql}
chown -R mysql:mysql /data/mysql
cd /opt
tar zxvf mysql-5.5.13.tar.gz
cd mysql-5.5.13/
rm -rf CMakeCache.txt
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/data/mysql/data \
-DMYSQL_TCP_PORT=3306
make;make install
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
#编辑mysql配置文件
vi /etc/my.cnf
- [client]
- port = 3306
- socket = /data/mysql/mysql.sock
- [mysqld]
- character_set_server = utf8
- collation-server = utf8_general_ci
- replicate-ignore-db = mysql
- replicate-ignore-db = test
- replicate-ignore-db = information_schema
- user = mysql
- port = 3306
- socket = /data/mysql/mysql.sock
- basedir = /usr/local/mysql
- datadir = /data/mysql/data
- log-error = /data/mysql/mysql_error.log
- pid-file = /data/mysql/mysql.pid
- open_files_limit = 10240
- back_log = 600
- max_connections = 5000
- max_connect_errors = 6000
- table_cache = 512
- external-locking = FALSE
- max_allowed_packet = 32M
- sort_buffer_size = 6M
- join_buffer_size = 8M
- thread_cache_size = 300
- thread_concurrency = 8
- query_cache_size = 512M
- query_cache_limit = 2M
- query_cache_min_res_unit = 2k
- default-storage-engine = MyISAM
- thread_stack = 256K
- transaction_isolation = READ-COMMITTED
- tmp_table_size = 256M
- max_heap_table_size = 256M
- long_query_time = 3
- log-slave-updates
- log-bin = /data/mysql/binlog/binlog
- binlog_cache_size = 4M
- binlog_format = MIXED
- max_binlog_cache_size = 8M
- max_binlog_size = 100M
- relay-log-index = /data/mysql/relaylog/relaylog
- relay-log-info-file = /data/mysql/relaylog/relaylog
- relay-log = /data/mysql/relaylog/relaylog
- expire_logs_days = 30
- key_buffer_size = 384M
- read_buffer_size = 4M
- read_rnd_buffer_size = 16M
- bulk_insert_buffer_size = 64M
- myisam_sort_buffer_size = 128M
- myisam_max_sort_file_size = 100G
- myisam_repair_threads = 1
- myisam_recover
- interactive_timeout = 120
- wait_timeout = 120
- skip_external_locking
- skip-name-resolve
- #master-connect-retry = 10
- slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
- #master-host = 192.168.1.2
- #master-user = username
- #master-password = password
- #master-port = 3306
- server-id = 1
- skip-innodb
- log-slow-queries = /data/mysql/slow.log
- long_query_time = 2
- [mysqldump]
- quick
- max_allowed_packet = 32M
#初始化mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/data --user=mysql
#利用TCMalloc提高mysql在高并发下的性能
vi /usr/local/mysql/bin/mysqld_safe
#在# executing mysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
#使用lsof命令查看tcmalloc是否起效
/usr/sbin/lsof -n | grep tcmalloc
#设置mysql启动文件
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
vi /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql/data
chmod 700 /etc/rc.d/init.d/mysqld
/etc/rc.d/init.d/mysqld start
/sbin/chkconfig --add mysqld
/sbin/chkconfig --level 2345 mysqld on
ln -s /usr/local/mysql/bin/mysql /sbin/mysql
ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin
#设置root密码(753951)
/sbin/mysqladmin -u root password 753951
#/usr/local/mysql/bin/mysqladmin -u root -p password 456 --修改root已设置好的密码
#配置库文件搜索路径
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
/sbin/ldconfig
#添加/usr/local/mysql/bin到环境变量PATH中
export PATH=$PATH:/usr/local/mysql/bin
#添加mysql管理帐户
#mysql -h localhost -u root -p753951
#msqyl> use mysql;
#msqyl> grant all on *.* to 'kerry'@'192.168.9.100' identified by '852741';
#msqyl> flush privileges;
#msqyl> exit;
#---------------------------- 安装resin、JDK -------------------------#
cd /opt
mv jdk-6u29-linux-x64.bin /usr/local/
#设置环境变量
- cat >>/etc/profile<<EOF
- export JAVA_HOME=/usr/local/jdk1.6.0_25
- export CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
- export PATH=$PATH:$JAVA_HOME/bin
- EOF
source /etc/profile
#查看java版本
java -version
tar -zxvf resin-4.0.26.tar.gz
cd resin-4.0.26
./configure --prefix=/usr/local/resin
make;make install
#启动resin
/usr/local/resin/bin/resin.sh start
#设置resin开机启动
cp -r init.d/resin /etc/init.d/resin
chmod +x /etc/init.d/resin
/sbin/chkconfig --add resin
/sbin/chkconfig --level 2345 resin on
#---------------------------- 安装nginx ------------------------------#
cd /opt
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www -s /sbin/nologin
mkdir -p /data/www
chmod +w /data/www
chown -R www:www /data/www
tar zxvf pcre-8.12.tar.gz
cd pcre-8.12/
./configure
make;make install
cd ../
tar -zxvf nginx-1.0.13.tar.gz
cd nginx-1.0.13
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-google_perftools_module
make;make install
cd ../
mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs
#编辑nginx配置文件
mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
- cat >>/usr/local/nginx/conf/nginx.conf<<EOF
- user www www;
- worker_processes 8;
- error_log /usr/local/nginx/logs/nginx_error.log crit;
- pid /usr/local/nginx/nginx.pid;
- #使用Tcmalloc优化nginx性能
- google_perftools_profiles /var/tmp/tcmalloc;
- #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服务器,利用它的反向代理功能提供负载均衡支持
- http
- {
- #设定mime类型
- 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 300m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- server_tokens off;
- client_body_buffer_size 512k;
- proxy_connect_timeout 5;
- proxy_send_timeout 60;
- proxy_read_timeout 5;
- proxy_buffer_size 16k;
- proxy_buffers 4 64k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- # 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.1;
- 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;
- ###禁止通过ip访问站点
- server{
- server_name _;
- return 404;
- }
- server
- {
- listen 80;
- server_name www.king.com;
- index index.html index.htm index.jsp index.do;#设定访问的默认首页地址
- root /data/www/web001/;#设定网站的资源存放路径
- #limit_conn crawler 20;
- if (-d $request_filename)
- {
- rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
- }
- #所有jsp的页面均交由resin处理
- location ~ \.(jsp|jspx|do)?$ {
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_pass http://127.0.0.1:8080;#转向resin处理
- }
- location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ #设定访问静态文件直接读取不经过resin
- {
- expires 30d;
- }
- location ~ .*\.(js|css)?$
- {
- expires 1h;
- }
- #定义访问日志的写入格式
- log_format wwwlog '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- access_log /data/logs/www_nginx.log wwwlog;#设定访问日志的存放路径
- }
- server
- {
- listen 80;
- server_name status.www.kerry.com;
- location / {
- stub_status on;
- access_log off;
- }
- }
- }
- EOF
#添加启动脚本
vi /etc/init.d/nginx
- #! /bin/sh
- # Description: Startup script for webserver on CentOS. cp it in /etc/init.d and
- # chkconfig --add nginx && chkconfig nginx on
- # then you can use server command control nginx
- #
- # chkconfig: 2345 08 99
- # description: Starts, stops nginx
- set -e
- PATH=$PATH:/usr/local/nginx/sbin/
- DESC="nginx daemon"
- NAME=nginx
- DAEMON=/usr/local/nginx/sbin/$NAME
- CONFIGFILE=/usr/local/nginx/conf/nginx.conf
- PIDFILE=/usr/local/nginx/$NAME.pid
- SCRIPTNAME=/etc/init.d/$NAME
- # Gracefully exit if the package has been removed.
- test -x $DAEMON || exit 0
- d_start() {
- $DAEMON -c $CONFIGFILE || echo -n " already running"
- }
- d_stop() {
- kill -QUIT `cat $PIDFILE` || echo -n " not running"
- }
- d_reload() {
- kill -HUP `cat $PIDFILE` || echo -n " can't reload"
- }
- case "$1" in
- start)
- echo -n "Starting $DESC: $NAME"
- d_start
- echo "."
- ;;
- stop)
- echo -n "Stopping $DESC: $NAME"
- d_stop
- echo "."
- ;;
- reload)
- echo -n "Reloading $DESC configuration..."
- d_reload
- echo "reloaded."
- ;;
- restart)
- echo -n "Restarting $DESC: $NAME"
- d_stop
- sleep 1
- d_start
- echo "."
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
- exit 3
- ;;
- esac
- exit 0
#将nginx添加到启动服务中
chmod 700 /etc/init.d/nginx
/etc/init.d/nginx start
/sbin/chkconfig --add nginx
/sbin/chkconfig --level 2345 nginx on
#---------------------------- 安装memcache ------------------------------#
cd /opt
tar -xzf libevent-2.0.11-stable.tar.gz
cd libevent-2.0.11-stable
./configure
make;make install
ln -s /usr/local/lib/libevent-2.0.so.5 /lib64/libevent-2.0.so.5
cd /opt
tar -xzf memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure --prefix=/usr/local/memcached --with-libevent=/usr
make;make install
#基本使用方法:
-l 监听的地址memcached 无身份验证功能,严禁在无防护
-p 监听的端口状态下,直接监听外网端口!!!默认11211
-d 以daemon 形式运行,一般皆需增加此参数
-u 以何用户身份运行,一般选nobody 等低权用户
-m 最大可用内存,以兆为单位
-c 最大的同时并发数,默认1024
-f 增长因子
-P PID 文件
启动:
/usr/local/memcached/bin/memcached -d -m 1024 -p 11211 -u www -c 4096
关闭:
killall -9 memcached
#开面启动
echo "/usr/local/memcached/bin/memcached -d -m 1024 -p 11211 -u www -c 4096" >> /etc/rc.local
#---------------------------- nginx、resin整合 -------------------------#
#将resin的默认目录与nginx的目录相同
vi /usr/local/resin/conf/resin.xml
将
- <host id="" root-directory=".">
- <!--
- - webapps can be overridden/extended in the resin.xml
- -->
- <web-app id="/" root-directory="webapps/ROOT"/>
修改成:
- <host id="" root-directory=".">
- <!--
- - webapps can be overridden/extended in the resin.xml
- -->
- <web-app id="/" root-directory="/data/www/web001"/>
#创建一个测试文件
vi /data/www/web001/index.jsp
2 + 2 = <%= 2 + 2 %>
#重启nginx、resin
/etc/init.d/nginx restart
/etc/init.d/resin restart
#访问http://www.king.com 如果看到2 + 2 = 4,就证明nginx、resin整合成功