Deploy tomcat, Nginx, and linuxtomcatnginx on a linux Server

Source: Internet
Author: User

Deploy tomcat, Nginx, and linuxtomcatnginx on a linux Server

Project requirements: I have applied for three test machines. Fortunately, there is nothing bare in the test machine, so I can study it well! Next, let's talk about the pitfalls and small gains I have encountered in the form of text!

I. Preparations

First, you have to have a linux server that can be played with, know the user name and password, and then you need to install two tools on the windows computer that can be connected to the linux server, Xshell 5 and Xftp 5, the installation tutorial is not mentioned much. The supplementary diagram shows how to connect to the server:

The xshell connection interface is as follows. Enter the Host ip address and click OK. In the displayed window, enter the user name and password to connect successfully!

  

The xftp connection interface is as follows. Just fill in the blanks and you can simply leave it blank. Here, why do you choose SFTP instead of FTP? See http://blog.sina.com.cn/s/blog_7a06b2f70101_r1_html!

  

Ii. installation and deployment

To deploy a project on a linux server, installing jdk and tomcat is essential. Here, we must emphasize that if you want to deploy multiple applications on one server, you need to have your own tomcat for each application, because the tomcat configuration is definitely different under different applications. In this case, you can install a tomcat installation directory and a working directory of tomcat under multiple applications. For tomcat that can share the same part of the installation directory, the following describes how to perform this operation separately, how to install tomcat and jdk on your server

  1. Directory Division   

This is an important step. You can create a directory in this way. Some files related to project startup and deployment access are placed under this directory, I have installed Multiple jdk versions and multiple tomcat versions under this directory. During project deployment, the specific tomcat and jdk versions can be implemented through configuration.

For actual applications, we put the name of each application under an instance directory, and place the corresponding war package and decompressed files under the directory, wait for tomcat to read the Directory and deploy the application as follows:

  2. modify configuration 

Tomcat under each application is also placed in a directory for convenient management, for example, but pay attention to the configuration of each tomcat

/Bin directory, start. sh file and stop. sh files must be modified. First, you must specify the jdk path, which is similar to the configuration environment variable. Second, you must specify the tomcat path in the tomcat working directory, CATALINA_HOME and CATALINA_BASE correspond to the tomcat installation directory and working directory respectively. Then, you can see from the last line that start the project using start under the installation directory. sh, but the tomcat configuration file used for project startup is the configuration file configured by tomcat. The same applies to stop. sh.

For xshell execution. /start. sh and. /stop. sh requires permissions. If there is no permission, we can use xftp to start. sh mail modification permission, press and tick.

  

Under the/conf directory, you must note that the modified configurations are: server first. modify the corresponding port number in the xml file. The tomcat of each application must be modified with different port numbers. Otherwise, the port cannot be started because it is occupied when multiple tomcat servers are started, remember the tomcat port number configured for this application, which will be used later for port ing using nginx;

  

Another point to modify is the ROOT. xml file under the localhost directory under the Catalina Directory, which is used to specify the project instance under the directory to be loaded when tomcat is started.

  

Other/logs directories,/temp directories,/webapps directories, and/work directories do not need to be modified. As you can see above, directories are clearly divided, service Installation Directory (jdk, tomcat, nginx, etc.), application instance directory, and Application Deployment directory. You can also use this method to deploy multiple projects on the linux server.

  3. nginx implements port 80 reverse proxy

In principle, at this time, we can upload the war package of our own application to the corresponding directory and decompress it, and then execute it under the corresponding tomcat working directory of the application. /start. sh to start the project! You may try it first.

After the project is deployed, we have configured hosts to start accessing. At this time, we will find that you must add the tomcat port number configured above to access it, can we add a port number (using the default port 80) for access? As mentioned above, we have configured different port numbers for different tomcat because one port number can only be used by one tomcat, which means that the project access must be accessed by adding a port number. However, we can also achieve that every application can use port 80 for access. At this time, we need to use nginx. After installing nginx, we can add some configurations to implement reverse proxy, the port 80 access requests used by different applications are forwarded to the corresponding port number. In tomcat's view, external requests still use the port number configured by tomcat, which achieves the desired effect, the specific configuration is as follows (of course we have to do a proper job of directory Division ):

The nginx directory is as follows:

  

We only need to care about nginx under the/conf directory. conf configuration file. I have opened the specific configuration information for you. Have you noticed the last line? "include domains/*" indicates that the configuration is not complete yet, contains all configuration files in the/domains directory,

  

At this time, we can configure the nginx configuration for each application with different named files.

  

Then let's take a look at how to configure it, listen to auction.center.jd.net: 80 domain name access, and then forward the access to port 1605 of 127.0.0.1 on the local machine. We can also see from the configuration that for some abnormal states, for example, http_500 http_502 http_504 will be recorded and processed differently. In general, you can jump to the homepage of the website, and then record the log. After this configuration file is configured, we must not forget to create a new log directory. Otherwise, if nginx cannot find the directory, an error will be reported during startup.

  

After all these work is done, we need to restart nginx. I will not talk about how to restart nginx. There are a lot of related information on Baidu. If the restart fails, let's do it, kill the nginx process, and then use the nginx service start command to start the nginx service.

After we finish this work, can we use the method we are familiar with (without adding a port number) to access it? Well, yes, that's right, many people are concerned about whether to restart tomcat to redeploy the application. This only means that you do not understand the layer of nginx route conversion at the network layer, the accessed domain name first performs DNS resolution through the configured hosts, converts it to the corresponding Ip address, finds the server corresponding to the ip address, goes through the server firewall, and then when we configure Nginx for the server, nginx will first intercept requests, process and distribute the requests, and finally hit the corresponding tomcat (personal understanding, may not understand, Do not spray ), at this time, we can send requests to access the domain name with confidence !!

  4. pitfall

.. Bad !! I was so happy that I couldn't access the specified application even when I jumped to the homepage. When I tried to add the port number, I could access it in a miraculous way, which means that the Nginx we configured didn't take effect, what is the problem?

Next let's talk about my solution. First, we have to confirm that all requests are sent to Nginx. This shows the benefits of logs. When we use port 80 to access our applications, staring at the log, two times, three times .. No logs. At this time, we can roughly conclude that the Nginx service has not gone through the nginx service at all, and we cannot determine that the Nginx service is not effective for the server. It should be due to a problem with our log configuration, at this time, I made a bold operation, that is, to turn it off and access the nginx service, or to jump to the homepage of the website. What is going on? Didn't I configure the page to jump to the homepage through nginx? Indeed, the company has already done some processing at the network layer. At this time, I am very confused about all kinds of attempts. You may wish to modify the tomcat configuration and configure one of the applications to port 80, in this way, you can access it. Soon, after the modification is complete and the deployment is completed, you will be waiting for the address bar to enter the domain name for access, because the Nginx service has been disabled, at this time, the request must be directly sent to tomcat. The request is sent twice or three times, and the request cannot be accessed in a miraculous way. Nanni ??? In this case, the server firewall has disabled port 80, and I tried to telnet 192.168.108.37 80 locally to make sure it was not accessible. It proved that I was right and it was easy to handle it, you only need to enable port 80 access permission in the firewall. The following describes how to configure iptables firewall in CentOS to open port 80.

1. Open the iptables configuration file. The Code is as follows: vi/etc/sysconfig/iptables

2. Run the/etc/init. d/iptables status Command to check whether port 80 is enabled.

3. Method 1: Modify the vi/etc/sysconfig/iptables command to make the firewall open port 80. The Code is as follows:

 -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

4. The code for disabling/enabling/restarting the firewall is as follows:/etc/init. d/iptables stop (# start enabling and # restart restarting)

5. Method 2: run the following command to add a Port:

[root@centos httpd]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT[root@centos httpd]# /etc/rc.d/init.d/iptables save[root@centos httpd]# /etc/init.d/iptables restart

6. view the results: [root @ centos httpd] #/etc/init. d/iptables status

7. in addition, we recommend that you use the second method. The operation is simple and the success rate is high. Take me for example. When I use the first method to modify the firewall configuration, the restart will fail, the second method is perfect for success.

Iii. Summary

The final result is expected. Even if tomcat is configured with port 1605, port 80 can still be used for access. This is the strength of nginx, but the installation package of a few megabytes can be used, I can play so many roles. This is a simple introduction to nginx, but I have a crush on nginx through this practice, slowly, I will learn from my understanding and familiarity (although I cannot know each other ).

Supplement: All of the above are based on some basic linux commands. If you do not know how to create a file, modify the file, upload the war package, start tomcat, and view the process, to view logs and other basic commands, you 'd better first learn the basic linux commands. Slowly, you will find that many things that can be done on the xftp visual interface prefer to use command operations.

 

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.