-
Notifications
You must be signed in to change notification settings - Fork 2
/
add-php
executable file
·94 lines (78 loc) · 2.46 KB
/
add-php
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
#!/bin/bash
#
# Add a new site with support for php
# Ensure running as root
if [ "$(id -u)" -ne 0 ]; then
echo 'This script is designed to run as root'
exit
fi
# Check arguments
if [ $# -lt 2 ]; then
echo 'Syntax: add-php <site-name> <domain>'
exit
fi
# Include common functions
source "${0%/*}/common"
# Determine site information. Separate domain name and port from second argument.
SITENAME=$1
SITEDOMAIN=$2
# Check number of dots in domain and use if we should add a www subdomain
DOTCOUNT=$(awk -F. '{print NF-1}' <<<"$SITEDOMAIN")
SERVERNAME=$SITEDOMAIN
if [ $DOTCOUNT -le 1 ]; then
SERVERNAME="$SERVERNAME www.$SITEDOMAIN"
fi
# Determine site directory and user
DESTDIR=/var/www/$SITENAME
SITE_USER='www-data'
SITE_GROUP='www-data'
SOCKET_PATH='/var/run/php/php7.2-fpm.sock'
# Create destination directories
sub_dirs=(wwwroot logs)
for subdir in "${sub_dirs[@]}"; do
if [ ! -d "$DESTDIR/$subdir" ]; then
mkdir -p "$DESTDIR/$subdir"
fi
done
# Create nginx configuration
# Rename any existing file before creating the new one
if [ -f /etc/nginx/sites-available/$SITENAME.conf ]; then
mv /etc/nginx/sites-available/$SITENAME.conf /etc/nginx/sites-available/$SITENAME.conf.old
fi
tee /etc/nginx/sites-available/$SITENAME.conf > /dev/null << EOF
server {
listen 80;
server_name $SERVERNAME;
access_log $DESTDIR/logs/access.log;
error_log $DESTDIR/logs/error.log;
root $DESTDIR/wwwroot;
index index.php index.html;
client_max_body_size 20M;
location / {
try_files \$uri \$uri/ /index.php$is_args$args;
}
location ~ .php\$ {
fastcgi_pass unix:$SOCKET_PATH;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $DESTDIR/wwwroot\$fastcgi_script_name;
fastcgi_param SITE_NAME "$SITENAME";
include fastcgi_params;
}
}
EOF
ln -sf /etc/nginx/sites-available/$SITENAME.conf /etc/nginx/sites-enabled/$SITENAME.conf
# Set permissions for configuration file
chmod 0660 /etc/nginx/sites-available/$SITENAME.conf
# Copy template files and set permissions on site content
cp -R "$(dirname $0)/php-template/." "$DESTDIR/wwwroot"
set_site_permissions $DESTDIR $SITE_USER $SITE_GROUP
# Check that nginx is happy with configuration
nginx -t
if [ $? -ne 0 ]; then
echo 'Nginx reported error in configuration'
exit
fi
nginx -s reload
# Create Letsencrypt certificate
create_certificate $SITENAME $SITEDOMAIN $DOTCOUNT
echo "Created php site $SITENAME successfully at $DESTDIR"