Nginx PHP MySql 编译安装

以CentOS5.6为平台编译安装。确保系统已经安装gcc/gcc-c++编译器!

1.Nginx-1.0.14

2.PHP-5.3.10

3.MySql-5.1.61

安装相关依赖开发库:

autoconf curl freetype gd jpegsrc libiconv libmcrypt libpng libxml2

mhash ncurses openssl opensll-devel pcre pcre-devel zlib zlib-devel

1.Nginx 编译安装

# groupadd www 创建www用户组

# useradd -g www -s /sbin/nologin -M www 创建www用户并将其添加到www用户组

# mkdir /www 创建/www网站目录

# chmod +w /www 给/www目录写权限

# chown -R www:www /www 将网站根目录/www所有者和所属组设置为www用户和组

[./configure 编译参数]

--prefix=/usr/local/nginx Nginx安装路径

--user=www nginx启动运行所使用的用户,该用户必须已经存在。

--group=www nginx启动运行所使用的用户组,该用户组必须已经存在。

--with-http_realip_module

--with-http_addition_module

--with-http_gzip_static_module 开启gzip压缩功能,对页面进行压缩。

--with-http_random_index_module

--with-http_stub_status_module

--with-http_sub_module

--with-http_dav_module

配置 Nginx.conf 配置文件修改或添加下列内容:

?

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

user www www;//nginx默认以用户www和用户组www身份启动

worker_processes 8;//默认启动时开启的进程数

gzipon;//开启gzip的压缩功能

gzip_min_length 1100;

gzip_buffers 4 8k;

gzip_types text/plain;

server {

listen 80;//默认nginx服务器使用的端口为80端口

server_name localhost;//服务器名称,例如www.baidu.com

location / {

root/www;//web服务器网站跟目录

index index.php index.html index.htm;//网站默认主页

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

#

location ~ \.php$ {

root/www;//默认所有php脚本文件所在目录

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

//开启对静态图片和flash本地浏览器缓存1天。

location ~* ^.+\.(gif|jpg|jpeg|png|bmp|swf)$

{

root/www;

access_log off;

expires 1d;

}

//开启对CSS样式表的本地缓存1小时。

location ~* ^.+\.(js|css)?$

{

root/www;

access_log off;

expires 1h;

}

创建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

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# this script create it by jackbillow at 2007.10.15.

# it is v.0.0.2 version.

# if you find any errors on this scripts,please contact jackbillow.

# and send mail to jackbillow at gmail dot com.

#

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

# It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /usr/local/nginx/logs/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pshell plain">/usr/local/nginx/logs/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

./etc/rc.d/init.d/functions

# Source networking configuration.

./etc/sysconfig/network

# Check that networking is up.

[ ${NETWORKING} ="no"] &&exit0

[ -x $nginxd ] ||exit0

# Start nginx daemons functions.

start() {

if[ -e $nginx_pid ];then

echo"nginx already running...."

exit1

fi

echo-n $"Starting $prog: "

daemon $nginxd -c ${nginx_config}

RETVAL=$?

echo

[ $RETVAL = 0 ] &&touch/var/lock/subsys/nginx

return$RETVAL

}

# Stop nginx daemons functions.

stop() {

echo-n $"Stopping $prog: "

killproc $nginxd

RETVAL=$?

echo

[ $RETVAL = 0 ] &&rm-f/var/lock/subsys/nginx/usr/local/nginx/logs/nginx.pid

}

# reload nginx service functions.

reload() {

echo-n $"Reloading $prog: "

#kill -HUP `cat ${nginx_pid}`

killproc $nginxd -HUP

RETVAL=$?

echo

}

# See how we were called.

case"$1"in

start)

start

;;

stop)

stop

;;

reload)

reload

;;

restart)

stop

start

;;

status)

status $prog

RETVAL=$?

;;

*)

echo$"Usage: $prog {start|stop|restart|reload|status|help}"

exit1

esac

exit$RETVAL

将以上脚本保存为nginx文件并复制到 /etc/init.d 目录下。执行下列命令:

# chmod 755 /etc/init.d/nginx 设置nginx启动脚本可执行权限

# chkconfig --add nginx 添加nginx为系统服务

# chkconfig nginx on 开启nginx服务随系统自动启动

# chown -R www:www /usr/local/nginx 设置nginx安装目录权限所有者和所属组为www

[手动启动 Nginx 命令]

# service nginx start

2.MySql 编译安装

# groupadd mysql 创建mysql用户组

# useradd -g mysql -s /sbin/nologin -M mysql 创建mysql用户并将其添加到mysql用户组

[./configure 编译参数]

--prefix=/usr/local/mysql mysql安装路径。

--localstatedir=/var/data DATA数据库位置。

--with-mysqld-user=mysql 以mysql用户身份运行mysql数据库。

--enable-assembler 使用一些字符函数的汇编模式(优化性能)

--with-big-tables 启用对大于4G的数据库的支持。

--with-charset=utf8 数据库编码字符集。多种编码字符集之间用逗号隔开。

--enable-static 静态编译,以静态方式编译客户端和服务端,能提高13%性能。

--with-client-ldflags=-all-static

--with-mysqld-ldflags=-all-static

--with-ssl 开启对SSL安全传输协议的支持。、

--with-embedded-server 构建嵌入式服务器

--enable-local-infile

# chown -R mysql:mysql /usr/local/mysql 设置MySql相关目录的权限

# cp /usr/local/mysql/share/mysql/my-medium.cnf /usr/local/mysql/my.cnf 复制创建MySql的配置文件

# /usr/local/mysql/bin/mysql_install_db --user=mysql 以mysql用户身份建立初始化数据库

# cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql 创建 Mysql 开机启动脚本,并设置可执行权限

# chmod 755 /etc/init.d/mysql

# chkconfig --add mysql 添加 MySql 为系统服务,并设置为开机自动启动

# chkconfig mysql on

# service mysql start 启动 Mysql

# /usr/local/mysql/bin/mysqladmin -u root password 123456 设置 MySql 的root密码为123456

3.PHP 编译安装

[./configure配置参数]

--prefix=/usr/local/php PHP安装路径

--with-config-file-path=/etc PHP配置文件路径

--with-mysql=/usr/local/mysql MySql安装路径

--with-mysqli=/usr/local/mysql/bin/mysql_config 开启对mysqli的支持

--enable-fpm

--with-fpm-user=www 以www用户身份运行PHP

--with-fpm-group=www 以www用户组身份运行PHP

--with-iconv-dir

--with-freetype-dir

--with-jpeg-dir

--with-png-dir

--with-zlib

--with-gd

--enable-gd-native-ttf

--with-libxml-dir

--with-curl

--enable-safe-mode

--with-xmlrpc

--with-openssl

--with-mhash

--with-mcrypt

--enable-bcmath

--enable-shmop

--enable-sysvsem

--with-curlwrappers

--enable-mbstring

--enable-sockets

--enable-magic-quotes

--with-pear

--enable-sysvshm

--enable-zip

[编译并安装PHP]

# make ZEND_EXTRA_LIBS='-liconv'

# make install

# cp php.ini-production /etc/php.ini 复制并修改php.ini配置文件

[修改php.ini配置文件内容:]

?

1

2

3

4

register_globals=Off 改为 register_globals=On//使传递全局变量有效<p></p>

date.timezone = Asia/Shanghai//设置当前服务器的时区

zend_optimizer.optimization_level=15//加载Zend加速器

zend_extension="/usr/local/Zend/ZendGuardLoader.so"//Zend加速器安装目录

[创建并修改php-fpm的配置文件]

# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

修改内容:

?

1

2

3

4

5

6

7

pid = run/php-fpm.pid

error_log = log/php-fpm.log

log_level = error

emergency_restart_interval = 3d

user = www

group = www

listen = 127.0.0.1:9000

[创建并添加php-fpm开机启动服务脚本]

# vi /etc/init.d/php-fpm

?

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

#!/bin/sh

#chkconfig: - 85 15

#description: php-fpm is PHP FastCGI Process Manage.

#processname:php-fpm

### BEGIN INIT INFO

# Provides: php-fpm

# Required-Start: $remote_fs $network

# Required-Stop: $remote_fs $network

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: starts php-fpm

# Description: starts the PHP FastCGI Process Manager daemon

### END INIT INFO

prefix=/usr/local/php

exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm

php_fpm_CONF=${prefix}/etc/php-fpm.conf

php_fpm_Pshell plain">/var/run/php-fpm.pid

php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {

try=0

whiletest$try -lt 35 ;do

case"$1"in

'created')

if[ -f"$2"] ;then

try=''

break

fi

;;

'removed')

if[ ! -f"$2"] ;then

try=''

break

fi

;;

esac

echo-n .

try=`expr$try + 1`

sleep1

done

}

case"$1"in

start)

echo-n"Starting php-fpm "

$php_fpm_BIN $php_opts

if["$?"!= 0 ] ;then

echo" failed"

exit1

fi

wait_for_pid created $php_fpm_PID

if[ -n"$try"] ;then

echo" failed"

exit1

else

echo" done"

fi

;;

stop)

echo-n"Gracefully shutting down php-fpm "

if[ ! -r $php_fpm_PID ] ;then

echo"warning, no pid file found - php-fpm is not running ?"

exit1

fi

kill-QUIT `cat$php_fpm_PID`

wait_for_pid removed $php_fpm_PID

if[ -n"$try"] ;then

echo" failed. Use force-quit"

exit1

else

echo" done"

fi

;;

force-quit)

echo-n"Terminating php-fpm "

if[ ! -r $php_fpm_PID ] ;then

echo"warning, no pid file found - php-fpm is not running ?"

exit1

fi

kill-TERM `cat$php_fpm_PID`

wait_for_pid removed $php_fpm_PID

if[ -n"$try"] ;then

echo" failed"

exit1

else

echo" done"

fi

;;

restart)

$0 stop

$0 start

;;

reload)

echo-n"Reload service php-fpm "

if[ ! -r $php_fpm_PID ] ;then

echo"warning, no pid file found - php-fpm is not running ?"

exit1

fi

kill-USR2 `cat$php_fpm_PID`

echo" done"

;;

*)

echo"Usage: $0 {start|stop|force-quit|restart|reload}"

exit1

;;

esac

# chmod 755 /etc/init.d/php-fpm

# chkconfig --add php-fpm 添加 PHP 为系统服务,并设置为开机自动启动

# chkconfig php-fpm on

# chown -R www:www /usr/local/php 给PHP相关文件目录设置权限

[手动启动 PHP 命令]

# service php-fpm start

!如果某个服务无法启动关闭系统防火墙。