Last active
August 29, 2015 14:20
-
-
Save jeka-kiselyov/9d3e1d3bd2a35f3511ab to your computer and use it in GitHub Desktop.
SH to add apache domain to dynamic virtual hosts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
run() | |
{ | |
echo "Adding new apache domain" | |
if [ "$(id -u)" != "0" ]; then | |
echo "Sorry, but this script should be executed by root only. Try: sudo ./add_apache_domain.sh" | |
return | |
fi | |
echo "Please enter domain name you want to add:" | |
read domain_name | |
domain_name="${domain_name/www./}" | |
domain_name="${domain_name/http:\/\//}" | |
domain_name="${domain_name//\//}" | |
domain_name="${domain_name,,}" #lowercase | |
if [ -z "$domain_name" ]; then | |
echo "No domain? Why? Ok, cya..." | |
return | |
else | |
echo "So the domain name is: $domain_name Good choice!" | |
arr=(${domain_name//./ }) | |
username="${arr[0]}" | |
if [ -z "$username" ]; then | |
echo "Can not get username from it" | |
return | |
fi | |
if getent passwd $username > /dev/null 2>&1; then | |
echo "Sad. But user $username already exists. I'm too stupid script to do anything about it." | |
echo "Try to add apache domain yourself please" | |
return | |
fi | |
echo "And the username is: $username Love it!" | |
fi | |
echo "And the password is:" | |
read password | |
while [ "${#password}" -lt 6 ]; do | |
echo "Oh c'mon. It's too short. I know you can do better:" | |
read password | |
done | |
echo "Nice!" | |
echo "Ok, creating new user..." | |
useradd $username | |
echo -e "$password\n$password" | passwd $username | |
echo "Ok, creating htdocs directory..." | |
mkdir /var/www/$domain_name | |
echo "Ok, creating symbolic link for www alias..." | |
ln -s /var/www/$domain_name /var/www/www.$domain_name | |
echo "Is it good? Will change directory ownership now..." | |
chown $username:www-data /var/www/www.$domain_name | |
echo "And will change user home folder to that new one..." | |
usermod -d /var/www/$domain_name $username | |
echo "I'm almost there. Going to restart apache now..." | |
service apache2 restart | |
echo "Ready! Ready! Ready! But can you please check it one more time?" | |
} | |
run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
run() | |
{ | |
echo "Adding new mysql database and user for it" | |
echo "Please enter database and username you want to add:" | |
read db_name | |
db_name="${db_name,,}" #lowercase | |
if [ -z "$db_name" ]; then | |
echo "No database name? Why? Ok, cya..." | |
return | |
else | |
echo "So the database name is: $db_name Good choice!" | |
fi | |
echo "MySQL host:" | |
read dbhost | |
echo "MySQL root user name:" | |
read dbuser | |
echo "MySQL root password:" | |
read dbpassword | |
echo "And the password for the new user is:" | |
read password | |
while [ "${#password}" -lt 6 ]; do | |
echo "Oh c'mon. It's too short. I know you can do better:" | |
read password | |
done | |
echo "Nice!" | |
echo "Ok, creating database..." | |
mysql --user=$dbuser --password=$dbpassword --host=$dbhost -e "CREATE DATABASE IF NOT EXISTS $db_name;" | |
echo "Ok, creating user..." | |
mysql --user=$dbuser --password=$dbpassword --host=$dbhost -e "CREATE USER '$db_name'@'%' IDENTIFIED BY '$password';" | |
echo "Ok, grunting access..." | |
mysql --user=$dbuser --password=$dbpassword --host=$dbhost -e "GRANT ALL PRIVILEGES ON $db_name.* TO '$db_name'@'%';" | |
echo "Looks good? And now I'm going to ask db if everything is fine..." | |
mysql --user=$dbuser --password=$dbpassword --host=$dbhost -e "SHOW GRANTS FOR '$db_name'@'%';" | |
echo "Ready! Ready! Ready! But can you please check it one more time?" | |
} | |
run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
run() | |
{ | |
echo "Running chmod on wordpress" | |
if [ "$(id -u)" != "0" ]; then | |
echo "Sorry, but this script should be executed by root only. Try: sudo ./chmod_wordpress.sh" | |
return | |
fi | |
echo "Please enter domain name you want to chmod:" | |
read domain_name | |
domain_name="${domain_name/www./}" | |
domain_name="${domain_name/http:\/\//}" | |
domain_name="${domain_name//\//}" | |
domain_name="${domain_name,,}" #lowercase | |
if [ -z "$domain_name" ]; then | |
echo "No domain? Why? Ok, cya..." | |
return | |
else | |
echo "So the domain name is: $domain_name Good choice"; | |
echo "So the domain name is: $domain_name Good choice!" | |
arr=(${domain_name//./ }) | |
username="${arr[0]}" | |
echo "And the username is: $username Love it! Is it good?" | |
fi | |
read good | |
if [ $good == "y" ]; then | |
echo "Good" | |
else | |
echo "Bye" | |
return | |
fi | |
# echo "Ok, creating htdocs directory..." | |
# mkdir /var/www/$domain_name | |
echo "Is it good? Will change directory ownership now..." | |
chown -R $username:$username /var/www/www.$domain_name | |
find /var/www/$domain_name/ -type d -exec chmod 755 {} \; | |
find /var/www/$domain_name/ -type f -exec chmod 644 {} \; | |
find /var/www/$domain_name/ -name "cache" -type d -exec chmod -R 775 {} \; | |
find /var/www/$domain_name/ -name "uploads" -type d -exec chmod -R 775 {} \; | |
find /var/www/$domain_name/ -name "w3tc-config" -type d -exec chmod -R 775 {} \; | |
find /var/www/$domain_name/ -name "seo_params_script" -type d -exec chmod -R 775 {} \; | |
find /var/www/$domain_name/seo_params_script/ -type f -exec chmod 664 {} \; | |
find /var/www/$domain_name/wp-content/cache/ -type f -exec chmod 664 {} \; | |
find /var/www/$domain_name/wp-content/uploads/ -type f -exec chmod 664 {} \; | |
find /var/www/$domain_name/wp-content/w3tc-config/ -type f -exec chmod 664 {} \; | |
echo "Ready! Ready! Ready! But can you please check it one more time?" | |
} | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment