Linux*centos6.5 安裝nginx+tomcat 服務器整合
前言:研究了2天也出現過很多錯誤,網上的資料很多但是都不是很全,我是拼湊出來在結合自己的理解配置出來的,怕以後忘了記載在這裏,如有不對的地方還請包涵。
一、首先,你要安裝jdk(我的版本問1.8)自己到官網下載(我是root的權限)。
我的服務器環境爲---centos 6.5 64
[root@naginx ROOT]#rpm -ivh jdk-8u60-linux-x64.rpm
[root@naginx ROOT]# java -version
java version "1.8.0_60" Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
出現以上信息表示成功(我用的是rpm 包,環境變量我沒有設置。默認即可。)
二、 防火牆設置,開啓端口80 和 8080 。
[root@naginx ROOT]#vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #允許80端口通過防火牆-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT #允許8080端口通過防火牆 [root@naginx ROOT]# cat /etc/sysconfig/iptables # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMI
[root@naginx ROOT]# service iptables restart #重啓防火牆
2、關閉 selinux
[root@naginx ROOT]# vim /etc/selinux/config [root@naginx ROOT]# cat /etc/selinux/config
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #SELINUX=enforcing #註釋掉 # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. #SELINUXTYPE=targeted #註釋掉 SELINUX=disabled #增加
[root@naginx ROOT]# shutdown -r now #重啓系統
三、安裝YUM源
[root@naginx ROOT]#wget http://www.atomicorp.com/installers/atomic #下載 [root@naginx ROOT]#sh ./atomic [root@naginx ROOT]#yum check-update #更新yum源
四、安裝nginx (我用Yum 安裝也可以編譯安裝)
[root@naginx ROOT]#yum install nginx -y [root@naginx ROOT]# chkconfig nginx on #設置開機啓動 [root@naginx ROOT]# service nginx start
五、安裝tomcat (安裝包到官網下載,隨便下載到任意目錄)
[root@naginx ROOT]#tar -zxvf apache-tomcat-8.0.36.tar.gz -C /usr/local/ [root@naginx ROOT]#cd /usr/local/ [root@naginx ROOT]#mv apache-tomcat-8.0.36 tomcat #更改爲tomcat [root@naginx ROOT]#useradd centos # 創建啓動tomcat 帳號爲了安全 [root@naginx ROOT]#chown -R centos.centos /usr/local/tomcat/#設置tomcat 目錄權限 [root@naginx ROOT]#ll /usr/local/ total 4 drwxr-xr-x. 9 centos centos 4096 Jul 4 19:32 tomcat [root@naginx ROOT]#su - centos /usr/local/tomcat/bin/startup.sh #啓動tomcat [root@naginx ROOT]#ps -ef | grep tomcat centos 2300 1 0 Jul06 pts/0 00:01:50 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/usr/local/tomcat/endorsed -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat/Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start root 16658 2143 0 19:20 pts/0 00:00:00 grep tomcat
[root@naginx ROOT]#echo "su - centos /usr/local/tomcat/bin/startup.sh" >> /etc/rc.local #設置開機啓動
[root@naginx ROOT]#cat /etc/rc.local #查看開機啓動 #!/bin/sh # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local su - centos /usr/local/tomcat/bin/startup.sh
六、配置nginx+ tomcat 整合
[root@naginx ROOT]#vim /etc/nginx/proxy.conf #新建proxy.conf 文件
[root@naginx ROOT]# cat /etc/nginx/proxy.conf proxy_connect_timeout 300s; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 32k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding ''; proxy_set_header Host $host; proxy_set_header Referer $http_referer; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
[root@naginx ROOT]#cat /etc/nginx/nginx.conf
user centos centos; #注意這裏 worker_processes 2; error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; 中間 以上省略…………………… # Load config files from the /etc/nginx/conf.d directory # The default server is in conf.d/default.conf include /etc/nginx/conf.d/*.conf; #這行注意
[root@naginx ROOT]#mkdir -p /home/centos/tomcattest/www.test.com#創建網站根目錄(nginx和tomcat的網站根目錄)
[root@naginx ROOT]#mkdir -p /home/centos/tomcattest/admin.test.com #創建網站根目錄(nginx 和 tomcat的網站根目錄)
[root@naginx ROOT]# ll /home/centos/tomcattest/ drwxr-xr-x. 2 centos centos 4096 Aug 26 2015 Music drwxr-xr-x. 2 centos centos 4096 Aug 26 2015 Pictures drwxr-xr-x. 2 centos centos 4096 Aug 26 2015 Public drwxr-xr-x. 2 centos centos 4096 Aug 26 2015 Templates drwxrwxr-x 3 centos centos 4096 Jul 4 20:17 tomcattest#權限用戶和組都是centos
[root@naginx ROOT]#vim /etc/nginx/conf.d/test.conf#nginx默認配置文件中設置 (nginx.conf),創建網站單獨配置文件,
[root@naginx ROOT]# cat /etc/nginx/conf.d/test.conf server { listen80; server_name www.test.com; root /home/centos/tomcattest/www.test.com; access_log /var/log/nginx/www.test.com.access.log main; error_log /var/log/nginx/www.test.com.error.log; location / { index index.jsp index.php; if ($request_filename !~* /(\.ico|static|wap|robots\.txt|index\.jsp)) { rewrite ^/(.*)$ /index.jsp?$1 last; } } location ~ .*\.(jsp|jspx|do)?$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://127.0.0.1:8080; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|apk)$ { expires 7d; } location ~ .*\.(jsp|js|css)?$ { expires 1d; } } server { listen 80; server_name admin.test.com; root /home/centos/tomcattest/admin.test.com; access_log /var/log/nginx/admin.test.access.log main; error_log /var/log/nginx/admin.test.error.log; location / { index index.jsp index.php; if ($request_filename !~* /(favicon\.ico|static|robots\.txt|index\.jsp)) { set $path_info $1; rewrite ^/(.*)$ /index.jsp?$1 last; } client_max_body_size 800m; } location ~ .*\.(jsp|jspx|do)?$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://127.0.0.1:8080; } location ~ .*\.(gif|jpg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(jsp|js|css)?$ { expires 1d; } } server{ listen 80; server_name test.com *.test.com; return 301 http://www.test.com$request_uri; }
[root@naginx ROOT]#nginx -t #檢測配置文件 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok#出現ok 正確 nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@naginx ROOT]#service nginx reload #刷新配置文件
2、tomcat 虛擬主機設置; #tomcat 服務器的網站根目錄和nginx的網站根目錄一定一樣。
[root@naginx /]#vim /usr/local/tomcat/conf/server.xml #配置最下邊的Host [root@naginx /]#cat /usr/local/tomcat/conf/server.xml
</Host> <Host name="www.test.com" appBase="/home/centos/tomcattest/www.test.com" unpackWARs="true" autoDeploy="true"> <Context path="/" docBase="/home/centos/tomcattest/www.test.com" reloadable="true"/> #這裏我是後加的路徑 </Host> <Host name="admin.test.com" appBase="/home/centos/tomcattest/admin.test.com" unpackWARs="true" autoDeploy="true"> <Context path="/" docBase="/home/centos/tomcattest/admin.test.com" reloadable="true"/> #這裏我是後加的路徑 </Host>
[root@naginx /]#su - centos /usr/local/tomcat/bin/shutdown.sh#停止tomcat [root@naginx /]#su - centos /usr/local/tomcat/bin/startup.sh#啓動tomcat
七、安裝dnsmasq 服務器設置
[root@naginx /]#yum -y install dnsmasq [root@naginx /]#vim /etc/dnsmasq.d/dns.conf [root@naginx /]#cat /etc/dnsmasq.d/dns.conf #------------- test.com ------------------------------------- address=/test.com/192.168.128.129 address=/.test.com/192.168.128.129
至此 nginx + tomcat 的整合完畢
測試結果 http://admin.test.com