debian_install 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ### Debian Server Installation Guide ###
  2. ##################################################
  3. # Connecting to Server
  4. ssh root@207.246.121.231
  5. ##################################################
  6. # Configuring Users
  7. passwd &&
  8. useradd -s /bin/bash -m user &&
  9. passwd user &&
  10. sudo usermod -aG sudo user
  11. ##################################################
  12. # Configuring SSH Authentication
  13. ssh-keygen -t rsa -b 4096 &&
  14. ssh-copy-id -i ~/.ssh/id_rsa.pub user@207.246.121.231 &&
  15. ssh user@207.246.121.231
  16. ##################################################
  17. # Updating Server
  18. sudo apt update && sudo apt upgrade -y &&
  19. # Installing Packages
  20. sudo apt install -y nginx python3-certbot-nginx rsync apparmor apparmor-utils fail2ban &&
  21. # Configuring Editor
  22. echo -e "set number relativenumber\nset hlsearch\nset incsearch\nsyntax on\nnnoremap ZW :w<CR>\nnnoremap S :%s//<Left>" | tee ~/.vimrc > /dev/null &&
  23. # Configuring Firewall
  24. sudo ufw allow 80 && sudo ufw allow 443 && sudo ufw allow 2356 && sudo ufw delete allow 22/tcp &&
  25. sudo ufw reload &&
  26. # Configuring AppArmor
  27. sudo systemctl enable apparmor &&
  28. # Configuring Fail2ban
  29. sudo cp /etc/fail2ban/fail2ban.conf /etc/fail2ban/fail2ban.local &&
  30. sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local &&
  31. sudo sed -i 's/#allowipv6 = auto/allowipv6 = auto/' /etc/fail2ban/fail2ban.local &&
  32. sudo sed -i '/backend =.*%(sshd_backend)/a enabled = true\nmaxretry = 3\nbantime = 31536000\nfindtime = 18144000' /etc/fail2ban/jail.local &&
  33. sudo systemctl enable fail2ban.service &&
  34. sudo systemctl start fail2ban.service &&
  35. # Configuring SSH
  36. sudo sed -i 's/#Port 22/Port 2356/' /etc/ssh/sshd_config &&
  37. sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_config &&
  38. sudo sed -i 's/#StrictModes yes/StrictModes yes/' /etc/ssh/sshd_config &&
  39. sudo sed -i 's/#MaxAuthTries 6/MaxAuthTries 1/' /etc/ssh/sshd_config &&
  40. sudo sed -i 's/#MaxSessions 10/MaxSessions 1/' /etc/ssh/sshd_config &&
  41. sudo sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config &&
  42. sudo sed -i 's/#PermitEmptyPasswords no/PermitEmptyPasswords no/' /etc/ssh/sshd_config &&
  43. sudo sed -i 's/UsePAM yes/UsePAM no/' /etc/ssh/sshd_config &&
  44. sudo systemctl reload sshd &&
  45. # Nginx
  46. sudo sed -i 's/# server_tokens off;/server_tokens off;/' /etc/nginx/nginx.conf &&
  47. sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/website.com &&
  48. echo 'server {
  49. listen 80;
  50. listen [::]:80;
  51. root /var/www/website.com;
  52. index index.html index.htm index.nginx-debian.html;
  53. server_name website.com www.website.com;
  54. gzip on;
  55. gzip_min_length 1100;
  56. gzip_buffers 4 32k;
  57. gzip_types text/plain application/x-javascript text/xml text/css;
  58. gzip_vary on;
  59. # Media: images, icons, video, audio, HTC
  60. location ~* \.(?:jpg|jpeg|gif|png|ico|svg|webp|mp3)$ {
  61. expires 1M;
  62. access_log off;
  63. # max-age must be in seconds
  64. add_header Cache-Control "max-age=2629746, public";
  65. }
  66. # CSS and Javascript
  67. location ~* \.(?:css|js)$ {
  68. expires 1y;
  69. access_log off;
  70. add_header Cache-Control "max-age=31556952, public";
  71. }
  72. location / {
  73. if ($request_uri ~ ^/(.*)\.html(\?|$)) {
  74. return 302 /$1;
  75. }
  76. try_files $uri $uri.html $uri/ =404;
  77. }
  78. }' | sudo tee /etc/nginx/sites-available/website.com > /dev/null &&
  79. sudo ln -s /etc/nginx/sites-available/website.com /etc/nginx/sites-enabled/ &&
  80. sudo chmod -R 777 /var/www/ &&
  81. mkdir /var/www/website.com &&
  82. sudo systemctl reload nginx &&
  83. # Certbot
  84. sudo certbot --nginx --register-unsafely-without-email &&
  85. # Crontab
  86. echo "0 0 * * 0 /usr/local/bin/backupscript" | crontab - &&
  87. echo "0 */1 * * * certbot --nginx renew" | sudo crontab - &&
  88. # Backups
  89. mkdir ~/backups &&
  90. echo '#!/bin/sh
  91. rsync -artvzP /var/www/website.com ~/backups/$(date "+%d_%m_%Y")' | sudo tee /usr/local/bin/backupscript > /dev/null &&
  92. sudo chmod +x /usr/local/bin/backupscript
  93. ##################################################
  94. # Syncing Website
  95. rsync -rtvzPe 'ssh -p 2356' ~/documents/websites/website.com user@207.246.121.231:/var/www/ --delete