Nginx整合Apache2和Tomcat

通常情况下,如果PHP业务和Java Web业务占用资源都不是很多的情况下,为了节省服务器开销,可以放到一台服务器上。此时可以利用Nginx依旧80端口,做一个请求转发来分别访问PHP应用和Java Web应用

环境及版本

1
2
3
4
5
Ubuntu 14.04 Server x64
MySQL 5.5
Apache 2.4.7
Nginx 1.4.6
Tomcat 7.0.69

MySQL+PHP5+Apache2安装

请参考:Apache2部署WordPress


Nginx安装

1
sudo apt-get install nginx

Tomcat安装

Tomcat官方下载

  • 解压文件(opt下)
1
tar -zxvf apache-tomcat-7.0.69.tar.gz
  • 开启Tomcat
1
2
3
cd apache-tomcat-7.0.69/bin/

./startup.sh
  • 浏览器输入:

http://IP:8080


Nginx配置

1
sudo vim /etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#user www-data;
worker_processes 4;
pid /run/nginx.pid;

#error_log  /home/jabo/nginx_logs/error.log;
#error_log  /home/jabo/nginx_logs/error.log  notice;
#error_log  /home/jabo/nginx_logs/error.log  info;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##                          
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        #反向代理
        upstream tomcat {
                server 127.0.0.1:8080;
        }

        server {
                listen       80;
                server_name  www.javaapp.com;

                location / {
                        proxy_pass      http://tomcat; #转发到上边定义的java代理中去

                        proxy_redirect          off;
                        proxy_set_header        Host $host;
                        proxy_set_header        X-Real-IP $remote_addr;
                        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        }

        upstream php {
                server 127.0.0.1:9090;
        }

        server {
                listen       80;
                server_name  www.phpapp.com;

                location / {
                        proxy_pass      http://php; #转发到php代理
                        proxy_redirect          off;
                        proxy_set_header        Host $host;
                        proxy_set_header        X-Real-IP $remote_addr;
                        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                }
        }
}

#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
# 
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

修改Apache2配置

  • 修改Apache2监听端口
1
2
3
4
sudo vim /etc/apache2/ports.conf

# 修改Listen 80
Listen 9090
  • 添加虚拟主机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
sudo vim /etc/apache2/sites-available/test.local.conf

# 加入以下内容
<VirtualHost *:9090>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/wordpress
        ServerName www.phpapp.com

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>
  • 让test.local.config生效
1
sudo a2ensite test.local.conf
  • 重启Apache2
1
sudo service apache2 restart

关于WordPress的部署请看: Apache2部署WordPress


Java Web测试程序

GitHub地址

将程序打成war包,放入Tomcat的 webapps 目录下


其他工作

  • 重启Nginx
1
sudo service nginx restart
  • 修改hosts
1
2
3
4
sudo vim /etc/hosts

127.0.0.1       www.javaapp.com
127.0.0.1       www.phpapp.com

测试

  • 浏览器输入:

www.javaapp.com/myTest/index.jsp

转到Java Web测试程序主页

  • 浏览器输入:

www.phpapp.com

转到WordPress安装主页

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值