RSieve

CentOS 7 web installer

Jun 8th, 2021
2,497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 11.01 KB | None | 0 0
  1. #!/bin/bash
  2. ##############################################################################
  3. #                    Web server installer on CentOS 7                        #
  4. ##############################################################################
  5. #           This program is distributed under the "GPLv3" License            #
  6. #                       TODO: add dialog for minimal GUI                     #
  7. ##############################################################################
  8.  
  9. header()
  10. {
  11.     echo "Web server installer version 1.0"
  12.     echo "Installer for web server with Apache and Tomcat"
  13.     echo "GPLv3"
  14.     echo
  15. }
  16.  
  17. showhelp()
  18. {
  19.     header
  20.     echo 'Usage: ./web_server_installer [OPTIONS]'
  21.     echo
  22.     echo 'OPTIONS:'
  23.     echo '-h   | --help: Show this help screen. Install java before apache.'
  24.     echo '-j   | --java <url>: Java url to be downloaded. Base url is ' \
  25.         'https://download.oracle.com/otn-pub/java/jdk/  -- append the ' \
  26.         'remaining part of the url.'
  27.     echo '-t   | --tomcat <tomcat-version> <java-version>: Add the apache ' \
  28.         'tomcat version (e.g. "9.0.31") and add the java version as well ' \
  29.         '(e.g. 11.0.6).'
  30.     echo '-a   | --apache <host> <port>: install apache with the config for ' \
  31.         'the host provided'
  32.     echo '-c   | --certificate <host> <port>: install letsencrypt for apache ' \
  33.         'the host provided'
  34. }
  35.  
  36. java_setup()
  37. {
  38.     # Extract java version
  39.     version=`echo "$1" | cut -d'_' -f 1 | rev | cut -d"/" -f 1 | rev`
  40.  
  41.     # Download Java from the Oracle repository
  42.     # stanrdard url works as https://download.oracle.com/otn-pub/java/jdk/
  43.     # in java11 is https://download.oracle.com/otn-pub/java/jdk/11.0.6+8/90eb79fb590d45c8971362673c5ab495/jdk-11.0.6_linux-x64_bin.rpm
  44.     # for java 13 wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/13.0.2+8/d4173c853231432d94f001e99d882ca7/jdk-13.0.2_linux-x64_bin.rpm
  45.     wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-cookie" $1
  46.  
  47.     # Install java
  48.     sudo rpm -Uvh /tmp/${version}_linux-x64_bin.rpm
  49.    
  50.     # File cleanup
  51.     sudo rm /tmp/${version}_linux-x64_bin.rpm
  52. }
  53.  
  54. tomcat_setup()
  55. {
  56.     # Check if java has already been installed
  57.     if type -p java; then
  58.         echo found java executable in PATH
  59.         _java=java
  60.     elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
  61.         echo found java executable in JAVA_HOME    
  62.         _java="$JAVA_HOME/bin/java"
  63.     else
  64.         echo "Java must be installed for tomcat to be installed and setup"
  65.         exit
  66.     fi
  67.  
  68.     # Variable for the latest Tomcat version. Change this varible for
  69.     # newer versions
  70.     tommy="apache-tomcat-"$1
  71.     ver=`echo "$1" | cut -d'.' -f 1`
  72.     # version=`echo "$tommy" | rev | cut -d'-' -f 1 | rev`
  73.     ext=".tar.gz"
  74.     url="http://mirror.nohup.it/apache/tomcat/tomcat-"$ver"/v"$1"/bin/"
  75.  
  76.     # Add the Tomcat user and group to which our application will attach to
  77.     # note that the -s is done to avoid the chance of logging in as tomcat
  78.     sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
  79.  
  80.     # Download the latest Tomcat Version
  81.     wget $url$tommy$ext
  82.  
  83.     # Unzip the tar file into the /tmp folder
  84.     tar -xf $tommy$ext
  85.  
  86.     # Move the folder into its folder
  87.     sudo mv $tommy /opt/tomcat/
  88.  
  89.     # Create a soft link for the tomcat folder as latest
  90.     sudo ln -s /opt/tomcat/$tommy /opt/tomcat/latest
  91.  
  92.     # Set the owner of the tomcat folder to the tomcat user
  93.     sudo chown -R tomcat: /opt/tomcat
  94.  
  95.     # We need to make the shell script executable to make sure we can invoke them
  96.     # into a service for startup and shutdown of the tomcat platform
  97.     sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
  98.  
  99.     # Create the service file. We configure it so that by enabling it we will
  100.     # be able to make our tomcat starting up on boot by enabling the service
  101.     echo '
  102.    [Unit]
  103.    Description=Tomcat 9 servlet container
  104.    After=network.target
  105.  
  106.    [Service]
  107.    Type=forking
  108.  
  109.    User=tomcat
  110.    Group=tomcat
  111.  
  112.    Environment="JAVA_HOME=/usr/java/jdk-'$2'"
  113.    Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
  114.  
  115.    Environment="CATALINA_BASE=/opt/tomcat/latest"
  116.    Environment="CATALINA_HOME=/opt/tomcat/latest"
  117.    Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
  118.    Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
  119.  
  120.    ExecStart=/opt/tomcat/latest/bin/startup.sh
  121.    ExecStop=/opt/tomcat/latest/bin/shutdown.sh
  122.  
  123.    [Install]
  124.    WantedBy=multi-user.target' > /etc/systemd/system/tomcat.service
  125.  
  126.     # Reload the daemon since we created a new service
  127.     sudo systemctl daemon-reload
  128.  
  129.     # Enable the Tomcat service and start it
  130.     sudo systemctl enable tomcat
  131.     sudo systemctl start tomcat
  132.    
  133.     # File cleanup
  134.     sudo rm /tmp/$tommy$ext
  135.  
  136.     # Exclusion for firewall
  137.     sudo firewall-cmd --permanent --add-port=8090/tcp
  138.  
  139.     # Restart the firewall
  140.     sudo systemctl restart firewalld
  141.  
  142.     # Note that this files need to be configured
  143.     # /opt/tomcat/latest/conf/server.xml to add ajp and port 8090 for http
  144.     #       for the ajp the connector need to be decommendted and add
  145.     #           secretRequired="false"
  146.     #       for the 8090 port you just need to find the simple connector
  147.     #           with port 8080 and change it
  148.     #
  149.     # /opt/tomcat/latest/conf/tomcat-users.xml to add roles and users
  150.     #       add for example:
  151.     #           <role rolename="manager"/>
  152.     #           <role rolename="manager-gui"/>
  153.     #           <role rolename="manager-script"/>
  154.     #           <role rolename="admin"/>
  155.     #              <user username="tommy-man" password="<password>"
  156.     #                   roles="manager,manager-gui,manager-script" />
  157.     #               <user username="xelinion" password="<passwrd>"
  158.     #                   roles="admin" />
  159.     #
  160.     #
  161.     # /opt/tomcat/latest/webapps/manager/META-INF/context.xml to add manager
  162.     #   outside localhost
  163.     #       add the ips, |192.168.1.\d+ (it may vary with your network)
  164. }
  165.  
  166. apache_setup()
  167. {
  168.     # We do not want www
  169.     if [ `echo "$1" | cut -d'.' -f 1` == 'www' ]
  170.     then
  171.         echo "Do not add www to the host pls"
  172.         exit
  173.     fi
  174.  
  175.     # Install httpd and add the exclusion to the firewall
  176.     sudo yum install httpd -y
  177.  
  178.     # Add the http and https for the certification part and the 8443 for tomcat
  179.     sudo firewall-cmd --permanent --add-service=http
  180.     sudo firewall-cmd --permanent --add-service=https
  181.     sudo firewall-cmd --permanent --add-port=8443/tcp
  182.  
  183.     # Restart the firewall
  184.     sudo systemctl restart firewalld
  185.  
  186.     # Enable and start the service
  187.     sudo systemctl enable httpd
  188.     sudo systemctl start httpd
  189.  
  190.     # We'll get the first element of the domain as user, therefore, if we have
  191.     # example.com our base user will be example.
  192.     user=`echo "$1" | cut -d'.' -f 1`
  193.  
  194.     i=1
  195.  
  196.     # The first if shoul be enough, but never be too sure
  197.     while [ $user == "www" ]
  198.     do
  199.         # If the user passed www we want to exclude it
  200.         user=`echo "$1" | cut -d'.' -f "$i" | rev | cut -d'.' | rev`
  201.         i=$i+1
  202.     done
  203.  
  204.     # Add the Tomcat user and group to which our application will attach to
  205.     # note that the -s is done to avoid the chance of logging in as tomcat
  206.     sudo useradd -m -U -d /var/www/$1/ -s /bin/false $user
  207.  
  208.     # Create the base folder for the base url
  209.     sudo mkdir -p /var/www/html/$1/
  210.     sudo mkdir -p /var/www/$1/log
  211.  
  212.     # Change owner of the folder
  213.     sudo chown -R $user:$user /var/www/html/$1
  214.  
  215.     # Change mod
  216.     sudo chmod -R 755 /var/www/
  217.  
  218.     # Create the sites-available and sites-enabled folder for apache
  219.     sudo mkdir /etc/httpd/sites-available /etc/httpd/sites-enabled
  220.  
  221.     # We also add the configuration for tomcat (we will need to make it
  222.     # available from the server tomcat file as well)
  223.     echo '
  224. Listen '$2'
  225. IncludeOptional sites-enabled/*.conf
  226. LoadModule proxy_module modules/mod_proxy.so
  227. LoadModule proxy_ajp_module modules/mod_proxy_ajp.so' >> /etc/httpd/conf/httpd.conf
  228.  
  229.     echo '
  230. <VirtualHost *:80>
  231.    ServerName www.'$1'
  232.    ServerAlias '$1'
  233.    DocumentRoot /var/www/html/'$1'/
  234.    ErrorLog /var/www/'$1'/log/error.log
  235.    CustomLog /var/www/'$1'/log/requests.log combined
  236.    ProxyRequests Off
  237.    ProxyPreserveHost On
  238.  
  239.    <Proxy *>
  240.        Order deny,allow
  241.        Allow from all
  242.    </Proxy>
  243.  
  244.    ProxyPass / ajp://localhost:8009/
  245. </VirtualHost>
  246.    ' > /etc/httpd/sites-available/$1.conf
  247.    
  248.     # Create the softlink
  249.     sudo ln -s /etc/httpd/sites-available/$1.conf /etc/httpd/sites-enabled/$1.conf
  250.  
  251.     # Adjust the SELinux policy for Apache globally
  252.     sudo setsebool -P httpd_unified 1
  253.  
  254.     # Restart the service
  255.     sudo systemctl restart httpd
  256.  
  257.     # Check the list of contents of thhe log folder to see whether or not
  258.     # Apache created the files
  259.     ls -lZ /var/www/$1/log
  260. }
  261.  
  262. certification_setup()
  263. {
  264.     # We do not want www
  265.     if [ `echo "$1" | cut -d'.' -f 1` == 'www' ]
  266.     then
  267.         echo "Do not add www to the host pls"
  268.         exit
  269.     fi
  270.    
  271.     # Install the epel release
  272.     sudo yum install epel-release -y
  273.  
  274.     # Install certbot
  275.     sudo yum install certbot python2-certbot-apache mod_ssl -y
  276.  
  277.     # Obtain the certificate: if you want the alias decomment it
  278.     sudo certbot --apache -d $1 # -d www.$1
  279.    
  280.     # The next part is interactive and require the user to choose: it is better
  281.     # to redirect to https
  282.  
  283.     # Renewal
  284.     crontab -l | { cat; echo "0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew"; } | crontab -
  285.  
  286.     # Change default listening port
  287.     sudo sed -i 's/443/'$2'/g' /etc/httpd/sites-available/$1-le-ssl.conf
  288.  
  289.     # Restart the service
  290.     sudo systemctl restart httpd
  291.  
  292.     # Add permanent zone to firewall
  293.     firewall-cmd --zone=public --add-masquerade --permanent
  294.     # Forward of the 8443 port to 443 port since letsencrypt works only on
  295.     # that
  296.     firewall-cmd --zone=public \
  297.         --add-forward-port=port=8443:proto=tcp:toport=443 --permanent
  298.  
  299.     # Restart the firewall
  300.     systemctl restart firewalld
  301. }
  302.  
  303. # We need super user privileges to execute the script
  304. user_id=$(id -u)
  305.  
  306. if [ "$user_id" != "0" ]; then
  307.     echo "You need super user priviliges for this."
  308.     exit
  309. fi
  310.  
  311. # Download and execute the commands inside the tmp folder
  312. cd /tmp
  313.  
  314. while [ "$1" ]; do
  315.     case $1 in
  316.         '-h' | '--help' | '?' )
  317.             showhelp
  318.             exit
  319.             ;;
  320.         '--java' | '-j' )
  321.             java_setup "$2"
  322.             exit
  323.             ;;
  324.         '--tomcat' | '-t' )
  325.             tomcat_setup "$2" "$3"
  326.             exit
  327.             ;;
  328.         '--apache' | '-a' )
  329.             apache_setup "$2" "$3"
  330.             exit
  331.             ;;
  332.         '--certificate' | '-c' )
  333.             certification_setup "$2" "$3"
  334.             exit
  335.             ;;
  336.     esac
  337.  
  338.     shift
  339. done
  340.  
Advertisement
Add Comment
Please, Sign In to add comment