构建高性能Web环境(linux/nginx/mysql/php-fastcgi)

分类:CentOS运维 阅读:29962 次

本文主要是撰写Linux系统中PHPWeb环境的配置,介绍在CentOS系统下安装:Nginx、MySQL、php-FastCGI,文中用的软件都是截至2011/6/22最新稳定版。网络上关于这种配置很多,配置过程相差不大,但是参数有所不同。开始之前先介绍一下nginx:

Nginx(engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、腾讯,另外知名的微网志Plurk也使用nginx。

下面开始叙述详细配置过程:

一、Centos系统安装完毕之后,关闭不需要的服务优化系统,安装系统更新和所需软件支持。
关闭不需要的服务

chkconfig --level 3 acpid off chkconfig --level 3 anacron off chkconfig --level 3 apmd off chkconfig --level 3 mdmonitor off chkconfig --level 3 xinetd off chkconfig --level 3 sendmail off chkconfig --level 3 rpcgssd off chkconfig --level 3 rawdevices off chkconfig --level 3 messagebus off chkconfig --level 3 atd off chkconfig --level 3 gpm off chkconfig --level 3 autofs off chkconfig --level 3 cpuspeed off chkconfig --level 3 haldaemon off chkconfig --level 3 nfslock off chkconfig --level 3 portmap off chkconfig --level 3 xfs off chkconfig --level 3 netfs off chkconfig --level 3 smartd off chkconfig --level 3 ip6tables off chkconfig --level 3 isdn off chkconfig --level 3 rpcidmapd off chkconfig --level 3 microcode_ctl off

 service acpid stop service anacron stop service apmd stop service mdmonitor stop service xinetd stop service sendmail stop service rpcgssd stop service rawdevices stop service messagebus stop service atd stop service gpm stop service autofs stop service cpuspeed stop service haldaemon stop service nfslock stop service portmap stop service xfs stop service netfs stop service smartd stop service ip6tables stop service isdn stop service rpcidmapd stop service microcode_ctl stop

安装系统更新和所需软件支持



 yum -y update 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 ntp rar

下载需要用到的软件包(以下地址都是官方下载、也可以自己查找)

【下载PHP需要的库】 wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz wget http://ncu.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz wget http://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz wget http://ncu.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz 【下载mysql、php、nginx】 wget http://mysql.mirrors.hoobly.com/Downloads/MySQL-5.1/mysql-5.1.57.tar.gz wget http://us.php.net/distributions/php-5.2.17.tar.gz wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz wget http://www.nginx.org/download/nginx-1.0.2.tar.gz wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz 【下载php扩展】 wget http://pecl.php.net/get/memcache-2.2.6.tgz wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2 wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick-6.7.0-8.tar.gz wget http://pecl.php.net/get/imagick-3.0.0.tgz

二、安装PHP需要的库程序

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/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

tar zxvf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8/ && /sbin/ldconfig

./configure && make && make install && cd ..

三、编译安装MySQL

tar zxvf mysql-5.1.57.tar.gz && cd mysql-5.1.57 ./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile make && make install && cd ..

创建mysql用户组,创建数据库、日志等存储目录并赋予权限

mkdir -p /data/mysqldata mkdir -p /data/mysqldata/database mkdir -p /data/mysqldata/log mkdir -p /data/mysqldata/pid groupadd mysql useradd -g mysql -d /data/mysqldata/database -s /sbin/nologin mysql chgrp -R mysql /usr/local/webserver/mysql/. chown -R root:mysql /usr/local/webserver/mysql/* chown -R mysql:mysql /data/mysqldata/*

修改my.cnf文件参数(参数适用于大于2G内存服务器)

[client] port = 3306 socket = /tmp/mysql.sock [mysqld] user = mysql port = 3306 socket = /tmp/mysql.sock basedir = /usr/local/webserver/mysql datadir = /data/mysqldata/database log-error = /data/mysqldata/log/mysql_error.log pid-file = /data/mysqldata/pid/mysql.pid skip-external-locking skip-name-resolve back_log = 500 key_buffer_size = 256M max_allowed_packet = 32M thread_stack = 192K table_cache = 256 sort_buffer_size = 4M read_buffer_size = 4M join_buffer_size = 4M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache = 128 thread_cache_size = 10 query_cache_size = 32M tmp_table_size = 128M max_connections = 5000 wait_timeout = 60 max_connect_errors = 6000 expire_logs_days = 30 server-id = 1 [mysqldump] quick max_allowed_packet = 32M [mysql] no-auto-rehash

初始化MySQL服务

/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/data/mysqldata/database --user=mysql

启动MySQL服务

/usr/local/webserver/mysql/bin/mysqld_safe --user=mysql & //启动服务 /usr/local/webserver/mysql/bin/mysqladmin -u root -p shutdown //停止服务

修改root密码

/usr/local/webserver/mysql/bin/mysqladmin -u root password ‘yourpassword’

四、编译安装PHP(FastCGI模式)

tar zxvf php-5.2.17.tar.gz gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1 cd php-5.2.17/ ./buildconf --force ./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-gd --with-jpeg-dir --with-iconv-dir=/usr/local --with-libxml-dir=/usr --with-zlib --with-png-dir --with-curl --with-ldap --with-xmlrpc --with-openssl --with-mhash --with-mcrypt --with-freetype-dir --with-ldap-sasl --with-curlwrappers --enable-xml --enable-shared --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring=all --enable-gd-native-ttf --enable-pcntl --enable-sockets --enable-zip --enable-soap make ZEND_EXTRA_LIBS='-liconv' make install && cd ..

五、安装PHP扩展模块

tar zxvf memcache-2.2.6.tgz cd memcache-2.2.6/ /usr/local/webserver/php/bin/phpize ./configure --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 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 ImageMagick-6.7.0-8.tar.gz cd ImageMagick-6.7.0-8/ ./configure && make && make install && cd .. tar zxvf imagick-3.0.0.tgz cd imagick-3.0.0/ /usr/local/webserver/php/bin/phpize ./configure --with-php-config=/usr/local/webserver/php/bin/php-config make && make install && cd ..

修改php.ini文件

vim /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

创建缓存目录,配置eAccelerator加速PHP:

mkdir -p /usr/local/webserver/eaccelerator_cache

将以下参数添加到php.ini文件末尾

zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/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" eaccelerator.compress="1" eaccelerator.compress_level="9"

修改php-fpm.conf配置文件

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

如果安装 Nginx + PHP 用于程序调试,请将以下的

<value name="display_errors">0</value>

改为

<value name="display_errors">1</value>

以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页

启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为:www

Ulimit-SHn 65535

/usr/local/webserver/php/sbin/php-fpm start //在www用户创建之后在开启。

注:/usr/local/webserver/php/sbin/php-fpm 还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。

六、编译安装nginx

创建www用户和组,以及网站目录,日志目录

groupadd www useradd -g www -s /sbin/nologin www mkdir -p /data/wsdata/wwwroot mkdir -p /data/wsdata/wwwroot/error echo "404" > /data/wsdata/wwwroot/error/404.html echo "server is too busy" > /data/wsdata/wwwroot/error/50x.html chmod +w /data/wsdata/wwwroot mkdir -p /data/wslogs/wwwroot chmod 777 /data/wslogs/wwwroot chown -R www:www /data/wsdata/wwwroot tar zxvf pcre-8.12.tar.gz && cd pcre-8.12/ ./configure && make && make install && cd .. tar zxvf nginx-1.0.2.tar.gz && cd nginx-1.0.2/ ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-cc-opt='-O3' --with-cpu-opt=opteron --with-http_gzip_static_module make && make install && cd ..

创建或修改nginx配置文件

cat nginx.conf

#user options user www www; #CPU Core options worker_processes 4; #nginx Process options pid /usr/local/webserver/nginx/nginx.pid; # [ debug | info | notice | warn | error | crit ] error_log /wslogs/nginx_error.log crit; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; #maxclient = worker_processes * worker_connections / cpu_number worker_connections 51200; } http { include mime.types; default_type application/octet-stream; #charset gb2312; #General options server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; ignore_invalid_headers on; recursive_error_pages on; server_name_in_redirect off; sendfile on; #timeouts keepalive_timeout 60; #TCP options tcp_nopush on; tcp_nodelay on; #fastcgi options fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_intercept_errors on; #size limits client_max_body_size 300m; client_body_buffer_size 256k; #gzip compression gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain text/css application/x-javascript application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; #virtual hosts options include vhosts.conf; }

cat vhosts.conf

server { listen 80; server_name www.linuxde.net linuxde.net; access_log /data/wslogs/linuxde_www_access.log combined; index index.html index.htm index.php; root /data/wsdata/wwwroot/linuxde/www; #error_page 404 = ./error/404.html; #error_page 500 502 503 504 = ./error/50x.html; #linuxde.net 重定向到 www.linuxde.net if ($host !~ "^www.linuxde.net$") { rewrite ^(.*) http://www.linuxde.net$1 permanent; } location ~ .*.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include enable_fcgi.conf; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { access_log off; expires 30d; } location ~ .*.(js|css)$ { access_log off; expires 1d; } #wordpress伪静态 location / { if (-f $request_filename/index.html){ rewrite (.*) $1/index.html break; } if (-f $request_filename/index.php){ rewrite (.*) $1/index.php; } if (!-f $request_filename){ rewrite (.*) /index.php; } } }

cat enable_fcgi.conf

#PHP PATH_INFO bugs if ($request_filename ~* (.*).php) { set $php_url $1; } if (!-e $php_url.php) { return 403; } 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; #for test fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate; # PHP only, required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200;

启动nginx服务

/usr/local/webserver/nginx/sbin/nginx -t //检测配置文件是否正确 /usr/local/webserver/nginx/sbin/nginx //启动nginx