· 10 years ago · Jun 20, 2016, 12:36 AM
1#!/bin/bash
2
3# Скрипт Ð´Ð»Ñ Ñ€Ð°Ð·Ð²Ð¾Ñ€Ð°Ñ‡Ð¸Ð²Ð°Ð½Ð¸Ñ Ð¿Ñ€Ð¸Ð»Ð¾Ð¶ÐµÐ½Ð¸Ñ Ð½Ð° Ubuntu 15.04 Ñ LAMP
4
5set -e
6
7github_repo='https://github.com/applejacky/students'
8apache_root='/var/www'
9project_name='students.loc'
10host='127.0.0.1'
11
12config_ini_path="${apache_root}/${project_name}/app/config.ini"
13required_packages=(
14 'git'
15 'composer'
16 'mysql'
17)
18
19function die {
20 message=$1
21 echo "Error: ${message}" 1>&2
22 kill $$
23 exit 1
24}
25
26function check_permission {
27 if [[ $EUID -ne 0 ]]; then
28 die "This script must be run as root."
29 fi
30}
31
32function check_packages_installed {
33 packages=("$@")
34 for package in "${packages[@]}"; do
35 if ! which "${package}" > /dev/null; then
36 echo "Package ${package} is not installed. Installing."
37 apt-get install $package
38 fi
39 done
40}
41
42function get_value_from_config_ini {
43 config_key=$1
44 path_to_config_ini=$2
45 config_value=$(sed -n -e "s/${config_key}\s*=\s*//p" "$config_ini_path")
46 [[ -z $config_value ]] && die "Variable ${config_key} not found in ${config_ini_path}"
47 echo $config_value
48}
49
50function write_value_to_config_ini {
51 config_key=$1
52 config_value=$2
53 path_to_config_ini=$3
54 if check_config_key_presented $config_key "$path_to_config_ini"; then
55 sed -ri "/^${config_key}\s?=/s/=.*/= ${config_value}/" "$path_to_config_ini"
56 else
57 die "Key ${config_key} not found in ${path_to_config_ini}"
58 fi
59}
60
61function check_config_key_presented {
62 config_key=$1
63 path_to_config_ini=$2
64 egrep -q "${config_key}(\s)?=" "$path_to_config_ini"
65}
66
67function create_db_if_not_exists {
68 dbname=$1
69 mysql -u$username -p$password -e "CREATE DATABASE IF NOT EXISTS ${dbname}" > /dev/null 2>&1
70}
71
72function create_students_table {
73 mysql -u$username -p$password $dbname < "${apache_root}/${project_name}"/dump.sql > /dev/null 2>&1
74}
75
76check_permission
77
78check_packages_installed "${required_packages[@]}"
79
80destination_dir="${apache_root}/${project_name}"
81echo "Downloading ${github_repo} to \"$destination_dir\"..."
82git clone $github_repo "$destination_dir"
83
84echo "Type a database name: "
85read dbname;
86write_value_to_config_ini 'dbname' "$dbname $config_ini_path"
87
88echo "Type a host: "
89read host
90write_value_to_config_ini 'host' "$host $config_ini_path"
91
92echo "Type a username: "
93read username
94write_value_to_config_ini 'username' "$username $config_ini_path"
95
96echo "Type a password: "
97read password
98write_value_to_config_ini 'password' "$password $config_ini_path"
99
100cat "$config_ini_path"
101
102dbname=$(get_value_from_config_ini 'dbname' $"config_ini_path")
103host=$(get_value_from_config_ini 'host' "$config_ini_path")
104username=$(get_value_from_config_ini 'username' "$config_ini_path")
105password=$(get_value_from_config_ini 'password' "$config_ini_path")
106
107create_db_if_not_exists $dbname
108create_students_table
109
110cd "${apache_root}/${project_name}"
111echo "Installing dependencies and configuring autoloader..."
112composer install
113
114echo "Seeding students table..."
115"${apache_root}/${project_name}"/util/student_table_seeder.php
116
117echo "Creating virtual host configuration..."
118virtual_host_path="/etc/apache2/sites-available/${project_name}.conf"
119tee $virtual_host_path > /dev/null <<EOF
120<VirtualHost *:80>
121ServerAdmin admin@${project_name}
122ServerName ${project_name}
123ServerAlias www.${project_name}
124DocumentRoot /var/www/${project_name}/public
125ErrorLog ${APACHE_LOG_DIR}/error.log
126CustomLog ${APACHE_LOG_DIR}/access.log combined
127<Directory /var/www/${project_name}/public>
128Options FollowSymLinks
129Allowoverride All
130Require all granted
131</Directory>
132</VirtualHost>
133EOF
134
135systemctl start apache2
136a2ensite "${project_name}".conf
137a2enmod rewrite
138systemctl restart apache2
139
140sed -i '/^'"${host}"'/ d' /etc/hosts
141cat <<EOF >> ./etc/hosts
142${host} localhost
143${host} ${project_name}
144EOF