Configure nginx and tomcat using chroot (jail)

Source: Internet
Author: User
: This article mainly introduces the configuration of nginx and tomcat using chroot (jail). If you are interested in the PHP Tutorial, refer to it. This article focuses on configuration records. the benefits of chroot (jail) are not described in detail.

This article is divided into three parts: Configure basic chroot jail, nginx configure chroot jail, and tomcat configure chrootjail.

1. configure the basic chroot jail

1. create a directory as the root directory of chroot jail.

# mkdir /home/chroot/jail

. Use the ldd command to find the dependent database of the command you want to run in chroot jail.

# ldd /bin/bashlinux-vdso.so.1 =>  (0x00007fff56fcc000)libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003ad1200000)libdl.so.2 => /lib64/libdl.so.2 (0x0000003abe600000)libc.so.6 => /lib64/libc.so.6 (0x0000003abe200000)/lib64/ld-linux-x86-64.so.2 (0x0000003abde00000)

. Create a subfolder in the chroot jail root directory. create a subfolder for whatever you want to run under the chroot Directory. remember the principle of service minimization.

# mkdir /home/chroot/jail/bin# mkdir /home/chroot/jail/lib64

. Copy binary files (which must be run under chroot) and dependent libraries to the chroot jail directory.

# cp /bin/bash /home/chroot/jail/bin# cp /lib64/{libtinfo.so.5,libdl.so.2,libc.so.6,ld-linux-x86-64.so.2} \  /home/chroot/jail/lib64

. After completing the above operations, you can go to chroot to view them.

The above operations can only run bash under chroot, but other operations are not allowed. Therefore, the ls command in the following example cannot be found.

# chroot /home/chroot/jailbash-4.1# pwd/bash-4.1# lsbash: ls: command not foundbash-4.1# exitexit#
Only embedded shells, such as pwd, can be run here, but other commands cannot be run unless you copy its binary files and related dependent libraries to chroot jail.

. If an error occurs when a service is started, use strace to check the error. It will be added at the end of nginx configuration.


2. configure chroot jail in nginx

The nginxinstallation method is omitted. download tar.gz, decompress, configure, make, and make install...

The configuration here is for the CentOS6.x 64-bit system.

2. 1. specify the chroot Directory, that is, the root directory described in 1.1.

Make the following directory structure for uniform naming rules:

# D=/home/nginx/jail# mkdir -p $D
2. create an isolation environment, a subdirectory under the jail root directory.
# mkdir -p $D/etc# mkdir -p $D/dev# mkdir -p $D/var# mkdir -p $D/usr# mkdir -p $D/usr/local/nginx# mkdir -p $D/tmp# chmod 1777 $D/tmp# mkdir -p $D/var/tmp# chmod 1777 $D/var/tmp# mkdir -p $D/lib64

2. 3. create the required device in $ D/dev, which should be a buffer zone.

Run the mknod command to create a cache file:

# /bin/mknod -m 0666 $D/dev/null c 1 3# /bin/mknod -m 0666 $D/dev/random c 1 8# /bin/mknod -m 0444 $D/dev/urandom c 1 9

. Copy all nginx files to jail.

# /bin/cp -farv /usr/local/nginx/* $D/usr/local/nginx

. Copy the dependent database to jail. (2.5 and 2.4 are equivalent to 1.4 processes)

# ldd /usr/local/nginx/sbin/nginx
Minimize the number of copies required.

2. 6. Copy/etc to jail.

The nixCraft tutorial specifically points out that these should be used during nginx runtime.

# cp -fv /etc/{group,prelink.cache,services,adjtime,shells,gshadow,shadow,hosts.deny,localtime,nsswitch.conf,nscd.conf,prelink.conf,protocols,hosts,passwd,ld.so.cache,ld.so.conf,resolv.conf,host.conf} $D/etc
Add some folders:

# cp -avr /etc/{ld.so.conf.d,prelink.conf.d} $D/etc

2. 7. start nginx.

If nginx is running, close it first:

# killall -9 nginx

Enable nginx in chroot:
# /usr/sbin/chroot /home/nginx/jail /usr/local/nginx/sbin/nginx -t# /usr/sbin/chroot /home/nginx/jail /usr/local/nginx/sbin/nginx
You can also enable chroot first and then:

# chroot /home/nginx/jail bash-4.1# /usr/local/nginx/sbin/nginx -tbash-4.1# /usr/local/nginx/sbin/nginx

Configure it as self-starting upon startup:

# echo '/usr/sbin/chroot /home/nginx/jail /usr/local/nginx/sbin/nginx' >> /etc/rc.local
Edit The nginx configuration file in chroot:

# cd /home/nginx/jail/usr/local/nginx/conf/# vi nginx.conf
After saving and disabling, test and restart nginx:

# /usr/sbin/chroot /home/nginx/jail /usr/local/nginx/sbin/nginx -t# /usr/sbin/chroot /home/nginx/jail /usr/local/nginx/sbin/nginx -s reload
If you copy the chroot environment directly, nginx may not recognize the configuration file. run the following command:

# /usr/sbin/chroot /home/nginx/jail /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Then test and restart nginx.
. If the environment is still faulty, use strace to track errors. (Tomcat or other services configured in this method are also used for trial and will not be repeated later)

# strace -f -o /tmp/nginx.strace chroot /nginx /usr/local/nginx/sbin/nginx -t 
Strace may need to be installed.
After the statement is executed, some tracing will be performed. in the/tmp/nginx. strace file, more detailed tracing will be performed.

When you configure the environment, you will find in this file that a package is missing, add it to chroot jail, and start properly.

3. configure chroot jail for tomcat

3. 1. create the chroot jail root directory.

# mkdir /home/tomcat/jail
3. 2. create the chroot jail directory structure.

# cd /home/tomcat/jail# mkdir -p lib lib64 etc tmp dev usr# chmod 755 etc dev usr# chmod 1777 tmp
# cp -a /etc/hosts etc/hosts

3. install the java environment and copy the root installation environment to chroot.

# mkdir -p usr/java# cp -a /usr/java/jdk1.7.0_67 usr/java

3. 4. search for the dependent libraries for copying java

# ldd /usr/java/jdk1.7.0_67/bin/javalinux-vdso.so.1 =>  (0x00007fff532d1000)libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc36c8f2000)libjli.so => /usr/java/jdk1.7.0_67/bin/../lib/amd64/jli/libjli.so (0x00007fc36c6da000)libdl.so.2 => /lib64/libdl.so.2 (0x00007fc36c4d6000)libc.so.6 => /lib64/libc.so.6 (0x00007fc36c142000)/lib64/ld-linux-x86-64.so.2 (0x00007fc36cb17000)
Copy only the libraries in the above four lib64 libraries, and copy the libraries required by two JVMs:

# cp -p /lib64/libm.so.6 lib64/# cp -p /lib64/libnsl.so.1 lib64/

. Before running JVM, you need to do some work to make chroot more like a real root environment.

Create/dev and its subitem:

# cd /home/tomcat/jail# mkdir -p /home/tomcat/jail/dev/pts# /dev/MAKEDEV -d /home/tomcat/jail/dev null random urandom zero loop* log console# cp /dev/MAKEDEV /home/tomcat/jail/dev# cp -a /dev/shm /home/tomcat/jail/dev/

3. 6. Mount/proc.

# mkdir -p /home/tomcat/jail/proc# mount -t proc proc /home/tomcat/jail/proc
After mounting this step, you can use java under chroot. you can enter chroot and run the following command to test:

bash-4.1# /usr/java/jdk1.7.0_67/bin/java -version
.. The premise is that you have made a copy of bash and sh in chroot. Or after all the 3.x is done, it's very wide ~

3. 7. add other required files and dependent libraries.

If you do not need to copy the three files under etc, try not to copy them .. Minimize the number of services ..

cp -a /etc/{hosts,resolv.conf,nsswitch.conf} /home/tomcat/jail/etc/ 

Three other dependent libraries supported by naming must be copied.

cp -p /lib64/libresolv.so.2 lib64/cp -p /lib64/libnss_dns.so.2 lib64/cp -p /lib64/libnss_files.so.2 lib64/

And then implement/bin/bash and/bin/sh as chroot. for details, see 1.x. Sh is a soft connection of bash in centos.

After this step, java can work in chroot, and then use strace to troubleshoot errors.

After the java Runtime Environment is set up, configure tomcat.

3.8.tomcat installation configuration.

Tomcat generally does not need to be installed. it is green. download and unzip the package and unzip it in the chroot jail directory.

I directly compressed it in the jail root directory.

If you try to run it, the following results will be displayed:

bash-4.1# /apache-tomcat-7.0.57/bin/catalina.sh start                      /apache-tomcat-7.0.57/bin/catalina.sh: line 102: uname: command not found/apache-tomcat-7.0.57/bin/catalina.sh: line 122: dirname: command not foundCannot find //bin/setclasspath.shThis file is needed to run this program
Copy the uname and dirname binary files and dependent libraries in the same way as bash.

# cp /bin/uname bin/# mkdir usr/bin# cp /usr/bin/dirname usr/bin

When running tomcat, we found that there are still some minor issues:

bash-4.1# /apache-tomcat-7.0.57/bin/catalina.sh start/apache-tomcat-7.0.57/bin/catalina.sh: line 203: tty: command not foundUsing CATALINA_BASE:   /apache-tomcat-7.0.57Using CATALINA_HOME:   /apache-tomcat-7.0.57Using CATALINA_TMPDIR: /apache-tomcat-7.0.57/tempUsing JRE_HOME:        /usr/java/jdk1.7.0_67Using CLASSPATH:       /apache-tomcat-7.0.57/bin/bootstrap.jar:/apache-tomcat-7.0.57/bin/tomcat-juli.jar/apache-tomcat-7.0.57/bin/catalina.sh: line 368: touch: command not foundTomcat started.
Add the following items:
# cp -p /lib64/librt.so.1 lib64/# cp /usr/bin/tty usr/bin/# cp /bin/touch bin/

In this way, tomcat can run normally in the chroot container.

Configure it to start automatically at startup and append it to the end of rc. local:

export JAVA_HOME=/usr/local/java/jdk1.7.0_25export JRE_HOME=$JAVA_HOME/jremount -t proc proc /home/tomcat/jail/proc &>/dev/null/usr/sbin/chroot /home/tomcat/jail /usr/tomcat/bin/catalina.sh start
Note that the java path, root and chroot must be consistent.

References:

[1] 3.13 logging and Using Chroot Jails Chapter 3 Implementing Oracle Linux Security Guide for Release 6

[2] Linux nginx: Chroot (Jail) Setup By NIXCRAFT

[3] Tomcat: The Definitive Guide By Jason Brittain, Ian F. Darwin

The above describes the configuration of nginx and tomcat using chroot (jail), including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.