Get into the habit of writing together! This is the 15th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the details of the event .
Welcome to my GitHub
All original works of Xinchen (including supporting source code) are classified and summarized here: github.com/zq2599/blog…
Why compile nginx-clojure source code
- As the second part of "Java Extending Nginx", I wanted to start the in-depth journey of nginx-clojure with you, but if there is a problem that is not solved, most interested readers will stop immediately, close the page, and never see again
- In the previous article, we used 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 nginx version is 1.18.0, as shown in the following figure:
- Directly using the official compiled installation package of nginx-clojure, although the advantage is simple and easy, but it also brings some fatal problems , so we dare not use it in the production environment, in fact, I believe you have already thought of it:
- If nginx 1.18.0 is exposed to have security problems and needs to be upgraded to a higher version, what should I do? Do you hope that nginx-clojure will officially launch a higher nginx version of the package?
- If problem 1 can be solved by waiting, then what if our nginx not only needs the 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 familiarize ourselves with the technical framework of nginx-clojure, or use it during development. As for the production environment, it is not suitable.
- At this moment, the experienced you must have seen Xinchen's routine: it's long-winded and rambling, so you can give a solution, um, not only use nginx-clojure, but also avoid the above two Fatal problem, the most suitable solution should be: download the source code of nginx and nginx-clojure, compile and install it yourself
Overview of this article
- 本篇的主题十分明确,就是编译源码和安装,所以整体上由以下几部分组成:
- 准备环境
- 编译安装操作
- 验证功能
- 本次实战,所用nginx源码的版本是1.21.6,nginx-clojure源码的版本是0.5.2
- 整个编译和验证的过程,由以下步骤组成:
- 不说废话,直接开始动手
准备环境
- 建议准备一个纯净的linux环境用来实战,我这里是租用的腾讯云轻应用服务器,安装了CentOS7.6,话说这轻应用服务器还真的方便,价格便宜,重装系统也很简单,如下图:
- 为了省事儿,全程使用root账号
- 远程连接腾讯云服务的客户端工具是FinalShell-3.9.2.2
安装jdk
- nginx-clojure的源码中有java文件,因此要准备好JDK用于编译
- 去oracle官网下载jdk安装包,例如jdk-8u291-linux-x64.tar.gz,将其上传到linux服务器
- 解压,移动到指定目录:
tar -zxvf jdk-8u291-linux-x64.tar.gz \
&& mkdir -p /usr/lib/jvm/ \
&& mv jdk1.8.0_291 /usr/lib/jvm/
复制代码
- 打开.bashrc,在尾部增加以下内容:
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
复制代码
- 执行source .bashrc使得配置生效
- 检查是否安装成功,如下:
[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)
复制代码
准备编译nginx所需的应用
- 更新yum:
yum update -y
复制代码
- 安装必要的应用:
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
复制代码
安装lein
- lein是编译nginx-clojure源码时用到的工具
- 安装步骤如下:
curl -o /usr/bin/lein https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein \
&& chmod a+x /usr/bin/lein \
&& lein
复制代码
- 实测在腾讯云服务器上执行上述命令,可能出现连接超时的错误(Failed to download github.com/technomancy…
),若遇到此类错误,请重试几次,即可成功
- 下载的过程有点耗时,就看您的网络状况了:
- 执行lein -version,控制台输出如下,表示lein安装成功:
[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
复制代码
下载nginx和nginx-clojure源码
- 用一行命令搞定下载nginx和nginx-clojure源码的压缩包,并将它们分别解压,然后删除压缩包:
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
复制代码
- 此刻新增了两个文件夹,它们的完整路径分别是/root/nginx-1.21.6和/root/nginx-clojure-0.5.2,前者是nginx源码,后者是nginx-clojure模块的源码
编译和安装nginx
- 执行以下命令,完成配置、编译、安装,注意add-module参数,里面指定了nginx-clojure模块的源码位置:
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
复制代码
- 还要增加名为nginx的用户组和用户:
groupadd nginx && useradd -d /home/nginx -g nginx -m nginx
复制代码
- 创建必要的文件夹:
mkdir -p /var/tmp/nginx/client
复制代码
- 此时nginx已经安装好了,验证一下:
[root@VM-20-17-centos ~]# /usr/local/nginx/sbin/nginx -version
nginx version: nginx/1.21.6
复制代码
编译nginx-clojure的jar包
- 二进制的nginx编译已经完成,还需要nginx-clojure模块的源码,得到的jar在运行时要用,执行以下命令:
cd ~/nginx-clojure-0.5.2 \
&& lein jar
复制代码
- 编译构建成功后,将得到的jar文件放入新建的目录/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/
复制代码
安装clojure的jar包
- nginx-clojure在运行的时候还要用到clojure-1.7.0.jar,我将其放在自己的GitHub仓库了,下载并放入新建的目录/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
复制代码
- 至此,完整的nginx和nginx-clojure已经安装完成,接下来验证是否可用
验证
- 既然是验证nginx-clojure是否可用,简简单单就好,就用前文的Hello World功能吧
- 前文的jar包,我已经上传到GitHub上,下载到/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
复制代码
- 还要修改/usr/local/nginx/conf/nginx.conf,先在http的配置中增加以下两行:
jvm_path auto;
jvm_classpath "/usr/local/nginx/libs/*:/usr/local/nginx/jars/*";
复制代码
- 然后在server的配置中增加一个location:
location /java {
content_handler_type 'java';
content_handler_name 'com.bolingcavalry.simplehello.HelloHandler';
}
复制代码
- 完整的/usr/local/nginx/conf/nginx.conf内容如下:
#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;
# }
#}
}
复制代码
- 启动nginx,命令是/usr/local/nginx/sbin/nginx
- 直接在服务器上用curl命令验证,命令是curl 127.0.0.1/java,响应如下图红框,可见服务正常,咱们写的java类被正常调用并且返回了预期的内容:
- At this point, the source code compilation and verification of nginx and nginx-clojure have been completed. Since the source code can be freely compiled, the previously mentioned problems of security and coexistence with other modules can be solved. Next, we will study nginx- clojure to better extend nginx for actual projects.
Welcome to the Nuggets: Programmer Xin Chen
On the road of learning, you are not alone, Xinchen Original will accompany you all the way...