install-debian-server.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/sh
  2. # bachir soussi chiadmi
  3. #
  4. # http://www.debian.org/doc/manuals/securing-debian-howto/
  5. # https://www.thefanclub.co.za/how-to/how-secure-ubuntu-1204-lts-server-part-1-basics
  6. # https://www.linode.com/docs/websites/lamp/lamp-server-on-debian-7-wheezy/
  7. # http://web-74.com/blog/reseaux/gerer-le-deploiement-facilement-avec-git/
  8. #
  9. echo -e "\033[35;1mThis script has been tested only on Linux Debian 7 \033[0m"
  10. echo "Please run this script as root"
  11. echo -n "Should we start? [Y:n]"
  12. read yn
  13. yn=${yn:-y}
  14. if [ "$yn" != 'y']; then
  15. echo "aborting script!"
  16. exit
  17. fi
  18. echo "* * *"
  19. exit
  20. apt-get update
  21. apt-get upgrade
  22. # get the current position
  23. _cwd="$(pwd)"
  24. echo -e "\033[35;1mInstalling harden \033[0m"
  25. sleep 5
  26. apt-get install harden
  27. echo "Harden instaled"
  28. echo "* * *"
  29. echo -e "\033[35;1mInstalling ufw and setup firewall (allowing only ssh and http) \033[0m"
  30. sleep 5
  31. apt-get install ufw
  32. ufw allow ssh
  33. ufw allow http
  34. ufw enable
  35. ufw status verbose
  36. echo "ufw installed and firwall configured"
  37. echo "* * *"
  38. echo -e "\033[35;1mCreate new user (you will be asked a user name and a password) \033[0m"
  39. sleep 5
  40. read -p "Enter user name: " user
  41. # read -p "Continue? (Y/N): " confirm && [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]] || exit 1
  42. adduser "$user"
  43. echo "adding $user to admin group and limiting su to the admin group"
  44. groupadd admin
  45. usermod -a -G admin "$user"
  46. dpkg-statoverride --update --add root admin 4750 /bin/su
  47. echo "user $user configured"
  48. echo "* * *"
  49. read -e -p "Securing ssh (disabling root login) [Y:n]" -i "y" securssh
  50. if [$securssh = 'y']; then
  51. sed -i 's/PermitRootLogin\ yes/PermitRootLogin no/g' /etc/ssh/sshd_config
  52. sed -i 's/PermitEmptyPasswords\ yes/PermitEmptyPasswords no/g' /etc/ssh/sshd_config
  53. sed -i 's/Protocol\ [0-9]/Protocol 2/g' /etc/ssh/sshd_config
  54. echo "SSH secured"
  55. else
  56. echo 'root user can stile coonect through ssh'
  57. fi
  58. echo "* * *"
  59. echo -e "\033[35;1mInstalling AMP web server \033[0m"
  60. echo -e "\033[35;1mInstalling Apache2 \033[0m"
  61. sleep 5
  62. apt-get install apache2
  63. a2enmod rewrite
  64. service apache2 restart
  65. echo "Apache2 installed"
  66. echo "* * *"
  67. echo -e "\033[35;1minstalling Mysql \033[0m"
  68. sleep 5
  69. apt-get install mysql-server
  70. mysql_secure_installation
  71. echo "mysql installed"
  72. echo "* * *"
  73. echo -e "\033[35;1mInstalling PHP \033[0m"
  74. sleep 5
  75. apt-get install php5 php-pear php5-gd
  76. echo "Configuring PHP"
  77. cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini.back
  78. sed -i "s/max_execution_time\ =\ [0-9]\+/max_execution_time = 60/g" /etc/php5/apache2/php.ini
  79. sed -i "s/max_input_time\ =\ [0-9]\+/max_input_time = 60/g" /etc/php5/apache2/php.ini
  80. sed -i "s/memory_limit\ =\ [0-9]\+M/memory_limit = 512M/g" /etc/php5/apache2/php.ini
  81. sed -i "s/;\?error_reporting\ =\ [^\n]\+/error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR/g" /etc/php5/apache2/php.ini
  82. sed -i "s/;\?display_errors\ =\ On/display_errors = Off/g" /etc/php5/apache2/php.ini
  83. sed -i "s/;\?log_errors\ =\ Off/log_errors = On/g" /etc/php5/apache2/php.ini
  84. # following command doesn't work, make teh change manualy
  85. #sed -ri ":a;$!{N;ba};s/;\?\ \?error_log\ =\ [^\n]\+([^\n]*\n(\n|$))/error_log = \/var\/log\/php\/error.log\1/g" /etc/php5/apache2/php.ini
  86. echo "register_globals = Off" >> /etc/php5/apache2/php.ini
  87. mkdir /var/log/php
  88. chown www-data /var/log/php
  89. apt-get install php5-mysql
  90. echo "php installed"
  91. echo "* * *"
  92. echo -e "\033[35;1mInstalling Awstat \033[0m"
  93. sleep 5
  94. apt-get install awstats
  95. echo "Awstat installed"
  96. echo "* * *"
  97. read -e -p "Should we install a vhost? [Y:n]" vh
  98. if [ $vh = "y"]; then
  99. read -p "hostname ? " _host_name
  100. cp "$_cwd"/assets/example.org.conf /etc/apache2/sites-available/"$_host_name".conf
  101. sed -ir "s/example\.org/$_host_name/g" /etc/apache2/sites-available/"$_host_name".conf
  102. mkdir -p /srv/www/"$_host_name"/public_html
  103. mkdir /srv/www/"$_host_name"/logs
  104. #set proper right to user will handle the app
  105. chown -R root:admin /srv/www/"$_host_name"/
  106. chmod -R g+w /srv/www/"$_host_name"/
  107. chmod -R g+r /srv/www/"$_host_name"/
  108. # create a shortcut to the site
  109. mkdir /home/"$user"/www/
  110. chown "$user":admin /home/"$user"/www/
  111. ln -s /srv/www/"$_host_name" /home/"$user"/www/"$_host_name"
  112. #activate the vhost
  113. a2ensite "$_host_name".conf
  114. #restart apache
  115. service apache2 restart
  116. echo "vhost $_host_name configured"
  117. else
  118. echo "Vhost installation aborted"
  119. fi
  120. echo "* * *"
  121. #installing better prompt and some goodies for root
  122. echo -e "\033[35;1mInstalling shell prompt for root \033[0m"
  123. sleep 5
  124. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd -
  125. source ~/.bashrc
  126. echo "done"
  127. echo "* * *"
  128. # __ _______ __________
  129. # / / / / ___// ____/ __ \
  130. # / / / /\__ \/ __/ / /_/ /
  131. # / /_/ /___/ / /___/ _, _/
  132. # \____//____/_____/_/ |_|
  133. # setup user environment
  134. echo -e "\033[35;1mInstalling shell prompt for $user \033[0m"
  135. sleep 5
  136. cd ~
  137. git clone git://github.com/bachy/dotfiles-server.git ~/.dotfiles-server && cd ~/.dotfiles-server && ./install.sh && cd -
  138. cd ~
  139. source .bashrc
  140. echo "done"
  141. echo "* * *"
  142. # setup bare repositorie to push to
  143. echo -e "\033[35;1msetup git repositories for $_host_name \033[0m"
  144. sleep 5
  145. mkdir ~/git-repositories
  146. mkdir ~/git-repositories/"$_host_name".git
  147. cd ~/git-repositories/"$_host_name".git
  148. git init --bare
  149. # setup git repo on site folder
  150. cd /srv/www/"$_host_name"/public_html/
  151. git init
  152. # link to the bare repo
  153. git remote add origin ~/git-repositories/"$_host_name".git
  154. # create hooks that will update the site repo
  155. cd ~
  156. cp "$_cwd"/assets/git-pre-receive ~/git-repositories/"$_host_name".git/hooks/pre-receive
  157. cp "$_cwd"/assets/git-post-receive ~/git-repositories/"$_host_name".git/hooks/post-receive
  158. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" ~/git-repositories/"$_host_name".git/hooks/pre-receive
  159. sed -ir "s/PRODDIR=\"www\"/PRODDIR=\/srv\/www\/$_host_name\/public_html/g" ~/git-repositories/"$_host_name".git/hooks/post-receive
  160. cd ~/git-repositories/"$_host_name".git/hooks/
  161. chmod +x post-receive pre-receive
  162. # done
  163. echo "git repos for $_host_name install succeed"
  164. echo "your site stay now to ~/www/$_host_name"
  165. echo "you can push updates on prod branch through $user@IP.IP.IP.IP:git-repositories/$_host_name.git"
  166. echo "* * *"