A concise tutorial on installing configuration of Tomcat+nginx Server environment under Linux _nginx

Source: Internet
Author: User
Tags create index mkdir openssl nginx server tomcat

First, installation
1. Install JDK
the downloaded JDK file is: Jdk-6u45-linux-x64.bin, performing the following command to install:

#./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'll rename the extracted apache-tomcat-6.0.29 to make it easy for Tomcat to operate.

3. Configure Environment variables:
edit the profile under/etc, plus 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 make the change take effect:

# Source/etc/profile

Start Tomcat and enter http://domain:8080, if you see the cat page that Tomcat and JDK are installed successfully
New file directory/home/www for Web site directory, set Server.xml file, at host name= "localhost" place appbase= point to the path to/home/www/web
Create index.jsp to/home/www/web/root, which reads: "Hello!" Restart Tomcat, re-access if you see index.jsp file content hello! Indicates that the setting was successful.

4. Install Nginx
perform the following command to extract the Nginx:

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

Also renamed a bit.
Install Nginx:

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

The result was an error: Error:c compiler cc is not found, install the tools and libraries needed to compile the source code as described on the Web:

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

Install again and find error: 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
Install

The installation directory after the successful installation of Nginx is/opt/app/nginx

Ii. configuration of the nginx aspect
install Nginx and Tomcat separately under the/usr/local/directory
Add a conf.d directory for Nginx, for each purpose configuration, 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;
}}

 

For Nginx conf/nginx.conf modification, the second line of user root gives Nginx root permission to add include/usr/local/nginx/conf.d/*.conf to HTTP;
Select an item such as Renhetoutiao, directory/home/renhetoutiao/renhetoutiao/

Three, the configuration of Tomcat
catalina.policy catalina.properties context.xml tomcat-users.xml server.xml web.xml
These 5 profiles allow you to start a Tomcat instance, where Server.xml must be configured.
Catalina.policy catalina.properties Web.xml can be used by default, which can be copy,context.xml from the installation path Tomcat-users.xml can not.
So each project we just need to configure server.xml, such as the Renhetoutiao project configuration

<?xml version= ' 1.0 ' encoding= ' utf-8 '?> <server the port= "8015" shutdown= "shutdown" > <listener classname= "O Rg.apache.catalina.core.AprLifecycleListener "sslengine=" on "/> <listener classname=" Org.apache.catalina.core.JasperListener "/> <listener classname=" Org.apache.catalina.core.JreMemoryLeakPreventionListener "/> <listener classname=" Org.apache.catalina.mbeans.ServerLifecycleListener "/> <listener classname="  Org.apache.catalina.mbeans.GlobalResourcesLifecycleListener "/> <service name=" Catalina "> <connector Port= "8081" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/> <engine name= "Catalina" Defa ulthost= "localhost" >  

Because the AJP that uses nginx,tomcat is not used, so you can remove it, Tomcat can be configured as simple. Note the connection and shutdown ports for each item without duplication.
because Tomcat goes back to load the class lib under the specified path, you must copy the configuration, dependencies, and so on to the web, start the script Bin/startup, and close the script Bin/shutdown as follows:

 #!/bin/sh export catalina_base= "$PWD" Export catalina_home= "/usr/local/tomcat" #启动为适 Prepared with the Tomcat directory #迁移tomcat必要的配置文件 catalina.policy catalina.properties context.xml tomcat-users.xml web.xml if [!-F ' $CATAL Ina_base "/conf/catalina.policy]; Then CP "$CATALINA _home"/conf/catalina.policy "$CATALINA _base"/conf fi if [!-F "$CATALINA _base"/conf/catalina.proper Ties]; Then CP "$CATALINA _home"/conf/catalina.properties "$CATALINA _base"/conf fi if [!-F "$CATALINA _base"/conf/web.xml] ; Then CP "$CATALINA _home"/conf/web.xml "$CATALINA _base"/conf fi if [!-d "$CATALINA _base"/logs]; Then mkdir logs fi #准备classes和lib目录 classes_path= "$CATALINA _base"/web/web-inf/classes lib_path= "$CATALINA _base"/web/ Web-inf/lib rm-rf "$CLASSES _path" RM-RF "$LIB _path" cp-r "$CATALINA _base"/conf "$CLASSES _path" cp-r "$CATALINA _base"/L IB "$LIB _path" #启动时会记录进程ID, stored in logs/tomcat.pid #shutdown时强制关闭该进程 $CATALINA _home/bin/startup.sh 
 #!/bin/sh export catalina_base= "$PWD" Export catalina_home= "/usr/local/tomcat" #启动为适 Prepared with the Tomcat directory #迁移tomcat必要的配置文件 catalina.policy catalina.properties context.xml tomcat-users.xml web.xml if [!-F ' $CATAL Ina_base "/conf/catalina.policy]; Then CP "$CATALINA _home"/conf/catalina.policy "$CATALINA _base"/conf fi if [!-F "$CATALINA _base"/conf/catalina.proper Ties]; Then CP "$CATALINA _home"/conf/catalina.properties "$CATALINA _base"/conf fi if [!-F "$CATALINA _base"/conf/web.xml] ; Then CP "$CATALINA _home"/conf/web.xml "$CATALINA _base"/conf fi if [!-d "$CATALINA _base"/logs]; Then mkdir logs fi #准备classes和lib目录 classes_path= "$CATALINA _base"/web/web-inf/classes lib_path= "$CATALINA _base"/web/ Web-inf/lib rm-rf "$CLASSES _path" RM-RF "$LIB _path" cp-r "$CATALINA _base"/conf "$CLASSES _path" cp-r "$CATALINA _base"/L IB "$LIB _path" #启动时会记录进程ID, stored in logs/tomcat.pid #shutdown时强制关闭该进程 $CATALINA _home/bin/startup.sh 
#!/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 are not released in time, and Tomcat cannot be shut down in time, causing a port conflict when the reboot occurs
Solution: Force the Tomcat process off, kill-9 PID, execute the script to log the process of each Tomcat instance at startup, where the process PID is saved in the project's logs directory.
Recording PID operations requires modifying the bin/catalina.sh script under the Tomcat installation directory
After the Prgdir=dirname "$PRG" plus

Tomcat_pid= "$PWD"/"Logs/tomcat.pid"
if [f "$TOMCAT _pid"]; then
  catalina_pid= "$TOMCAT _pid"
elif [$ = "Start"]; Then
  catalina_pid= "$TOMCAT _pid"
fi

tomcat_pid= "$PWD"/"Logs/tomcat.pid"
if [-F "$TOMCAT _pid"] ; Then
  catalina_pid= "$TOMCAT _pid"
elif [$ = "start]" then
  catalina_pid= "$TOMCAT _pid"
fi

Finally, you need to give the project user permission to execute chmod 777 catalina.sh

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.