The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with a document root script and Apache 1

Discussion in 'cPanel Developers' started by morissette, Oct 24, 2009.

  1. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Hey All,

    I have been working on an bash script for changing document roots to make like a bit more automated and it works perfect on Apache 2 just making the changes to the userdata file - however with Apache 1 these changes need to be made directly to the httpd.conf.

    I'm thinking that awk is going to be the way to go with this but not sure where to begin. any help would be appreciated.

    Currently the major problem I have is if I search for a document root say "/home/user/public_html" and go to use sed or replace this is going to also change the document roots for all of subdomains and addon domains. If I can get past this problem I should be able to finish this script within minutes.

    Please let me know if you need any additional information.

    Thanks.
     
  2. bvierra

    bvierra Well-Known Member

    Joined:
    Jul 28, 2006
    Messages:
    55
    Likes Received:
    1
    Trophy Points:
    8
    Location:
    Southern California
    cPanel will overwrite all changes that you make... Maybe if you explain a little bit more of what you are trying to do I could be of more help.
     
  3. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    I am trying to automate document root changes via command line - here is a copy of my script although not pretty b/c honestly I am not a programmer:

    Code:
    #!/bin/bash
    # Document Root Change shell script
    
    # Basic error checking
    
    if [ "$1" == "" ]
    then
            echo ""
            echo "  Usage: docroot <domain> <docroot>"
            echo "  Primary domain name: docroot domain.com /home/userna5/public_html/new"
            echo "  Addon domain name: docroot addon.primary.com /home/userna5/public_html/addon.com/new"
            echo ""
    
    elif [ "$1" == $HOSTNAME ]
    then
            echo "You can't change the document root of this domain name"
    
    # Define document root
    
    elif [ "$2" == "" ]
    then
            echo ""
            echo "  Please enter the new document root"
            echo ""
    
    # Does Document root exist
    
    elif [ ! -d "$2" ]
    then
            echo ""
            echo "  Document root does not exist. Please notify customer."
            echo ""
    
    fi
    
    # Checking Apache Version for Apache 2
    
    if [ `apachectl -v | awk '{print $3}' | head -1 | cut -d "." -f1` = "Apache/2" ]
    then
            echo ""
            echo "You are running Apache 2 - Proceeding"
            echo ""
    # Find username
            USERNA=$(grep "$1" /etc/userdomains |awk '{print $2}' | head -1)
    # Find current document root
            DOCROOT=$(cat /var/cpanel/userdata/$USERNA/$1 | grep documentroot: |awk '{print $2}')
    # Change document root in userdata file
            replace -s $DOCROOT $2 -- /var/cpanel/userdata/$USERNA/$1
    # Rebuild Apache
            /scripts/rebuildhttpdconf
    # Restart Apache
            /etc/init.d/httpd restart
    # Confirm Document Root Change
            echo "Document root changed for $1 to $2"
    
    # Checking Apache Version for Apache 1
    
    elif [ `/usr/local/apache/bin/httpd -v | awk '{print $3}' | head -1 | cut -d "." -f1` = "Apache/1" ]
    then
            echo ""
            echo "You are running Apache 1 - Proceeding"
            echo ""
    # Find username
            USERNA=$(grep "$1" /etc/userdomains |awk '{print $2}' | head -1)
    # Find current document root
            DOCROOT=$(cat /var/cpanel/userdata/$USERNA/$1 | grep documentroot: |awk '{print $2}')
    # Change document root in userdata file
            replace -s $DOCROOT $2 -- /var/cpanel/userdata/$USERNA/$1
    # Change document root in Apache conf
    
     
  4. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Here ya go

    I am trying to automate document root changes via command line - here is a copy of my script although not pretty b/c honestly I am not a programmer:

    Code:
    #!/bin/bash
    # Document Root Change shell script
    
    # Basic error checking
    
    if [ "$1" == "" ]
    then
            echo ""
            echo "  Usage: docroot <domain> <docroot>"
            echo "  Primary domain name: docroot domain.com /home/userna5/public_html/new"
            echo "  Addon domain name: docroot addon.primary.com /home/userna5/public_html/addon.com/new"
            echo ""
    
    elif [ "$1" == $HOSTNAME ]
    then
            echo "You can't change the document root of this domain name"
    
    # Define document root
    
    elif [ "$2" == "" ]
    then
            echo ""
            echo "  Please enter the new document root"
            echo ""
    
    # Does Document root exist
    
    elif [ ! -d "$2" ]
    then
            echo ""
            echo "  Document root does not exist. Please notify customer."
            echo ""
    
    fi
    
    # Checking Apache Version for Apache 2
    
    if [ `apachectl -v | awk '{print $3}' | head -1 | cut -d "." -f1` = "Apache/2" ]
    then
            echo ""
            echo "You are running Apache 2 - Proceeding"
            echo ""
    # Find username
            USERNA=$(grep "$1" /etc/userdomains |awk '{print $2}' | head -1)
    # Find current document root
            DOCROOT=$(cat /var/cpanel/userdata/$USERNA/$1 | grep documentroot: |awk '{print $2}')
    # Change document root in userdata file
            replace -s $DOCROOT $2 -- /var/cpanel/userdata/$USERNA/$1
    # Rebuild Apache
            /scripts/rebuildhttpdconf
    # Restart Apache
            /etc/init.d/httpd restart
    # Confirm Document Root Change
            echo "Document root changed for $1 to $2"
    
    # Checking Apache Version for Apache 1
    
    elif [ `/usr/local/apache/bin/httpd -v | awk '{print $3}' | head -1 | cut -d "." -f1` = "Apache/1" ]
    then
            echo ""
            echo "You are running Apache 1 - Proceeding"
            echo ""
    # Find username
            USERNA=$(grep "$1" /etc/userdomains |awk '{print $2}' | head -1)
    # Find current document root
            DOCROOT=$(cat /var/cpanel/userdata/$USERNA/$1 | grep documentroot: |awk '{print $2}')
    # Change document root in userdata file
            replace -s $DOCROOT $2 -- /var/cpanel/userdata/$USERNA/$1
    # Change document root in Apache conf
    
     
Loading...

Share This Page