因公司运营环境需求,需要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 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
1. [client]
2. port = 3306
3. socket = /data/mysql/mysql.sock
4.
5. [mysqld]
6. character_set_server = utf8
7. collation-server = utf8_general_ci
8. replicate-ignore-db = mysql
9. replicate-ignore-db = test
10. replicate-ignore-db = information_schema
11. user = mysql
12. port = 3306
13. socket = /data/mysql/mysql.sock
14. basedir = /usr/local/mysql
15. datadir = /data/mysql/data
16. log-error = /data/mysql/mysql_error.log
17. pid-file = /data/mysql/mysql.pid
18. open_files_limit = 10240
19. back_log = 600
20. max_connections = 5000
21. max_connect_errors = 6000
22. table_cache = 512
23. external-locking = FALSE
24. max_allowed_packet = 32M
25. sort_buffer_size = 6M
26. join_buffer_size = 8M
27. thread_cache_size = 300
28. thread_concurrency = 8
29. query_cache_size = 512M
30. query_cache_limit = 2M
31. query_cache_min_res_unit = 2k
32. default-storage-engine = MyISAM
33. thread_stack = 256K
34. transaction_isolation = READ-COMMITTED
35. tmp_table_size = 256M
36. max_heap_table_size = 256M
37. long_query_time = 3
38. log-slave-updates
39. log-bin = /data/mysql/binlog/binlog
40. binlog_cache_size = 4M
41. binlog_format = MIXED
42. max_binlog_cache_size = 8M
43. max_binlog_size = 100M
44. relay-log-index = /data/mysql/relaylog/relaylog
45. relay-log-info-file = /data/mysql/relaylog/relaylog
46. relay-log = /data/mysql/relaylog/relaylog
47. expire_logs_days = 30
48. key_buffer_size = 384M
49. read_buffer_size = 4M
50. read_rnd_buffer_size = 16M
51. bulk_insert_buffer_size = 64M
52. myisam_sort_buffer_size = 128M
53. myisam_max_sort_file_size = 100G
54. myisam_repair_threads = 1
55. myisam_recover
56.
57. interactive_timeout = 120
58. wait_timeout = 120
59.
60. skip_external_locking
61. skip-name-resolve
62. #master-connect-retry = 10
63. slave-skip-errors = 1032,1062,126,1114,1146,1048,1396
64.
65. #master-host = 192.168.1.2
66. #master-user = username
67. #master-password = password
68. #master-port = 3306
69.
70. server-id = 1
71.
72. skip-innodb
73.
74. log-slow-queries = /data/mysql/slow.log
75. long_query_time = 2
76.
77. [mysqldump]
78. quick
79. 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/
#设置环境变量
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.
57. #limit_zone crawler $binary_remote_addr 10m;
58.
59. ###禁止通过ip访问站点
60. server{
61. server_name _;
62. return 404;
63. }
64. server
65. {
66. listen 80;
67. server_name www.king.com;
68. index index.html index.htm index.jsp index.do;#设定访问的默认首页地址
69. root /data/www/web001/;#设定网站的资源存放路径
70. #limit_conn crawler 20;
71. if (-d $request_filename)
72. {
73. rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
74. }
75. #所有jsp的页面均交由tomcat处理
76. location ~ /.(jsp|jspx|do)?$ {
77. proxy_set_header Host $host;
78. proxy_set_header X-Real-IP $remote_addr;
79. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
80. proxy_pass http://127.0.0.1:8080;#转向tomcat处理
81. }
82. location ~ .*/.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ #设定访问静态文件直接读取不经过tomcat
83. {
84. expires 30d;
85. }
86. location ~ .*/.(js|css)?$
87. {
88. expires 1h;
89. }
90.
91. #定义访问日志的写入格式
92. log_format wwwlog '$remote_addr - $remote_user [$time_local] "$request" '
93. '$status $body_bytes_sent "$http_referer" '
94. '"$http_user_agent" $http_x_forwarded_for';
95. access_log /data/logs/www_nginx.log wwwlog;#设定访问日志的存放路径
96. }
97. server
98. {
99. listen 80;
100. server_name status.www.kerry.com;
101. location / {
102. stub_status on;
103. access_log off;
104. }
105. }
106.}
107.EOF
#添加启动脚本
vi /etc/init.d/nginx
1. #! /bin/sh
2.
3. # Description: Startup script for webserver on CentOS. cp it in /etc/init.d and
4. # chkconfig --add nginx && chkconfig nginx on
5. # then you can use server command control nginx
6. #
7. # chkconfig: 2345 08 99
8. # description: Starts, stops nginx
9.
10. set -e
11.
12. PATH=$PATH:/usr/local/nginx/sbin/
13. DESC="nginx daemon"
14. NAME=nginx
15. DAEMON=/usr/local/nginx/sbin/$NAME
16. CONFIGFILE=/usr/local/nginx/conf/nginx.conf
17. PIDFILE=/usr/local/nginx/$NAME.pid
18. SCRIPTNAME=/etc/init.d/$NAME
19.
20. # Gracefully exit if the package has been removed.
21. test -x $DAEMON || exit 0
22.
23. d_start() {
24. $DAEMON -c $CONFIGFILE || echo -n " already running"
25. }
26.
27. d_stop() {
28. kill -QUIT `cat $PIDFILE` || echo -n " not running"
29. }
30.
31. d_reload() {
32. kill -HUP `cat $PIDFILE` || echo -n " can't reload"
33. }
34.
35. case "$1" in
36. start)
37. echo -n "Starting $DESC: $NAME"
38. d_start
39. echo "."
40. ;;
41. stop)
42. echo -n "Stopping $DESC: $NAME"
43. d_stop
44. echo "."
45. ;;
46. reload)
47. echo -n "Reloading $DESC configuration..."
48. d_reload
49. echo "reloaded."
50. ;;
51. restart)
52. echo -n "Restarting $DESC: $NAME"
53. d_stop
54. sleep 1
55. d_start
56. echo "."
57. ;;
58. *)
59. echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
60. exit 3
61. ;;
62. esac
63.
64. 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. <!--
3. - webapps can be overridden/extended in the resin.xml
4. -->
5. <web-app id="/" root-directory="webapps/ROOT"/>
修改成:
1. <host id="" root-directory=".">
2. <!--
3. - webapps can be overridden/extended in the resin.xml
4. -->
5. <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整合成功
本文出自 “聆听未来” 博客
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)