Bash: Variable Isn't Used / Defined Properly - bash

working on my auto-install script. It isn't using my variable properly, debug log looks like this.
I've tried several variants, using "" instead of '', not using '' or "" but just $variable. The script looks like this:
echo "Creating Apache2 VHost and Wordpress blog $domain"
VAR1=$sitealias
VAR2=$domain
MOREF=$`pwgen 14 1`
echo $MOREF is SQL password.
mkdir /var/www/"$domain"
chown -R www-data:www-data /var/www/"$domain"
echo "<VirtualHost *:80>
ServerName myblog.example.com
ServerAdmin webmaster#example.com
DocumentRoot /usr/share/wordpress
Alias /wp-content /var/lib/wordpress/wp-content
<Directory /usr/share/wordpress>
Options FollowSymLinks
AllowOverride Limit Options FileInfo
DirectoryIndex index.php
Require all granted
</Directory>
<Directory /var/lib/wordpress/wp-content>
Options FollowSymLinks
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
" > /etc/apache2/sites-available/$domain.conf
mysqladmin -u root password supersecretpassword create wp_$sitealias
mysqladmin -u root password supersecretpassword CREATE USER $sitealias#'localhost' IDENTIFIED BY '$MOREF';
mysqladmin -u root password supersecretpassword GRANT ALL PRIVILEGES ON wp_'$sitealias.'* TO '$sitealias'#localhost;
FLUSH PRIVILEGES;
quit
The script should insert the variable in the commands, folder directories. Instead the variable is just ignored, as is shown in the debug log.
UPDATE:
variable capturing works fine now. However, SQL doesn't like your double quote (copy and pasted them)
root#hosting:/bin# hello.sh letstest.tld testtesttdjsuqwu
Creating Apache2 VHost and Wordpress blog
qui2AenaihoQu9 is SQL password.
mysqladmin: Unknown command: 'create wp_testtesttdjsuqwu'
mysqladmin: Unknown command: 'CREATE USER 'testtesttdjsuqwu'#'localhost' IDENTIFIED BY 'q'
mysqladmin: Unknown command: 'GRANT ALL PRIVILEGES ON wp_testtesttdjsuqwu.* TO 'testtesttd'
/bin/hello.sh: line 37: FLUSH: command not found
/bin/hello.sh: line 38: quit: command not found

If you want to use parameters inside your script (based on the comments above, 'domain' and 'sitealias') use the '$1' '$2' to capture them
domain=$1
sitealias=$2
# Fixed quoting of pwgen)
MOREF=$(pwgen 14 1)
# Rest of the script here, using $domain, $sitealias
echo $MOREF is SQL password.
mkdir /var/www/"$domain"
It's not completely clear what value do you want to 'APACHE_LOG_DIR'. Assuming that this variable need to be resolved at run-time, based on the Apache default value, you want to use single quote instead of double quote for the 'echo' statement. This will defer the resolution of this variable.
echo '<VirtualHost *:80>
ServerName myblog.example.com
ServerAdmin webmaster#example.com
DocumentRoot /usr/share/wordpress
# Lines removed for
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
' > /etc/apache2/sites-available/$domain.conf
Also, consider double-quoting for the mysqladmin setup command. Use double quote to enable variable substitution, Use single quote when needed by mysql.
mysqladmin -u root password supersecretpassword "create wp_$sitealias"
mysqladmin -u root password supersecretpassword "CREATE USER '$sitealias'#'localhost' IDENTIFIED BY '$MOREF'";
mysqladmin -u root password supersecretpassword "GRANT ALL PRIVILEGES ON wp_$sitealias.* TO '$sitealias'#'localhost';"
FLUSH PRIVILEGES;
quit

Related

Up laravel project and get (The requested URL /icons/create was not found on this server.) but route exist and any route works perfectly

Running a project in dev server run perfectly but I try to move on a live server, I got "The requested URL /icons/create was not found on this server."
i got that just in 1 route in : .../icon/create
but any route works fine.
this is my htaccess :
I try to composer dump-autoload in local but don't get any error"
or something problem in my htaccess file?
mod_rewrite already enable in the apache web server.
Very thank you, if someone wants to help me :)
as u r saying u have "mod_rewrite already enable"
try this editing /etc/apache2/sites-enabled/000-default or /etc/apache2/apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
Change the AllowOverride None to AllowOverride All
Comment Alias in /etc/apache2/mods-available/alias.conf.
<IfModule alias_module>
# Aliases: Add here as many aliases as you need (with no limit). The format is
# Alias fakename realname
#
# Note that if you include a trailing / on fakename then the server will
# require it to be present in the URL. So "/icons" isn't aliased in this
# example, only "/icons/". If the fakename is slash-terminated, then the
# realname must also be slash terminated, and if the fakename omits the
# trailing slash, the realname must also omit it.
#
# We include the /icons/ alias for FancyIndexed directory listings. If
# you do not use FancyIndexing, you may comment this out.
# Alias /icons/ "/usr/share/apache2/icons/"
# <Directory "/usr/share/apache2/icons">
# Options FollowSymlinks
# AllowOverride None
# Require all granted
# </Directory>
</IfModule>
After Comment these lines check apache conf using this command:
sudo apache2ctl -t
Then reload apache2:
sudo systemctl reload apache2

a2ensite does not create log files

I have a simple bash script which should generate Apache site.conf file and then run a2ensite site.name
Everything works fine but a2ensite does not generate log files automatically. Then when I run sudo systemctl reload apache2 it throws me an error Cannot access directory '/var/log/apache2/www/site.name/' Bash file looks like:
#!/bin/bash
read -p 'Write url without www and http prefixes: ' url
template=$(</home/Camo/custom-scripts/apache/site-template.conf)
# Next line is string substitution
template=("${template//__URL__/$url}")
configFile=${url}'.conf'
configDirPath="/etc/apache2/sites-available/"
wwwDirPath="/var/www/html/$url"
mkdir $wwwDirPath
chown -R www-data:www-data $wwwDirPath
chmod 644 -R $wwwDirPath
echo "$template" > $configDirPath$configFile
sudo a2ensite ${configFile}
sudo systemctl reload apache2
and here is site.cong file created by previous script
<VirtualHost *:80>
ServerAdmin xxxxx.xxxxx#gmail.com
ServerName camo.publicvm.com
ErrorLog ${APACHE_LOG_DIR}/www/camo.publicvm.com/error.log
CustomLog ${APACHE_LOG_DIR}/www/camo.publicvm.com/access.log combined
DocumentRoot /var/www/html/camo.publicvm.com
<Directory /var/www/html/camo.publicvm.com/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>
everything looks ok so why are log files missing? Thanks for any help.
Log files are not generated automatically, so you will have to create them manually:
touch ${APACHE_LOG_DIR}/www/camo.publicvm.com/error.log
touch ${APACHE_LOG_DIR}/www/camo.publicvm.com/access.log
And don't forget changing its ownership:
chown root.adm ${APACHE_LOG_DIR}/www/camo.publicvm.com/error.log
chown root.adm ${APACHE_LOG_DIR}/www/camo.publicvm.com/access.log
Then check if everything looks fine and if it does, reload apache2 service:
apachectl configtest
systemctl reload apache2

Bash echo multi line to end of file but dont echo in terminal

I'm writing a custom alias function to create my vhosts for me whenever i'm creating a new web project. In my function, I want to paste an entire new virtual host into my httpd-vhosts.conf file. I'm first asking for an input for the domain name and folder name to create like this
read -p 'Please enter the domain for your new site: ' domain;
read -p 'Please enter the folder name for your new site: ' folder;
and then in my function, i'm writing to my httpd-vhosts.conf file like this
echo "
<VirtualHost *:80>
ServerAdmin $domain
DocumentRoot \"/Users/sam/Development/Websites/$folder\"
ServerName $domain
ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
</VirtualHost>" | sudo tee -a /usr/local/etc/httpd/extra/httpd-vhosts.conf;
But i've noticed this echo's in the terminal as well as in the conf file. The function works perfectly, but how do I get it to NOT echo in the terminal?
Do I have to use something other than echo to append to the file to achieve this?
Just redirect to /dev/null:
echo "
<VirtualHost *:80>
ServerAdmin $domain
DocumentRoot \"/Users/sam/Development/Websites/$folder\"
ServerName $domain
ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
</VirtualHost>" | sudo tee -a /usr/local/etc/httpd/extra/httpd-vhosts.conf > /dev/null
This allows you to append with sudo privileges while also not showing the output on screen.
instead of tee you may consider to use >>
sudo sh -c "echo \"
<VirtualHost *:80>
ServerAdmin $domain
DocumentRoot \"/Users/sam/Development/Websites/$folder\"
ServerName $domain
ErrorLog \"/usr/local/var/log/httpd/$folder/error_log\"
CustomLog \"/usr/local/var/log/httpd/$folder/access_log\" common
</VirtualHost>\" >> test2.out"

CGI script not executing but displaying content after installing mooshak

Recently, I downloaded and installed mooshak in Ubuntu 14.04. I followed all the steps given in the requirements and installation page.
When I try to access http://localhost/~mooshak, I'm being redirected to http://localhost/~mooshak/cgi-bin/execute and the following content is being displayed :
#!/bin/sh
# the next line restarts using tclsh \
PATH=$PATH:/usr/local/bin:/usr/contrib/bin ; exec tclsh "$0" "$#"
#-*-Mode: TCL; iso-accents-mode: t;-*-
cd ../..
lappend auto_path packages
source .config
execute::command_line
Can anyone point me in the right direction? This is the content of my /etc/apache2/mods-enabled/userdir.conf file :
<IfModule mod_userdir.c>
UserDir public_html
UserDir disabled root
<Directory /home/*/public_html>
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<Limit GET POST OPTIONS>
Require all granted
</Limit>
<LimitExcept GET POST OPTIONS>
Require all denied
</LimitExcept>
</Directory>
<Directory /home/*/public_html/cgi-bin>
Options +ExecCGI -Includes -Indexes
SetHandler cgi-script
Order allow,deny
Allow from all
</Directory>
</IfModule>
Thanks in advance!
Make sure you have the modules userdir and suexec are enabled in Apache2. If needed execute the following commands
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/userdir.conf
sudo ln -s ../mods-available/userdir.load
sudo ln -s ../mods-available/suexec.load
You will need to restart Apache to activate the configuration.
sudo apache2ctl graceful

Same Rightscript used multiple times on a ServerTemplate with different inputs

I have just created my first Rightscale ServerTemplate and Deployment using a bunch of Rightscripts. One of the scripts I created was to add a virtual host to apache.
#!/bin/bash -e
if [ $RS_DISTRO = ubuntu ]; then
export apache=apache2
export apache_extra_conf_dir=/etc/apache2/conf.d
elif [ $RS_DISTRO = centos ]; then
export apache=httpd
export apache_extra_conf_dir=/etc/httpd/conf.d
fi
server_name=$SERVER_NAME
echo "Adding virtual hosts to ${apache_extra_conf_dir}/vh-${server_name}.conf"
cat > $apache_extra_conf_dir/vh-${server_name}.conf <<EOF
NameVirtualHost $SITE_IP:$SITE_PORT
<VirtualHost $SITE_IP:$SITE_PORT>
ServerName $SERVER_NAME
ServerAlias $SITE_DOMAIN *.$SITE_DOMAIN
UseCanonicalName Off
ServerAdmin $ADMIN_EMAIL
DocumentRoot $APACHE_WWW_DIR
<Directory "$APACHE_WWW_DIR">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
EOF
service $apache restart
exit 0
My question is can I use the same Rightscript twice on the ServerTemplate but set different inputs for each (IP, Port, www dir and Servername)? eg.
ServerTemplate:
Execute Rightscript vhost: *:80 /www-x/ x.com
Execute Rightscript vhost: *:80 /www-y/ y.com
OR do I have to create a special Rightscript just for this server deployment that has both virtual hosts defined in the same script?
Execute Rightscript vhost: *:80 /www-x/ x.com | *:80 /www-y/ y.com
You can use the same RightScript with different inputs only if you put that script in the "Operational Scripts" section of your ServerTemplate. Then if you leave the inputs blank, you can execute that operational script with different inputs many times.
If you want that behavior in the "Boot Scripts" section, you'll have to either create a single script which handles two sets of inputs (for two vhosts) or create a clone of the RightScript with a different name and different input names.
You might consider taking a look at the Chef based tools. You can create a Chef recipe which takes an array of vhost names as input, then executes the "apache_site" resource that sets up a vhost. You can see an example in the RightScale Chef code linked below.
https://github.com/rightscale/cookbooks_public/blob/master/cookbooks/web_apache/recipes/setup_frontend_http_vhost.rb

Resources