imcrazytwkr

[runit] Nginx run script

Jan 13th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.63 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Configuration
  4. owner='root:root'
  5. confdir='/etc/nginx'
  6. logdir='/var/log/nginx'
  7. rundir='/run/nginx'
  8. conf='nginx.conf'
  9.  
  10. exec 2>&1
  11.  
  12. if [ ! -d $confdir ]; then
  13.   printf 'Configuration directory %s does not exist!\n' $confdir
  14.   exit 1
  15. fi
  16.  
  17. # Absolute path to config
  18. conffile="${confdir}/${conf}"
  19.  
  20. if [ ! -f $conffile ]; then
  21.   printf 'Main nginx configuration file %s does not exist!\n' $conffile
  22.   exit 1
  23. fi
  24.  
  25. if [ ! -d $rundir ]; then
  26.   printf 'Run directory %s does not exist, creating...' $rundir
  27.   rm -rf $rundir > /dev/null
  28.   mkdir $rundir
  29.   echo ' [done]'
  30. fi
  31.  
  32. if [ $(stat -c '%U:%G' $rundir) != $owner ]; then
  33.   printf 'Run directory %s has incorrect owner, changing...' $rundir
  34.   chown $owner $rundir
  35.   echo ' [done]'
  36. fi
  37.  
  38. if [ $(stat -c '%U:%G' $logdir) != $owner ]; then
  39.   printf 'Log directory %s has incorrect owner, changing...' $logdir
  40.   chown -R $owner $logdir
  41.   echo ' [done]'
  42. fi
  43.  
  44. # Checking if somebody left this instance of nginx running as a daemon.
  45. pidfile="${rundir}/nginx.pid"
  46. if [ -f $pidfile ]; then
  47.   read pid <$pidfile
  48.  
  49.   if [ -n "${pid}" -a -d "/proc/${pid}" ]; then
  50.     echo 'Nginx has been daemonized! Please adjust your config so that it includes a \"daemon off;\" line.'
  51.     exit 10
  52.   fi
  53. fi
  54.  
  55. # Cleaning up trash from previous instance.
  56. rm -rf "${rundir}/*" > /dev/null
  57.  
  58. # Testing nginx config before launching
  59. conftest=$(nginx -tqc $conffile)
  60.  
  61. if [ -n "${conftest}" ]; then
  62.   echo 'Nginx configuration test failed!'
  63.   echo $conftest
  64.   exit 11
  65. else
  66.   printf 'Successfully tested nginx configuration. Starting from %s...\n' $root
  67.   cd $root
  68.   exec nginx -c $conffile
  69. fi
Advertisement
Add Comment
Please, Sign In to add comment