Techminded

Tomcat6, Nginx and PHP in one Linux box

Tomcat

You should install tomcat either from packages

$sudo apt-get install tomcat6 tomcat6-admin

or from downloadable bundle and ensure it's running on port 8081 by finding in server configuration file (server.xml) lines similar to the following:

    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="443" URIEncoding="UTF-8" />

Nginx and PHP

Install packages

$sudo apt-get install nginx php5cli php5-fpm

After installed you should either add virtual host or make /etc/nginx/sies-enabled/default looks like this:

server {
listen 80;
server_name site.ru www.site.ru;
root /var/lib/tomcat6/webapps/ROOT;
location / {
index index.jsp index.php;
}
## force www in URL
if ($host !~* ^(www|subdomain))
{
rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
}
## human readable aliases for URLs
if (!-e $request_filename)
{
rewrite ^/dj/(.*)$ /dj.jsp?name=$1 last;
rewrite ^/music/(.*)$ /music.jsp?track=$1 last;

}

location ~ \.do$ {
proxy_pass http://localhost:8081;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}

location ~ \.jsp$ {
proxy_pass http://localhost:8081;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}

location ^~/servlets/* {
proxy_pass http://localhost:8081;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/lib/tomcat6/webapps/ROOT$fastcgi_script_name;
include fastcgi_params;
}
}

Configuratioin contains some optional rewrite configurations that you can use or not. Anyway you should adopt it to your needs or use as an example for own configuration as details may differs.

Comments