Chinaunix首页 | 论坛 | 博客
  • 博客访问: 531798
  • 博文数量: 126
  • 博客积分: 851
  • 博客等级: 准尉
  • 技术积分: 1287
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-06 11:21
个人简介

个人最新博客地址http://www.skylway.com/

文章分类

全部博文(126)

文章存档

2016年(2)

2014年(60)

2013年(35)

2012年(29)

分类: LINUX

2013-04-10 17:56:23

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://blog.chinaunix.net/space.php?uid=9419692&do=blog&id=3182595

因公司运营环境需求,需要nginx、resin整合,nginx负责处理静态部份,resin负责处理动态部份

系统环境:CentOS 5.6 X64


#安装常用组件

  1. yum -y install gcc gcc-c++ bison patch unzip mlocate flex wget automake autoconf gd cpp gettext readline-devel libjpeg \
  2. libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 \
  3. glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openldap \
  4. 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
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

  1. [client]
  2. port = 3306
  3. socket = /data/mysql/mysql.sock
  4. [mysqld]
  5. character_set_server = utf8
  6. collation-server = utf8_general_ci
  7. replicate-ignore-db = mysql
  8. replicate-ignore-db = test
  9. replicate-ignore-db = information_schema
  10. user = mysql
  11. port = 3306
  12. socket = /data/mysql/mysql.sock
  13. basedir = /usr/local/mysql
  14. datadir = /data/mysql/data
  15. log-error = /data/mysql/mysql_error.log
  16. pid-file = /data/mysql/mysql.pid
  17. open_files_limit = 10240
  18. back_log = 600
  19. max_connections = 5000
  20. max_connect_errors = 6000
  21. table_cache = 512
  22. external-locking = FALSE
  23. max_allowed_packet = 32M
  24. sort_buffer_size = 6M
  25. join_buffer_size = 8M
  26. thread_cache_size = 300
  27. thread_concurrency = 8
  28. query_cache_size = 512M
  29. query_cache_limit = 2M
  30. query_cache_min_res_unit = 2k
  31. default-storage-engine = MyISAM
  32. thread_stack = 256K
  33. transaction_isolation = READ-COMMITTED
  34. tmp_table_size = 256M
  35. max_heap_table_size = 256M
  36. long_query_time = 3
  37. log-slave-updates
  38. log-bin = /data/mysql/binlog/binlog
  39. binlog_cache_size = 4M
  40. binlog_format = MIXED
  41. max_binlog_cache_size = 8M
  42. max_binlog_size = 100M
  43. relay-log-index = /data/mysql/relaylog/relaylog
  44. relay-log-info-file = /data/mysql/relaylog/relaylog
  45. relay-log = /data/mysql/relaylog/relaylog
  46. expire_logs_days = 30
  47. key_buffer_size = 384M
  48. read_buffer_size = 4M
  49. read_rnd_buffer_size = 16M
  50. bulk_insert_buffer_size = 64M
  51. myisam_sort_buffer_size = 128M
  52. myisam_max_sort_file_size = 100G
  53. myisam_repair_threads = 1
  54. myisam_recover
  55. interactive_timeout = 120
  56. wait_timeout = 120
  57. skip_external_locking
  58. skip-name-resolve
  59. #master-connect-retry = 10
  60. slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
  61. #master-host = 192.168.1.2
  62. #master-user = username
  63. #master-password = password
  64. #master-port = 3306
  65. server-id = 1
  66. skip-innodb
  67. log-slow-queries = /data/mysql/slow.log
  68. long_query_time = 2
  69. [mysqldump]
  70. quick
  71. 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 identified by '852741';
#msqyl> flush privileges;
#msqyl> exit;

#---------------------------- 安装resin、JDK -------------------------#
cd /opt
mv jdk-6u29-linux-x64.bin /usr/local/

#设置环境变量

  1. cat >>/etc/profile<<EOF
  2. export JAVA_HOME=/usr/local/jdk1.6.0_25
  3. export CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
  4. export PATH=$PATH:$JAVA_HOME/bin
  5. 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

  1. cat >>/usr/local/nginx/conf/nginx.conf<<EOF
  2. user www www;
  3. worker_processes 8;
  4. error_log /usr/local/nginx/logs/nginx_error.log crit;
  5. pid /usr/local/nginx/nginx.pid;
  6. #使用Tcmalloc优化nginx性能
  7. google_perftools_profiles /var/tmp/tcmalloc;
  8. #Specifies the value for maximum file descriptors that can be opened by this process.
  9. worker_rlimit_nofile 65535;
  10. #工作模式及连接数上限
  11. events
  12. {
  13. use epoll;
  14. worker_connections 65535;
  15. }
  16. #设定http服务器,利用它的反向代理功能提供负载均衡支持
  17. http
  18. {
  19. #设定mime类型
  20. include mime.types;
  21. default_type application/octet-stream;
  22. #charset gb2312;
  23. #设定请求缓冲
  24. server_names_hash_bucket_size 128;
  25. client_header_buffer_size 32k;
  26. large_client_header_buffers 4 32k;
  27. #设置客户端能够上传文件大小的限制
  28. client_max_body_size 300m;
  29. sendfile on;
  30. tcp_nopush on;
  31. keepalive_timeout 60;
  32. tcp_nodelay on;
  33. server_tokens off;
  34. client_body_buffer_size 512k;
  35. proxy_connect_timeout 5;
  36. proxy_send_timeout 60;
  37. proxy_read_timeout 5;
  38. proxy_buffer_size 16k;
  39. proxy_buffers 4 64k;
  40. proxy_busy_buffers_size 128k;
  41. proxy_temp_file_write_size 128k;
  42. # fastcgi_connect_timeout 300;
  43. # fastcgi_send_timeout 300;
  44. # fastcgi_read_timeout 300;
  45. # fastcgi_buffer_size 64k;
  46. # fastcgi_buffers 4 64k;
  47. # fastcgi_busy_buffers_size 128k;
  48. # fastcgi_temp_file_write_size 128k;
  49. gzip on;
  50. gzip_min_length 1k;
  51. gzip_buffers 4 16k;
  52. gzip_http_version 1.1;
  53. gzip_comp_level 2;
  54. gzip_types text/plain application/x-javascript text/css application/xml;
  55. gzip_vary on;
  56. #limit_zone crawler $binary_remote_addr 10m;
  57. ###禁止通过ip访问站点
  58. server{
  59. server_name _;
  60. return 404;
  61. }
  62. server
  63. {
  64. listen 80;
  65. server_name
  66. index index.html index.htm index.jsp index.do;#设定访问的默认首页地址
  67. root /data/www/web001/;#设定网站的资源存放路径
  68. #limit_conn crawler 20;
  69. if (-d $request_filename)
  70. {
  71. rewrite ^/(.*)([^/])$ permanent;
  72. }
  73. #所有jsp的页面均交由resin处理
  74. location ~ \.(jsp|jspx|do)?$ {
  75. proxy_set_header Host $host;
  76. proxy_set_header X-Real-IP $remote_addr;
  77. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  78. proxy_pass ;#转向resin处理
  79. }
  80. location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ #设定访问静态文件直接读取不经过resin
  81. {
  82. expires 30d;
  83. }
  84. location ~ .*\.(js|css)?$
  85. {
  86. expires 1h;
  87. }
  88. #定义访问日志的写入格式
  89. log_format wwwlog '$remote_addr - $remote_user [$time_local] "$request" '
  90. '$status $body_bytes_sent "$http_referer" '
  91. '"$http_user_agent" $http_x_forwarded_for';
  92. access_log /data/logs/www_nginx.log wwwlog;#设定访问日志的存放路径
  93. }
  94. server
  95. {
  96. listen 80;
  97. server_name status.
  98. location / {
  99. stub_status on;
  100. access_log off;
  101. }
  102. }
  103. }
  104. EOF

#添加启动脚本
vi /etc/init.d/nginx

  1. #! /bin/sh
  2. # Description: Startup script for webserver on CentOS. cp it in /etc/init.d and
  3. # chkconfig --add nginx && chkconfig nginx on
  4. # then you can use server command control nginx
  5. #
  6. # chkconfig: 2345 08 99
  7. # description: Starts, stops nginx
  8. set -e
  9. PATH=$PATH:/usr/local/nginx/sbin/
  10. DESC="nginx daemon"
  11. NAME=nginx
  12. DAEMON=/usr/local/nginx/sbin/$NAME
  13. CONFIGFILE=/usr/local/nginx/conf/nginx.conf
  14. PIDFILE=/usr/local/nginx/$NAME.pid
  15. SCRIPTNAME=/etc/init.d/$NAME
  16. # Gracefully exit if the package has been removed.
  17. test -x $DAEMON || exit 0
  18. d_start() {
  19. $DAEMON -c $CONFIGFILE || echo -n " already running"
  20. }
  21. d_stop() {
  22. kill -QUIT `cat $PIDFILE` || echo -n " not running"
  23. }
  24. d_reload() {
  25. kill -HUP `cat $PIDFILE` || echo -n " can't reload"
  26. }
  27. case "$1" in
  28. start)
  29. echo -n "Starting $DESC: $NAME"
  30. d_start
  31. echo "."
  32. ;;
  33. stop)
  34. echo -n "Stopping $DESC: $NAME"
  35. d_stop
  36. echo "."
  37. ;;
  38. reload)
  39. echo -n "Reloading $DESC configuration..."
  40. d_reload
  41. echo "reloaded."
  42. ;;
  43. restart)
  44. echo -n "Restarting $DESC: $NAME"
  45. d_stop
  46. sleep 1
  47. d_start
  48. echo "."
  49. ;;
  50. *)
  51. echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  52. exit 3
  53. ;;
  54. esac
  55. 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

  1. <host id="" root-directory=".">
  2. <web-app id="/" root-directory="webapps/ROOT"/>


修改成:

  1. <host id="" root-directory=".">
  2. <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

#访问 如果看到2 + 2 = 4,就证明nginx、resin整合成功


本文出自 “聆听未来” 博客,请务必保留此出处http://blog.chinaunix.net/space.php?uid=9419692&do=blog&id=3182595

阅读(601) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~