一:Nginx+Tomcat的動靜分離php
所謂動靜分離就是經過nginx(或apache等)來處理用戶端請求的圖片、html等靜態的文件,tomcat(或weblogic)處理jsp、do等動態文件,從而達到動靜頁面訪問時經過不一樣的容器來處理。html
nginx處理靜態頁面效率遠高於tomcat,而tomcat擅長動態頁面處理,這樣一來就能更好的提升併發,處理性能。java
頁面壓力測試linux
ab -n 1000 -c 200 http://10.10.54.157/index.htmlnginx
二:具體步驟web
//環境介紹shell
1.主機 centos6.4 IP:10.10.54.157 2.在此主機上安裝nginx,及兩個tomcat,nginx的反向代理功能能夠把用戶請求負載到tomcat上 nginx使用80端口,兩個tomcat分別使用8080,9080端口 目的:當訪問10.10.54.157這臺服務器時,靜態頁面由nginx處理,動態頁面由tomcat處理 3.軟件下載 wget http://nginx.org/download/nginx-1.4.5.tar.gz wget http://download.oracle.com/otn-pub/java/jdk/7u51-b13/jdk-7u51-linux-x64.rpm #jdk提供tomcat運行環境 wget http://apache.dataguru.cn/tomcat/tomcat-7/v7.0.52/bin/apache-tomcat-7.0.52.tar.gz
//安裝nginx,並配置nginx支持phpapache
#nginx使用fpm的方式調用php #php5.4之後版本中已經支持php-fpm,因此只須要在php編譯參數裏指定--enable-fpm便可 1.編譯安裝 shell> yum -y install zlib-devel pcre-devel openssl-devel #nginx依賴包 shell> tar xvf nginx-1.4.5.tar.gz shell> cd nginx-1.4.5 shell> ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --user=apache --group=apache --with-http_ssl_module --with-http_gzip_static_module shell> make && make install 2.啓動nginx shell> /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ##添加nginx支持php 3.首先爲php建立配置文件 shell> cp php.ini-production /usr/local/php/php.ini shell> mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf shell> ln -s /usr/local/php/bin/php /usr/bin/ 4.配置php-fpm.conf shell> vi /usr/local/php/etc/php-fpm.conf -------------------------------- listen = /var/run/php-fpm.sock # 使用unix socket -------------------------------- 5.啓動php-fpm shell> /usr/local/php/sbin/php-fpm
//安裝JDKvim
shell> rpm -ivh jdk-7u51-linux-x64.rpm shell> vi /etc/profile ----------------------- export JAVA_HOME="/usr/java/jdk1.7.0_51" export CLASSPATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib" export PATH="$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$HOME/bin" CATALINA_HOME="/usr/local/tomcat" ----------------------- shell> source /etc/profile shell> java -version #顯示版本說明成功 java version"1.7.0_45"
//安裝多個tomcat
centos
1.解壓 shell> tar xvf apache-tomcat-7.0.52.tar.gz shell> cp apache-tomcat-7.0.52 /usr/local/tomcat1 shell> cp apache-tomcat-7.0.52 /usr/local/tomcat2 shell> chown apache.apache /usr/local/tomcat1 shell> chown apache.apache /usr/local/tomcat2 【tomcat1 and tomcat2】 2.製做tomcat啓動腳本 shell> cp catalina.sh /etc/init.d/tomcat1 shell> chmod +x /etc/init.d/tomcat1 shell> vim /etc/init.d/tomcat1 shell> chkconfig --add tomcat1 【tomcat1 and tomcat2】 3.tomcat配置用戶管理(能夠不設置) shell>cd /usr/local/tomcat1/conf shell> vim tomcat-users.xml ----------------------------------------- <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="admin-gui,manager-gui"/> #用戶名和密碼都爲tomcat ----------------------------------------- 【tomcat1 and tomcat2】 4.重啓tomcat shell> /etc/init.d/tomcat stop shell> /etc/init.d/tomcat start
//配置nginx.conf實現負載均衡,動靜分離
shell> vim /usr/local/nginx/conf/nginx.conf -------------------------------------------------------- user apache apache; #用戶,tomcat默認首頁也要改成apache用戶 worker_processes 2; #error_log logs/error_nginx.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; keepalive_timeout 60; gzip on; #負載均衡 upstream www_zijian_com { server localhost:8080 max_fails=3 weight=1 fail_timeout=60s; server localhost:9080 max_fails=3 weight=1 fail_timeout=60s; } server { listen 80; server_name www.zijian.com; index index.jsp index.php index.do index.jsp; access_log logs/www.zijian.com_access_nginx.log; error_log logs/www.zijian.com_error.log; root /var/www/nginx; #nginx默認根目錄 location ~ \.(php|php5)$ { #支持php fastcgi_pass unix:/var/run/php-fpm.sock; #php-fpm.sock模塊 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include fastcgi_params; include fastcgi.conf; } location ~ \.(jsp|do)$ { #jsp,do等頁面交由tomcat處理 proxy_pass http://www_zijian_com; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } } }
//測試
1.瀏覽器訪問10.10.54.157 默認顯示nginx根目錄下的index.php頁面
2.瀏覽器訪問10.10.54.157/index.jsp 瀏覽器跳轉到tomcat默認首頁,在首頁點擊Server Status頁面,並刷新瀏覽器,能夠看到頁面在兩個tomcat之間切換