forked from vladgh/VladGh.com-LEMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·499 lines (433 loc) · 15.1 KB
/
install.sh
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/bin/bash
### Ubuntu LEMP Install Script --- VladGh.com
#
####################
### LICENSE: ###
####################
# This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
# To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
# or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
#
###################
### DISCLAIMER: ###
###################
# All content provided here including the scripts is provided without
# any warranty. You use it at your own risk. I can not be held responsible
# for any damage that may occur because of it. By using the scripts I
# provide here you accept this terms.
#
# Please bear in mind that this method is intended for development
# and testing purposes only. If you care about stability and security
# you should use the packages provided by your distribution.
### Program Versions:
NGINX_STABLE="1.0.12"
NGINX_DEV="1.1.15"
PHP_VER="5.3.10"
APC_VER="3.1.9"
SUHOSIN_VER="0.9.33"
### Directories
DSTDIR="/opt"
WEBDIR="/var/www"
SRCDIR=`dirname $(readlink -f $0)`
TMPDIR="$SRCDIR/sources"
### Log file
LOG_FILE="install.log"
### Active user
USER=$(who mom likes | awk '{print $1}')
### Essential Packages
ESSENTIAL_PACKAGES="htop vim-nox binutils cpp flex gcc libarchive-zip-perl libc6-dev libcompress-zlib-perl m4 libpcre3 libpcre3-dev libssl-dev libpopt-dev lynx make perl perl-modules openssl unzip zip autoconf2.13 gnu-standards automake libtool bison build-essential zlib1g-dev ntp ntpdate autotools-dev g++ bc subversion psmisc"
### PHP Libraries
PHP_LIBRARIES="libmysqlclient-dev libcurl4-openssl-dev libgd2-xpm-dev libjpeg62-dev libpng3-dev libxpm-dev libfreetype6-dev libt1-dev libmcrypt-dev libxslt1-dev libbz2-dev libxml2-dev libevent-dev libltdl-dev libmagickwand-dev imagemagick libreadline-dev libc-client-dev libsnmp-dev snmpd snmp"
function progress() {
# Simple progress indicator at the end of line (followed by "Done" when command is completed)
while ps |grep $!; do
echo -en "\b-" >&3; sleep 1
echo -en "\b\\" >&3; sleep 1
echo -en "\b|" >&3; sleep 1
echo -en "\b/" >&3; sleep 1
done
echo -e '\E[47;34m\b\b\b\b'"Done" >&3; tput sgr0 >&3
}
function prepare_system() {
# Upgrading APT-GET
echo "Updating apt-get..." >&3
apt-get -y update & progress
# Install essential packages for Ubuntu
echo "Installing dependencies..." >&3
apt-get -y install $ESSENTIAL_PACKAGES & progress
# Create temporary folder for the sources
if [ -d $TMPDIR ]; then
rm -r $TMPDIR
else
mkdir $TMPDIR
fi
}
function check_download () {
# Simple function to check if the download and extraction finished successfully.
if [ -e "$2" ] ; then
echo -e '\E[47;34m'"$1 download and extraction was successful." >&3; tput sgr0 >&3
else
echo "Error: $1 Download was unsuccessful." >&3
echo "Check the install.log for errors." >&3
echo "Press any key to exit..." >&3
read -n 1
exit 1
fi
}
function install_mysql() {
# Installing MySQL server (this is escaped in order to be able to type the password in the initial dialog)
echo "Installing MySQL..." >&3
env DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server mysql-client & progress
mysql_secure_installation >&3
}
function install_php() {
# Install all PHP Libraries
echo "Installing PHP libraries..." >&3
apt-get -y install $PHP_LIBRARIES & progress
# Get PHP package
echo "Downloading and extracting PHP-$PHP_VER..." >&3
cd $TMPDIR
wget "http://us.php.net/distributions/php-$PHP_VER.tar.gz" & progress
tar xzvf php-$PHP_VER.tar.gz
check_download "PHP5" "$TMPDIR/php-$PHP_VER.tar.gz"
### Fix Ubuntu 11.04 LIB PATH ###
[ -f /usr/lib/x86_64-linux-gnu/libjpeg.so ] && ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
[ -f /usr/lib/i386-linux-gnu/libjpeg.so ] && ln -s /usr/lib/i386-linux-gnu/libjpeg.so /usr/lib/libjpeg.so
[ -f /usr/lib/x86_64-linux-gnu/libpng.so ] && ln -s /usr/lib/x86_64-linux-gnu/libpng.so /usr/lib/libpng.so
[ -f /usr/lib/i386-linux-gnu/libpng.so ] && ln -s /usr/lib/i386-linux-gnu/libpng.so /usr/lib/libpng.so
[ -d /usr/lib/x86_64-linux-gnu/mit-krb5 ] && ln -s /usr/lib/x86_64-linux-gnu/mit-krb5/lib*.so /usr/lib/
[ -d /usr/lib/i386-linux-gnu/mit-krb5 ] && ln -s /usr/lib/i386-linux-gnu/mit-krb5/lib*.so /usr/lib/
##################################
# Compile php source
cd $TMPDIR/php-$PHP_VER
./buildconf --force
echo "Configuring PHP (Please be patient, this will take a while...)" >&3
./configure \
--prefix=$DSTDIR/php5 \
--with-config-file-path=/etc/php5 \
--with-config-file-scan-dir=/etc/php5/conf.d \
--with-curl \
--with-pear \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-xpm-dir \
--with-freetype-dir \
--with-t1lib \
--with-mcrypt \
--with-mhash \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-openssl \
--with-xmlrpc \
--with-xsl \
--with-bz2 \
--with-gettext \
--with-readline \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-snmp \
--disable-debug \
--enable-fpm \
--enable-cli \
--enable-inline-optimization \
--enable-exif \
--enable-wddx \
--enable-zip \
--enable-bcmath \
--enable-calendar \
--enable-ftp \
--enable-mbstring \
--enable-soap \
--enable-sockets \
--enable-sqlite-utf8 \
--enable-shmop \
--enable-dba \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg & progress
echo "Compiling PHP (Please be patient, this will take a while...)" >&3
make -j8 & progress
echo "Installing PHP..." >&3
make install & progress
# Copy configuration files
echo 'Setting up PHP...' >&3
sed -i "s~^INSTALL_DIR=.$~INSTALL_DIR=\"$DSTDIR/php5\"~" $SRCDIR/init_files/php5-fpm
mkdir -p /etc/php5/conf.d /var/log/php5-fpm
cp -f php.ini-production /etc/php5/php.ini
cp $SRCDIR/conf_files/php-fpm.conf /etc/php5/php-fpm.conf
cp $SRCDIR/init_files/php5-fpm /etc/init.d/php5-fpm
chmod +x /etc/init.d/php5-fpm
update-rc.d -f php5-fpm defaults
# The newer versions of php complain if a time zone is not set on php.ini (so we grab the system's one)
TIMEZONE=$([ -f /etc/timezone ] && cat /etc/timezone | sed "s/\//\\\\\//g")
sed -i "s/^\;date\.timezone.*$/date\.timezone = \"${TIMEZONE}\" /g" /etc/php5/php.ini
chown -R www-data:www-data /var/log/php5-fpm & progress
# Create log rotation script
echo 'Creating logrotate script...' >&3
echo '/var/log/php5-fpm/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 www-data www-data
sharedscripts
postrotate
[ ! -f /var/run/php5-fpm.pid ] || kill -USR1 `cat /var/run/php5-fpm.pid`
endscript
}' > /etc/logrotate.d/php5-fpm
echo -e '\E[47;34m\b\b\b\b'"Done" >&3; tput sgr0 >&3
}
function install_apc() {
# Get APC package
echo "Downloading and extracting APC-$APC_VER..." >&3
cd $TMPDIR
wget "http://pecl.php.net/get/APC-$APC_VER.tgz" & progress
tar xzvf APC-$APC_VER.tgz
check_download "APC" "$TMPDIR/APC-$APC_VER.tgz"
cd $TMPDIR/APC-$APC_VER
# Compile APC source
echo 'Configuring APC...' >&3
$DSTDIR/php5/bin/phpize -clean
./configure --enable-apc --with-php-config=$DSTDIR/php5/bin/php-config --with-libdir=$DSTDIR/php5/lib/php & progress
echo 'Compiling APC...' >&3
make -j8 & progress
echo 'Installing APC...' >&3
make install
# Copy configuration files
echo 'extension = apc.so
apc.enabled = 1
apc.shm_size = 128M
apc.shm_segments=1
apc.write_lock = 1
apc.rfc1867 = On
apc.ttl=7200
apc.user_ttl=7200
apc.num_files_hint=1024
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.enable_cli=1
; Optional, for "[apc-warning] Potential cache slam averted for key... errors"
; apc.slam_defense = Off
' > /etc/php5/conf.d/apc.ini
echo -e '\E[47;34m\b\b\b\b'"Done" >&3; tput sgr0 >&3
}
function install_suhosin() {
#Get Suhosin packages
echo "Downloading and extracting Suhosin-$SUHOSIN_VER..." >&3
cd $TMPDIR
wget "http://download.suhosin.org/suhosin-$SUHOSIN_VER.tgz" & progress
tar zxvf suhosin-$SUHOSIN_VER.tgz
check_download "Suhosin" "$TMPDIR/suhosin-$SUHOSIN_VER.tgz"
cd $TMPDIR/suhosin-$SUHOSIN_VER
# Compile Suhosin source
echo 'Configuring Suhosin...' >&3
$DSTDIR/php5/bin/phpize -clean
./configure --with-php-config=$DSTDIR/php5/bin/php-config --with-libdir=$DSTDIR/php5/lib/php & progress
echo 'Compiling Suhosin...' >&3
make -j8 & progress
echo 'Installing Suhosin...' >&3
make install
# Copy configuration files
echo '; Suhosin Extension
extension = suhosin.so' > /etc/php5/conf.d/suhosin.ini
echo -e '\E[47;34m\b\b\b\b'"Done" >&3; tput sgr0 >&3
}
function check_php () {
# Check if the PHP executable exists and has the APC and Suhosin modules compiled.
if [ -x "$DSTDIR/php5/bin/php" ] && [ $($DSTDIR/php5/bin/php -m | grep apc) ] && [ $($DSTDIR/php5/bin/php -m | grep suhosin) ] ; then
echo "=========================================================================" >&3
echo 'PHP with APC and Suhosin was successfully installed.' >&3
$DSTDIR/php5/bin/php -v >&3
echo "=========================================================================" >&3
else
echo 'Error: PHP installation was unsuccessful.' >&3
echo "Check the install.log for errors." >&3
echo 'Press any key to exit...' >&3
read -n 1
exit 1
fi
}
function install_nginx() {
#Get NginX package
echo "Downloading and extracting nginx-$NGINX_VER..." >&3
mkdir $WEBDIR;
cd $TMPDIR
wget "http://nginx.org/download/nginx-$NGINX_VER.tar.gz" & progress
tar zxvf nginx-$NGINX_VER.tar.gz
check_download "NginX" "$TMPDIR/nginx-$NGINX_VER.tar.gz"
cd $TMPDIR/nginx-$NGINX_VER/
# Compile php source
echo 'Configuring NginX...' >&3
./configure --prefix=$DSTDIR/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module & progress
echo 'Compiling NginX...' >&3
make -j8 & progress
echo 'Installing NginX...' >&3
make install
# Copy configuration files
sed -i "s~^INSTALL_DIR=.$~INSTALL_DIR=\"$DSTDIR/nginx\"~" $SRCDIR/init_files/nginx
cp $SRCDIR/init_files/nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
update-rc.d -f nginx defaults
cp $SRCDIR/conf_files/nginx.conf /etc/nginx/nginx.conf
mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
cp $SRCDIR/conf_files/default /etc/nginx/sites-available/default
ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default
cp $SRCDIR/ext/nxensite $DSTDIR/nginx/sbin/nxensite
cp $SRCDIR/ext/nxdissite $DSTDIR/nginx/sbin/nxdissite
chmod +x $DSTDIR/nginx/sbin/*
cp $SRCDIR/web_files/* $WEBDIR
echo -e '\E[47;34m\b\b\b\b'"Done" >&3; tput sgr0 >&3
# Create log rotation script
echo 'Creating logrotate script...' >&3
chown -R www-data:www-data /var/log/nginx
echo '/var/log/nginx/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}' > /etc/logrotate.d/nginx
}
function check_nginx () {
# Check if Nginx exists and is executable and display the version.
if [ -x "$DSTDIR/nginx/sbin/nginx" ] ; then
echo "=========================================================================" >&3
echo 'NginX was successfully installed.' >&3
$DSTDIR/nginx/sbin/nginx -v >&3
echo "=========================================================================" >&3
else
echo 'Error: NginX installation was unsuccessful.' >&3
echo "Check the install.log for errors." >&3
echo 'Press any key to exit...' >&3
read -n 1
exit 1
fi
}
function set_paths() {
# Make the NginX and PHP paths global.
echo 'Setting up paths...' >&3
export PATH="$PATH:$DSTDIR/nginx/sbin:$DSTDIR/php5/bin:$DSTDIR/php5/sbin"
echo "PATH=\"$PATH\"" > /etc/environment
source /etc/environment
}
function restart_servers() {
# Restart both NginX and PHP daemons
echo 'Restarting servers...' >&3
if [ $(ps -ef | egrep -c "(nginx|php-fpm)") -gt 1 ]; then
ps -e | egrep "(nginx|php)" | awk '{print $1}' | xargs sudo kill -INT
fi
sleep 2
/etc/init.d/php5-fpm start
/etc/init.d/nginx start
}
function check_root() {
# Check if you are root
if [ $(id -u) != "0" ]; then
echo "Error: You must be root to run this installer."
echo "Error: Please use 'sudo'."
exit 1
fi
}
function log2file() {
# Logging everything to LOG_FILE
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOG_FILE 2>&1
}
###################################################################################
### RUN ALL THE FUNCTIONS:
check_root
log2file
# Traps CTRL-C
trap ctrl_c INT
function ctrl_c() {
echo -e '\nCancelled by user' >&3; echo -e '\nCancelled by user'; if [ -n "$!" ]; then kill $!; fi; exit 1
}
clear >&3
echo "=========================================================================" >&3
echo "This script will install the following:" >&3
echo "=========================================================================" >&3
echo " - Nginx $NGINX_DEV (development) or $NGINX_STABLE (stable);" >&3
echo " - PHP $PHP_VER;" >&3
echo " - APC $APC_VER;" >&3
echo " - Suhosin $SUHOSIN_VER;" >&3
echo "=========================================================================" >&3
echo "For more information please visit:" >&3
echo "https://github.com/vladgh/VladGh.com-LEMP" >&3
echo "=========================================================================" >&3
echo "Do you want to continue[Y/n]:" >&3
read continue_install
case $continue_install in
'n'|'N'|'No'|'no')
echo -e "\nCancelled." >&3
exit 1
;;
*)
esac
echo "Which of the following NginX releases do you want installed:" >&3
echo "1) Latest Development Release ($NGINX_DEV)(default)" >&3
echo "2) Latest Stable Release ($NGINX_STABLE)" >&3
echo -n "Enter your menu choice [1 or 2]: " >&3
read nginxchoice
case $nginxchoice in
1) NGINX_VER=$NGINX_DEV ;;
2) NGINX_VER=$NGINX_STABLE ;;
*) NGINX_VER=$NGINX_DEV ;
esac
prepare_system
install_mysql
install_php
install_apc
install_suhosin
check_php
install_nginx
check_nginx
set_paths
restart_servers
chown -R $USER:$USER $SRCDIR
rm -r $TMPDIR
sleep 5
### Final check
if [ -e "/var/run/nginx.pid" ] && [ -e "/var/run/php-fpm.pid" ] ; then
echo "=========================================================================" >&3
echo 'NginX, PHP, APC and Suhosin were successfully installed.' >&3
echo "If your hosts are setup correctly you should be able to see some stats at:" >&3
echo "- http://$(hostname -f)/index.php (PHP Status page)" >&3
echo "- http://$(hostname -f)/apc.php (APC Status page)" >&3
echo "- http://$(hostname -f)/nginx_status (NginX Status page)" >&3
echo "- http://$(hostname -f)/status?html (FPM Status page)" >&3
echo 'Press any key to exit...' >&3
read -n 1
exit 0
else
echo "=========================================================================" >&3
echo "Errors encountered. Check the install.log." >&3
echo 'Press any key to exit...' >&3
read -n 1
exit 1
fi