Linux + Nginx 0.8.x + PHP 5.2.14(FastCGI)+ Mysql

更新时间:2023-04-25 12:24:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Linux + Nginx 0.8.x + PHP 5.2.14(FastCGI)+ Mysql

为什么Nginx的性能要比Apache高得多?这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型。

处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:

假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之一,select和epoll的性能谁的性能更高,同样十分明了。

安装步骤:

(系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 5.3,另在RedHat AS4上也安装成功)

一、获取相关开源程序:

1、【适用CentOS操作系统】利用CentOS Linux系统自带的yum命令安装、升级所需的程序库(RedHat等其他Linux发行版可从安装光盘中找到这些程序库的RPM包,进行安装):

sudo -s

LANG=C

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5

krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

2、【适用RedHat操作系统】RedHat等其他Linux发行版可从安装光盘中找到这些程序库的RPM包(事先可通过类似“rpm -qa | grep libjpeg”的命令查看所需的RPM包是否存在,通常是“xxx-devel”不存在,需要安装)。RedHat可以直接利用CentOS的RPM包安装,以下是RPM包下载网址:

①、RedHat AS4 & CentOS 4

79264284e53a580216fcfe44/centos/4/os/i386/CentOS/RPMS/

79264284e53a580216fcfe44/centos/4/os/x86_64/CentOS/RPMS/

②、RedHat AS5 & CentOS 5

79264284e53a580216fcfe44/centos/5/os/i386/CentOS/

79264284e53a580216fcfe44/centos/5/os/x86_64/CentOS/

③、RPM包搜索网站

79264284e53a580216fcfe44/

79264284e53a580216fcfe44/

④、RedHat AS4 系统环境,通常情况下缺少的支持包安装:

Ⅰ、i386 系统

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/rpm/i386/libjpeg-devel-6b-33.i386.rpm rpm -ivh libjpeg-devel-6b-33.i386.rpm

wget

79264284e53a580216fcfe44/soft/linux/nginx_php/rpm/i386/freetype-devel-2.1.9-1.i386.rpm rpm -ivh freetype-devel-2.1.9-1.i386.rpm

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/rpm/i386/libpng-devel-1.2.7-1.i386.rpm rpm -ivh libpng-devel-1.2.7-1.i386.rpm

Ⅱ、x86_64 系统

wget

79264284e53a580216fcfe44/soft/linux/nginx_php/rpm/x86_64/libjpeg-devel-6b-33.x86_64.rpm rpm -ivh libjpeg-devel-6b-33.x86_64.rpm

wget

79264284e53a580216fcfe44/soft/linux/nginx_php/rpm/x86_64/freetype-devel-2.1.9-1.x86_64.rp m

rpm -ivh freetype-devel-2.1.9-1.x86_64.rpm

wget

79264284e53a580216fcfe44/soft/linux/nginx_php/rpm/x86_64/libpng-devel-1.2.7-1.x86_64.rpm rpm -ivh libpng-devel-1.2.7-1.x86_64.rpm

3、【适用CentOS、RedHat及其它Linux操作系统】下载程序源码包:

本文中提到的所有开源软件为截止到2010年07月26日的最新稳定版。

①、从软件的官方网站下载:

mkdir -p /data0/software

cd /data0/software

wget sysoev.ru/nginx/nginx-0.8.46.tar.gz

wget 79264284e53a580216fcfe44/get/php-5.2.14.tar.gz/from/this/mirror

wget 79264284e53a580216fcfe44/downloads/php-5.2.14-fpm-0.5.14.diff.gz

wget

79264284e53a580216fcfe44/get/Downloads/MySQL-5.5/mysql-5.5.3-m3.tar.gz/from/mys 79264284e53a580216fcfe44/

wget 79264284e53a580216fcfe44/pub/gnu/libiconv/libiconv-1.13.1.tar.gz

wget

"79264284e53a580216fcfe44/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460 &big_mirror=0"

wget

"79264284e53a580216fcfe44/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&bi g_mirror=0"

wget 79264284e53a580216fcfe44/get/memcache-2.2.5.tgz

wget

"79264284e53a580216fcfe44/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843& big_mirror=0"

wget ftp://79264284e53a580216fcfe44/pub/software/programming/pcre/pcre-8.10.tar.gz

wget 79264284e53a580216fcfe44/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2

wget 79264284e53a580216fcfe44/get/PDO_MYSQL-1.0.2.tgz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/imagick/ImageMagick.tar.gz

wget 79264284e53a580216fcfe44/get/imagick-2.3.0.tgz

②、从79264284e53a580216fcfe44下载(比较稳定,只允许在本站,或者在Linux/Unix下通过Wget、Curl等命令下载以下软件):

mkdir -p /data0/software

cd /data0/software

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/php/php-5.2.14.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz wget 79264284e53a580216fcfe44/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2 wget 79264284e53a580216fcfe44/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/imagick/ImageMagick.tar.gz

wget 79264284e53a580216fcfe44/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz

二、安装PHP 5.2.14(FastCGI模式)

1、编译安装PHP 5.2.14所需的支持库:

二、安装PHP 5.2.14(FastCGI模式)

1、编译安装PHP 5.2.14所需的支持库:

tar zxvf libiconv-1.13.1.tar.gz

cd libiconv-1.13.1/

./configure --prefix=/usr/local

make

make install

cd ../

tar zxvf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8/

./configure

make

make install

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make

make install

cd ../../

tar zxvf mhash-0.9.9.9.tar.gz

cd mhash-0.9.9.9/

./configure

make

make install

cd ../

ln -s /usr/local/lib/79264284e53a580216fcfe44 /usr/lib/79264284e53a580216fcfe44

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/79264284e53a580216fcfe44 /usr/lib/79264284e53a580216fcfe44

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

附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下几步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。

①、创建MySQL数据库存放目录

mkdir -p /data0/mysql/3306/data/

mkdir -p /data0/mysql/3306/binlog/

mkdir -p /data0/mysql/3306/relaylog/

chown -R mysql:mysql /data0/mysql/

②、以mysql用户帐号的身份建立数据表:

/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mysql

--datadir=/data0/mysql/3306/data --user=mysql

③、创建79264284e53a580216fcfe44f配置文件:

vi /data0/mysql/3306/79264284e53a580216fcfe44f

输入以下内容:

引用

[client]

character-set-server = utf8

port = 3306

socket = /tmp/mysql.sock

[mysqld]

character-set-server = utf8

replicate-ignore-db = mysql

replicate-ignore-db = test

replicate-ignore-db = information_schema user = mysql

port = 3306

socket = /tmp/mysql.sock

basedir = /usr/local/webserver/mysql

datadir = /data0/mysql/3306/data

log-error = /data0/mysql/3306/mysql_error.log pid-file = /data0/mysql/3306/mysql.pid

open_files_limit = 10240

back_log = 600

max_connections = 5000

max_connect_errors = 6000

table_cache = 614

external-locking = FALSE

max_allowed_packet = 32M

sort_buffer_size = 1M

join_buffer_size = 1M

thread_cache_size = 300

#thread_concurrency = 8

query_cache_size = 512M

query_cache_limit = 2M

query_cache_min_res_unit = 2k

default-storage-engine = MyISAM

thread_stack = 192K

transaction_isolation = READ-COMMITTED

tmp_table_size = 246M

max_heap_table_size = 246M

long_query_time = 3

log-slave-updates

log-bin = /data0/mysql/3306/binlog/binlog

binlog_cache_size = 4M

binlog_format = MIXED

max_binlog_cache_size = 8M

max_binlog_size = 1G

relay-log-index = /data0/mysql/3306/relaylog/relaylog relay-log-info-file = /data0/mysql/3306/relaylog/relaylog relay-log = /data0/mysql/3306/relaylog/relaylog expire_logs_days = 30

key_buffer_size = 256M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

myisam_sort_buffer_size = 128M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

interactive_timeout = 120

wait_timeout = 120

skip-name-resolve

#master-connect-retry = 10

slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

#master-host = 192.168.1.2

#master-user = username

#master-password = password

#master-port = 3306

server-id = 1

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size = 512M

innodb_data_file_path = ibdata1:256M:autoextend innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 16M

innodb_log_file_size = 128M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

#log-slow-queries = /data0/mysql/3306/slow.log

#long_query_time = 10

[mysqldump]

quick

max_allowed_packet = 32M

④、创建管理MySQL数据库的shell脚本:

vi /data0/mysql/3306/mysql

输入以下内容(这里的用户名admin和密码12345678接下来的步骤会创建):#!/bin/sh

mysql_port=3306

mysql_username="admin"

mysql_password="12345678"

function_start_mysql()

{

printf "Starting MySQL...\n"

/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe

--defaults-file=/data0/mysql/${mysql_port}/79264284e53a580216fcfe44f 2>&1 > /dev/null &

}

function_stop_mysql()

{

printf "Stoping MySQL...\n"

/usr/local/webserver/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown

}

function_restart_mysql()

{

printf "Restarting MySQL...\n"

function_stop_mysql

sleep 5

function_start_mysql

}

function_kill_mysql()

{

kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk

'{printf $2}')

kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk

'{printf $2}')

}

if [ "$1" = "start" ]; then

function_start_mysql

elif [ "$1" = "stop" ]; then

function_stop_mysql

elif [ "$1" = "restart" ]; then

function_restart_mysql

elif [ "$1" = "kill" ]; then

function_kill_mysql

else

printf "Usage: /data0/mysql/${mysql_port}/mysql

{start|stop|restart|kill}\n"

fi

⑤、赋予shell脚本可执行权限:

chmod +x /data0/mysql/3306/mysql

⑥、启动MySQL:

/data0/mysql/3306/mysql start

⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车):

/usr/local/webserver/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

⑧、输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678):

GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';

⑨、(可选)停止MySQL:

4、编译安装PHP5扩展模块

tar zxvf memcache-2.2.5.tgz

cd memcache-2.2.5/

/usr/local/webserver/php/bin/phpize

./configure --with-php-config=/usr/local/webserver/php/bin/php-config make

make install

cd ../

tar jxvf eaccelerator-0.9.6.1.tar.bz2

cd eaccelerator-0.9.6.1/

/usr/local/webserver/php/bin/phpize

./configure --enable-eaccelerator=shared

--with-php-config=/usr/local/webserver/php/bin/php-config

make

make install

cd ../

tar zxvf PDO_MYSQL-1.0.2.tgz

cd PDO_MYSQL-1.0.2/

/usr/local/webserver/php/bin/phpize

./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql

make

make install

cd ../

tar zxvf ImageMagick.tar.gz

cd ImageMagick-6.5.1-2/

./configure

make

make install

cd ../

tar zxvf imagick-2.3.0.tgz

cd imagick-2.3.0/

/usr/local/webserver/php/bin/phpize

./configure --with-php-config=/usr/local/webserver/php/bin/php-config

make

make install

cd ../

5、修改php.ini文件

手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"

修改为extension_dir =

"/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"

并在此行后增加以下几行,然后保存:

extension = "memcache.so"

extension = "pdo_mysql.so"

extension = "imagick.so"

再查找output_buffering = Off

修改为output_buffering = On

再查找; cgi.fix_pathinfo=0

修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。

自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:

sed -i 's#extension_dir = "./"#extension_dir =

"/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\n#'

/usr/local/webserver/php/etc/php.ini

sed -i 's#output_buffering = Off#output_buffering = On#'

/usr/local/webserver/php/etc/php.ini

sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /usr/local/webserver/php/etc/php.ini

sed -i "s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g"

/usr/local/webserver/php/etc/php.ini

6、配置eAccelerator加速PHP:

mkdir -p /usr/local/webserver/eaccelerator_cache

vi /usr/local/webserver/php/etc/php.ini

按shift+g键跳到配置文件的最末尾,加上以下配置信息:

引用

[eaccelerator]

zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-200606 13/eaccelerator.so"

eaccelerator.shm_size="64"

eaccelerator.cache_dir="/usr/local/webserver/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"

79264284e53a580216fcfe44press="1"

79264284e53a580216fcfe44press_level="9"

7、创建www用户和组,以及供79264284e53a580216fcfe44和79264284e53a580216fcfe44两个虚拟主机使用的目录:

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

mkdir -p /data0/htdocs/blog

chmod +w /data0/htdocs/blog

chown -R www:www /data0/htdocs/blog

mkdir -p /data0/htdocs/www

chmod +w /data0/htdocs/www

chown -R www:www /data0/htdocs/www

8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):

在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件:

rm -f /usr/local/webserver/php/etc/php-fpm.conf

vi /usr/local/webserver/php/etc/php-fpm.conf

输入以下内容(如果您安装Nginx + PHP 用于程序调试,请将以下的

name="display_errors">0改为1,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页):

All relative paths in this config are relative to php's install prefix

Pid file

/usr/local/webserver/php/logs/php-fpm.pid

Error log file

/usr/local/webserver/php/logs/php-fpm.log

Log level

notice

When this amount of php processes exited with SIGSEGV or SIGBUS ...

10

... in a less than this interval of time, a graceful restart will be initiated. Useful to work around accidental curruptions in accelerator's shared memory. 1m

Time limit on waiting child's reaction on signals from master

5s

Set to 'no' to debug fpm

yes

Name of pool. Used in logs and stats.

default

Address to accept fastcgi requests on.

本文来源:https://www.bwwdw.com/article/d7wq.html

Top