summaryrefslogtreecommitdiff
path: root/automated/linux/lamp/lamp.sh
blob: fc995c075b4097508a35e4e46c0f14c4017f8780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh

# shellcheck disable=SC1091
. ../../lib/sh-test-lib
OUTPUT="$(pwd)/output"
RESULT_FILE="${OUTPUT}/result.txt"
export RESULT_FILE

usage() {
    echo "Usage: $0 [-s <true|false>]" 1>&2
    exit 1
}

while getopts "s:" o; do
  case "$o" in
    s) SKIP_INSTALL="${OPTARG}" ;;
    *) usage ;;
  esac
done

! check_root && error_msg "This script must be run as root"
[ -d "${OUTPUT}" ] && mv "${OUTPUT}" "${OUTPUT}_$(date +%Y%m%d%H%M%S)"
mkdir -p "${OUTPUT}"

# Install lamp and use systemctl for service management. Tested on Ubuntu 16.04,
# Debian 8, CentOS 7 and Fedora 24. systemctl should available on newer releases
# as well.
if [ "${SKIP_INSTALL}" = "True" ] || [ "${SKIP_INSTALL}" = "true" ]; then
    warn_msg "LAMP package installation skipped"
else
    dist_name
    # shellcheck disable=SC2154
    case "${dist}" in
      Debian|Ubuntu)
        if [ "${dist}" = "Debian" ]; then
            pkgs="apache2 mysql-server php5-mysql php5-common libapache2-mod-php5"
        elif [ "${dist}" = "Ubuntu" ]; then
            pkgs="apache2 mysql-server php-mysql php-common libapache2-mod-php"
        fi
        install_deps "curl ${pkgs}"
        echo "extension=mysqli.so" >> /etc/php5/apache2/php.ini
        systemctl restart apache2
        systemctl restart mysql
        ;;
      CentOS|Fedora)
        pkgs="httpd mariadb-server mariadb php php-mysql"
        install_deps "curl ${pkgs}"
        systemctl start httpd.service
        systemctl start mariadb
        ;;
      *)
        error_msg "Unsupported distribution!"
    esac
fi

cp ./html/* /var/www/html/

# Test Apache.
curl -o "${OUTPUT}/index.html" "http://localhost/index.html"
grep "Test Page for the Apache HTTP Server" "${OUTPUT}/index.html"
check_return "apache2-test-page"

# Test MySQL.
mysqladmin -u root password lamptest  > /dev/null 2>&1 || true
mysql --user="root" --password="lamptest" -e "show databases"
check_return "mysql-show-databases"

# Test PHP.
curl -o "${OUTPUT}/phpinfo.html" "http://localhost/info.php"
grep "PHP Version" "${OUTPUT}/phpinfo.html"
check_return "phpinfo"

# PHP Connect to MySQL.
curl -o "${OUTPUT}/connect-db" "http://localhost/connect-db.php"
grep "Connected successfully" "${OUTPUT}/connect-db"
exit_on_fail "php-connect-db"

# PHP Create a MySQL Database.
curl -o "${OUTPUT}/create-db" "http://localhost/create-db.php"
grep "Database created successfully" "${OUTPUT}/create-db"
check_return "php-create-db"

# PHP Create MySQL table.
curl -o "${OUTPUT}/create-table" "http://localhost/create-table.php"
grep "Table MyGuests created successfully" "${OUTPUT}/create-table"
check_return "php-create-table"

# PHP add record to MySQL table.
curl -o "${OUTPUT}/add-record" "http://localhost/add-record.php"
grep "New record created successfully" "${OUTPUT}/add-record"
check_return "php-add-record"

# PHP select record from MySQL table.
curl -o "${OUTPUT}/select-record" "http://localhost/select-record.php"
grep "id: 1 - Name: John Doe" "${OUTPUT}/select-record"
check_return "php-select-record"

# PHP delete record from MySQL table.
curl -o "${OUTPUT}/delete-record" "http://localhost/delete-record.php"
grep "Record deleted successfully" "${OUTPUT}/delete-record"
check_return "php-delete-record"

# Delete myDB for the next run.
mysql --user='root' --password='lamptest' -e 'DROP DATABASE myDB'