Advertisement
Guest User

server_nginx

a guest
May 9th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.19 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # ---------------- USER CONFIGURATION -------------------------------
  4. # -------------------------------------------------------------------
  5.  
  6. # You can set some parameters here.
  7. # Make sure that these parameters fit your needs.
  8.  
  9. # dirbase is where all the virtual hosts will be created.
  10. # Default value is "/var/www" which is Apache's default document root.
  11. # Whatever you change it to, do NOT add a slash to the end of this variable!!!
  12. # If you change it to something else, e.g. "/home", then you must take extra steps
  13. # to make suexec work with a non-default document root.
  14. # One simple way to do this is to bind /home to /var/www.
  15.  
  16. dirbase="/var/www"
  17.  
  18. # dirprefix is a word that will be attached to the front of all directory names.
  19. # By default, this is left blank. It can be changed to something like "vhost_".
  20.  
  21. dirprefix=""
  22.  
  23. # dirpattern can be either "username" or "domain".
  24. # If set to "username", directory names will follow user names, e.g. "example".
  25. # If set to "domain", directory names will follow domain names, e.g. "example.com".
  26.  
  27. dirpattern="username"
  28.  
  29. # apacheuser is "www-data" by default.
  30. # Most Debian-based distros will follow this pattern, but change it if necessary.
  31.  
  32. apacheuser="www-data"
  33.  
  34.  
  35. # ---------------- COLLECT INFORMATION ------------------------------------
  36. # -------------------------------------------------------------------------
  37.  
  38. echo "+------------------------------------------------------------------+"
  39. echo "|  Easy Website Creation Tool with NGINX AND PHP-FPM  ver. 0.1.1  |"
  40. echo "+------------------------------------------------------------------+"
  41.  
  42. echo -n "Enter domain name (without www): "
  43. read domn
  44.  
  45. echo -n "Enter new user name: "
  46. read usrn
  47.  
  48.  
  49. # ---------------- CREATE USER & GROUP ------------------------------------
  50. # -------------------------------------------------------------------------
  51.  
  52. # Add user/group and ask for password.
  53. # If username already exists, exit with error.
  54. # Group is automatically created in Ubuntu, but let's check just in case.
  55.  
  56. useradd $usrn || exit 1
  57. passwd $usrn
  58. groupadd -f $usrn
  59.  
  60. # Add Apache user to the same group.
  61. # This allows Apache to read files with 640 permissions.
  62.  
  63. usermod -G $usrn -a $apacheuser
  64.  
  65.  
  66. # ---------------- CREATE USER DIRECTORY STRUCTURE ------------------------
  67. # -------------------------------------------------------------------------
  68.  
  69. if [ "$dirpattern" == "username" ]; then
  70. dirname="$usrn"
  71. else
  72. dirname="$domn"
  73. fi
  74.  
  75. dirn="$dirbase/$dirprefix$dirname"
  76. echo "Creating directory structure at $dirn"
  77.  
  78. mkdir $dirn
  79. #mkdir $dirn/cgi-bin
  80. #mkdir $dirn/.cgi-bin/php5-fcgi-wrapper
  81. #mkdir $dirn/conf
  82. #mkdir $dirn/lib
  83. mkdir $dirn/logs
  84. mkdir $dirn/public_html
  85. #mkdir $dirn/public_html/stats
  86. #mkdir $dirn/tmp
  87.  
  88. # Set user's home directory.
  89.  
  90. usermod -d $dirn $usrn
  91.  
  92.  
  93. # ---------------- CREATE CONFIG FILES ------------------------------------
  94. # -------------------------------------------------------------------------
  95.  
  96. echo "Creating FastCGI configuration files"
  97. # Apache vhost config file
  98. # I have enabled per vhost server-side includes and indexfiles.
  99. # Per vhost logging is also enabled. (Also see Webalizer stats below)
  100. # Change this if you want to use different options.
  101.  
  102. cat > /etc/nginx/conf.d/$domn.conf <<- _EOF3_
  103.    server {
  104.     listen 127.0.0.1;
  105.     server_name $domn www.$domn;
  106.     autoindex on;
  107.     access_log  /var/www/$domn/logs/access.log;
  108.     error_log   /var/www/$domn/logs/error.log;
  109.    
  110.     location / {
  111.         root /var/www/$domn/public_html;
  112.         index index.php index.html;
  113. #       if (-f $request_filename ) {
  114. #           expires max;
  115. #           break;
  116. #       }
  117. #       if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
  118. #           rewrite ^(.*) /app.php last;
  119. #       }
  120.  
  121.         try_files $uri /index.php;
  122.     }
  123.     location ~ \.php$ {
  124.             root /var/www/$domn/public_html;
  125.             fastcgi_pass 127.0.0.1:9000;
  126.             fastcgi_index index.php;
  127.         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  128.         include /etc/nginx/fastcgi_params;
  129.         }
  130. }
  131. _EOF3_
  132.  
  133. # Default html placeholder page
  134.  
  135. cat > $dirn/public_html/index.php <<- _EOF4_
  136.    <?php phpinfo(); ?>
  137. _EOF4_
  138.  
  139.  
  140. # ---------------- SET OWNERSHIP AND PERMISSION ---------------------------
  141. # -------------------------------------------------------------------------
  142.  
  143. # Home directory must be owned by user, of course!
  144.  
  145. echo "Setting Permissions"
  146. chown -R $usrn:$usrn $dirn
  147.  
  148. # Log directory must be owned by www-data.
  149. # Otherwise Apache can't write logs to it.
  150.  
  151. chown www-data:$usrn $dirn/logs
  152. touch $dirn/logs/access.log
  153. touch $dirn/logs/error.log
  154.  
  155. # Never use permissions greater than 750 for directories
  156. # or greater than 640 for files. This also applies to .php files.
  157. # The only file that needs to be 750 is the FastCGI wrapper.
  158. # The wrapper & php-cgi executes .php files for you, so
  159. # .php files don't need to be executable themselves.
  160.  
  161. chmod -R 750 $dirn
  162. chmod 640 $dirn/public_html/index.php
  163. #chmod 640 $dirn/conf/php.ini
  164. chmod 640 $dirn/logs/*
  165. chmod -R 750 $dirn
  166.  
  167.  
  168.  
  169. # ---------------- RELOAD NGINX AND PHP-FPM WEBSERVER --------------------------------
  170. # -------------------------------------------------------------------------
  171.  
  172. /etc/init.d/nginx restart
  173. /etc/init.d/php-fpm restart
  174.  
  175. echo "Done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement