1. Preparation work
First you need a linux server, know the user name and password, and then you need to install it on a windows computer Install two tools that can connect to the Linux server, Xshell 5 and Xftp 5. I won’t go into the installation tutorial. The supplementary picture shows how to connect to the server:
The xshell connection interface is as follows, enter After confirming the host IP, click OK, and xshell will pop up a window to enter the user name and password to connect successfully!
You can understand it!
2. Installation and deployment work
To deploy the project to On a Linux server, it is essential to install jdk and tomcat. What I want to emphasize here is that if you want to deploy multiple applications on a server, you need to have its own tomcat for each application, because different Under the application, the configuration of tomcat is definitely different. In this case, you can install a tomcat installation directory and tomcat working directories under multiple applications. For the same parts, you can share the tomcat installation directory. The details will be introduced below. How to operate, how to install tomcat and jdk on your server
1. Directory division
This is A very important step is to create a new directory as shown in the figure below. Under this directory are placed some files related to project startup and deployment access. As you can see, I have placed multiple versions of jdk and multiple Version of tomcat, which tomcat and jdk are used during project deployment can be configured through configuration.
For actual applications, we specifically place them in an instance directory, establish the name of each application, and then place the corresponding war package, as well as the decompressed files, wait for tomcat to read the directory and deploy the application, as follows:
2 .Modify the configuration
We also place the tomcat under each application in a directory for easy management, as shown below, but you must pay attention to the configuration of each tomcat
Under the /bin directory, the start.sh file and stop.sh file must be modified. First, the jdk path must be specified, similar to configuring environment variables, and secondly That is, specify the tomcat path of the tomcat working directory. You can notice that CATALINA_HOME and CATALINA_BASE correspond to the installation directory and working directory of tomcat respectively. Then you can see from the last line that the project starts using start.sh under the installation directory. However, the tomcat configuration file used for project startup is the configuration file configured by each application tomcat. The same is true for stop.sh.
Permission is required to execute ./start.sh and ./stop.sh using xshell. If there is no permission, we can use xftp can modify the permissions of the start.sh email as shown in the figure and put a check mark.
Under the
/conf directory, you need to pay attention to the configuration modifications. The first is the server.xml file. Modify the corresponding port number and each application. Tomcat needs to modify different port numbers, otherwise the port will be occupied when starting multiple tomcats and cannot be started. It is best to remember the port number of tomcat configured for this application, which will be used later when using nginx for port mapping;
Another place to modify is the ROOT.xml file in the localhost directory under the Catalina directory. This file is used to specify which directory to load when tomcat starts. Project Example
Other /logs directory, /temp directory, /webapps directory, /work directory do not need to be modified; it can also be seen from the above , the directory division is very clear, divided into service installation directory (jdk, tomcat, nginx, etc.), application instance directory, and application deployment directory. You can also divide it in this way when using the Linux server to deploy multiple projects. .
3.nginx implements port 80 reverse proxy
Logically speaking, this At that time, we can transfer the war package of our own application to the corresponding directory and decompress it, and then go to the corresponding application tomcat working directory and execute ./start.sh to start the project! You might as well give it a try first.
After the project is deployed, we configure the hosts and start accessing it. At this time, we will find that we must add the tomcat port number configured above before we can access it. Can we not What about adding a port number (using the default port 80) for access? As mentioned above, we have configured different port numbers for different tomcats. The reason is that one port number can only be used by one tomcat. This means that project access must add port numbers for access. However, it must be done for each It is also possible for an application to use port 80 for access. This is when nginx is used. After installing nginx, we can add some configurations to achieve reverse proxy, and forward access to port 80 used by different applications to The corresponding port number. From the perspective of tomcat, external requests still use the port number configured by tomcat, which achieves the effect we want. The specific configuration is as follows (of course we also have to do the corresponding directory division work):
nginx directory is as follows:
We only need to care about the nginx.conf configuration file under the /conf directory , I also opened the specific configuration information for everyone to see. Did you notice the last line, "include domains/*", which means that the configuration is not complete yet and includes all configuration files in the /domains directory,
At this time, we can configure the corresponding nginx configuration of each application using files with different names.
Then let’s take a look at how to configure it. Listen for domain name access at auction.center.jd.net:80, and then forward it to the 1605 port of 127.0.0.1 of the local machine. We can also see from the configuration Yes, for some abnormal states, such as http_500 http_502 http_504, etc., they will be recorded and processed differently. Generally, you can jump to the homepage of the website, etc., and then record the log. After configuring this configuration file, we must not forget it. Create a new log directory, otherwise nginx cannot find the directory and will report an error when starting.
, if the restart fails, let's just take a tough step and kill the nginx process, and then use the nginx service start command to start the nginx service.
After we have completed these tasks, can we use the familiar method (without adding a port number) to access it? Yes, yes, that’s right, there are many People are worried about restarting tomcat and redeploying applications. It only means that you do not understand the nginx routing conversion layer of the network layer. The domain name we visit first performs DNS resolution through the hosts we configured and converts it to the corresponding IP. , find the server corresponding to the IP, go through the server's firewall, and then when we configure Nginx on the server, Nginx will first intercept the request, process and distribute the request, and finally hit the corresponding tomcat (personal understanding, maybe understanding It’s not in place yet, don’t spray it), at this time we can access the domain name with confidence and send requests! !
4. Step into the pit
Uh. . not good! ! I was overjoyed and jumped to the homepage, but still could not access the specified application. When I tried to add the port number, I was able to access it "miraculously". This means that the Nginx we configured did not take effect. What is the problem? ?
Now let’s talk about my problem-solving ideas. First, we have to confirm that all requests go to Nginx. At this time, the benefits of logs are reflected. When we use port 80 to access When we apply it, we stare at the log, twice, three times. . There is no log. At this time, we can roughly conclude that the Nginx service has not been used at all. We cannot be sure that the nginx service has not taken effect on the server. Maybe there is a problem with our log configuration. At this time, I made a bold operation. That is to turn off the nginx service access, or jump to the homepage of the website. What's going on? Isn't jumping to the homepage configured through nginx? Indeed, the company has already dealt with it at the network layer. At this time, I was very confused. I just tried various attempts. I might as well modify the tomcat configuration and configure one of the applications to port 80, so that it can always be accessed. Soon, the modification was completed. After deploying it, just wait for the domain name to be entered in the address bar to access it. Because the Nginx service has been shut down, the request at this time must be directly sent to tomcat. The access is once, twice and three times. Miraculously, it cannot be accessed. Na Ni? ? ? At this time, I had a flash of inspiration. The server firewall must have disabled port 80. I quickly tried to telnet 192.168.108.37 80 locally and it turned out that I couldn't access it. This proved that my guess was correct. It was easy to handle now. I just needed to enable it in the firewall. Access to port 80 is enough. Let’s add how to configure the iptables firewall to open port 80 under CentOS.
1. Open the configuration file of iptables, the code is as follows: vi /etc/sysconfig/iptables
2. Through /etc/init. d/iptables status, command to query whether port 80 is open. If not, there are two ways to handle it
3. The first way: modify the vi /etc/sysconfig/iptables command to add 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 to close/enable/restart the firewall is as follows:/etc/init.d/iptables stop (# start, #restart restart)
5. The second way: add the port through the command, the code is as follows:
[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. Check the effect: [root@centos httpd]# /etc/init.d/iptables status
7. In addition, it is recommended to use the second method, operation Simple and high success rate. Take me as an example. When I use the first method to modify the firewall configuration, the restart fails. When I use the second method, it succeeds perfectly.
三、Summary
The final result is also expected. Even if tomcat is configured with port 1605, we can still use port 80 to access it. This is The power of nginx, just a few megabytes of installation package, can actually play so many roles. I just started to learn nginx, and my understanding is very rudimentary, but through this practice, I have a good impression of nginx, and slowly I will get to know each other from acquaintance (although we cannot know each other and love each other).
The above is the detailed content of Server deployment of tomcat and Nginx. For more information, please follow other related articles on the PHP Chinese website!