Linux Server Deployment Tomcat and Nginx

Source: Internet
Author: User
Tags nginx server

  Project needs, applied for three test machines, in the test machine inside bare Nothing, I can learn a play a play! Next, in the form of graphics and pictures I met the pit and small harvest it!

First, the preparatory work

  First you have to have a playable Linux server, know the username and password, and then you need to install two tools on the Windows computer that can connect to the Linux server, Xshell 5 and Xftp 5, the installation tutorial does not say much, the complement diagram shows how to connect to the server:

Xshell connection interface as follows, enter a good host IP Click OK, Xshell will pop-up window input user name and password to connect successfully!

  

  Xftp connection interface as follows, fill in the blanks can be, very simple not to say, mention here why choose SFTP instead of FTP, see http://blog.sina.com.cn/s/blog_7a06b2f7010116rw.html can understand!

  

II. Installation and deployment work

  To deploy the project to a Linux server, it is essential to install the JDK and Tomcat, and it is important to emphasize that if you are deploying multiple applications on one server, you need to have your own tomcat for each application, because under different applications, The configuration of Tomcat must be different, for this case, you can install a Tomcat installation directory and the working directory of Tomcat under multiple apps, for the same part can share the installation directory of Tomcat, the following separately detailed how to operate, How to install Tomcat and JDK on your server

  1. Directory Division   

  This is a very important step, you can create a new directory in this way, the directory below the project launch and deployment access related to some of the files, you can see, I placed under this directory multiple versions of the JDK and multiple versions of Tomcat, Which Tomcat and JDK are specifically used during the deployment of a project can be implemented through configuration.

  For the actual application, we specifically placed in an instance directory, set up the name of each application, and then put the corresponding war package under the directory, as well as the extracted files, waiting for Tomcat to read the directory deployment of the application, as follows:

  2. Modify the configuration 

  Each app below the tomcat we also placed under a directory easy to manage, such as, but pay attention to each tomcat configuration

  /bin directory, start.sh files and stop.sh files are to be modified, first to specify the JDK path, similar to the configuration environment variables, followed by the Tomcat working directory to specify the tomcat path, you can notice that the Catalina_ Home and Catalina_base correspond to Tomcat's installation directory and working directory, and then you can see from the last line that the project started using the start.sh under the installation directory. However, the Tomcat profile used by the project startup is the configuration file that each app Tomcat configures itself. Stop.sh is the same thing.

  For the use of Xshell execution./start.sh and./stop.sh are required permission, if there is no permission, we can use the xftp to modify the start.sh mail permissions, press, tick.

  

  Under the/conf directory, you need to be aware that the modified configuration is The first is the Server.xml file, modify the corresponding port number, each application of Tomcat needs to modify the different port number, otherwise it will lead to start multiple Tomcat port occupancy and can not be started, it is best to remember this application configuration of the Tomcat port number, after using the Nginx to use the ports mapping;

  

  Another place to modify is the Root.xml file under the localhost directory in the Catalina directory, which specifies which directory the project instance is loaded with when Tomcat starts.

  

  Other/logs directories,/temp directories,/webapps directories,/work directories do not need to be modified; As can be seen from the above, the directory is clearly divided into, respectively, the Service installation directory (JDK, Tomcat, nginx, etc.), application instance directory, and application deployment directory, When you deploy multiple projects using a Linux server, you can also divide it in this way.

  3.nginx implementation of 80-port reverse Proxy

According to reason, this time we can put our own application of the war package to the corresponding directory to extract, and then go to the corresponding application Tomcat working directory under the execution./start.sh can start the project! You might as well try it first.

After the project was deployed, we configured the hosts to start the visit, and this time we will find that we have to add the Tomcat port number configured above to access it, can we not add the upper port number (using the default 80 port) Access? Also mentioned above, for different tomcat we configured a different port number, because a port number can only be used for a tomcat, which means that the project access must be added port number access, but to do every application can use 80 port access we can do, This time will be used to Nginx, installed nginx After we add some configuration can do reverse proxy, for different applications use 80 port access to the corresponding port number, in Tomcat's view, external requests or the use of Tomcat configuration port number, This also achieves the effect that we want, the specific configuration is as follows (of course we have to do the corresponding directory division work):

  The Nginx directory is as follows:

  

  We only need to care about the/conf directory below the nginx.conf configuration file, specific configuration information I also open to everyone to see, notice the last line, "include domains/*", means that the configuration is not finished, including all the configuration files in the/domains directory,

  

 This time we put the corresponding Nginx configuration of each application, with a different named file configuration is OK

  

 Then look at how to configure it, listen to auction.center.jd.net:80 domain access, and then forward to the local 127.0.0.1 1605 port access, from the configuration we can also see, for some abnormal state, such as http_500 http_502 http_504 and so will record down, do different processing, generally can jump to the homepage of the website and so on, then is the record log, configured this configuration file, we must not forget the new log directory, or nginx can not find the directory, start will error.

  

  After these work, not finished, we need to restart Nginx, specifically how to restart I do not say much, Baidu on a lot of relevant information, if the restart failed, we simply to malicious recruit, kill the Nginx process, and then use the Nginx Service start command to start Nginx services.

After we've done this, are we going to be able to access it in a familiar way (without adding a port number)? Yes, that's right. Many people are concerned about whether or not to restart Tomcat to redeploy the application, it only means that you do not understand the level of the Nginx routing transformation of the network layer, We visit the domain name first through the hosts we configured to do DNS resolution, switch to the corresponding IP, locate the IP corresponding server, through the server's firewall, and then when we configure Nginx server, Nginx will first intercept the request, processing and distribution of requests, Finally will hit the corresponding tomcat on (personal understanding, may not be in place, do not spray), this time we can confidently access the domain name to send the request!!

  4. Step on the Pit

  Amount: Not good!! Empty joy a game, jump to the home page, or can not access the specified application, when I try to add the port number, "miracle" like to access, which means that we configured the Nginx does not take effect, the problem is?

The following is my problem-solving ideas, first we have to confirm that all the requests have come to nginx, this time embodies the benefits of the log, when we use 80 port access to our application, staring at the log to see, two times three times again. There is no log, this time we can roughly conclude that there is no nginx service, and can not determine the Nginx service for the server does not take effect, should be our log configuration is problematic, this time I made a bold operation, that is to switch off, Nginx service access, or jump to the homepage of the website, what's going on? Does jumping to the homepage not be configured by Nginx? Indeed, the company in the network layer has already done the processing, this time I am very confused, completely is a variety of attempts, may wish to modify the Tomcat configuration, one of the applications configured to 80 port, so that can always access it, quickly, modify the completion and deployment, waiting for the address bar input domain name access, Because Nginx service has been closed, this time the request is definitely directly hit Tomcat on, visit two times three times, "miracle" like incredibly can't access, Nani??? This is a flash, absolutely the server firewall disabled 80 port, hurriedly try to local Telnet 192.168.108.37 80 really can not access, prove that my guess is right, this time is good to do, as long as the firewall to open 80 port access rights can be, Here's how to configure the Iptables firewall to release port 80 under CentOS.

  1. Open the Iptables configuration file with the following code: Vi/etc/sysconfig/iptables

2. Through the/etc/init.d/iptables status, the command queries whether there are open 80 ports, if there are no two ways to handle

3. The first way: Modify the Vi/etc/sysconfig/iptables command to add the firewall to open 80 ports, the code is as follows:

-A rh-firewall-1-input-m state--state new-m tcp-p TCP--dport 80-j ACCEPT

  4. Turn off/On/Restart the firewall code as follows:/etc/init.d/iptables stop (#start on, #restart restart)

5. The second way: by command to add the port, the code is as follows:

[Email protected] httpd]#/sbin/iptables-i input-p TCP--dport-/etc/rc.d/init.d//etc/init.d/ iptables restart

  6. View the effect: [[email protected] httpd]#/etc/init.d/iptables status

7. Add one, recommend the use of the second way, simple operation, high success rate, take me, when I use the first way to modify the firewall configuration, restart failure, using the second way, perfect success

Iii. Summary

  The final result is also expected, even if Tomcat is configured with 1605 ports, we can still use 80 port access, this is Nginx's powerful, but a few trillion of installation package, incredibly can play so much role, this is I just learn nginx introduction, understand very shallow, But through this practice let me have a good impression of nginx, slowly I will learn from the realization of the familiar (although not know Love).

  Add : So many of the above are based on some basic Linux commands, for do not know how to create files, modify files, upload War package, start Tomcat, view the process, check the log and so on basic commands, it is best to learn the basic Linux command, Slowly you will find that many things can be done with xftp visual interface, we prefer to use the command to operate.

Linux Server Deployment Tomcat and Nginx

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.