RHEL5.8系统上安装Nginx+php(FastCGI)+MySQL 来构建一个高效的Web服务器

这段时间由于来上海找工作空档期,所以自己研究互联网开源相关工具。在VMWARE虚拟机环境搭建,现将过程共享之。

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 ( b a s e n a m e 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” ]; then                  # echo “creating”
value
mkdir -p value && chown -R value && chown -R user $value
fi
fi
done
}

start() {
[ -x nginx]||exit5[f n g i n x ] | | e x i t 5 [ − f NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n "Starting " S t a r t i n g prog: ”
daemon nginxc n g i n x − c NGINX_CONF_FILE
retval= ?echo[ ? e c h o [ retval -eq 0 ] && touch lockfilereturn l o c k f i l e r e t u r n retval
}

stop() {
echo -n "Stopping " S t o p p i n g prog: ”
killproc progQUITretval= p r o g − Q U I T r e t v a l = ?
echo
[ retval -eq 0 ] && rm -f retval -eq 0 ] && rm -f lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return ?echon ? e c h o − n "Reloading prog:killproc p r o g : ” k i l l p r o c nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
nginxtc n g i n x − 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” in       start)           rh_status_q && exit 0
1
;;
stop)
rh_status_q || exit 0
1;;restart|configtest) 1 ; ; r e s t a r t | c o n f i g t e s t ) 1
;;
reload)
rh_status_q || exit 7
1;;forcereload)forcereload;;status)rhstatus;;condrestart|tryrestart)rhstatusq||exit0;;)echo 1 ; ; f o r c e − r e l o a d ) f o r c e r e l o a d ; ; s t a t u s ) r h s t a t u s ; ; c o n d r e s t a r t | t r y − r e s t a r t ) r h s t a t u s q | | e x i t 0 ; ; ∗ ) e c h o "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

  1. 安装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

  1. 安装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

  1. 安装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

  1. 为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[见《Linux下安装php加速软件Xcache的方法.txt》]
1.编译安装xcache

cd /usr/src

tar xzvf xcache-2.0.0.tar.gz

cd xcache-2.0.0

/usr/local/php/bin/phpize #用phpize生成configure配置文件

./configure –enable-xcache –with-php-config=/usr/local/php/bin/php-config

make

make install

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626

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(PS:路径需要人工确认为xcache make install后的)

七,整合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 documentroot d o c u m e n t r o o t fastcgi_script_name;
fastcgi_param QUERY_STRING querystring;fastcgiparamREQUESTMETHOD q u e r y s t r i n g ; f a s t c g i p a r a m R E Q U E S T M E T H O D request_method;
fastcgi_param CONTENT_TYPE contenttype;fastcgiparamCONTENTLENGTH c o n t e n t t y p e ; f a s t c g i p a r a m C O N T E N T L E N G T H content_length;

fastcgi_param SCRIPT_NAME fastcgiscriptname;fastcgiparamREQUESTURI f a s t c g i s c r i p t n a m e ; f a s t c g i p a r a m R E Q U E S T U R I request_uri;
fastcgi_param DOCUMENT_URI documenturi;fastcgiparamDOCUMENTROOT d o c u m e n t u r i ; f a s t c g i p a r a m D O C U M E N T R O O T document_root;
fastcgi_param SERVER_PROTOCOL serverprotocol;fastcgiparamHTTPS s e r v e r p r o t o c o l ; f a s t c g i p a r a m H T T P S https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR remoteaddr;fastcgiparamREMOTEPORT r e m o t e a d d r ; f a s t c g i p a r a m R E M O T E P O R T remote_port;
fastcgi_param SERVER_ADDR serveraddr;fastcgiparamSERVERPORT s e r v e r a d d r ; f a s t c g i p a r a m S E R V E R P O R T 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

vim /web/bbs/index.php 内容如下 这个页面是测试php跟mysql

service nginx restart

service php-fpm restart

service mysqld restart

ok 到这里基本已经结束了。

总结

RHEL5.8+MYSQL5.6.17+nginx/1.2.9+PHP5.3.8+XCACHE2.0.0+zabbix

[root@hmaster bin]# uname -a
Linux hmaster 2.6.18-308.el5 #1 SMP Fri Jan 27 17:21:15 EST 2012 i686 i686 i386 GNU/Linux
[root@hmaster bin]# lsb_release
LSB Version: :core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-ia32:printing-4.0-noarch

[root@hmaster sbin]# ./nginx -v
nginx version: nginx/1.2.9
[root@hmaster sbin]# pwd
/usr/local/nginx/sbin

[root@hmaster bin]# pwd
/usr/local/php/bin
[root@hmaster bin]# ./php -v
PHP 5.3.8 (cli) (built: Jul 6 2018 16:52:58)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
with XCache v2.0.0, Copyright (c) 2005-2012, by mOo

[root@hmaster bin]# ./mysql -V
./mysql Ver 14.14 Distrib 5.6.17, for Linux (i686) using EditLine wrapper
[root@hmaster bin]# pwd
/usr/local/mysql/bin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值