Java extension Nginx II: compiling Nginx clojure source code

Welcome to my GitHub

Here we classify and summarize all the original works of Xinchen (including supporting source code): https://github.com/zq2599/blog_demos

Why compile nginx clojure source code

  • As the second article of "Java extends Nginx", I wanted to start the in-depth journey of Nginx clojure with you, but if one problem is not solved, most interested readers will stop immediately, close the web page and never see you again
  • Above We use the official installation package of nginx clojure, which is a compiled nginx executable file that can be used out of the box. At that time, we also used the command to check that the version of nginx is 1.18.0, as shown in the following figure:
  • Using the installation package compiled by nginx clojure directly, although it is simple and easy, it also brings some fatal problems, which makes us dare not use it in the production environment. In fact, I believe you have thought of it:
  1. If nginx1 18.0 is exposed to have security problems and needs to be upgraded to a higher version. What should I do? Hope that nginx clojure will officially launch a higher version of nginx package?
  2. If problem 1 can be solved by waiting, what if our nginx not only needs nginx clojure capability, but also needs to integrate other third-party or self-developed modules?
  • Therefore, the installation package provided by nginx clojure can only be used as a learning tool to help us get familiar with the nginx clojure technical framework or use it in development. As for the production environment, it is not suitable
  • At the moment, you with rich experience must see Xinchen's routine: it's wordy and beat around the bush. You can give a solution. Uh huh, you should not only use nginx clojure, but also avoid the above two fatal problems. The most appropriate solution should be to download the source code of nginx and nginx clojure, compile and install it by yourself

Overview of this article

  • The theme of this article is very clear, that is, compiling source code and installing, so it is composed of the following parts as a whole:
  1. Prepare the environment
  2. Compile and install operation
  3. Verification function
  • In this actual battle, the version of nginx source code is 1.21.6, and the version of nginx clojure source code is 0.5.2

  • The whole compilation and verification process consists of the following steps:

  • Don't talk nonsense, just start

Prepare the environment

  • It is suggested to prepare a pure linux environment for actual combat. Here is a rented Tencent cloud light application server with centos7 installed 6. In other words, the light application server is really convenient and cheap. It is also very simple to reinstall the system, as shown in the following figure:
  • To save trouble, use the root account throughout the process
  • The client tool for remote connection to Tencent cloud service is FinalShell-3.9.2.2

Install jdk

  • There are java files in the source code of nginx clojure, so you should prepare JDK for compilation
  • Go to the official website of oracle to download the JDK installation package, such as jdk-8u291-linux-x64 tar. GZ, upload it to the Linux server
  • Unzip and move to the specified directory:
tar -zxvf jdk-8u291-linux-x64.tar.gz \
&& mkdir -p /usr/lib/jvm/ \
&& mv jdk1.8.0_291 /usr/lib/jvm/
  • Open bashrc, add the following at the end:
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_291
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
  • Execute source Bashrc makes the configuration effective
  • Check whether the installation is successful, as follows:
[root@VM-20-17-centos ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

Prepare the application needed to compile nginx

  • Update yum:
yum update -y
  • Install the necessary applications:
yum install -y epel-release \
vim \
net-tools \
bridge-utils \
firewalld \
bc \
iotop \
bc \
gcc \
gcc-c++ \
glibc \
glibc-devel \
pcre \
pcre-devel \
openssl \
openssl-devel \
zip \
unzip \
zlib-devel \
lrzsz \
tree \
ntpdate \
telnet \
lsof \
tcpdump \
wget \
libevent \
libevent-devel \
systemd-devel \
bash-completion \
traceroute \
psmisc

Install lein

  • lein is a tool used to compile the source code of nginx clojure

  • The installation steps are as follows:

curl -o /usr/bin/lein https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein \
&& chmod a+x /usr/bin/lein \
&& lein
  • When the above command is executed on Tencent ECs, the connection timeout error (failed to download) may occur https://github.com/technomancy/leiningen/releases/download/2.9.8/leiningen-2.9.8-standalone.jar
    ), if you encounter such errors, please try again several times and you will succeed
  • The download process is a little time-consuming, depending on your network status:
  • Execute lein -version, and the console output is as follows, indicating that lein installation is successful:
[root@VM-20-17-centos ~]# lein -version
WARNING: You have $CLASSPATH set, probably by accident.
It is strongly recommended to unset this before proceeding.
Leiningen 2.9.8 on Java 1.8.0_291 Java HotSpot(TM) 64-Bit Server VM

Download nginx and nginx clojure source code

  • Download the compressed package of nginx and nginx clojure source code with one line of command, decompress them respectively, and then delete the compressed package:
cd ~ \
&& curl -O http://nginx.org/download/nginx-1.21.6.tar.gz \
&& curl -o nginx-clojure-0.5.2.zip https://codeload.github.com/nginx-clojure/nginx-clojure/zip/refs/tags/v0.5.2 \
&& tar -zxvf nginx-1.21.6.tar.gz \
&& unzip nginx-clojure-0.5.2.zip \
&& rm -f nginx-1.21.6.tar.gz nginx-clojure-0.5.2.zip
  • At this moment, two new folders are added. Their full paths are / root/nginx-1.21.6 and / root/nginx-clojure-0.5.2. The former is the source code of nginx and the latter is the source code of nginx clojure module

Compiling and installing nginx

  • Execute the following commands to complete configuration, compilation and installation. Pay attention to the add module parameter, which specifies the source code location of nginx clojure module:
cd ~/nginx-1.21.6 \
&& ./configure  \
--prefix=/usr/local/nginx  \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log  \
--http-log-path=/var/log/nginx/access.log  \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock  \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre \
--add-module=/root/nginx-clojure-0.5.2/src/c \
&& make \
&& make install
  • Also add user groups and users named nginx:
groupadd nginx && useradd -d /home/nginx -g nginx -m nginx
  • Create the necessary folders:
mkdir -p /var/tmp/nginx/client
  • At this point, nginx has been installed. Verify:
[root@VM-20-17-centos ~]# /usr/local/nginx/sbin/nginx -version
nginx version: nginx/1.21.6

Compile the jar package of nginx clojure

  • The compilation of binary nginx has been completed, and the source code of nginx clojure module is also required. The obtained jar should be used at runtime. Execute the following command:
cd ~/nginx-clojure-0.5.2 \
&& lein jar
  • After the compilation and construction are successful, put the obtained jar file into the new directory / usr/local/nginx/jars:
mkdir /usr/local/nginx/jars \
&& mv ~/nginx-clojure-0.5.2/target/nginx-clojure-0.5.2.jar /usr/local/nginx/jars/

Install the jar package of clojure

  • When nginx clojure is running, clojure-1.7.0 is also used Jar, I put it in my GitHub warehouse, download it and put it into the new directory / usr/local/nginx/libs:
mkdir /usr/local/nginx/libs \
&& curl -o /usr/local/nginx/libs/clojure-1.7.0.jar https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/clojure-1.7.0.jar
  • So far, the complete nginx and nginx clojure have been installed. Next, verify whether they are available

verification

  • Since it's to verify whether nginx clojure is available, it's easy to use it Above Hello World feature bar
  • I have uploaded the above jar package to GitHub and downloaded it to / usr/local/nginx/jars /
curl -o /usr/local/nginx/jars/simple-hello-1.0-SNAPSHOT.jar https://raw.githubusercontent.com/zq2599/blog_download_files/master/files/simple-hello-1.0-SNAPSHOT.jar
  • Also modify / usr / local / nginx / conf / nginx Conf, first add the following two lines to the http configuration:
  jvm_path auto;
  jvm_classpath "/usr/local/nginx/libs/*:/usr/local/nginx/jars/*";
  • Then add a location in the server configuration:
location /java {
  content_handler_type 'java';
  content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
}
  • Complete / usr / local / nginx / conf / nginx The contents of conf are as follows:
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    jvm_path auto;
    jvm_classpath "/usr/local/nginx/libs/*:/usr/local/nginx/jars/*";

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /java {
         content_handler_type 'java';
         content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
       }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
  • Start nginx. The command is / usr/local/nginx/sbin/nginx
  • Directly verify with curl command on the server. The command is curl 127.0.0.1/java. The response is shown in the red box below. It can be seen that the service is normal. The Java class we wrote is called normally and returns the expected content:
  • Now that we have completed the compilation of clongre injunx and other modules, we can better verify the source code of clongre injunx and the actual compilation of clongre injunx.

You're not alone. Xinchen's original accompanies you all the way

  1. Java series
  2. Spring series
  3. Docker series
  4. kubernetes series
  5. Middleware + database Series
  6. DevOps series

Keywords: Java Nginx

Added by Awesome Trinity on Sun, 06 Feb 2022 01:41:45 +0200