Skip to content

Instantly share code, notes, and snippets.

@leadscloud
Last active August 29, 2015 14:02
Show Gist options
  • Save leadscloud/6650e27b317d6af3ca99 to your computer and use it in GitHub Desktop.
Save leadscloud/6650e27b317d6af3ca99 to your computer and use it in GitHub Desktop.
一些常用的操作lnmp的shell脚本
# http://linux-bash.googlecode.com/files/damnp-actgod.sh
function check_install {
if [ -z "`which "$1" 2>/dev/null`" ]
then
executable=$1
shift
while [ -n "$1" ]
do
DEBIAN_FRONTEND=noninteractive apt-get -q -y --force-yes install "$1"
print_info "$1 installed for $executable"
shift
done
else
print_warn "$2 already installed"
fi
}
function get_domain_name() {
# Getting rid of the lowest part.
domain=${1%.*}
lowest=`expr "$domain" : '.*\.\([a-z][a-z]*\)'`
case "$lowest" in
com|net|org|gov|edu|co)
domain=${domain%.*}
;;
esac
lowest=`expr "$domain" : '.*\.\([a-z][a-z]*\)'`
[ -z "$lowest" ] && echo "$domain" || echo "$lowest"
}
function get_password() {
# Check whether our local salt is present.
SALT=/var/lib/radom_salt
if [ ! -f "$SALT" ]
then
head -c 512 /dev/urandom > "$SALT"
chmod 400 "$SALT"
fi
password=`(cat "$SALT"; echo $1) | md5sum | base64`
echo ${password:0:13}
}
function install_typecho {
check_install wget wget
if [ ! -d /var/www ];
then
mkdir /var/www
fi
if [ -z "$1" ]
then
die "Usage: `basename $0` wordpress <hostname>"
fi
# Downloading the WordPress' latest and greatest distribution.
rm -rf /tmp/build
wget -O - "http://typecho.googlecode.com/files/0.8(10.8.15)-release.tar.gz" | \
tar zxf - -C /tmp/
mv /tmp/build/ "/var/www/$1"
rm -rf /tmp/build
chown -R www-data "/var/www/$1"
chmod -R 755 "/var/www/$1"
wget -P "/var/www/$1" http://linux-bash.googlecode.com/files/tz.php
cat > "/var/www/$1/phpmyadmin.sh" <<END
#!/bin/bash
mkdir /tmp/wordpress.\$$
wget -O - http://linux-bash.googlecode.com/files/phpMyAdmin.tar.gz | \
tar zxf - -C /tmp/wordpress.\$$
mv /tmp/wordpress.\$$/phpMyAdmin \${PWD}
rm -rf /tmp/wordpress.\$$
END
# Setting up the MySQL database
dbname=`echo $1 | tr . _`
userid=`get_domain_name $1`
# MySQL userid cannot be more than 15 characters long
userid="${userid:0:15}"
passwd=`get_password "$userid@mysql"`
mysqladmin create "$dbname"
echo "GRANT ALL PRIVILEGES ON \`$dbname\`.* TO \`$userid\`@localhost IDENTIFIED BY '$passwd';" | \
mysql
# Setting up Nginx mapping
cat > "/etc/nginx/conf.d/$1.conf" <<END
server
{
listen 80;
server_name $1;
index index.html index.htm index.php default.html default.htm default.php;
root /var/www/$1;
location / {
try_files \$uri @apache;
}
location @apache {
internal;
proxy_pass http://127.0.0.1:168;
include proxy.conf;
}
location ~ .*\.(php|php5)?$
{
proxy_pass http://127.0.0.1:168;
include proxy.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
$al
}
END
cat >> "/root/$1.mysql.txt" <<END
[wordpress_myqsl]
dbname = $dbname
username = $userid
password = $passwd
END
invoke-rc.d nginx reload
ServerAdmin=""
read -p "Please input Administrator Email Address:" ServerAdmin
if [ "$ServerAdmin" == "" ]; then
echo "Administrator Email Address will set to [email protected]!"
ServerAdmin="[email protected]"
else
echo "==========================="
echo Server Administrator Email="$ServerAdmin"
echo "==========================="
fi
cat >/etc/apache2/conf.d/$1.conf<<eof
<VirtualHost *:168>
ServerAdmin $ServerAdmin
php_admin_value open_basedir "/var/www/$1:/tmp/:/var/tmp/:/proc/"
DocumentRoot /var/www/$1
ServerName $1
</VirtualHost>
#ErrorLog /var/log/apache2/$1_error.log
#CustomLog /var/log/apache2/$1_access.log combined
eof
/etc/init.d/apache2 restart
cat >> "/root/$1.mysql.txt" <<END
[typycho_myqsl]
dbname = $dbname
username = $userid
password = $passwd
END
echo "mysql dataname:" $dbname
echo "mysql username:" $userid
echo "mysql passwd:" $passwd
}
function print_info {
echo -n -e '\e[1;36m'
echo -n $1
echo -e '\e[0m'
}
function print_warn {
echo -n -e '\e[1;33m'
echo -n $1
echo -e '\e[0m'
}
# Generating a new password for the root user.
passwd=`get_password root@mysql`
mysqladmin password "$passwd"
cat > ~/.my.cnf <<END
[client]
user = root
password = $passwd
END
chmod 600 ~/.my.cnf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment