Advertisement
Mark2020H

Bash script that auto installs Apache2 and documentation

Nov 27th, 2024 (edited)
162
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.07 KB | Source Code | 0 0
  1. #!/bin/bash
  2.  
  3. :'
  4.  
  5. Script that will auto install all  required to get apache2 up and running easily on Debian 12 systems
  6. Targeting operating systems :
  7. PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
  8. NAME="Debian GNU/Linux"
  9. VERSION_ID="12"
  10. VERSION="12 (bookworm)"
  11. VERSION_CODENAME=bookworm
  12. ID=debian
  13. HOME_URL="https://www.debian.org/"
  14. SUPPORT_URL="https://www.debian.org/support"
  15. BUG_REPORT_URL="https://bugs.debian.org/"
  16.  
  17. '
  18. :'
  19.  
  20. Link  for full explanation on git hub plus code at this
  21. address  below
  22. https://github.com/markh2024/AutoInstallApache/tree/main
  23.  
  24. To fetch pages  from  your own home area defined as public_html
  25. you use  http://localhost/~yourlogon name
  26.  
  27. To pull up the main site with all help pages Apache manual  you use
  28. http://localhost/ the click the link marked manual
  29.  
  30. Or you can use http://localhost/manual
  31.  
  32. '
  33.  
  34.  
  35.  
  36. # Adjust for your system's actual location of cowsay
  37. COWSAY_PATH="/usr/games/cowsay"  # Adjust this if necessary
  38.  
  39. # Function to prompt user for confirmation
  40. prompt_user() {
  41.   while true; do
  42.     echo -n "$1 (Y/N): "
  43.     read -r response
  44.     if [[ "$response" =~ ^[Yy]$ ]]; then
  45.       return 0
  46.     elif [[ "$response" =~ ^[Nn]$ ]]; then
  47.       echo "Exiting script."
  48.       exit 0
  49.     else
  50.       echo "Incorrect input. Please only enter 'y' or 'n'."
  51.     fi
  52.   done
  53. }
  54.  
  55.  
  56. # Function to display text with cowsay, accepts text as an argument
  57. cowsay_text() {
  58.   clear
  59.    
  60.   if [ -x "$COWSAY_PATH" ]; then
  61.     "$COWSAY_PATH" "$1"
  62.   else
  63.     echo "cowsay is not installed or not in the correct path."
  64.   fi
  65.   sleep 8
  66.   clear
  67. }
  68.  
  69. # Ensure script is run with sudo privileges
  70. if [ "$(id -u)" -ne 0 ]; then
  71.   echo "Please run this script as root or with sudo."
  72.   exit 1
  73. fi
  74.  
  75. # Get the current user and group
  76. USER=$(logname)
  77. GROUP=$(id -gn)
  78.  
  79. # Get the current date and time
  80. current_datetime=$(date +"%Y-%m-%d %H:%M:%S")
  81.  
  82. cowsay_text "My Install script for Apache2 MD Harrington London DA6 8NP : $current_datetime"
  83.  
  84. # Function to execute a command and check its success
  85. execute_and_check() {
  86.   "$@"
  87.   if [ $? -ne 0 ]; then
  88.     echo "Error: Command failed -> $@"
  89.     exit 1
  90.   fi
  91. }
  92.  
  93. # Step 1: Update system packages
  94. prompt_user "Update package list and upgrade installed packages?"
  95. execute_and_check sudo apt update
  96. execute_and_check sudo apt upgrade -y
  97.  
  98. # Step 2: Install required tools
  99. prompt_user "Install gnome-system-tools?"
  100. execute_and_check sudo apt install gnome-system-tools -y
  101.  
  102. # Step 3: Install Apache2 web server
  103. prompt_user "Install Apache2?"
  104. execute_and_check sudo apt install apache2 -y
  105.  
  106. # Step 4: Start Apache2 service
  107. prompt_user "Start Apache2?"
  108. execute_and_check sudo systemctl start apache2
  109.  
  110. # Step 5: Check if Apache is running on localhost
  111. prompt_user "Check Apache2 status?"
  112. echo "Checking Apache2 status..."
  113. execute_and_check systemctl status apache2
  114.  
  115. # Step 6: Create public_html directory in home area
  116. prompt_user "Create public_html directory in /home/$USER?"
  117. execute_and_check mkdir -p "/home/$USER/public_html"
  118.  
  119. # Step 7: Set ownership for public_html directory
  120. prompt_user "Set ownership for /home/$USER/public_html to $USER:$GROUP?"
  121. execute_and_check sudo chown -R "$USER:$GROUP" "/home/$USER/public_html"
  122.  
  123. # Step 8: Add Directory block for /home/$USER/public_html
  124. prompt_user "Add Directory block for /home/$USER/public_html in 000-default.conf?"
  125. echo "Adding Directory block for /home/$USER/public_html in 000-default.conf..."
  126. sudo sed -i "/<\/VirtualHost>/i \
  127. <Directory /home/$USER/public_html/>\n\
  128.    Options Indexes FollowSymLinks\n\
  129.    AllowOverride None\n\
  130.    Require all granted\n\
  131. </Directory>" /etc/apache2/sites-available/000-default.conf
  132.  
  133.  
  134. # Step 9: Backup and modify userdir.conf
  135. prompt_user "Edit userdir.conf to allow user directories?"
  136. execute_and_check sudo cp /etc/apache2/mods-available/userdir.conf /etc/apache2/mods-available/userdir.conf.bck
  137. sudo bash -c 'cat <<EOF > /etc/apache2/mods-available/userdir.conf
  138. <IfModule mod_userdir.c>
  139.    UserDir public_html
  140.    <Directory /home/*/public_html>
  141.        AllowOverride FileInfo AuthConfig Limit
  142.        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
  143.        Require method GET POST OPTIONS
  144.    </Directory>
  145. </IfModule>
  146. EOF'
  147.  
  148. # Step 10: Enable userdir module
  149. prompt_user "Enable userdir module?"
  150. execute_and_check sudo a2enmod userdir
  151.  
  152. # Step 11: Restart Apache2 to apply changes
  153. prompt_user "Restart Apache2 to apply changes?"
  154. execute_and_check sudo systemctl restart apache2
  155.  
  156. # Step 12: Add www-data user to the $USER group
  157. prompt_user "Add www-data user to the $USER group?"
  158. execute_and_check sudo usermod -aG "$USER" www-data
  159.  
  160. # Step 13: Reset directory permissions
  161. prompt_user "Reset directory permissions for /home/$USER and /home/$USER/public_html?"
  162. execute_and_check sudo chmod 750 "/home/$USER"
  163. execute_and_check sudo chmod 750 "/home/$USER/public_html"
  164. execute_and_check sudo chown -R "$USER:$USER" "/home/$USER"
  165.  
  166. # Step 14: Place index.html in public_html folder
  167. prompt_user "Place index.html file in public_html folder?"
  168. cat > "/home/$USER/public_html/index.html" <<EOF
  169. <html>
  170.   <head>
  171.     <title>Hello $USER</title>
  172.   </head>
  173.   <body>
  174.     <div id="whatever">
  175.       <p>Hello page and script written by no less than MD Harrington, and welcome to my page.</p>
  176.     </div>
  177.   </body>
  178. </html>
  179. EOF
  180.  
  181. # Step 15: Install ACL and set execute permission for www-data
  182. prompt_user "Install ACL and set execute permission for www-data?"
  183. execute_and_check sudo apt install acl -y
  184. execute_and_check sudo setfacl -m u:www-data:x "/home/$USER"
  185.  
  186. # Step 16: Install Apache documentation
  187. prompt_user "Install Apache documentation?"
  188. execute_and_check sudo apt update
  189. execute_and_check sudo apt install apache2-doc -y
  190. execute_and_check sudo a2enmod alias
  191.  
  192. # Check if apache2-doc.conf exists and create it if missing
  193. # For some reason installs  fail  but this ensures all is there
  194. if [ ! -f "/etc/apache2/conf-available/apache2-doc.conf" ]; then
  195.   echo "apache2-doc.conf not found. Creating it now..."
  196.   cat > /etc/apache2/conf-available/apache2-doc.conf <<EOF
  197. Alias /manual "/usr/share/doc/apache2-doc/manual"
  198.  
  199. <Directory "/usr/share/doc/apache2-doc/manual">
  200.     Options Indexes MultiViews
  201.     AllowOverride None
  202.     Require all granted
  203. </Directory>
  204. EOF
  205.   echo "apache2-doc.conf created successfully."
  206. else
  207.   echo "apache2-doc.conf already exists."
  208. fi
  209.  
  210. # Enable the apache2-doc configuration
  211. execute_and_check sudo a2enconf apache2-doc
  212.  
  213. # Restart Apache to apply changes
  214. execute_and_check sudo systemctl restart apache2
  215.  
  216.  
  217. #step 17
  218.  
  219. # Step 17: Launch web browser to display pages
  220.  
  221.   echo "Please open the following URLs manually: to test your install "
  222.   echo "1. http://localhost"
  223.   echo "2. http://localhost/~$USER"
  224.   echo "3. http://localhost/manual"
  225.   sleep 3
  226.   echo "Nearly finished "
  227.  
  228.  
  229.  
  230. echo "Apache setup and testing completed successfully!"
  231.  
  232. # Get the current date and time
  233. current_datetime=$(date +"%Y-%m-%d %H:%M:%S")
  234.  
  235. # Call cowsay with the message and current date/time
  236. cowsay_text "Thank you for watching MD Harrington London Kent DA6 8NP : $current_datetime"
  237.  
  238.  
Advertisement
Comments
  • Mark2020H
    25 days
    # text 0.19 KB | 0 0
    1. I will be uploading a separate script for php and phpmyadmin later for you
    2.  
    3. Enjoy !! It makes life 100X easier for all Better than trying to do this all by hand and remember all points
  • Mark2020H
    25 days
    # text 0.41 KB | 0 0
    1. Re wrote script
    2.  
    3. Altered file completely to ensure as best I can no errors Noticed sometimes this doesn't always install docs correctly for some reason
    4. Running sudo apachectl -t -D DUMP_INCLUDES does not reveal Include /etc/apache2/conf-enabled/apache2-doc.conf
    5.  
    6. To correct this Ive included a test function to check if exists and then manually create this with the script
    7.  
    8. That should now solve issues
Add Comment
Please, Sign In to add comment
Advertisement