15

System: Ubuntu 14.04 LAMP running on Parallels VM set up with Vagrant

I'm writing my first non-trivial shell script to add new web projects to a dev VM on my Mac laptop.

  1. Create a default folder structure in /var/www/
  2. Add a .conf vhost file to /etc/apache2/sites-available with the new domain replacing placeholders via sed
  3. Enable the new site and restart apache

I've got the folders and files copying over and sed seems happy customizing my index.html and .conf vhost file, but a2ensite doesn't seem to see the .conf file in /etc/apache2/sites-available/

I test for its existence and even print a debug listing: ls -al /etc/apache2/sites-available/ | grep $CONFFILE before attempting to enable the site.

I've read here and elsewhere about the importance of having the .conf extension since Ubuntu 13 (or 14) which seems to be a very common issue. My vhost file has the .conf extension so this seems like a different issue.

Can anyone point me in the right direction? I haven't been able to find other postings with this particular problem.

My feeling is that I've got an error in my $CONFFILE variable expansion in the a2ensite command because the error does not show the .conf extension even though the directory listing does:

ERROR: Site /etc/apache2/sites-available/example-com-80 does not exist!

Edit:

After running a2ensite from the command line per Micheal's suggestion below, it seemed to parse fine, but still doesn't show the extension:

$ sudo a2ensite example-com-80.conf
Enabling site example-com-80.
To activate the new configuration, you need to run:
  service apache2 reload

End Edit

Edit: Found answer

After searching with broader terms, a2ensite instead of Ubuntu 14.04 Vagrant etc, I found a two year old question where @raina77ow points out that a2ensite just wants the site name, not the whole path. Changing sudo a2ensite /etc/apache2/sites-available/$CONFFILE to sudo a2ensite $CONFFILE makes the script work as intended. This also explains why my previous attempts to run a2ensite from the command line failed; I was running it from inside /var/www/templates/ and passing in the whole path to the .conf file.

Now, a stackoverflow question, how best should I indicate this is the solution with the limited reputation that I have? And give credit properly?

See edit above for solution

Console output with example.com:

$ ./newvhost
New Server Name with Top Level Domain: example.com
Validating: example.com
New DocumentRoot created: /var/www/example
Copying template structure
Creating: example-com-80.conf
-rw-r--r--  1 root root  811 Feb 17 15:11 example-com-80.conf
Enabling site
ERROR: Site /etc/apache2/sites-available/example-com-80 does not exist!

newvhost script:

OLDIFS=$IFS
IFS="."
printf "New Server Name with Top Level Domain: "
read NEW_SUBDOMAIN NEW_TLD
IFS=$OLDIFS

NEW_FULL_NAME="$NEW_SUBDOMAIN.$NEW_TLD"

echo "Validating: $NEW_FULL_NAME"

if [[ "$NEW_TLD" != "com" && "$NEW_TLD" != "dev" ]] ; then
  echo -e "\E[31;1mTLD must be com or dev! \033[0m" 
  exit 1
fi


if [ -d "/var/www/$NEW_SUBDOMAIN" ]; then
  echo -e "\E[31;1mRoot directory /var/www/$NEW_SUBDOMAIN already exists!\033[0m"
  exit 1
fi

mkdir /var/www/$NEW_SUBDOMAIN

if [ -d "/var/www/$NEW_SUBDOMAIN" ]; then
    echo "New DocumentRoot created: /var/www/$NEW_SUBDOMAIN"
else  
  echo -e "\E[31;1mUnable to make directory\033[0m"
  exit 1
fi

echo "Copying template structure"

cp /var/www/templates/structure/. /var/www/$NEW_SUBDOMAIN/ -R

sed -i "s/TEMPLATE/$NEW_FULL_NAME/g" /var/www/$NEW_SUBDOMAIN/index.html

CONFFILE="$NEW_SUBDOMAIN-$NEW_TLD-80.conf"

echo "Creating: $CONFFILE"

sudo cp /var/www/templates/vhost_template.conf /etc/apache2/sites-available/$CONFFILE

sudo sed -i "s/FULLNAME/$NEW_FULL_NAME/g" /etc/apache2/sites-available/$CONFFILE
sudo sed -i "s/DOMAINNAME/$NEW_SUBDOMAIN/g" /etc/apache2/sites-available/$CONFFILE

if [ -e "/etc/apache2/sites-available/$CONFFILE" ]; then
  ls -al /etc/apache2/sites-available/ | grep $CONFFILE # DEGBUG Listing to  doubly confirm $CONFFILE exists
  echo "Enabling site"
  sudo a2ensite /etc/apache2/sites-available/$CONFFILE
  sudo apache2ctl graceful
fi

Thanks,

Any other suggestions for improving the script are very welcome as long as that doesn't run afoul with the terms of StackOverflow.

Community
  • 1
  • 1
jslanger
  • 433
  • 1
  • 4
  • 8
  • I would try checking that the `example-com-80.conf` file itself is not bad, try manually parsing it from command line `$ sudo a2ensite example-com-80.conf` – Michael Coleman Feb 17 '15 at 23:16
  • @MichaelColeman, I added the console output from your suggestion to the original post. As well, a sym link now appears in /sites-enabled/ – jslanger Feb 18 '15 at 03:28
  • @raina77ow had a solution in [an older question I hadn't found yet](http://stackoverflow.com/questions/13035385/a2ensite-from-script-path) – jslanger Feb 18 '15 at 04:09

1 Answers1

28

The answer, in short, is that a2ensite just wants the name of the site.conf and not the whole path to the file.

So sudo a2ensite example-com-80.conf

I found this in an earlier answer by @raina77ow.

Community
  • 1
  • 1
jslanger
  • 433
  • 1
  • 4
  • 8
  • 1
    I was scratching my head and checked all paths 10 times before I saw your answer - thanks a lot for clear answer – R. Rahul Dec 03 '16 at 18:03
  • I've been reading through many, many SO threads and cannot find an answer. I have tried your solution, the full path, with and without the .conf extension and I still get the "Site xxx does not exist!" error. I am using Ubuntu 18.04.4 LTS Is this an issue with newer versions of Ubuntu? – Carl Du Plessis May 12 '20 at 05:10