Home Operation and Maintenance Nginx How to configure the server-side environment of Nginx+Tomcat

How to configure the server-side environment of Nginx+Tomcat

May 13, 2023 pm 07:58 PM
nginx tomcat

1. Java jdk installation:

#Download the corresponding jdk software package, then unzip and install it. The name of my package here is: jdk-7u25-linux-x64.tar.gz

tar -xzf jdk-7u25-linux-x64.tar.gz ;mkdir -p /usr/java/ ;mv jdk1.7.0_25/ /usr/java/ 下.


#Then configure the environment variables so that jdk can be referenced anywhere, configure as follows:

#vi /etc/profile Finally add the following statement:

export java_home=/usr/java/jdk1.7.0_25 
 
export classpath=$classpath:$java_home/lib:$java_home/jre/lib 
 
export path=$java_home/bin:$java_home/jre/bin:$path:$homr/bin


#source /etc/profile #Make the environment variables take effect immediately

#java --version #Check the java version and see jdk1.7.0_25 The version means that java jdk is successfully installed.
2. nginx installation:

wget http://nginx.org/download/nginx-1.2.6.tar.gz 
 
useradd www 
 
tar zxvf nginx-1.2.6.tar.gz 
 
cd nginx-1.2.6/ 
 
./configure --user=www --group=www --prefix=/usr/local/nginx 
 \--with-http_stub_status_module --with-http_ssl_module 
 
make && make install


#nginx is installed, then use the command:/usr/local/nginx/sbin/nginx -t to test ok, which represents nginx Successful installation.

/usr/local/nginx/sbin/nginx Press Enter to start nginx. You can see the nginx default page by accessing http://ip/.
3. Tomcat installation:

# Official website to download tomcat 6.0.30 or other versions:

cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz


#Directly After decompression, you can use it. After decompression is completed, copy two tomcats at the same time and name them tomcat1 tomcat2

mv apache-tomcat-6.0.30 /usr/local/tomcat1 
 
cp /usr/local/tomcat1 /usr/local/tomcat2 -r


#Modify the tomcat1 and tomcat2 ports respectively. There are three ports that need to be modified. They are as follows:

shutdown port: 8005 is mainly responsible for startup and shutdown.

ajp port: 8009 is mainly responsible for balancing through ajp (commonly used for apache and tomcat integration)

http port: 8080 can be accessed directly through the web page (nginx tomcata integration)

#Note* If the three ports of tomcat1 are: 8005 8009 8080, then the tomcat2 port will be 1 on this basis, which is: 8006 8010 8081

#On a server, the port cannot be repeated, otherwise an error will be reported.

#After modifying the port, then start two tomcats. The startup command is:

#How to prompt that there is no such file or insufficient permissions. You need to grant execution permissions to the sh file in the bin directory of tomcat: chmod o You can use http://ip:8080 http://ip:8081 to access the tomcat default page.
#If you need to modify the tomcat release directory to your own directory, you need to make the following adjustments and create two release directories:

/usr/local/tomcat1/bin/startup.sh 
 
/usr/local/tomcat2/bin/startup.sh

Edit vi /usr/local/tomcat1/conf/server.xml at the end Add the content in the previous line:

mkdir -p /usr/webapps/{www1,www2}

Edit vi /usr/local/tomcat2/conf/server.xml and add the content in the last line before :
<context path="" docbase="/usr/webapps/www1" reloadable="false"/>

tomcat1 publishes directory content:

<context path="" docbase="/usr/webapps/www2" reloadable="false"/>

tomcat2 publishes directory content:

<html> 
<body> 
<h1>tomcat_1 jsp test page</h1> 
<%=new java.util.date()%> 
</body> 
</html>

Then visit http://ip:8080, 8081 to view the test content.

4. nginx tomcat integration:

Integration is mainly to modify the nginx.conf configuration and give a complete nginx.conf online configuration. Some parameters can be customized according to the actual situation. Requirement modification:

<html> 
<body> 
<h1>tomcat_2 jsp test page</h1> 
<%=new java.util.date()%> 
</body> 
</html>
#Note* The web_app defined by proxy_pass in the server segment needs to be consistent with the web_app defined in upstream, otherwise the server cannot find the balance.

# As configured above, the nginx tomcat reverse proxy load balancing configuration is completed. If you want to separate dynamic and static, you only need to add the following configuration to nginx and it will be ok.

#Configuring nginx dynamic and static separation

user www www; 
worker_processes 8; 
pid /usr/local/nginx/nginx.pid; 
worker_rlimit_nofile 102400; 
events 
{ 
use epoll; 
worker_connections 102400; 
} 
http 
{ 
 include  mime.types; 
 default_type application/octet-stream; 
 fastcgi_intercept_errors on; 
 charset utf-8; 
 server_names_hash_bucket_size 128; 
 client_header_buffer_size 4k; 
 large_client_header_buffers 4 32k; 
 client_max_body_size 300m; 
 sendfile on; 
 tcp_nopush  on; 
  
 keepalive_timeout 60; 
  
 tcp_nodelay on; 
 client_body_buffer_size 512k; 
 
 proxy_connect_timeout 5; 
 proxy_read_timeout  60; 
 proxy_send_timeout  5; 
 proxy_buffer_size  16k; 
 proxy_buffers   4 64k; 
 proxy_busy_buffers_size 128k; 
 proxy_temp_file_write_size 128k; 
  
 gzip on; 
 gzip_min_length 1k; 
 gzip_buffers  4 16k; 
 gzip_http_version 1.1; 
 gzip_comp_level 2; 
 gzip_types  text/plain application/x-javascript text/css application/xml; 
 gzip_vary on; 
  
###2012-12-19 change nginx logs 
log_format main &#39;$http_x_forwarded_for - $remote_user [$time_local] "$request" &#39; 
    &#39;$status $body_bytes_sent "$http_referer" &#39; 
    &#39;"$http_user_agent" $request_time $remote_addr&#39;; 
     
upstream web_app { 
 server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; 
 server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s; 
} 
 
####chinaapp.sinaapp.com 
server { 
 listen 80; 
 server_name chinaapp.sinaapp.com; 
 index index.jsp index.html index.htm; 
 #发布目录/data/www 
 root /data/www; 
  
 location / 
 { 
 proxy_next_upstream http_502 http_504 error timeout invalid_header; 
 proxy_set_header host $host; 
 proxy_set_header x-real-ip $remote_addr; 
 proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
 proxy_pass http://web_app; 
 expires  3d; 
 } 
  
 } 
 
}

5. Turn on nginx monitoring

1), nginx simple status monitoring

Add the following code in nginx.conf to monitor the current status of nginx, and then access http://serverip/status to access

location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ 
 
{ 
 
root /data/www; 
 
#expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 
 
expires  3d; 
 
}

Generally displayed as
location /status {
stub_status on;
access_log off;
}

ctive connections: Number of active connections initiated to the backend.

server accepts handled requests: nginx processed a total of 24 connections, successfully created 24 handshakes (proof that there were no failures in the middle), and processed a total of 129 requests.

reading: nginx reads the number of header information from the client.

writing: nginx returns the number of header information to the client.

waiting: When keep-alive is turned on, this The value is equal to active – (reading writing), which means that nginx has completed processing and is waiting for the next request for the resident connection.

Note that this module will not be compiled into nginx by default. If you want to use this module , you need to specify when compiling and installing nginx:

active connections: 16 
server accepts handled requests
191226 191226 305915 
reading: 0 writing: 1 waiting: 15

Check whether the installed nginx contains the stub_status module


 ./configure –with-http_stub_status_module
 #/usr/local/nginx/sbin/nginx -v

2), graphical monitoring of nginx -nginx-rrd stats


nginx-rrd is an nginx monitoring tool officially recommended by nginx. nginx-rrd can be used to easily generate charts for us to view.

a. Operating environment (centos):

在安装前需要安装好rrdtool这个画图工具和相应的perl模块,可以先运行:

yum install rrdtool libhtml-parser-perl libwww-perl librrds-perl librrd2-dev

确保rrdtool和相应的perl被安装上。

b、安装配置

下载:

wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz

解压:

tar zxvf nginx-rrd-0.1.4.tgz

进入nginx-rrd目录,

cd nginx-rrd-0.1.4/

复制主程序:

cp usr/sbin/* /usr/sbin

复制配置文件

cp etc/nginx-rrd.conf /etc

复制定时执行文件:

cp etc/cron.d/nginx-rrd.cron /etc/cron.d

创建nginx-rrd生成目录:

mkdir /home/wwwroot/nginx && mkdir /home/wwwroot/nginx/rrd

cp html/index.php /home/wwwroot/nginx

编辑/home/wwwroot/nginx/index.php修改登录密码

<?php
header("content-type: text/html; charset=utf-8");

$password = "admin"; 

.........

编辑配置文件nginx-rrd.conf,修改完成后如下:

#####################################################
#
# dir where rrd databases are stored
rrd_dir="/home/wwwroot/nginx-rrd/";
# dir where png images are presented
www_dir="/home/wwwroot/nginx/";
# process nice level
nice_level="-19";
# bin dir
bin_dir="/usr/sbin";
# servers to test
# server_utl;server_name
servers_url="http://127.0.0.1/status;127.0.0.1"

多个server,可以servers_url中空格分开,前部分为nginx_status的地址,后面为被监控主机的域名。

severs_url 格式

注意,nginx.conf虚拟主机server{}中,需要已经加入:

location /status {
stub_status on;
access_log off;
}

以上设置就完成,可以自行运行一下:/usr/sbin/nginx-collect ,启动收集程序。cron会15分钟生成一次数据。

如果没有定时执行生成数据,可以在/etc/crontab最后面加上:

* * * * * root /usr/sbin/nginx-collect
*/15 * * * * root /usr/sbin/nginx-graph

然后输入然后访问http://serverip/nginx/即可访问。

How to configure the server-side environment of Nginx+Tomcat

6、开启tomcat的监控

1)、tomcat6的配置

修改tomcat/conftomcat-users.xml文件中节点之前添加如下代码即可。

 <user username="admin" password="admin" roles="manager"/>

然后输入然后访问http://serverip:8080/manager/status即可访问。

2)tomcat7的配置

修改tomcat/conftomcat-users.xml文件中节点之前添加如下代码即可。

<role rolename="manager-gui"/>
<user username="tomcat" admin="admin" roles="manager-gui"/>

然后输入然后访问http://serverip:8080/manager/status即可访问。

How to configure the server-side environment of Nginx+Tomcat

The above is the detailed content of How to configure the server-side environment of Nginx+Tomcat. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to execute php code after writing php code? Several common ways to execute php code How to execute php code after writing php code? Several common ways to execute php code May 23, 2025 pm 08:33 PM

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

Detailed introduction to each directory of Linux and each directory (reprinted) Detailed introduction to each directory of Linux and each directory (reprinted) May 22, 2025 pm 07:54 PM

[Common Directory Description] Directory/bin stores binary executable files (ls, cat, mkdir, etc.), and common commands are generally here. /etc stores system management and configuration files/home stores all user files. The root directory of the user's home directory is the basis of the user's home directory. For example, the home directory of the user user is /home/user. You can use ~user to represent /usr to store system applications. The more important directory /usr/local Local system administrator software installation directory (install system-level applications). This is the largest directory, and almost all the applications and files to be used are in this directory. /usr/x11r6 Directory for storing x window/usr/bin Many

How to update Debian Tomcat How to update Debian Tomcat May 28, 2025 pm 04:54 PM

Updating the Tomcat version in the Debian system generally includes the following process: Before performing the update operation, be sure to do a complete backup of the existing Tomcat environment. This covers the /opt/tomcat folder and its related configuration documents, such as server.xml, context.xml, and web.xml. The backup task can be completed through the following command: sudocp-r/opt/tomcat/opt/tomcat_backup Get the new version Tomcat Go to ApacheTomcat's official website to download the latest version. According to your Debian system

How to limit user resources in Linux? How to configure ulimit? How to limit user resources in Linux? How to configure ulimit? May 29, 2025 pm 11:09 PM

Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

What does java middleware mean? Definition and typical applications of middleware What does java middleware mean? Definition and typical applications of middleware May 28, 2025 pm 05:51 PM

Java middleware is a software that connects operating systems and application software, providing general services to help developers focus on business logic. Typical applications include: 1. Web server (such as Tomcat and Jetty), which handles HTTP requests; 2. Message queue (such as Kafka and RabbitMQ), which handles asynchronous communication; 3. Transaction management (such as SpringTransaction), which ensures data consistency; 4. ORM framework (such as Hibernate and MyBatis), which simplifies database operations.

What are the Debian Nginx configuration skills? What are the Debian Nginx configuration skills? May 29, 2025 pm 11:06 PM

When configuring Nginx on Debian system, the following are some practical tips: The basic structure of the configuration file global settings: Define behavioral parameters that affect the entire Nginx service, such as the number of worker threads and the permissions of running users. Event handling part: Deciding how Nginx deals with network connections is a key configuration for improving performance. HTTP service part: contains a large number of settings related to HTTP service, and can embed multiple servers and location blocks. Core configuration options worker_connections: Define the maximum number of connections that each worker thread can handle, usually set to 1024. multi_accept: Activate the multi-connection reception mode and enhance the ability of concurrent processing. s

Configure PhpStorm and Docker containerized development environment Configure PhpStorm and Docker containerized development environment May 20, 2025 pm 07:54 PM

Through Docker containerization technology, PHP developers can use PhpStorm to improve development efficiency and environmental consistency. The specific steps include: 1. Create a Dockerfile to define the PHP environment; 2. Configure the Docker connection in PhpStorm; 3. Create a DockerCompose file to define the service; 4. Configure the remote PHP interpreter. The advantages are strong environmental consistency, and the disadvantages include long startup time and complex debugging.

What are the SEO optimization techniques for Debian Apache2? What are the SEO optimization techniques for Debian Apache2? May 28, 2025 pm 05:03 PM

DebianApache2's SEO optimization skills cover multiple levels. Here are some key methods: Keyword research: Use tools (such as keyword magic tools) to mine the core and auxiliary keywords of the page. High-quality content creation: produce valuable and original content, and the content needs to be conducted in-depth research to ensure smooth language and clear format. Content layout and structure optimization: Use titles and subtitles to guide reading. Write concise and clear paragraphs and sentences. Use the list to display key information. Combining multimedia such as pictures and videos to enhance expression. The blank design improves the readability of text. Technical level SEO improvement: robots.txt file: Specifies the access rights of search engine crawlers. Accelerate web page loading: optimized with the help of caching mechanism and Apache configuration

See all articles