nginx 是一个高性能的HTTP和反向代理,负载均衡服务器,nginx是以事件驱动的方式编写的,所以有着非常高效的性能,闲话不多说,下面将在一台默认安装有rhel5.8的机器上,安装nginx + php (FastCGI模式)+mysql 配置成一个高效的web服务器,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
目录
一,安装基本开发库及依赖的软件包
二,安装并配置mysql数据库
三,安装并配置nginx
四,安装PHP的加密算法库,编码转换库,等软件
五,安装php并配置为fastcgi模式
六,安装php加速器,xcache
七,整合nginx跟php(FastCGI模式)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~需要准备以下软件包,并复制到系统的/usr/src目录下
mysql-5.0.56.tar.gz 数据库,当然也可以是更高的版本,或者使用rpm包安装也是可以的
nginx-1.2.2.tar.gz nginx,目前官方最新版是1.2.2的
libiconv-1.13.1.tar.gz 编码转换库
libmcrypt-2.5.8.tar.bz2 加密算法库
mhash-0.9.9.9.tar.bz2 加密算法扩展库
mcrypt-2.6.8.tar.gz 加密算法工具
php-5.4.4.tar.bz2 php官方目前最新版,5.4的版本不用打fpm补丁,
xcache-2.0.0.tar.gz xcacne PHP加速器,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rhel5.8的系统上安装nginx+php(FastCGI)+mysql 来构建一个高效的web服务器
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
一,安装基本开发库,及相关依赖软件包
- #yum -y groupinstall "Development Libraries" "Development Tools" "X Software Development"
- #yum -y install pcre-devel
二,安装并配置mysql数据库
1 ,编译安装mysql数据库
- #cd /usr/src
- #tar xzvf mysql-5.0.56.tar.gz
- #cd mysql-5.0.56
- #./configure --prefix=/usr/local/mysql --sysconfdir=/usr/local/mysql/etc --with-ssl \
- --localstatedir=/usr/local/mysql/database --enable-assembler --with-readline \
- --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables \
- --with-embedded-server --enable-local-infile --with-plugins=innobase
- #make && make install
2.把mysql的命令加入到系统搜索路径
- #vim /etc/profile
- PATH=/usr/local/mysql/bin:$PATH 将次行加入到文件的44行
- #export PATH=/usr/local/mysql/bin:$PATH
3.把mysql的头文件添加到系统搜索路径
- #ln -sv /usr/local/mysql/include/mysql /usr/include/mysql
4,把mysql的库文件添加到系统搜索路径
- #vim /etc/ld.so.conf.d/mysql.conf
- /usr/local/mysql/lib/mysql
- #ldconfig
5,给mysql提供配置文件及服务启动脚本
- #cp /usr/src/mysql-5.0.56/support-files/my-huge.cnf /etc/my.cnf
- #cp /usr/src/mysql-5.0.56/support-files/mysql.server /etc/init.d/mysqld
- #chmod a+x /etc/init.d/mysqld
- #chkconfig --add mysqld
- #chkconfig mysqld on
6,添加执行mysql的用户,及初始化数据库,
- #useradd -s /sbin/nologin mysql
- #chown -R mysql:mysql /usr/local/mysql
- #mysql_install_db --user=mysql
- #service mysqld start
三,安装并配置nginx
1. 编译安装nginx
- #useradd -s /sbin/nologin nginx
- #cd /usr/src
- #tar xzvf nginx-1.2.2.tar.gz
- #cd nginx-1.2.2
- #./configure --prefix=/usr/local/nginx --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/ --with-pcre
- #make && make install
2.给nginx提供SysV服务启动脚本
- #vim /etc/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" ] && 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' -`
- 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
3.加入系统服务,并启动服务,
- 给脚本执行权限
- #chmod a+x /etc/init.d/nginx
- #chkconfig --add nginx
- #chkconfig nginx on
- #service nginx restart
四,安装PHP的加密算法库,编码转换库,等软件
1. 安装libiconv编码转换库
- #cd /usr/src
- #tar xzvf libiconv-1.13.1.tar.gz
- #cd libiconv-1.13.1
- #./configure --prefix=/usr/local
- #make && make install
2. 安装libmcryp加密算法扩展库
- #cd /usr/src
- #tar xjvf libmcrypt-2.5.8.tar.bz2
- #cd libmcrypt-2.5.8
- #./configure
- #make && make install
- #ldconfig
- #cd libltdl/
- #./configure --enable-ltdl-install
- #make && make install
3. 安装mhash 加密算法扩展库
- #cd /usr/src
- #tar xjvf 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
4. 安装mcrypt 加密算法工具
- #cd /usr/src
- #tar xzvf mcrypt-2.6.8.tar.gz
- #cd mcrypt-2.6.8
- #./configure
- #make && make install
五,安装php并配置为fastcgi模式
1. 编译安装php
- #cd /usr/src
- #tar xjvf php-5.4.4.tar.bz2
- #cd php-5.4.4
- #./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-openssl \
- --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config \
- --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir \
- --with-libxml-dir=/usr/ --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc \
- --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl --with-ldap --with-iconv-dir
- #make ZEND_EXTRA_LIBS='-liconv'
- #make install
2.为php提供配置文件
- #cp /usr/src/php-5.4.4/php.ini-production /etc/php.ini
3. 为fpm提供SysV服务启动脚本
- #cp /usr/src/php-5.4.4/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
- #chmod a+x /etc/init.d/php-fpm
- #chkconfig --add php-fpm
- #chkconfig php-fpm on
4, 为fpm提供配置文件
- #cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
- #vim /usr/local/php/etc/php-fpm.conf 修改以下项
- pid = /usr/local/php/var/run/php-fpm.pid
- pm.max_children = 50
- pm.start_servers = 5
- pm.min_spare_servers = 2
- pm.max_spare_servers = 8
5.启动php-fpm服务
- #service php-fpm restart
六,安装php加速器,xcache
1.编译安装xcache
- #cd /usr/src
- #tar xzvf xcache-2.0.0.tar.gz
- #cd xcache-2.0.0
- #/usr/local/php/bin/phpize
- #./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
- #make
- #make install
2.整合php跟xcache
- #mkdir /etc/php.d
- #cp /usr/src/xcache-2.0.0/xcache.ini /etc/php.d/
- #vim /etc/php.d/xcache.ini 将三行替换为以下行
- zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache.so
七,整合nginx跟php(FastCGI模式)
1. 编辑nginx主配置文件
- vim /usr/local/nginx/conf/nginx.conf 内容如下,可以将原文替换为以下内容
- user nginx;
- worker_processes 10;
- error_log logs/error.log crit;
- pid logs/nginx.pid;
- events
- {
- use epoll;
- worker_connections 51000;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- client_header_buffer_size 32k;
- large_client_header_buffers 4 32k;
- client_max_body_size 10m;
- sendfile on;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- 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;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.0;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/ccs application/xml;
- gzip_vary on;
- server {
- listen 80;
- server_name www.andy.com;
- index index.html index.htm index.php;
- root /web/www;
- location ~ .*\.(php|php5)?$
- {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param HTTPS on;
- include fastcgi.conf;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 30d;
- }
- location ~ .*\.(js|css)?$
- {
- expires 1h;
- }
- access_log logs/www.log;
- }
- server {
- listen 80;
- server_name bbs.andy.com;
- index index.html index.htm index.php;
- root /web/bbs;
- location ~ .*\.(php|php5)?$
- {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param HTTPS on;
- include fastcgi.conf;
- }
- access_log logs/bbs.log;
- }
- ##下面的是查看nginx状态的访问地址 配置下面的之后在地址栏输入 status.andy.com
- ##就可以看到并发连接数等等信息
- server {
- listen 80;
- server_name status.andy.com;
- location / {
- stub_status on;
- access_log off;
- }
- }
- }
2.编辑/usr/local/nginx/conf/fastcgi.conf文件
- #vim /usr/local/nginx/conf/fastcgi.conf
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- 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_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 HTTPS $https if_not_empty;
- fastcgi_param GATEWAY_INTERFACE CGI/1.1;
- fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
- 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;
- # PHP only, required if PHP was built with --enable-force-cgi-redirect
- fastcgi_param REDIRECT_STATUS 200;
3.建立网站目录及测试文件
- #mkdir -pv /web/{www,bbs}
- #vim /web/www/index.php 内容如下 这个页面是测试php跟nginx
- php
- phpinfo();
- ?>
- #vim /web/bbs/index.php 内容如下 这个页面是测试php跟mysql
- php
- $link=mysql_connect('localhost','root','');
- if ($link)
- echo ok;
- else
- echo no;
- mysql_close($link);
- ?>
4.重启服务
- #service nginx restart
- #service php-fpm restart
- #service mysqld restart
ok 到这里基本已经结束了,