nginx+php

注:本文很多源碼包都源自sourceforge.net,我直接wget的時候不允許下載,URL都已經給出具體怎麼下載合適,請讀者自己做主!本文所有的軟件包來源的URL均來官網

1、安裝LNMP所依賴的軟件包組件

1
yum -y install gcc gcc-c++ pcre-devel openssl-devel mysql-devel  libxml2-devel patch bzip2 bzip2-devel curl-devel libjpeg libjpeg-devel  libpng-devel  libpng freetype freetype-devel openldap  openldap-devel perl-CPAN bison ncurses-devel

2、下載安裝libiconv

1
2
3
4
5
wget  http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tarxf libiconv-1.14.tar.gz
cdlibiconv-1.14
./configure--prefix=/usr/local/
make&& makeinstall

3、下載安裝libmcrypt(mhash,mcrypt,libmcrypt是我這邊線上開發要用的擴展,讀者可以自行選擇是否安裝)

1
2
3
4
5
6
7
8
9
10
wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
cp/root/下載/libmcrypt-2.5.8.tar.gz  .  sourceforge好像不允許直接wget下載
tarxf libmcrypt-2.5.8.tar.bz2
cdlibmcrypt-2.5.8
./configure
make&& makeinstall
/sbin/ldconfig
cdlibltdl/
./configure--enable-ltdl-install
make&&makeinstall

4、下載安裝mhash

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wget  http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download
cp /root/下載/mhash-0.9.9.9.tar.gz  .
tar xf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9
./configure
make && make install
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

5、下載安裝mcrypt

1
2
3
4
5
6
7
wget  http://sourceforge.net/projects/mcrypt/files/latest/download
cp/root/下載/mcrypt-2.6.8.tar.gz  .
tarxf mcrypt-2.6.8.tar.gz
cdmcrypt-2.6.8

/sbin/ldconfig

export LD_LIBRARY_PATH=/usr/local/lib: LD_LIBRARY_PATH

./configure
make&& makeinstall

6、添加Nginx運行所需的用戶、組

1
2
groupadd -r nginx
useradd-r -g nginx nginx

7、下載解壓Nginx

1
2
wget http://nginx.org/download/nginx-1.4.5.tar.gz
tarxf nginx-1.4.5.tar.gz

8、爲了避免在http響應的時候會出現Nginx等的信息,我們在編譯的時候可以稍作配置自定義一個信息!

1
2
3
4
5
6
7
8
cd nginx-1.4.5/src/http/
vim ngx_http_header_filter_module.c
修改以下兩行:
staticchar ngx_http_server_string[] = "Server: nginx"CRLF;
staticchar ngx_http_server_full_string[] = "Server: "NGINX_VER CRLF;
修改爲:
staticchar ngx_http_server_string[] = "Server: XZWeb 1.0"CRLF;                                   #XZWeb爲想要顯示的名稱
staticchar ngx_http_server_full_string[] = "Server:  XZWeb 1.0"NGINX_VER CRLF;                   #XZWeb爲想要顯示的名稱

9、編譯安裝Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
./configure\
--prefix=/usr\
--sbin-path=/usr/sbin/nginx\
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid  \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/\
--http-proxy-temp-path=/var/tmp/nginx/proxy/\
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi\
--http-scgi-temp-path=/var/tmp/nginx/scgi\
--with-pcre
make&& makeinstall

10、編寫Nginx啓動腳本

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
vim /etc/rc.d/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING"= "no"] && exit0
nginx="/usr/sbin/nginx"
prog=$(basename$nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep"configure arguments:"| sed's/[^*]*--user=\([^ ]*\).*/\1/g'-`
options=`$nginx -V 2>&1 | grep'configure arguments:'`
foropt in$options; do
if[ `echo$opt | grep'.*-temp-path'` ]; then
value=`echo$opt | cut-d "="-f 2`
if[ ! -d "$value"]; then
# echo "creating" $value
mkdir-p $value && chown-R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit5
[ -f $NGINX_CONF_FILE ] || exit6
make_dirs
echo-n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq0 ] && touch$lockfile
return$retval
}
stop() {
echo-n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq0 ] && rm-f $lockfile
return$retval
}
restart() {
configtest || return$?
stop
sleep1
start
}
reload() {
configtest || return$?
echo-n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null2>&1
}
case"$1"in
start)
rh_status_q && exit0
$1
;;
stop)
rh_status_q || exit0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit0
;;
*)
echo$"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit2
esac

11、添加Nginx爲系統服務並設置開機自啓動

1
2
3
chmod+x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

12、修改Nginx配置文件,屏蔽http響應信息中的Nginx版本等信息

  在http{}端添加server_tokens = off;

13、鏈接複製linux一些64位的庫文件防止在安裝PHP的時候報錯

1
2
3
4
5
cp-frp /usr/lib64/libjpeg.* /usr/lib
cp-frp /usr/lib64/libpng* /usr/lib
cp-frp /usr/lib64/libldap* /usr/lib
ln-s /usr/lib64/mysql/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
ln-s /usr/lib/libmysqlclient.so /usr/lib64/libmysqlclient.so

14、下載安裝PHP

1
2
3
4
5
6
7
wget  http://cn2.php.net/get/php-5.5.9.tar.gz/from/this/mirror
tarxf  php-5.5.9
cdphp-5.5.9
./configure--prefix=/data/install/php--with-mysql --with-mysqli --with-iconv-dir=/usr/local--with-freetype-dir--with-jpeg-dir--with-png-dir--with-zlib  --with-libxml-dir=/usr--enable-xml  --disable-rpath      --enable-bcmath   --enable-shmop   --enable-sysvsem   --enable-inline-optimization  --enable-mbregex     --enable-fpm    --enable-mbstring   --with-mcrypt   --with-gd   --enable-gd-native-ttf   --with-openssl   --with-mhash   --enable-pcntl   --enable-sockets   --with-ldap   --with-ldap-sasl   --with-xmlrpc   --enable-zip   --enable-soap   --with-config-file-path=/etc--with-config-file-scan-dir=/etc/php.d   --with-bz2
makeZEND_EXTRA_LIBS='-liconv'
maketest
makeintall

15、爲PHP提供配置文件

1
cpphp.ini-production /etc/php.ini

16、爲php-fpm提供Sysv init腳本,並將其添加至服務列表

1
2
3
4
cpsapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
chmod+x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

17、爲php-fpm提供配置文件

1
cp/usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

18、編輯php-fpm的配置文件(此處只貼一些我修改過的,具體還有哪些需要修改會在另一篇中補充)

1
2
3
4
5
6
7
vim /usr/local/php/etc/php-fpm.conf
配置fpm的相關選項爲你所需要的值,並啓用pid文件(如下最後一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /data/install/php/var/run/php-fpm.pid

19、接下來就可以啓動php-fpm了

1
service php-fpm start

20、使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啓動成功了)

1
ps-ef |grepphp-fpm

21、下載安裝memcache擴展(PHP連接memcached服務器的擴展)

1
2
3
4
5
6
7
cd/data/software/src/
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tarxf memcache-2.2.6.tgz
cdmemcache-2.2.6
/data/install/php/bin/phpize
./configure--with-php-config=/data/install/php/bin/php-config
make&& makeinstall

22、下載安裝xcache擴展

1
2
3
4
5
6
wget  http://xcache.lighttpd.net/pub/Releases/3.1.0/xcache-3.1.0.tar.gz
tarxf xcache-3.1.0.tar.gz
cdxcache-3.1.0
/data/install/php/bin/phpize
./configure--enable-xcache --with-php-config=/data/install/php/bin/php-config
make&& makeinstall

23、配置PHP文件,添加擴展

1
2
3
4
5
6
7
8
9
10
11
vim /etc/php.ini
extension_dir = "/data/install/php/lib/php/extensions/no-debug-non-zts-20121212/"
[memcache]
extension = "memcache.so"
[xcache]
extension = "xcache.so"
Xcache.size = 128M
Xcache.count = 2
Xcache.ttl = 86400
Xcache.gc_interval = 3600
Xcache.var_size = 0

24、查看擴展是否加載成功

1
2
/data/install/php/bin/php-v#查看下xcache信息,此處先看下xcache跟zend的,其他的可以在phpinfo中查看;
#有關xcache的詳細官方配置介紹  http://xcache.lighttpd.net/wiki/XcacheIni

25、修改下php-fpm的配置文件,適應線上環境(此處只給出我修改的一些項目,具體還能改哪些會在後面的博文中補充)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pid = /data/install/php/var/run/php-fpm.pid
error_log = /data/logs/php-fpm-error.log
log_level = notice
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
daemonize = yes
user = nobody
group = nobody
listen = 127.0.0.1:9000
pm.max_children = 192
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35

26、配置fastcgi

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vim /etc/nginx/fastcgi_params
fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

27、重啓php-fpm

1
service php-fpm restart

28、修改Nginx配置文件,使其適合線上環境

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
vim /etc/nginx/nginx.conf
user  nobody;
worker_processes  4;
error_log  /data/logs/error.log crit;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 20480;     #可以直接設置爲65535
pid        /data/logs/nginx.pid;
events {
use epoll;
worker_connections  20480;  #可以直接設置成65535,注意儘量跟上面的設置統一
}
http {
include       mime.types;
default_type  application/octet-stream;
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
charaset utf-8;
server_tokens   off;
sendfile        on;
tcp_nopush      on;
tcp_nodelay     on;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
keepalive_timeout  65;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 600;
fastcgi_buffer_size 16k;
fastcgi_buffers 16 16k;
fastcgi_busy_buffers_size 32k;
#open_file_cache max=204800 inactive=20s;
#open_file_cache_min_uses 1;
#open_file_cache_valid 30s;
gzipon;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 3;
gzip_types text/plainapplication/x-javascripttext/cssapplication/xml;
gzip_vary on;
server {
listen       80;
server_name  192.168.239.130;
#charset koi8-r;
access_log  /data/logs/www.access.log  main;
location / {
root   /data/web;
#    index  index.html index.htm;
index index.php;
}
location ~ \.(js|css|jpg|png|gif)$ {
root           /data/web;
expires        30d;
}
location ~ \.php$ {
root           /data/web;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_pass_header  on;
if( $fastcgi_script_name ~ \..*\/.*php ) {
return403;
}
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny  all;
}
}
}

29、重啓Nginx(也可以重新reload一下配置文件)

1
service nginx restart

30、關於這篇博文的一些問題

 1、有人可能會問爲什麼沒有安裝mysql的步驟呢,這裏安裝的只是Nginx跟PHP,一般線上很少會        PHP、Nginx、mysql同在一臺服務器吧(即使就有一臺服務器最好是用虛擬機的方式將不同服務安      裝到不同虛擬機做業務分離),要想解決PHP安裝依賴mysql某些文件的話直接yum安裝mysql-          devel就可以了,節省時間節省空間多好!!!

 2、博文中提到的後續文章什麼時候能更新?這個不好說 ,我也是菜鳥在摸索中,等我摸索好了自      然會發出來的,哈哈@_@;

 3、博文中提到的屏蔽Nginx信息,PHP信息是指什麼?兩張圖便知曉

  修改前:wKioL1MVnbPD-4q5AAJGwGeHg0E933.jpg

修改後

wKioL1MVnc6CkA--AAHJcPyppek469.jpg

看圖你們就知道我的良苦用心了吧!!!!!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章