Shell script: create virtual hosts with PHP/fastCGI (mod_fcgi)

UPDATES:

Version 1.1 – June 20 2011

  • Added  “FcgidMaxRequestLen 1000000000” to virtual host settings in order to get the WordPress Flash based image uploader to work. The default value in fcgid caused it to throw a “HTTP Error” when crunching the images.
  • Commented out the Apache directive “ServerAlias” which in previous version defaulted to *.example.com. That of course might cause problems when setting up both root domain and sub domains on the same server. Feel free to uncomment or add custom aliases.
  • Moved the ports.conf tip from script echo to comments at the end.

#!/bin/bash
# Shell script to create virtual hosts running PHP/FastCGI with mod_fcgi
# on Apache 2
# -------------------------------------------------------------------------
# Version 1.1 (June 20 2011)
# -------------------------------------------------------------------------
# Copyright (c) 2011 Finn Hensner <http://www.gerillafilm.se>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------

clear
echo "# -----------------------------------------------------------"
echo "# ---------- Virtual host with mod_fcgi - installer ---------"
echo "# -----------------------------------------------------------"

read -p "Enter domain name to use: " domainName

read -p "Enter port number for virtual host: " port

if [ $(id -u) -eq 0 ]; then
    read -p "Enter username : " userName
    read -s -p "Enter password : " userPassword
    egrep "^$userName" /etc/passwd >/dev/null
    if [ $? -eq 0 ]; then
        echo "$userName exists!"
        exit 1
    else
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $userPassword)
                groupadd $userName;
                useradd -s /bin/sh -p $pass -d /var/www/$domainName -m -g $userName $userName

        [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
    fi
else
    echo "Only root may add a user to the system"
    exit 2
fi

clear
echo -e "\n\tDomain name: $domainName:$port"
echo -e "\tUser name: $userName"
echo -e "\tPassword: $userPassword"
echo "(Your user credentials will be saved in a .txt-file outside your web root)"
echo -e "\tFinish with these settings?\n\n"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) echo "Preparing...";

#sleep 1
#groupadd $userName;
echo "Added group $groupName"

#sleep 1
#useradd -s /bin/sh -p $userPassword -d /var/www/$domainName -m -g $userName $userName
#echo "Added user $userName"

sleep 1
mkdir -p /var/www/$domainName/public_html
echo "Creating folder /var/www/$domainName/public_html"

sleep 2
chown -R $userName:$userName /var/www/$domainName
echo "Added ownership to webroot"

sleep 2
mkdir -p /var/www/php-fcgi-scripts/$domainName
echo "Created cgi script folder for $domainName"

sleep 1
echo -e "#!/bin/sh\nPHPRC=/etc/php5/cgi/\nexport PHPRC\nexport PHP_FCGI_MAX_REQUESTS=5000\nexport PHP_FCGI_CHILDREN=8\nexec /usr/lib/cgi-bin/php" > /var/www/php-fcgi-scripts/$domainName/php-fcgi-starter
echo "Created cgi-wrapper for $domainName"

sleep 1
chmod 755 /var/www/php-fcgi-scripts/$domainName/php-fcgi-starter
chown -R $userName:$userName /var/www/php-fcgi-scripts/$domainName
echo "Changed ownership on fcgi-wrapper"

sleep 1
echo -e "<VirtualHost *:$port>\n\tServerName $domainName\n\t#ServerAlias $domainName *.$domainName\n\tServerAdmin webmaster@$domainName\n\tDocumentRoot /var/www/$domainName/public_html\n\n\n\t<IfModule mod_fcgid.c>\n\t\tSuexecUserGroup $userName $userName\n\t\tFcgidMaxRequestLen 1000000000\n\t\t<Directory /var/www/$domainName/public_html>\n\t\t\tOptions +ExecCGI\n\t\t\tAllowOverride All\n\t\t\tAddHandler fcgid-script .php\n\t\t\tFCGIWrapper /var/www/php-fcgi-scripts/$domainName/php-fcgi-starter .php\n\t\t\tOrder allow,deny\n\t\t\tAllow from all\n\t\t</Directory>\n\t</IfModule>\n\n\nServerSignature Off\n\n\n</VirtualHost>" > /etc/apache2/sites-available/$domainName
echo "Added virtual host for $domainName"

sleep 1
a2ensite $domainName
echo "Enabling virtual host $domainName"

sleep 1
/etc/init.d/apache2 reload
echo "Restarting apache"

sleep 1
echo -e "Your account credentials:\n\nDomain name: $domainName:$port\nUser name: $userName\nPassword: $userPassword" > /var/www/$domainName/login-$domainName.txt
echo -e "******************************************************************************"
echo -e "Your account credentials:\n\nDomain name: $domainName:$port\nUser name: $userName\nPassword: $userPassword"
echo -e "Saved text file with credentials in /var/www/$domainName/login-$domainName.txt"
echo -e "******************************************************************************\n\n"
chown $userName:$userName /var/www/$domainName/login-$domainName.txt
chmod 600 /var/www/$domainName/login-$domainName.txt

break;;
        No ) exit;;
    esac
done

# -------------------------------------------------------------------------
# Make sure you edit /etc/apache2/ports.conf and add another NameVirtualHost
# if you want your server to listen to additional ports, ie 8080 for proxying.
# -------------------------------------------------------------------------


Instructions

To run the script, begin with pasting it into a new file:

nano vhost-fcgi.sh

Make it executable:

chmod 755 vhost-fcgi.sh

Run it:

./vhost-fcgi.sh

To easily install Apache 2 and dependencies, read Shell script: install Apache 2 with dependencies for PHP/mod_fcgi

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>