Nginx listens on port 80 and forwards requests to Tomcat on port 8080.

This tutorial will cover the installation of the LNMP environment, JAVA, Nginx and Tomcat, using Tomcat to obtain real visitor IP, etc

Related links:

  • Tencent cloud: console.cloud.tencent.com/
  • LNMP:lnmp.org/
  • Tomcat:tomcat.apache.org/
  • Java:www.oracle.com/

Preparing the server

Here is a recommendation to buy Tencent Cloud lightweight server, cheap and affordable (if you already have the server you need, you can skip this step)

Purchasing a server

First log in to Tencent Cloud Lightweight server console, the interface should be as follows, select “New”

It can be seen that there are many regions and mirrors. It is suggested to choose Hong Kong, which is fast and can be recorded without registration. (System mirrors are selected according to requirements, and CentOS7 system will be used in the demonstration here.)

After the purchase is complete, go to admin to change the password, find the network and domain name, and copy the public IP address

This allows you to connect to the server over SSH

Connecting to the server

Here are three common connection software

  • XSHELL:www.netsarang.com/zh/xshell/

  • PuTTY:www.chiark.greenend.org.uk/~sgtatham/p…

  • FinalShell:www.hostbuf.com/t/988.html

    (I won’t go into more details about these software here, but you can use them as required. If you don’t want to use them, you can also use PowerShell (ssh-pport root@IP or Domain). In addition, the following demo will use PuTTY.)

Note: When connecting to the server, the account is root and the password is the one you changed in the previous step

Double-click PuTTY to write IP and Port and click Open. If you want to Save, write the name in Saved Sessions and click Save

You will see login as: : root, root@IP’s password: enter the password you set (note that the password will not be displayed, enter it, it is not stuck)

Install the LNMP environment

Enter the following command

Yum install screen - y wget http://soft.vpser.net/lnmp/lnmp1.7.tar.gz - cO lnmp1.7. Tar. Gz && tar ZXF lnmp1.7. Tar. Gz &&cdLnmp1.7 &&. / install. Sh LNMPCopy the code

Tips are as follows

+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + | LNMP V1.7for CentOS Linux Server, Written by Licess          |
+------------------------------------------------------------------------+
|        A tool to auto-compile & install LNMP/LNMPA/LAMP on Linux       |
+------------------------------------------------------------------------+
|           For more information please visit https://lnmp.org           |
+------------------------------------------------------------------------+
You have 11 options forYour DataBase install. 1: install MySQL 5.1.73 2: install MySQL 5.5.62 (Default) 3: Install MySQL 5.6.48 4: Install MySQL 5.7.30 5: Install MySQL 8.0.20 6: Install MariaDB 5.5.68 7: Install MariaDB 10.1.45 8: Install MariaDB 10.2.32 9: Install MariaDB 10.3.23 10: Install MariaDB 10.4.13 0: DO NOT Install MySQL/MariaDB Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or 0):Copy the code

MySQL 5.7.30: Install MySQL 5.7.30: Install MySQL 5.7.30: Install MySQL 5.7.30: Install MySQL 5.7.30: Install MySQL 5.7.30

Do you want to enable or disable the InnoDB Storage Engine? Enter is enabled by default

You have 9 options for2: Install PHP 5.3.29 3: Install PHP 5.4.45 4: Install PHP 5.5.38 5: Install PHP 5.6.40 (Default) 6: Install PHP 7.0.33 7: Install PHP 7.1.33 8: Install PHP 7.2.34 9: Install PHP 7.3.23 10: Install PHP 7.4.12 Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10):Copy the code

8: Install PHP 7.2.34: Select 8 and press Enter to wait for the installation to complete (for details, see installation link)

Install Tomcat

The version selected here is Tomcat 8

cd~ wget unzip at https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.63/bin/apache-tomcat-8.5.63.zip Apache-tomcat-8.5.63. zip rm -f apache-tomcat-8.5.63.zip mv apache-tomcat-8.5.63 /usr/local/tomcat
cd /usr/local/tomcat/
Copy the code

Now that you are in the directory where Tomcat is located, you can see that the Tomcat directory has the following structure

Tomcat ├─bin # Tomcat Run Start Stop Script ├─ build.txt ├─conf # Tomcat Config File ├─CONTRIBUTING. Md ├─lib # Tomcat Run required JAR File ├─LICENSE ├─ ├─ all exercises, ├─ all exercises, ├─ all exercises, ├─ all exercises, ├─ all exercises, ├─ all exercises, ├─ all Exercises, ├─ all Exercises, ├─ all Exercises, ├─ all Exercises, ├─ all Exercises, ├─ all Exercises, ├─ all Exercises, ├─ all exercises JSP generated Servlet source files and bytecode filesCopy the code

Yum directly installs the Java environment

Configure the yum install Java -y command to install the Java environment. If Java -version is displayed, the installation is normal

openjdk version "1.8.0 comes with _282"
OpenJDK Runtime Environment (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
Copy the code

Note: If you do not want to use yum to install Java, you need to manually install and configure the Java environment. However, Oracle is very difficult to download, you need to register an account, so I will not go into details

Start Tomcat

Make sure you are in the Tomcat directory at this point, otherwise CD to the corresponding directory first

chmod 755 bin/*
bin/startup.sh
Copy the code

If the following information is displayed, the system starts normally

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
Copy the code

Lsof -i:8080 (Tomcat default port :8080)

COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
java    22812 root   55u  IPv6 4333119      0t0  TCP *:webcache (LISTEN)
Copy the code

Create the site and modify the configuration file

The choice here is to configure a single site file to make it easier to use Tomcat when there are multiple sites on the server

To create the site

Resolve the domain name to your server IP, this is not too much to repeat

lnmp vhost add

Please enter domain(example: www.lnmp.org): youdomain.com
 Your domain: youdomain.com
Enter more domain name(example: lnmp.org *.lnmp.org): Enter
Please enter the directory for the domain: youdomain.com
Default directory: /home/wwwroot/youdomain.com: 
Virtual Host Directory: /home/wwwroot/youdomain.com/dist
Allow Rewrite rule? (y/n) n
You choose rewrite: none
Enable PHP Pathinfo? (y/n) n
Disable pathinfo.
Allow access log? (y/n) n
Add SSL Certificate (y/n) y
1: Use your own SSL Certificate and Key
2: Use Let's Encrypt to create SSL Certificate and Key
Enter 1 or 2: 2
Copy the code

Note: above youdomain.com shows the domain name you want to use

Modify the site configuration file

So let’s go to the directory

cd /usr/local/nginx/conf/vhost/
cp youdomain.com.conf youdomain.com.conf.bak
vim youdomain.com.conf
# Note that you can use any editor like vi Vim nano, if you are not familiar with vim, recommend SCP to local operation or use nano
Copy the code

Nginx configuration file attached

server
    {
        listen 80;
        server_name www.youdomain.com youdomain.com;
        rewrite ^ https://$server_name$request_uri? permanent;
    }

server
    {
        listen 443 ssl http2;
        server_name www.youdomain.com youdomain.com;
        index index.html index.htm index.jsp index.jspx index.do default.html default.htm default.jsp default.jspx default.do;
        root  /usr/local/tomcat/webapps;

        ssl_certificate /usr/local/nginx/conf/ssl/youdomain.com/youdomain.com.pem;
        ssl_certificate_key /usr/local/nginx/conf/ssl/youdomain.com/youdomain.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-1 28-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:! MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;

        include enable-php-pathinfo.conf;
     
        location/ {root html;
            index index.jsp index.jspx index.do index.html;
            proxy_pass http://localhost:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_redirect off;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
            proxy_pass http://localhost:8080;
        }

        location ~ .*\.(js|css)? $
        {
            expires      12h;
            proxy_pass http://localhost:8080;
        }

        location ~ /.well-known {
            allow all;
        }

        location ~ / \.
        {
            deny all;
        }
        
        access_log off;
    }

Copy the code

After reload, visit your domain name, you will see the following webpage directly, which proves that the operation is normal

Modify the Tomcat configuration file to obtain the real IP address of the client

Enter the directory

cd /usr/local/tomcat/conf/
vim server.xml
Copy the code

This can be seen in the Host field

<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true">

  <! -- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html -->
  <! -- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> -->

  <! -- Access log processes all example. Documentation at: /docs/config/valve.htmlNote: The pattern used is equivalent to using pattern="common" -->
  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="localhost_access_log" suffix=".txt"
         pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>
Copy the code

I’m going to change it to

<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true">
  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="localhost_access_log" suffix=".txt"
         pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %{User-Agent}i" resolveHosts="false" />
  <Valve className="org.apache.catalina.valves.RemoteIpValve" />
</Host>
Copy the code

In this way, you can obtain the real IP address of the client

Original text: loliloli. MOE / 2021/03/10 /…