#! /bin/bash # Rocky 9 compatible . /usr/src/k_framework/main.sh SCRIPT_NAME="Passenger install" VERSION="1.1.5" k_config_details k_detect_os GEMSET="" k_detect_os if [[ $centos_version == 7 ]]; then RUBY_VERSION="2.2.2" PASSENGER_VERSION="4.0.58" elif [[ $centos_version == 6 ]]; then RUBY_VERSION="2.1.2" PASSENGER_VERSION="4.0.48" elif [[ $ROCKY9 == 1 ]]; then RUBY_VERSION="3.1.3" PASSENGER_VERSION="6.0.17" fi source "/etc/profile.d/rvm.sh" passenger_gem() { local passenger_gem=`rvm $RUBY_VERSION@$GEMSET do gem list | grep passenger | wc -l` if [ "$passenger_gem" -ge "1" ] && [ -f /etc/httpd/conf.d/$SYSTEM_CONFIG_DIR.conf ] && [ -f /etc/httpd/conf.d/passenger.conf ] && [ -f `grep mod_passenger.so /etc/httpd/conf.d/passenger.conf | awk '{print $NF}'` ] && [ `grep "@$GEMSET" /etc/httpd/conf.d/passenger.conf | wc -l` != "0" ]; then report "Passenger gem is installed, v$PASSENGER_VERSION" 0 else yum install -y curl-devel if [[ $ROCKY9 == 1 ]]; then rvm $RUBY_VERSION@$GEMSET do gem install passenger -v=$PASSENGER_VERSION # --no-ri --no-rdoc not supported in newer RubyGems else rvm $RUBY_VERSION@$GEMSET do gem install passenger -v=$PASSENGER_VERSION --no-ri --no-rdoc fi chmod 777 -R /tmp chmod o+t -R /tmp report "Compiling passenger apache-module mod_passenger.so..." 3 if [[ $centos_version != 6 ]]; then # Centos7 and Rocky9 rvm $RUBY_VERSION@$GEMSET do passenger-install-apache2-module --apxs2-path='/usr/bin/apxs' -a > /dev/null 2>&1 else rvm $RUBY_VERSION@$GEMSET do passenger-install-apache2-module -a > /dev/null 2>&1 fi rvm $RUBY_VERSION@$GEMSET do passenger-install-apache2-module --snippet > /etc/httpd/conf.d/passenger.conf passenger_module=`grep mod_passenger.so /etc/httpd/conf.d/passenger.conf | awk '{print $NF}'` if [ ! -f "$passenger_module" ]; then report "Failed to compile mod_passenger.so" 1 fi local passenger_gem=`rvm $RUBY_VERSION@$GEMSET do gem list | grep passenger | wc -l` if [ "$passenger_gem" -ge "1" ]; then report "Passenger gem is installed" 4 RESTART_REQUIRED=1 else report "Failed to install Ruby passenger" 1 fi fi } check_if_name_virtual_host_option_enabled() { local NameVirtualHost=`awk -F"#" '{print $1}' /etc/httpd/conf/httpd.conf | grep NameVirtualHost | wc -l` if [ "$NameVirtualHost" == "1" ]; then report "NameVirtualHost *:80 option in /etc/httpd/conf/httpd.conf found" 0 else report "NameVirtualHost *:80 option in /etc/httpd/conf/httpd.conf not found, fixing" 3 #replace_line /etc/httpd/conf/httpd.conf '#NameVirtualHost' 'NameVirtualHost *:80' #sed -i "s|#NameVirtualHost\+|NameVirtualHost *:80|g" /etc/httpd/conf/httpd.conf echo "NameVirtualHost *:80" >> /etc/httpd/conf/httpd.conf local NameVirtualHost=`awk -F"#" '{print $1}' /etc/httpd/conf/httpd.conf | grep NameVirtualHost | wc -l` if [ "$NameVirtualHost" == "1" ]; then report "NameVirtualHost *:80 option in /etc/httpd/conf/httpd.conf found" 4 RESTART_REQUIRED=1 else report "NameVirtualHost *:80 option in /etc/httpd/conf/httpd.conf NOT found. Please fix manually by adding NameVirtualHost *:80 option to /etc/httpd/conf/httpd.conf" 1 fi fi } migrate_apache_config() { source "/usr/local/rvm/scripts/rvm" oldCfg=`grep -F '^/billing/public' /etc/httpd/conf/httpd.conf | wc -l` if [ "$oldCfg" == "1" ]; then # creating a backup of existing Apache configuration mkdir -p /usr/local/$SYSTEM_CONFIG_DIR/backups/httpd_cfg k_time cp /etc/httpd/conf/httpd.conf /usr/local/$SYSTEM_CONFIG_DIR/backups/httpd_cfg/httpd.conf_before_passenger_"$CURRENT_TIME" if [ -f /usr/local/$SYSTEM_CONFIG_DIR/backups/httpd_cfg/httpd.conf_before_passenger_"$CURRENT_TIME" ]; then report "Created a backup of current httpd conf to /usr/local/$SYSTEM_CONFIG_DIR/backups/httpd_cfg/httpd.conf_before_passenger_$CURRENT_TIME" 3 fi # remove existing configuration from /etc/httpd/conf/httpd.conf # Black magic to delete old configuration from /etc/httpd/conf/httpd.conf report "Removing old configuration in /etc/httpd/conf/httpd.conf" 3 sed '/RewriteCond %{REQUEST_URI} !\^\/billing\/public/,/<\/Directory>/d' /etc/httpd/conf/httpd.conf > /tmp/.httpd_cfg mv /tmp/.httpd_cfg /etc/httpd/conf/httpd.conf oldCfg=`grep -F '^/billing/public' /etc/httpd/conf/httpd.conf | wc -l` if [ "$oldCfg" == "1" ]; then report "Migration of /etc/httpd/conf/httpd.conf was successful" 4 else report "Migration of /etc/httpd/conf/httpd.conf Failed" 1 fi fi # Generate new configuration check_if_name_virtual_host_option_enabled if [ ! -f /etc/httpd/conf.d/passenger.conf ]; then report "/etc/httpd/conf.d/passenger.conf not found, generating one" 3 rvm $RUBY_VERSION@$GEMSET do passenger-install-apache2-module --snippet > /etc/httpd/conf.d/passenger.conf local cfg_lines=`wc -l /etc/httpd/conf.d/passenger.conf | awk '{print $1}'` if [ "$cfg_lines" == "3" ]; then report "/etc/httpd/conf.d/passenger.conf generated" 4 RESTART_REQUIRED=1 else report "Failed to generate /etc/httpd/conf.d/passenger.conf" 1 fi fi if [ ! -s /etc/httpd/conf.d/$SYSTEM_CONFIG_DIR.conf ] then # If file is empty or does not exist echo " DocumentRoot /var/www/html Allow from all RailsBaseURI /billing Options -MultiViews RackEnv production PassengerDefaultUser apache PassengerDefaultGroup apache " > /etc/httpd/conf.d/$SYSTEM_CONFIG_DIR.conf else # If we have RailsEnv production line, replace it with RackEnv production if grep --quiet 'RailsEnv production' /etc/httpd/conf.d/$SYSTEM_CONFIG_DIR.conf then sed -i.bak 's/RailsEnv production/RackEnv production/g' /etc/httpd/conf.d/$SYSTEM_CONFIG_DIR.conf fi fi } check_if_passenger_configuration_not_empty() { # Author: Mindaugas Mardosas # Year: 2013 # About: This function checks if passenger configuration is correct. If not - configuration is regenerated if [ -f /etc/httpd/conf.d/passenger.conf ]; then if [ `cat /etc/httpd/conf.d/passenger.conf | wc -l` == "5" ]; then report "Passenger configuration is OK" 0 else rvm $RUBY_VERSION@$GEMSET do passenger-install-apache2-module --snippet > /etc/httpd/conf.d/passenger.conf if [ `cat /etc/httpd/conf.d/passenger.conf | wc -l` == "5" ]; then report "Passenger configuration is OK" 4 RESTART_REQUIRED=1 else report "Failed to generate passenger configuration. Please use this command to do this manually: passenger-install-apache2-module --snippet > /etc/httpd/conf.d/passenger.conf" 1 exit 1 fi fi fi if [ `grep "PassengerMaxPoolSize\|PassengerPoolIdleTime" /etc/httpd/conf.d/passenger.conf | wc -l` -lt 2 ]; then # magic settings for performace tuning # http://www.alfajango.com/blog/performance-tuning-for-phusion-passenger-an-introduction/ echo -e "PassengerMaxPoolSize 30\nPassengerPoolIdleTime 10" >> /etc/httpd/conf.d/passenger.conf fi } #===== Main ================== k_start RESTART_REQUIRED=0 passenger_gem check_if_name_virtual_host_option_enabled migrate_apache_config check_if_passenger_configuration_not_empty if [ "$RESTART_REQUIRED" == "1" ]; then service httpd restart fi k_exit $EXIT_CODE