Reputation: 43
I wrote a setup script for something and now I need to create a virtualhost with it. To do so I used this code:
echo -e \
"WSGISocketPrefix $DIRECTORY/socks/\n"\
"WSGIPythonHome $DIRECTORY/env/local\n"\
"WSGIRestrictStdout On\n"\
"WSGIRestrictSignal Off\n"\
"WSGIPythonOptimize 1\n"\
"<VirtualHost *:80>\n"\
" ServerAdmin [email protected]\n"\
" ServerName app.localhost\n"\
" DocumentRoot \"$DIRECTORY\"\n"\
" Alias /m/ $DIRECTORY/static/\n"\
" Alias /upfiles/ $DIRECTORY/askbot/upfiles/\n"\
" <DirectoryMatch \"$DIRECTORY/askbot/skins/([^/]+)/media\">\n"\
" Order deny,allow\n"\
" Allow from all\n"\
" </DirectoryMatch>\n"\
" <Directory \"$DIRECTORY/askbot/upfiles\">\n"\
" Order deny,allow\n"\
" Allow from all\n"\
" </Directory>\n"\
"\n"\
" WSGIDaemonProcess askbot_"$NUMBER"_\n"\
" WSGIProcessGroup askbot_"$NUMBER"_\n"\
" WSGIScriptAlias / $DIRECTORY/django.wsgi\n"\
"\n"\
' ErrorLog ${APACHE_LOG_DIR}/askbot_error.log'"\n"\
' CustomLog ${APACHE_LOG_DIR}/askbot_access.log combined'"\n"\
"</VirtualHost>\n" > /etc/apache2/sites-available/app.conf
$DIRECTORY
is a variable containing the path and therefore its content should be printed. ${APACHE_LOG_DIR}
however is no variable here and should be printed as is. Unfortunately instead of writing the content to the file it will echo it to the terminal with some errors (File not found etc). When I remove the last two lines it does work but of course thats not a solution but I cant seem to get it to work.
Any ideas?
Upvotes: 1
Views: 449
Reputation: 386058
Use a here-document. Use \
to escape the dollar signs where needed.
cat <<EOF > /etc/apache2/sites-available/app.conf
WSGISocketPrefix $DIRECTORY/socks/
WSGIPythonHome $DIRECTORY/env/local
WSGIRestrictStdout On
WSGIRestrictSignal Off
WSGIPythonOptimize 1
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName app.localhost
DocumentRoot "$DIRECTORY"
Alias /m/ $DIRECTORY/static/
Alias /upfiles/ $DIRECTORY/askbot/upfiles/
<DirectoryMatch "$DIRECTORY/askbot/skins/([^/]+)/media">
Order deny,allow
Allow from all
</DirectoryMatch>
<Directory "$DIRECTORY/askbot/upfiles">
Order deny,allow
Allow from all
</Directory>
WSGIDaemonProcess askbot_$NUMBER_
WSGIProcessGroup askbot_$NUMBER_
WSGIScriptAlias / $DIRECTORY/django.wsgi
ErrorLog \${APACHE_LOG_DIR}/askbot_error.log
CustomLog \${APACHE_LOG_DIR}/askbot_access.log combined
</VirtualHost>
EOF
Upvotes: 2
Reputation: 27360
echo
understands multiline strings:
echo "
fist section here with ${substitutions}
" > /etc/apache2/sites-available/app.conf
then append the last section:
echo '
second section here without substitutions
' >> /etc/apache2/sites-available/app.conf
but in this case it might be easier to create a template file and then use e.g. sed
to do string substitutions? I use the commandline interface to Jinja2 to do the same task (https://github.com/kolypto/j2cli), but that is almost certainly overkill for such a simple template ;-)
Upvotes: 2