在 Debian 6 上安裝 Apache, Percona 和 PHP


閒來無事.重裝了一下要用的主機環境.
順道來記錄一下

  • Apache 的 2.4.3 版本
  • Percona 的 5.5 版本 (取代 MySQL)
  • PHP 的 5.4.11 版本 (玩玩新的)
  • Memcached 的 1.4.15 版本
  • Redis 的 2.6.0 版本
  • eAccelerator 的 Dev 版本
  • Suhosin 的 Dev 版本
  • ImageMagic 的 3.1 版本
  • ProFTPD 的 1.3.4b 版本
  • iptables 的簡單設定
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# 查看目前的版本
cat /etc/debian_version

# 編輯更新源
vim /etc/apt/sources.list

deb http://ftp.debian.org/debian/ squeeze main non-free contrib
deb http://ftp.debian.org/debian/ squeeze-proposed-updates main non-free contrib

deb http://ftp.us.debian.org/debian/ squeeze main non-free contrib
deb http://ftp.us.debian.org/debian/ squeeze-proposed-updates main non-free contrib

# Offical source
deb http://security.debian.org/ squeeze/updates main

# 163 source
deb http://mirrors.163.com/debian/ squeeze main non-free contrib
deb http://mirrors.163.com/debian/ squeeze-proposed-updates main non-free contrib
deb http://mirrors.163.com/debian-security/ squeeze/updates main non-free contrib

# 設置 LC_ALL 出錯
locale -a
vim /etc/locale.gen

#en_US.UTF-8 UTF-8 => en_US.UTF-8 UTF-8

locale-gen
locale -a

# 更新和升級舊的套件
apt-get update
apt-get dist-upgrade

# 刪除原有的 apache
apt-get --purge remove apache2
apt-get --purge remove apache2.2-common
apt-get --purge remove apache2-utils

# 設置時區並檢查是否已更新
cp /usr/share/zoneinfo/Hongkong /etc/localtime && date -u
date

# 改 bash 為 zsh
apt-get install git
apt-get install zsh
wget --no-check-certificate https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh
chsh -s /bin/zsh
su - root

# 加入 htop 指令
apt-get install htop

# 改變 SSH 端口和禁止 root 登入
vim /etc/ssh/sshd_config

Port 22 => Port 3333
PermitRootLogin yes => PermitRootLogin no

ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM no

# 提升登錄速度
UseDNS no

# 建立新的用戶
useradd Your_User
passwd Your_User
mkdir /home/Your_User
chown Your_User:Your_User /home/Your_User
chmod 755 /home/Your_User

# 重啟 SSH
service ssh restart

# 開新連線作測試,並設置 zsh (同上)
ssh Your_User@Your_IP -p 3333

# 加入免密碼登入 SSH 的公匙到驗證文件中
# 在本機的生成方法: ssh-keygen -t rsa -f ~/.ssh/The_Name_Of_File
cd ~
mkdir ~/.ssh && chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
echo "THE_KEY_CONTENT" >> ~/.ssh/authorized_keys
chmod 400 ~/.ssh/authorized_keys

# 在本機中加入登入資料
Host 198.23.243.125
IdentityFile ~/.ssh/id_rsa.The_Name_Of_File
User Your_User
Port 3333

# 再開新連線作測試是否成功
ssh Your_IP

# 切換成 root
su - root

# 建立暫時目錄
cd ~ && mkdir server && cd server

# 為 Apache 準備,安裝 APR (Apache Portable Runtime)
apt-get install libapr1-dev
apt-get install libaprutil1-dev

# 為 Percona 準備,安裝 cmake
apt-get install cmake
apt-get install libncurses5-dev
apt-get install g++
apt-get install bison
apt-get install libaio-dev

# 為 PHP 準備
apt-get install libxml2
apt-get install libxml2-dev
apt-get install libjpeg-dev
apt-get install libpng-dev
apt-get install libfreetype6-dev
apt-get install libc-client-dev
apt-get install libmcrypt-dev
apt-get install libreadline5
apt-get install libreadline5-dev
apt-get install libtidy-dev
apt-get install libcurl4-dev

# 為 Memcached 準備
apt-get install libevent-dev
apt-get install autoconf

# 為 ImageMagic 準備
apt-get install re2c
apt-get install imagemagick
apt-get install libmagick9-dev

# 下載和安裝 Apache
groupadd www && useradd -g www -s /sbin/nologin www
wget http://ftp.cuhk.edu.hk/pub/packages/apache.org/httpd/httpd-2.4.3.tar.gz
tar zxvf httpd-2.4.3.tar.gz
cd httpd-2.4.3
./configure --prefix=/usr/local/apache2 --enable-load-all-modules --enable-suexec --with-suexec-docroot=/home
make && make install && make clean
chown -R root:www /usr/local/apache2

# 切到安裝目錄,建立儲存位置,增加 Percona 用戶和設置權限
cd ..
mkdir -p /usr/local/mysql/data
groupadd mysql && useradd mysql -g mysql -s /sbin/nologin
chown -R mysql.mysql /usr/local/mysql/data

# 下載和安裝 Percona
wget http://www.percona.com/redir/downloads/Percona-Server-5.5/LATEST/source/Percona-Server-5.5.28-rel29.3.tar.gz
tar zxvf Percona-Server-5.5.28-rel29.3.tar.gz
cd Percona-Server-5.5.28-rel29.3
cmake . -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_CONFIG=mysql_release -DFEATURE_SET=community -DWITH_EMBEDDED_SERVER=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_USER=mysql
make && make install

# 設定和建立 Percona 資料表
cp support-files/my-medium.cnf /etc/my.cnf
chown -R mysql:mysql /etc/my.cnf
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
ls /usr/local/mysql/data
chown -R root /usr/local/mysql
chown -R mysql /usr/local/mysql/data
chown root:mysql /var/run/mysqld

# 啟動 Percona
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

# 查看 Percona 啟動狀態
ps -ef|grep mysql

# 更新 root 的密碼
# - 如想要省去 --socket 參數,可以參考下面的解決方法
/usr/local/mysql/bin/mysqladmin -u root --socket=/tmp/mysql.sock password 'The_Password'

# 安裝 PHP
cd ..
wget http://downloads.php.net/stas/php-5.4.11.tar.gz
tar zxvf php-5.4.11.tar.gz
cd php-5.4.11
./configure --prefix=/usr/local/php-5.4 --with-apxs2=/usr/local/apache2/bin/apxs --enable-fpm --with-openssl --with-zlib --with-bz2 --enable-bcmath --with-curl --with-curlwrappers --enable-exif --enable-ftp --with-gd --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-imap --with-imap-ssl --enable-mbstring --with-mcrypt --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-pcntl --with-pdo-mysql --with-readline --enable-soap --enable-sockets --with-tidy --with-xmlrpc --enable-zip --enable-mysqlnd --with-kerberos --disable-fileinfo
make && make install
cp php.ini-production /usr/local/php-5.4/lib/php.ini

# 安裝 Memcached
cd ..
wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
tar zxvf memcached-1.4.15.tar.gz
cd memcached-1.4.15
./configure --prefix=/usr/local/memcached --enable-64bit
make && make install

# 安裝 Memcache 的 PHP Extension
cd ..
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php-5.4/bin/phpize
./configure --with-php-config=/usr/local/php-5.4/bin/php-config
make && make install

# 安裝 Redis
cd ..
wget http://redis.googlecode.com/files/redis-2.6.9.tar.gz
tar zxvf redis-2.6.9.tar.gz
cd redis-2.6.9
make PREFIX=/usr/local/redis
make install PREFIX=/usr/local/redis
mkdir /usr/local/redis/{conf,log,snapshot}
cp redis.conf /usr/local/redis/conf

# 安裝 Redis 的 PHP Extension
cd ..
wget https://github.com/nicolasff/phpredis/tarball/master -O phpredis-latest.tar.gz
tar zxvf phpredis-latest.tar.gz
cd nicolasff-phpredis-*
/usr/local/php-5.4/bin/phpize
./configure --with-php-config=/usr/local/php-5.4/bin/php-config
make && make install

# 安裝 eAccelerator
cd ..
wget https://github.com/eaccelerator/eaccelerator/tarball/master -O eaccelerator-latest.tar.gz
tar zxvf eaccelerator-latest.tar.gz
cd eaccelerator-eaccelerator-*
/usr/local/php-5.4/bin/phpize
./configure --with-php-config=/usr/local/php-5.4/bin/php-config
make && make install

# 建立 eAccelerator 緩存目錄
mkdir /tmp/eaccelerator_cache
chmod 0777 /tmp/eaccelerator_cache

# 安裝 Suhosin Dev
cd ..
wget https://github.com/stefanesser/suhosin/tarball/master -O suhosin-latest.tar.gz
tar zxvf suhosin-latest.tar.gz
cd stefanesser-suhosin-*
/usr/local/php-5.4/bin/phpize
./configure --with-php-config=/usr/local/php-5.4/bin/php-config
make && make install

# 安裝 ImageMagic
cd ..
wget http://pecl.php.net/get/imagick-3.1.0RC2.tgz
tar zxvf imagick-3.1.0RC2.tgz
cd imagick-3.1.0RC2
/usr/local/php-5.4/bin/phpize
./configure --with-php-config=/usr/local/php-5.4/bin/php-config
make && make install

# 安裝 ProFTPD
cd ..
groupadd ftp && useradd ftp -g ftp -s /sbin/nologin
wget ftp://ftp.proftpd.org/distrib/source/proftpd-1.3.4b.tar.gz
tar zxvf proftpd-1.3.4b.tar.gz
cd proftpd-1.3.4b
./configure --prefix=/usr/local/proftpd --enable-openssl
make && make install

# 設置 iptables
# 查看目前的 iptables 設置 (沒有任何規則)
iptables -L

# 建立新的規則文件
vim /etc/iptables.test.rules

*filter

# Clean all rules
-F

# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT

# Accepts all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allows all outbound traffic
# You could modify this to only allow certain traffic
-A OUTPUT -j ACCEPT

# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Allows SSH connections
# THE -dport NUMBER IS THE SAME ONE YOU SET UP IN THE SSHD_CONFIG FILE
-A INPUT -p tcp -m state --state NEW --dport 1688 -j ACCEPT

# Now you should read up on iptables rules and consider whether ssh access
# for everyone is really desired. Most likely you will only allow access from certain IPs.
-A INPUT -p tcp --dport 21 -j ACCEPT
-A INPUT -p tcp --dport 60000:65535 -j ACCEPT

#-A INPUT -p tcp --dport 53 -j ACCEPT
#-A INPUT -p udp --dport 53 -j ACCEPT
#-A INPUT -p tcp --dport 25 -j ACCEPT
#-A INPUT -p tcp --dport 110 -j ACCEPT
#-A INPUT -p tcp --dport 143 -j ACCEPT

# Allow ping
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# log iptables denied calls (access via 'dmesg' command)
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# Reject all other inbound - default deny unless explicitly allowed policy:
-A INPUT -j REJECT
-A FORWARD -j REJECT

COMMIT

# 將建立的規則加載到 iptables
iptables-restore < /etc/iptables.test.rules

# 再次查看新的 iptables 設置
iptables -L

# 保存目前的設置.給予重啟時使用
iptables-save > /etc/iptables.up.rules

# 在重啟後自動加載規則
vim /etc/network/if-pre-up.d/iptables

#!/bin/bash
/sbin/iptables-restore < /etc/iptables.up.rules

chmod +x /etc/network/if-pre-up.d/iptables

# 修改 Redis 的 redis.conf
vim /usr/local/redis/conf/redis.conf

daemonize yes
logfile /usr/local/redis/log/redis.log
dir /usr/local/redis/snapshot

# 修改 Proftpd 的 proftpd.conf
vim /usr/local/proftpd/etc/proftpd.conf

# 修改成以下內容
ServerName "The FTP Provider"
User ftp
Group ftp

# 刪除前方注解
DefaultRoot ~

# 在下方加入
RequireValidShell off
DelayEngine off
RootLogin off
UseReverseDNS off
ServerIdent off
PassivePorts 60000 65535

# 修改 Apache 的 httpd.conf
vim /usr/local/apache2/conf/httpd.conf

# 移除前方注解並,將換成自己的 IP 或 Domain
ServerName Your_IP_OR_Domain:80

# 改變執行者的身份
User daemon => User www
Group daemon => Group www

# 移除前方注解並加上 .pl
AddHandler cgi-script .cgi .pl

# 在 AddHandler cgi-script 下面加入
AddHandler php5-script .php

# 移除注解以下注解
#Include conf/extra/httpd-vhosts.conf
#LoadModule slotmem_shm_module modules/mod_slotmem_shm.so

# 加入預設的 index 檔名
DirectoryIndex index.html index.php index.pl index.cgi

# 在 LoadModule 下面加入語句,防止 iframe 跨域攻擊等等
Header always append X-Frame-Options SAMEORIGIN
Header always append X-XSS-Protection "1; mode=block"
Header always append X-Content-Type-Options nonsniff
Header unset Server

# 修改 Apache 的 httpd-vhosts.conf
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf

<virtualhost *:80>
ServerAdmin [email protected]
DocumentRoot "/home/user/domain.com/public_html"
ServerName domain.com
ServerAlias www.domain.com
ErrorLog "logs/www.domain.com-error_log"
CustomLog "logs/www.domain.com-access_log" common
<directory "/home/user/domain.com/public_html">
Require all granted
</directory>
</virtualhost>

<virtualhost *:80>
DocumentRoot "/home/user/sub.domain.com/public_html"
ServerName sub.domain.com
ErrorLog "logs/sub.domain.com-error_log"
CustomLog "logs/sub.domain.com-access_log" common
<directory "/home/user/domain.com/public_html">
Require all granted
</directory>
</virtualhost>

# 修改 PHP 的 php.ini
vim /usr/local/php-5.4/lib/php.ini

;date.timezone = => date.timezone = Asia/Hong_Kong
display_errors Off => display_errors On
upload_max_filesize = 2M => upload_max_filesize = 8M
disable_functions = => disable_functions = passthru,system,proc_open,popen,curl_multi_exec,parse_ini_file,show_source,dl,pclose,proc_nice,proc_terminate,proc_get_status,proc_close,leak,apache_child_terminate,shell-exec,exec,crack_check,crack_closedict,crack_getlastmessage,crack_opendict,psockopen,php_u,symlink,ini_restore,posix_getpwuid,system,pfsockopen,readfile,escapeshellarg,escapeshellcmd,proc_close,ini_alter,parse_ini_file,pcntl_exec,posix_kill,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,copy,curl_exec,tempnam,shell_exe

# 加入以下內容到 php.ini

extension_dir="/usr/local/php-5.4/lib/php/extensions/no-debug-zts-20100525/"
extension="memcache.so"
extension="suhosin.so"
extension="imagick.so"

[eaccelerator]
zend_extension="/usr/local/php-5.4/lib/php/extensions/no-debug-zts-20100525/eaccelerator.so"
eaccelerator.shm_size="1"
eaccelerator.cache_dir="/tmp/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.keys = "disk_only"
eaccelerator.sessions = "disk_only"
eaccelerator.content = "disk_only"

# 啟動 Memcached
/usr/local/memcached/bin/memcached -d -m 16 -l 127.0.0.1 -p 11211 -u root -c 64

# 啟動 Redis
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf

# 啟動 Apache
/usr/local/apache2/bin/apachectl start

# 啟動 FTP
/usr/local/proftpd/sbin/proftpd -c /usr/local/proftpd/etc/proftpd.conf

# 加入自動啟動列
vim /etc/rc.local

/usr/local/memcached/bin/memcached -d -m 16 -l 127.0.0.1 -p 11211 -u root -c 64
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &
/usr/local/proftpd/sbin/proftpd -c /usr/local/proftpd/etc/proftpd.conf
/usr/local/apache2/bin/apachectl start

---------------------------------------------

# Percona 的安裝過程問題記錄

出錯處理:
- rm CMakeCache.txt && make clean

參數參考:
- http://dev.mysql.com/doc/internals/en/autotools-to-cmake.html
- https://wikis.oracle.com/display/mysql/Autotools+to+CMake+Transition+Guide
- http://who0168.blog.51cto.com/253401/469898
- cmake . -LH

備注參考:
- myisam 已強制安裝

找不到 mysqld.sock 問題:
RROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

加入一個連結就可以解決:
ln -s /tmp/mysql.sock /var/run/mysqld/mysqld.sock

# PHP 的安裝過程問題記錄

問題: configure: error: jpeglib.h not found.
解決:
- 搜尋 aptitude search libjpeg
- 安裝 apt-get install libjpeg-dev

問題: make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
解決: 編譯時加入 --disable-fileinfo

# Apache 的安裝過程問題記錄

問題: 執行啟動指令後瀏覽相應網址看不到網頁
解決: tail /usr/local/apache2/logs/error_log 裡是否有模組沒加載

# 加入自動啟動 /etc/rc.local 的問題記錄

問題: 不會自動啟動裡面的命名行
解決:
- ls -la /etc/rc5.d/S19rc.local 是否指向 ../init.d/rc.local
- less /etc/init.d/rc.local 內容是否會正常執行 /etc/rc.local

# 加入 iptables 時的問題記錄

問題: 如果更新新的規則
解決: 在 restore 的時間就已經自動更新

問題: 如何檢查 Port 是否已被打開或禁止
解決:
- 自機中安裝 apt-get install nmap
- 自機中執行 nmap -sS Your_IP