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