-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·166 lines (141 loc) · 3.99 KB
/
setup.sh
File metadata and controls
executable file
·166 lines (141 loc) · 3.99 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env bash
# Check root
if [[ `id -u` -ne 0 ]]; then
echo "Setup must be run as root user."
exit 1
fi
# Setup vars
curdir=`pwd`
appdir=$(realpath "$(dirname $0)")
countpy_svc="/etc/systemd/system/countpy.service"
redis_svc="/etc/systemd/system/redis.service"
countpy_fqdn="countpy.com"
countpy_conf="/etc/nginx/sites-available/$countpy_fqdn"
# Install packages
packages=""
dpkg --list | grep build-essential &> /dev/null || packages="${packages} build-essential"
dpkg --list | grep python3.5 &> /dev/null || packages="${packages} python3.5"
dpkg --list | grep python3-dev &> /dev/null || packages="${packages} python3-dev"
dpkg --list | grep tcl &> /dev/null || packages="${packages} tcl"
dpkg --list | grep nodejs &> /dev/null || packages="${packages} nodejs"
dpkg --list | grep npm &> /dev/null || packages="${packages} npm"
dpkg --list | grep git-core &> /dev/null || packages="${packages} git-core"
dpkg --list | grep nginx &> /dev/null || packages="${packages} nginx"
if [[ ! -z "${packages}" ]]; then
apt-get update && apt-get install -y ${packages}
fi
# Install pip
if ! which pip3 &> /dev/null; then
cd /tmp
curl -O https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py
rm -f get-pip.py
cd $curdir
else
pip3 install pip --upgrade
fi
# Install python libraries
pip3 install -r $appdir/requirements.txt --upgrade
if ! which uwsgi &> /dev/null; then
echo "uWSGI is not installed"
exit 1
fi
# Install bower
if ! which bower &> /dev/null; then
npm install -g bower
fi
# Backward compatibility for nodejs
[ -f /usr/bin/node ] || ln -s `which nodejs` /usr/bin/node
# Install static libraries
cd $appdir/app
bower install --allow-root
cd $curdir
# Install redis
if ! systemctl status redis &> /dev/null; then
cd /tmp
curl -O http://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz
cd redis-stable
make
make test
make install
mkdir /etc/redis
cp redis.conf /etc/redis
sed -ri 's/^supervised .+/supervised systemd/g' /etc/redis/redis.conf
sed -ri 's:^dir .+:dir /var/lib/redis:g' /etc/redis/redis.conf
adduser --system --group --no-create-home redis
mkdir /var/lib/redis
chown redis:redis /var/lib/redis
cat > $redis_svc <<EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cd $curdir
rm -rf /tmp/redis-stable*
grep vm.overcommit_memory /etc/sysctl.conf > /dev/null || \
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
grep net.core.somaxconn /etc/sysctl.conf > /dev/null || \
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf
sysctl -p
systemctl daemon-reload
systemctl enable redis
systemctl start redis
fi
# Install countpy
if ! systemctl status countpy &> /dev/null; then
adduser --system --group --no-create-home countpy
chown -R countpy.www-data $appdir
uwsgi_cmd=`which uwsgi`
cat > $countpy_svc <<EOF
[Unit]
Description=uWSGI Server for countpy app
After=network.target
[Service]
User=countpy
Group=www-data
ExecStart=$uwsgi_cmd --ini $appdir/wsgi.ini
WorkingDirectory=$appdir
Restart=always
KillSignal=SIGTERM
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable countpy
systemctl start countpy
fi
# Configure nginx
[ -f /etc/nginx/sites-enabled/default ] && rm -f /etc/nginx/sites-enabled/default
if [ ! -f $countpy_conf ]; then
cat > $countpy_conf <<EOF
server {
listen 80;
server_name $countpy_fqdn www.$countpy_fqdn;
location /static {
alias $appdir/static;
}
location / {
include uwsgi_params;
uwsgi_pass unix:///$appdir/countpy.sock;
}
}
EOF
ln -s $countpy_conf /etc/nginx/sites-enabled/$countpy_fqdn
fi
# Restart nginx
systemctl enable nginx
systemctl restart nginx
# Finished
echo "Done!"