本文描述了如何在centos5.9环境下搭建LNMP环境
吐槽: 之前在ubuntu下面搭过一次,这次因为某种需要再来一次,无语。。。
使用网页的软件源
1
2
| mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget http://mirrors.163.com/.help/CentOS5-Base-163.repo -O /etc/yum.repos.d/CentOS-Base-163.repo
|
安装php
卸载系统默认php
1
| yum remove php php-common
|
安装php5.3
1
| yum install php53 php53-devel php53-cli
|
安装spawn-fcgi, php-fastcgi
1
2
| wget http://centos.alt.ru/repository/centos/5/i386/centalt-release-5-3.noarch.rpm
yum install spawn-fcgi
|
php-fastcgi脚本
/usr/bin/php-fastcgi
1
2
| #!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u nginx -g nginx -f /usr/bin/php-cgi
|
/etc/init.d/php-fastcgi
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
| #!/bin/sh
#
# php-fastcgi - Use PHP as a FastCGI process via nginx.
#
# chkconfig: - 85 15
# description: Use PHP as a FastCGI process via nginx.
# processname: php-fastcgi
# pidfile: /var/run/php-fastcgi.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
phpfastcgi="/usr/bin/php-fastcgi"
prog=$(basename php-cgi)
cgi='/usr/bin/php-cgi'
lockfile=/var/lock/subsys/php-fastcgi
start() {
[ -x $phpfastcgi ] || exit 5
echo -n $"Starting $prog: "
daemon $phpfastcgi
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -TERM
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
#configtest || return $?
stop
start
}
reload() {
#configtest || return $?
echo -n $"Reloading $prog: "
killproc $prog -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 2
esac
|
fastcgi加入服务
1
2
| chkconfig --add php-fastcgi
chkconfig php-fastcgi on
|
安装扩展
1
| yum install php53-mysql php53-mbstring
|
安装phpredis
1
2
3
4
5
| wget https://github.com/nicolasff/phpredis/archive/master.zip
unzip master
cd phpredis-master/
phpize
make && make install
|
1
2
| ; Enable redis extension module
extension=redis.so
|
安装nginx
编译安装
1
2
3
4
5
6
7
| yum install zlib-devel openssl openssl-devel pcre pcre-devel
wget http://nginx.org/download/nginx-1.4.1.tar.gz
tar xvfz nginx-1.4.1.tar.gz
cd nginx-1.4.1
./configure --with-http_ssl_module
make && make install
useradd -M -r --shell /sbin/nologin --home-dir /opt/nginx nginx
|
控制脚本
/etc/rc.d/init.d/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
128
129
| #!/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" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/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' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt 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 ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
#make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
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/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
|
加入服务
1
2
| chkconfig --add nginx
chkconfig --level 2345 nginx on
|
配置文件
1
2
| # 建立虚拟机目录
mkdir -p /usr/local/nginx/sites-enabled
|
虚拟机配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| server {
server_name www.demo.com demo.com;
access_log /srv/www/demo/logs/access.log;
error_log /srv/www/demo/logs/error.log;
root /srv/www/demo;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /usr/local/nginx/conf/fastcgi_params;
#fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/demo$fastcgi_script_name;
}
}
|
在/usr/local/nginx/conf/nginx.conf 加入如下行
1
| include /usr/local/nginx/sites-enabled/*;
|
安装mysql
1
| yum install mysql-server
|
启动
1
2
| service nginx start
service php-fastcgi start
|
tips
- 由于centos默认的iptables是开着的,所以上述装好之后,需要关闭iptables或者配置规则才能使用。