1. 程式人生 > >nginx + tomcat 8.5 優化配置,安裝nginx【centos6、7】

nginx + tomcat 8.5 優化配置,安裝nginx【centos6、7】

第一步安裝 軟體

1.1 依賴包

# yum -y install gcc
# yum install expat-devel
# sudo yum install  zlib-devel


到http://apr.apache.org/download.cgi#apr1這個地址下載所需要的包
apr-1.6.5.tar.gz
apr-iconv-1.2.2.tar.gz
apr-util-1.6.1.tar.gz

1.2安裝APR

tar zxvf apr-1.6.5.tar.gz
cd apr-1.6.5
./configure --prefix=/usr/local/apr
make
make install
cd ..

寫入到環境變數中

vi /etc/profile
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib

source /etc/profile
如果這個不寫,第一次啟動tomcat 沒有問題,第二次就掛了,不知道什麼鬼

1.3安裝apr-iconv

tar -zxvf apr-iconv-1.2.2.tar.gz
cd apr-iconv-1.2.2
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
make
make install
cd ..


1.4安裝 apr-util

tar zxvf apr-util-1.6.1.tar.gz
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv
make && make install
cd ..

1.5 安裝openssl-1.0.2

[[email protected] openssl-1.0.2f]# wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz
[
[email protected]
openssl-1.0.2f]# tar -xzf openssl-1.0.2f.tar.gz [[email protected] openssl-1.0.2f]# cd openssl-1.0.2f [[email protected] openssl-1.0.2f]# mkdir /usr/local/openssl [[email protected] openssl-1.0.2f]# ./config -fPIC --prefix=/usr/local/openssl enable-shared [[email protected] openssl-1.0.2f]# make clean [[email protected] openssl-1.0.2f]# make [[email protected]host openssl-1.0.2f]# make install 檢視路徑 [[email protected] openssl-1.0.2f]# which openssl /usr/local/openssl/bin/openssl [[email protected] openssl-1.0.2f]# mv /usr/bin/openssl /usr/bin/openssl.bak1 [[email protected] openssl-1.0.2f]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl [[email protected] openssl-1.0.2f]# cd /usr/local/openssl --------------------- 檢視版本 [[email protected] openssl-1.0.2f]# openssl version /usr/local/openssl/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or director 找不到動態庫libssl.so.1.1,小問題,執行如下命令: [[email protected] openssl-1.0.2f]# vim /etc/ld.so.conf 在最後追加一行: /usr/local/openssl/lib 然後執行: [[email protected] openssl-1.0.2f]# ldconfig /etc/ld.so.conf [[email protected] openssl-1.0.2f]# openssl version OpenSSL 1.1.0f 25 May 2017 4.檢測安裝是否成功 openssl version -a

第二步 tomcat優化
2.1進入資料夾tomcat/bin. 開啟catalina.sh檔案,
找到下面的註釋

# OS specific support.  $var _must_ be set to either true or false.

export JAVA_OPTS="-server -Xms512M -Xmx1400M -Xss512k -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicit

2.2tomcat 配置檔案

# mv /usr/local/tomcat/conf/server.xml /usr/local/tomcat/conf/server.xml.bak
# vi /usr/local/tomcat/conf/server.xml

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">
    <Connector port="8080" protocol="HTTP/1.1"
			   URIEncoding="UTF-8"			  
               maxHttpHeaderSize="8192"
			   maxThreads="1200"
			   enableLookups="false"
			   acceptCount="200"
               connectionTimeout="20000"
			   disableUploadTimeout="true"
			   compression="on"
			   compressionMinSize="2048"
			   compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
               redirectPort="8443" />
   
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">   
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
      </Host>
    </Engine>
  </Service>
</Server>

 

2.3tomcat 安裝native

cd /usr/local/tomcat/bin/
tar xzfv tomcat-native.tar.gz
cd tomcat-native-1.2.17-src/native/
./configure --with-apr=/usr/local/apr --with-java-home=/usr/java/jdk1.8.0_191-amd64 --with-ssl=/usr/local/openssl
		【備用命令./configure --with-apr=/usr/bin/apr-1-config】
make
make install

安裝成功後提示

2.4 tomcat整合 apr

vim /etc/profile
#在檔案中加入下面這句話
export CATALINA_OPTS=-Djava.library.path=/usr/local/apr/lib
#使配置生效
source /etc/profile

2.5 修改tomcat 配置檔案

vim tomcat/conf/server.xml
#將配置中的
Connector port="8080" protocol="HTTP/1.1"
改成
Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"

將配置中的
Connector port="8009" protocol="AJP/1.3" redirectPort="8443"
改成
Connector port="8009" protocol="org.apache.coyote.ajp.AjpAprProtocol" redirectPort="8443"

重啟tomcat,啟動日誌出現以下內容證明此時以APR模式啟動

3,安裝nginx 以及效能優化

3.1 SSL功能需要openssl庫,下載地址:http://www.openssl.org/

3.2 gzip模組需要zlib庫,下載地址:http://www.zlib.net/

3.3 rewrite模組需要pcre庫,下載地址:http://www.pcre.org/

mkdir -p /home/nginx/logs

mkdir -p /etc/nginx

yum zlib 
yum install -y nginx

mv /etc/nginx/nginx.conf  /etc/nginx/nginx.conf.bak

vi /etc/nginx/nginx.conf


mkdir -p /usr/local/nginx/fastcgi_cache  用於快取

nginx.conf

worker_processes                  8;
worker_rlimit_nofile              102400;

events {
    use epoll;
    worker_connections            65535;
    multi_accept                  on;
}


http {
    server_tokens                 off;
    open_file_cache               max=102400 inactive=20s;
    open_file_cache_valid         30s;
    open_file_cache_min_uses      1;
    reset_timedout_connection     on;
	charset   utf-8;

	server_names_hash_bucket_size 128;
	client_header_buffer_size 2k;
	large_client_header_buffers 4 4k;
	client_max_body_size 8m;
	
	fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
                 keys_zone=TEST:10m
                 inactive=5m;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 16k;
    fastcgi_buffers 16 16k;
    fastcgi_busy_buffers_size 16k;
    fastcgi_temp_file_write_size 16k;
    fastcgi_cache TEST;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500;  


    gzip                          on;
    gzip_min_length               2k;
    gzip_buffers                  4 32k;
    gzip_http_version             1.1;
    gzip_comp_level               6;
    gzip_types                    text/plain text/javascript text/xml text/css application/json application/javascript application/x-javascript application/xml;
    gzip_disable                  "MSIE [1-6]\."
    gzip_vary                     on;
    gzip_proxied                  any;

    tcp_nopush                    on;
    tcp_nodelay                   on;

    include                       mime.types;
    default_type                  application/octet-stream;
    sendfile                      on;

    keepalive_timeout             16;

    proxy_connect_timeout         8s;
    proxy_send_timeout            8s;
    proxy_read_timeout            8s;
    send_timeout                  3s;

    upstream web {
               server 127.0.0.1:80 max_fails=5 fail_timeout=30s weight=1;
    }

    server {
        listen       80;
        server_name  www.ityhy.com;
        
        proxy_set_header host                $host;
        proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
	    proxy_set_header   X-Real-IP   $remote_addr;

        location / {
            proxy_pass http://web;
        }
		
		location /status{
			 stub_status on;
		 }
		
		location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css){
		expires       30d;
		}

    }
}

nginx 啟動

nginx

nginx -s reload 

重啟Nginx

service nginx restart
/etc/init.d/nginx stop
/etc/init.d/nginx start

指定檔案啟動nginx -c /etc/nginx/nginx.conf

指定檔案重啟nginx -c /etc/nginx/nginx.conf -s reload

https://www.cnblogs.com/odbo/p/5295690.html 【yum 安裝 nginx 所在檔案位置】