nginxchroot.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. # nginxchroot generate script
  3. export NGINXCHROOT=/srv/nginxchroot
  4. export USER=http
  5. export GROUP=http
  6. mkdir -p $NGINXCHROOT
  7. mkdir -p $NGINXCHROOT/dev
  8. mkdir -p $NGINXCHROOT/etc
  9. mkdir -p $NGINXCHROOT/usr/lib
  10. mkdir -p $NGINXCHROOT/usr/lib64
  11. mkdir -p $NGINXCHROOT/usr/sbin
  12. mkdir -p $NGINXCHROOT/usr/share/nginx
  13. mkdir -p $NGINXCHROOT/tmp
  14. mkdir -p $NGINXCHROOT/var/run
  15. mkdir -p $NGINXCHROOT/var/lib/nginx
  16. mkdir -p $NGINXCHROOT/var/log/nginx
  17. mkdir -p $NGINXCHROOT/www
  18. mount -t tmpfs none $NGINXCHROOT/var/run -o 'noexec,size=1M'
  19. mount -t tmpfs none $NGINXCHROOT/tmp -o 'noexec,size=100M'
  20. echo "tmpfs $NGINXCHROOT/run tmpfs rw,noexec,relatime,size=1024k 0 0" > $NGINXCHROOT/etc/fstab
  21. echo "tmpfs $NGINXCHROOT/tmp tmpfs rw,noexec,relatime,size=102400k 0 0" >> $NGINXCHROOT/etc/fstab
  22. cp -r /usr/share/nginx/* $NGINXCHROOT/usr/share/nginx/
  23. cp -r /usr/share/nginx/html/* $NGINXCHROOT/www
  24. cp /usr/sbin/nginx $NGINXCHROOT/usr/sbin/
  25. cp -r /var/lib/nginx $NGINXCHROOT/var/lib/
  26. cp -r /etc/nginx $NGINXCHROOT/etc/
  27. cp /usr/libexec/nginx-hardened-scripts/nginx.conf $NGINXCHROOT/etc/nginx/
  28. mkdir -p $NGINXCHROOT/etc/nginx/conf.d
  29. mkdir -p $NGINXCHROOT/etc/nginx/sites-enabled
  30. cp /usr/libexec/nginx-hardened-scripts/example.conf $NGINXCHROOT/etc/nginx/conf.d/
  31. cd $NGINXCHROOT; ln -L etc/nginx/conf.d/example.conf etc/nginx/sites-enabled/
  32. mknod -m 644 $NGINXCHROOT/dev/random c 1 8
  33. mknod -m 644 $NGINXCHROOT/dev/urandom c 1 9
  34. mknod -m 666 $NGINXCHROOT/dev/null c 1 3
  35. cd $NGINXCHROOT; ln -s usr/lib lib
  36. cd $NGINXCHROOT; ln -s usr/lib64 lib64
  37. cp $(ldd /usr/sbin/nginx | grep /usr/lib/ | sed -sre 's/(.+)(\/usr\/lib\/\S+).+/\2/g') $NGINXCHROOT/usr/lib
  38. cp /usr/lib/libnss_* $NGINXCHROOT/usr/lib/
  39. cp /lib64/ld-linux-x86-64.so.2 $NGINXCHROOT/usr/lib64/
  40. cp /lib/libdl.so.2 $NGINXCHROOT/usr/lib/
  41. cp /lib/libpthread.so.0 $NGINXCHROOT/usr/lib/
  42. cp /lib/libcrypt.so.1 $NGINXCHROOT/usr/lib/
  43. cp /lib/libpcre.so.1 $NGINXCHROOT/usr/lib/
  44. cp /lib/libz.so.1 $NGINXCHROOT/usr/lib/
  45. cp /lib/libc.so.6 $NGINXCHROOT/usr/lib/
  46. cp /lib/libresolv.so.2 $NGINXCHROOT/usr/lib/
  47. # /etc{adjtime,hosts.deny} might also be needed.
  48. cp -Lrfv /etc/{services,localtime,nsswitch.conf,nscd.conf,protocols,hosts,ld.so.cache,ld.so.conf,resolv.conf,host.conf} $NGINXCHROOT/etc
  49. echo 'http:x:33:' > $NGINXCHROOT/etc/group
  50. echo 'nobody:x:99:' >> $NGINXCHROOT/etc/group
  51. echo 'http:x:33:33:http:/:/bin/false' > $NGINXCHROOT/etc/passwd
  52. echo 'nobody:x:99:99:nobody:/:/bin/false' >> $NGINXCHROOT/etc/passwd
  53. echo 'http:x:14871::::::' > $NGINXCHROOT/etc/shadow
  54. echo 'nobody:x:14871::::::' >> $NGINXCHROOT/etc/shadow
  55. echo 'http:::' > $NGINXCHROOT/etc/gshadow
  56. echo 'nobody:::' >> $NGINXCHROOT/etc/gshadow
  57. touch $NGINXCHROOT/etc/shells
  58. touch $NGINXCHROOT/var/run/nginx.pid
  59. chown -R root:root $NGINXCHROOT/
  60. chown -R $USER:$GROUP $NGINXCHROOT/www
  61. chown -R $USER:$GROUP $NGINXCHROOT/etc/nginx
  62. chown -R $USER:$GROUP $NGINXCHROOT/var/{log,lib}/nginx
  63. chown $USER:$GROUP $NGINXCHROOT/var/run/nginx.pid
  64. #find $NGINXCHROOT/ -gid 0 -uid 0 -type d -print | xargs chmod -rw
  65. #find $NGINXCHROOT/ -gid 0 -uid 0 -type d -print | xargs chmod +x
  66. #find $NGINXCHROOT/ -gid 0 -uid 0 -type f -print | xargs chmod -x
  67. #find $NGINXCHROOT/etc -gid 0 -uid 0 -type f -print | xargs chmod -x
  68. #find $NGINXCHROOT/usr/lib -gid 0 -uid 0 -type f -print | xargs chmod -x
  69. #find $NGINXCHROOT/usr/sbin -type f -print | xargs chmod ug+rx
  70. #find $NGINXCHROOT/ -gid 33 -uid 33 -print | xargs chmod o-rwx
  71. chmod +rw $NGINXCHROOT/tmp
  72. chmod +rw $NGINXCHROOT/var/run
  73. setcap 'cap_net_bind_service=+ep' $NGINXCHROOT/usr/sbin/nginx