Last active
December 21, 2016 12:53
-
-
Save SergeyStorm/7e06f57c1f840ee49d6c3f998271a980 to your computer and use it in GitHub Desktop.
This file contains 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 | |
domain='dspeak2.vtiger.pinstudio.ru'; | |
mysql_server_package='mysql-server-5.5'; | |
mysql_root_passwd='***************'; | |
crm_home='/var/www/crm'; | |
crm_user='crm'; | |
# 7 | |
#crm_passwd=$(tr -dc '[:graph:]' < /dev/urandom | head -c ${1:-31};); | |
passwd_length=31; | |
crm_passwd=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-$passwd_length};echo); | |
crm_db_name='crm'; | |
crm_db_user='crm'; | |
admin_email='admin@somewhere.there'; | |
install_crm_packages() { | |
echo "Installing CRM dependencies" | |
apt-get update; | |
# 1 | |
apt-get -y install nginx; | |
systemctl enable nginx; | |
# 2 | |
apt-get -y install php5-fpm; | |
systemctl enable php5-fpm; | |
# 3 | |
debconf-set-selections <<< "$mysql_server_package mysql-server/root_password password $mysql_root_passwd"; | |
debconf-set-selections <<< "$mysql_server_package mysql-server/root_password_again password $mysql_root_passwd"; | |
apt-get -y install $mysql_server_package; | |
systemctl enable mysql; | |
# 4a | |
# Install all other dependencies | |
aptitude -y install binutils cpp flex gcc libarchive-zip-perl \ | |
libc6-dev libcompress-zlib-perl libpcre3 libpopt-dev lynx m4 \ | |
make ncftp nmap openssl perl perl-modules unzip zip zlib1g-dev \ | |
autoconf automake1.9 libtool bison autotools-dev gcc \ | |
libpng12-dev libjpeg62-dev libfreetype6-dev libssl-dev \ | |
libxml2-dev libxml2 g++ gawk libapache2-mod-php5 php5-gd \ | |
php5-imap php5-zlib php5-curl php5-mysql; | |
} | |
# 8 | |
create_crm_dirs() { | |
echo -n "Creating CRM dirs" | |
mkdir -pv $crm_home; | |
mkdir -pv $crm_home/log; | |
mkdir -pv $crm_home/public_html; | |
echo -e "\tOK"; | |
} | |
# 9 | |
own_crm_dirs() { | |
echo -n "Owning CRM dirs" | |
chown $crm_user.$crm_user -R $crm_home; | |
chmod 0755 -R $crm_home; | |
echo -e "\tOK"; | |
} | |
change_passwd() { | |
echo "$crm_user:$crm_passwd" | chpasswd; | |
if [[ $? == 0 ]]; | |
then | |
echo "Password for user '$crm_user' is set to: '$crm_passwd'"; | |
else | |
echo "Failed to set 'crm' user password"; | |
fi; | |
} | |
create_crm_user() { | |
echo "Checking CRM user"; | |
id $crm_user &> /dev/null; | |
if [[ $? != 0 ]]; | |
then | |
echo -ne "Creating CRM user with login: '$crm_user'\t"; | |
useradd -M -d $crm_home $crm_user > /dev/null; | |
if [[ $? == 0 ]]; | |
then | |
echo "OK"; | |
change_passwd; | |
else | |
echo "Fail"; | |
exit 1; | |
fi; | |
else | |
echo "CRM user already exists, only changing password"; | |
change_passwd; | |
fi; | |
} | |
# 5 | |
config_php_fpm() { | |
echo -n "Configuring PHP-FPM" | |
cat > /etc/php5/fpm/pool.d/crm.conf << EOF | |
[crm] | |
listen = /var/run/php5-fpm-crm.sock | |
listen.owner = crm | |
listen.group = www-data | |
listen.mode = 0660 | |
user = crm | |
group = www-data | |
pm = dynamic | |
pm.max_children = 10 | |
pm.start_servers = 2 | |
pm.min_spare_servers = 1 | |
pm.max_spare_servers = 5 | |
pm.max_requests = 0 | |
chdir = / | |
env[HOSTNAME] = \$HOSTNAME | |
env[PATH] = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
php_admin_value[max_execution_time] = 1200 | |
php_admin_value[open_basedir] = /var/www/crm/public_html | |
php_admin_value[sendmail_path] = "/usr/sbin/sendmail -t -i -f $admin_email" | |
EOF | |
systemctl restart php5-fpm; | |
if [[ $? == 0 ]]; | |
then | |
echo -e "\tOK"; | |
else | |
echo -e "\tFail"; | |
fi; | |
} | |
# 10 | |
config_nginx() { | |
echo -n "Configuring NGINX" | |
if [ -f /etc/nginx/sites-enabled/default ]; | |
then | |
rm /etc/nginx/sites-enabled/default | |
fi; | |
cat > /etc/nginx/sites-available/crm << EOF | |
# Server configuration for CRM | |
server { | |
listen 80; | |
listen [::]:80; | |
access_log $crm_home/log/access.log; | |
error_log $crm_home/log/error.log; | |
root $crm_home/public_html; | |
index index.html index.htm index.php; | |
server_name $domain; | |
location / { | |
# First attempt to serve request as file, then | |
# as directory, then fall back to displaying a 404. | |
try_files \$uri \$uri/ =404; | |
} | |
location ~ \.php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/var/run/php5-fpm-crm.sock; | |
} | |
} | |
EOF | |
if [ -f /etc/nginx/sites-enabled/crm ]; | |
then | |
rm /etc/nginx/sites-enabled/crm; | |
ln -s /etc/nginx/sites-available/crm /etc/nginx/sites-enabled/crm; | |
fi; | |
systemctl restart nginx; | |
if [[ $? == 0 ]]; | |
then | |
echo -e "\tOK"; | |
else | |
echo -e "\tFail"; | |
fi; | |
} | |
create_database() { | |
echo -n "Creating Database" | |
result=0; | |
mysql -u root --password=$mysql_root_passwd -e "CREATE DATABASE IF NOT EXISTS $crm_db_name;"; | |
let "result += $?"; | |
mysql -u root --password=$mysql_root_passwd -e "GRANT ALL PRIVILEGES ON $crm_db_name.* TO '$crm_db_user'@'127.0.0.1' IDENTIFIED BY '$crm_passwd';"; | |
let "result += $?"; | |
mysql -u root --password=$mysql_root_passwd -e "FLUSH PRIVILEGES;"; | |
let "result += $?"; | |
if [[ $result == 0 ]]; | |
then | |
echo -e "\tOK"; | |
else | |
echo -e "\tFail"; | |
fi; | |
} | |
install_crm_packages; | |
create_crm_dirs; | |
create_crm_user; | |
own_crm_dirs; | |
config_php_fpm; | |
config_nginx; | |
create_database; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment