A concise tutorial on installing configuration for the Tomcat+Nginx server environment under Linux

  • 2020-05-10 23:32:26
  • OfStack

1. Install
1. Install JDK
The downloaded jdk file is: jdk-6u45-linux-x64.bin.


#./jdk-6u12-linux-i586.bin

2. Install tomcat:


#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat

Here I will rename the apache-tomcat-6.0.29 after unzipping to tomcat for easy operation.

3. Configure environment variables:
Edit the profile file under /etc and add the following:


JAVA_HOME="/opt/app/jdk1.6.0_45"
CLASS_PATH="$JAVA_HOME/lib:$JAVA_HOME/jre/lib"
PATH=".:$PATH:$JAVA_HOME/bin"
CATALINA_HOME="/opt/app/tomcat"
export JAVA_HOME CATALINA_HOME

Execute the following command to effect the change:


# source /etc/profile

Launch tomcat and enter http://domain:8080. If you see the cat page, tomcat and jdk installed successfully
Create a new file directory /home/www for the site directory, set the server.xml file, and change the direction of appBase= to /home/www/web at Host name= "localhost"
Create index. jsp to/home www/web/ROOT, content is: "hello!"   restart tomcat, access again, if you see index.jsp file content hello! The setup was successful.

4. Install Nginx
Unzip nginx by executing the following command:


# tar zxvf nginx-1.4.4.tar.gz
# mv nginx-1.4.4 nginx

I also renamed it 1.
Install nginx:


# ./configure --prefix=/opt/app/nginx

An error occurred: error: C compiler cc is not found, installing the tools and libraries required to compile the source code as described on the web:


#yum install gcc gcc-c++ ncurses-devel perl

Install again and find errors: the HTTP rewrite module requires the PCRE library.
perform


# yum -y install pcre-devel openssl openssl-devel

Finally succeeded,


# ./configure --prefix=/opt/app/nginx
# make
# make install

After the successful installation of nginx, the installation directory is /opt/app/nginx

2. Configuration of Nginx
Install nginx and tomcat respectively in the /usr/local/ directory
Add the conf.d directory to nginx for configuration purposes, such as creating a new renhetoutiao.conf file


server {
  listen   80;
  server_name devtoutiao.renhe.cn;
  root    /home/renhetoutiao/renhetoutiao/htdocs;
  access_log /home/renhetoutiao/renhetoutiao/logs/access_log.log;
 
  location = / {
    rewrite ^/$ /index.shtml last;
  }
 
  location ~ .shtml {
    proxy_pass http://localhost:8081;
    proxy_set_header  Host  $host;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  }
 
  location ~ .*.(js|css)?$ {
    expires 1h;
  }
}

server {
  listen   80;
  server_name devtoutiao.renhe.cn;
  root    /home/renhetoutiao/renhetoutiao/htdocs;
  access_log /home/renhetoutiao/renhetoutiao/logs/access_log.log;
 
  location = / {
    rewrite ^/$ /index.shtml last;
  }
 
  location ~ .shtml {
    proxy_pass http://localhost:8081;
    proxy_set_header  Host  $host;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  }
 
  location ~ .*.(js|css)?$ {
    expires 1h;
  }
}

Of nginx conf/nginx conf modification, line 2 user root, give nginx root permissions, added in the http include/usr/local/nginx/conf d / *. conf;
Select a project such as renhetoutiao, directory/home/renhetoutiao renhetoutiao /

3. Configuration of Tomcat
catalina.policy catalina.properties context.xml tomcat-users.xml server.xml web.xml
These five profiles will enable you to start one instance of tomcat, where server.xml must be configured.
catalina. properties web. xml can be used by default from the installation path copy, context. xml tomcat-users. xml can be used without.
So for each project, we only need to configure server.xml, such as the configuration of renhetoutiao project


#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat
0

Because nginx is used, tomcat's ajp will not be used, so you can get rid of it. tomcat can be simply configured like this. Note the connection port and the closing port for each item, so there is no duplication.
Since tomcat goes back to load class lib under the specified path, copy, copy, copy, bin, startup, bin, shutdown, bin, bin, shutdown, bin, bin, shutdown, bin, bin, shutdown


#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat
1

#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat
2

#!/bin/sh
 
export CATALINA_BASE="$PWD"
export CATALINA_HOME="/usr/local/tomcat"
 
$CATALINA_HOME/bin/shutdown.sh

#!/bin/sh
 
export CATALINA_BASE="$PWD"
export CATALINA_HOME="/usr/local/tomcat"
 
$CATALINA_HOME/bin/shutdown.sh

Through tomcat's startup.sh script, the threads allocated in the project will not be released in time, and tomcat will not be closed in time, resulting in port conflicts when restarting
Solution: force down the tomcat process, kill-9 PID, and record the processes of each tomcat instance when the script needs to be started. Here, the process PID is saved in the logs directory of the project.
Recording PID requires modifying the bin/ catalina.sh script in the tomcat installation directory
Add PRGDIR=dirname "$PRG"


#tar zxvf apache-tomcat-6.0.18.tar.gz
#mv apache-tomcat-6.0.29 tomcat
4

Finally, project users need to be given executable permission to chmod 777 catalina.sh


Related articles: