通常情况下,如果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官方下载
1
| tar -zxvf apache-tomcat-7.0.69.tar.gz
|
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
| worker_processes 4; pid /run/nginx.pid;
events { worker_connections 768; }
http {
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048;
include /etc/nginx/mime.types; default_type application/octet-stream;
access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log;
gzip on; gzip_disable "msie6";
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;
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; 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; } } }
|
修改Apache2配置
1 2 3 4
| sudo vim /etc/apache2/ports.conf
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>
|
1
| sudo a2ensite test.local.conf
|
1
| sudo service apache2 restart
|
关于WordPress的部署请看: Apache2部署WordPress
Java Web测试程序
GitHub地址
将程序打成war包,放入Tomcat的 webapps 目录下
其他工作
1
| sudo service nginx restart
|
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安装主页