Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Скрипт для разворачивания приложения на Ubuntu 15.04 с LAMP
- set -e
- github_repo='https://github.com/applejacky/students'
- apache_root='/var/www'
- project_name='students.loc'
- host='127.0.0.1'
- config_ini_path="${apache_root}/${project_name}/app/config.ini"
- required_packages=(
- 'git'
- 'composer'
- 'mysql'
- )
- function die {
- message=$1
- echo "Error: ${message}" 1>&2
- kill $$
- exit 1
- }
- function check_permission {
- if [[ $EUID -ne 0 ]]; then
- die "This script must be run as root."
- fi
- }
- function check_packages_installed {
- packages=("$@")
- for package in "${packages[@]}"; do
- if ! which "${package}" > /dev/null; then
- echo "Package ${package} is not installed. Installing."
- apt-get install $package
- fi
- done
- }
- function get_value_from_config_ini {
- config_key=$1
- path_to_config_ini=$2
- config_value=$(sed -n -e "s/${config_key}\s*=\s*//p" "$config_ini_path")
- [[ -z $config_value ]] && die "Variable ${config_key} not found in ${config_ini_path}"
- echo $config_value
- }
- function write_value_to_config_ini {
- config_key=$1
- config_value=$2
- path_to_config_ini=$3
- if check_config_key_presented $config_key "$path_to_config_ini"; then
- sed -ri "/^${config_key}\s?=/s/=.*/= ${config_value}/" "$path_to_config_ini"
- else
- die "Key ${config_key} not found in ${path_to_config_ini}"
- fi
- }
- function check_config_key_presented {
- config_key=$1
- path_to_config_ini=$2
- egrep -q "${config_key}(\s)?=" "$path_to_config_ini"
- }
- function create_db_if_not_exists {
- dbname=$1
- mysql -u$username -p$password -e "CREATE DATABASE IF NOT EXISTS ${dbname}" > /dev/null 2>&1
- }
- function create_students_table {
- mysql -u$username -p$password $dbname < "${apache_root}/${project_name}"/dump.sql > /dev/null 2>&1
- }
- check_permission
- check_packages_installed "${required_packages[@]}"
- destination_dir="${apache_root}/${project_name}"
- echo "Downloading ${github_repo} to \"$destination_dir\"..."
- git clone $github_repo "$destination_dir"
- echo "Type a database name: "
- read dbname;
- write_value_to_config_ini 'dbname' "$dbname $config_ini_path"
- echo "Type a host: "
- read host
- write_value_to_config_ini 'host' "$host $config_ini_path"
- echo "Type a username: "
- read username
- write_value_to_config_ini 'username' "$username $config_ini_path"
- echo "Type a password: "
- read password
- write_value_to_config_ini 'password' "$password $config_ini_path"
- cat "$config_ini_path"
- dbname=$(get_value_from_config_ini 'dbname' $"config_ini_path")
- host=$(get_value_from_config_ini 'host' "$config_ini_path")
- username=$(get_value_from_config_ini 'username' "$config_ini_path")
- password=$(get_value_from_config_ini 'password' "$config_ini_path")
- create_db_if_not_exists $dbname
- create_students_table
- cd "${apache_root}/${project_name}"
- echo "Installing dependencies and configuring autoloader..."
- composer install
- echo "Seeding students table..."
- "${apache_root}/${project_name}"/util/student_table_seeder.php
- echo "Creating virtual host configuration..."
- virtual_host_path="/etc/apache2/sites-available/${project_name}.conf"
- tee $virtual_host_path > /dev/null <<EOF
- <VirtualHost *:80>
- ServerAdmin admin@${project_name}
- ServerName ${project_name}
- ServerAlias www.${project_name}
- DocumentRoot /var/www/${project_name}/public
- ErrorLog ${APACHE_LOG_DIR}/error.log
- CustomLog ${APACHE_LOG_DIR}/access.log combined
- <Directory /var/www/${project_name}/public>
- Options FollowSymLinks
- Allowoverride All
- Require all granted
- </Directory>
- </VirtualHost>
- EOF
- systemctl start apache2
- a2ensite "${project_name}".conf
- a2enmod rewrite
- systemctl restart apache2
- sed -i '/^'"${host}"'/ d' /etc/hosts
- cat <<EOF >> ./etc/hosts
- ${host} localhost
- ${host} ${project_name}
- EOF
Add Comment
Please, Sign In to add comment