CentOS - Nginx PHP Python MySQL Memcached


CentOS
Nginx-1.4.0, PHP-5.4.14, Python-2.7.4
MySQL-5.5.31, Memcached-1.4.15

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
#
rpm -ivh http://pkgs.repoforge.org/htop/htop-1.0.2-1.el6.rf.i686.rpm
rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.i686.rpm

vim /etc/yum/pluginconf.d/fastestmirror.conf

enabled=1

#
yum install gcc
yum install htop
yum install libevent-devel
yum install pcre-devel
yum install openssl-devel
yum install cmake
yum install libaio-devel
yum install ncurses-devel
yum install gcc-c++
yum install libxml2-devel
yum install bison
yum install libcurl-devel
yum install libjpeg-devel
yum install libpng-devel
yum install libmcrypt-devel

# 安裝 Memcache
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
make && make install

# 下載 Nginx 模組
cd ..
wget https://github.com/agentzh/memc-nginx-module/archive/master.zip -O memc-nginx-module.zip
wget https://github.com/agentzh/srcache-nginx-module/archive/master.zip -O srcache-nginx-module.zip

unzip memc-nginx-module.zip
unzip srcache-nginx-module.zip

# 這個模組在 1.1.4 已經成為預設編譯性
# 不用再 add-module
# 之前的就要用 --add-module=../ngx_http_upstream_keepalive
wget http://mdounin.ru/hg/ngx_http_upstream_keepalive/archive/tip.tar.gz -O ngx_http_upstream_keepalive.tar.gz
tar zxvf ngx_http_upstream_keepalive.tar.gz
mv ngx_http_upstream_keepalive-* ngx_http_upstream_keepalive

# 安裝 Nginx
groupadd www && useradd -g www -s /sbin/nologin www
wget http://nginx.org/download/nginx-1.4.0.tar.gz
tar zxvf nginx-1.4.0.tar.gz
cd nginx-1.4.0
./configure --prefix=/usr/local/nginx --add-module=../memc-nginx-module-master --add-module=../srcache-nginx-module-master --user=www --group=www --with-select_module --with-poll_module --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_stub_status_module --with-pcre
make && make install

# 安裝 MySQL
wget http://www.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.31.tar.gz/from/http://cdn.mysql.com/
tar zxvf mysql-5.5.31.tar.gz
cd mysql-5.5.31
groupadd mysql && useradd -s /sbin/nologin -g mysql mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=complex -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql;
make
make install
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
wget https://gist.github.com/zeuxisoo/cfc69868ae65f88ef61b/raw/f3a9b7e806907df2e10229af15ffeb1c36a21952/my.cnf -O /etc/my.cnf
/usr/local/mysql/scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
/usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/bin/mysqladmin password [[[PASSWORD]]]
/usr/local/mysql/bin/mysql -hlocalhost -uroot -p
USE mysql;
DELETE FROM user WHERE user='';
UPDATE user SET password=PASSWORD('[[[PASSWORD]]]') WHERE user='root';
DELETE FROM user WHERE NOT (user='root');
DROP USER ''@'%';
FLUSH PRIVILEGES;
EXIT;

# 安裝 PHP
cd ..
wget http://hk1.php.net/get/php-5.4.14.tar.gz/from/this/mirror
tar zxvf php-5.4.14.tar.gz
cd php-5.4.14
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-openssl --with-pcre-regex --with-zlib --with-curl --with-curlwrappers --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --with-gettext --with-mhash --enable-mbstring --with-mcrypt --with-iconv --with-mysql=/usr/local/mysql --with-mysqli --with-pdo-mysql --with-readline --enable-soap --enable-sockets --enable-zip --enable-mysqlnd --disable-fileinfo
make
make install

# 安裝 PHP-Memcached
cd ..
wget http://pecl.php.net/get/memcache-3.0.8.tgz
tar zxvf memcache-3.0.8.tgz
cd memcache-3.0.8
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

cp php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini

date.timezone = "Asia/Hong_Kong"

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

wget https://gist.github.com/zeuxisoo/cfc69868ae65f88ef61b/raw/5caf18b23f201b5d73936bc56acc4a59f2f0f3c5/php-fpm.conf -O /usr/local/php/etc/php-fpm.conf
/usr/local/php/sbin/php-fpm --test

# 修改 nginx.conf
vim /usr/local/nginx/conf/nginx.conf

user www www;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
}

http {
include mime.types;
default_type application/octet-stream;

log_not_found off;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

tcp_nopush on;
keepalive_timeout 60 60;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 16k;
fastcgi_buffers 16 16k;
fastcgi_busy_buffers_size 16k;
fastcgi_temp_file_write_size 16k;
fastcgi_intercept_errors on;

tcp_nodelay on;
server_tokens off;

gzip on;

include vhost/*.conf;

upstream memcache {
server localhost:11211;
keepalive 512;
}

server {
listen 80;
server_name default;

location /memc {
internal;
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string;
set $memc_exptime 300;
memc_pass memcache;
}

location / {
root html;
index index.html index.htm index.php;
}

location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

location ~ /\.ht {
deny all;
}
}
}

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

# 啟動 PHP-FPM
/usr/local/php/sbin/php-fpm

# 啟動 MySQL
/usr/local/mysql/support-files/mysql.server restart

# 啟動 Nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 安裝 Python 2.7.4
cd ..
wget wget http://python.org/ftp/python/2.7.4/Python-2.7.4.tar.bz2
tar xvf Python-2.7.4.tar.bz2
cd Python-2.7.4
./configure --prefix=/usr/local
make
make altinstall

# 安裝 Python distribute
cd ..
wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.36.tar.gz
tar zxvf distribute-0.6.36.tar.gz
cd distribute-0.6.36
python2.7 setup.py install

# 安裝 Python 的 virtualenv
cd ..
easy_install-2.7 virtualenv

# 管建 Virtualenv 安裝 memcache-cli 管理 memecahce
virtualenv-2.7 --no-site-package memecahce-cli
source memecahce-cli/bin/activate
pip install memcache-cli

memcache-cli 127.0.0.1:11211
> help
> get_stats items

#
問題: fix: TIMESTAMP with implicit DEFAULT value is deprecated
解決: /etc/my.cnf 加入 explicit_defaults_for_timestamp