centos5下搭建NGINX+PHP(fast cgi)+MySql+PHPMyAdmin

一、把这些东西都yum了

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

 

二、下载所需包:
wget http://sysoev.ru/nginx/nginx-0.7.22.tar.gz
wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.9.diff.gz
(FastCGI是一个可伸缩的,高速地在web server和脚本语言间交互的接口。多数流行的web server都支持FastCGI。包括Apache(mod_fastcgi和mod_fcgid),Zeus,nginx和lighttpd。FastCGI的主要优点是把动态语言和web serve分离开来。这种技术允许把web server和动态语言运行在不同的主机上,以大规模扩展和改进安全性而不损失生产效率。php-fpm可以和任何支持远端FastCGI的web server工作。php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi)
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.29-rc.tar.gz/from/http://mirror.x10.com/mirror/mysql/
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
(libiconv就是为这Unicode和其他传统编码之间转换的应用设计的编码转换库。)
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.7.tar.gz?modtime=1194463373&big_mirror=0"
(libmcrypt和mcrypt 都是加密php的)
wget http://pecl.php.net/get/memcache-2.2.4.tgz
 (缓存系统,用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等,同时也解决了数据库检索的压力)
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.tar.gz?modtime=1175740843&big_mirror=0"
(哈希函数库,也是用来加密php的)
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.8.tar.gz
(PCRE是一个Perl库,包含了perl兼容的正规表达式库,些在执行正规表达式模式匹配时用与Perl 5 同样的语法和语义是很有用的。)
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
(php加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍;)
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
(PDO_MYSQL是PHP Data Objects (PDO) interface的一个mysql扩展)
wget http://jaist.dl.sourceforge.net/sourceforge/phpmyadmin/phpMyAdmin-3.1.1-all-languages.tar.bz2
 

 

三、安装运行环境

 

安装php fastcgi模式  


tar zxvf libiconv-1.12.tar.gz
cd libiconv-1.12
./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/ldconfg 的时候报错说usr/lib/libmcrypt.so.4 is not a system link
 用以下方法给解决了,也不知道对不对,呵呵,反正是过去了,也没提示)
mv /usr/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4.bak
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
/sbin/ldconfig

cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../..

 

tar zxvf mhash-0.9.9.tar.gz
cd mhash-0.9.9
./configure
make
make install
cd ..
cp /usr/local/lib/libmcrypt.* /usr/lib
cd libmcrypt-2.5.8
/sbin/ldconfig
cd ..

ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2


tar zxvf mcrypt-2.6.7.tar.gz
cd mcrypt-2.6.7
/sbin/ldconfig
./configure
make
make install
cd ..

 


编译安装MySQL 5.1.29-rc

groupadd mysql
useradd -g mysql mysql


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


chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
cp support-files/my-medium.cnf /etc/my.cnf
cd ..
(我安装完以后启动不了,总是提示:
Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)Errno.: 2002   检查之后发现是权限问题,我这里是把data文件夹直接给777了,问题解决)


以mysql用户建立数据表
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

 

启动mysql
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf &

 

如果不是在本机运行,而只是想让php支持mysql的扩展库,不需在本机建立数据表和运行

 

安装phpmyadmin

tar zxvf phpMyAdmin-3.1.1-all-languages.tar.bz2

mv phpMyAdmin-3.1.1-all-languages phpmyadmin

cp phpmyadmin /data/host/www

 

安装php
tar zxvf php-5.2.6.tar.gz
gzip -cd php-5.2.6-fpm-0.5.9.diff.gz |patch -cd php-5.2.6 -p1 #为php打补丁
cd php-5.2.6
./configure --prefix=/usr/local/php --with-config-file path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-

bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl

--with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl


sed -i 's#-lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt#& -liconv#' Makefile
make
make install
cp php.ini-dist /usr/local/php/etc/php.ini
cd ..

 

安装php拓展模块

tar zxvf memcache-2.2.4.tgz
cd memcache-2.2.4
/usr/local/php/bin/phpize  (phpize 命令是用来准备 PHP 外挂模块的编译环境的)
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install

cd ..


tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
ls
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-

config=/usr/local/php/bin/php-config
make
make install
cd ..

 

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-

mysql=/usr/local/mysql
make
make install
cd ..

 

(以上3步在make install之后会有提示:
# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non

-zts-20060613/
把 “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”记录下来)
vi /usr/local/php/etc/php.ini
查找里面的extension_dir = "./"
把./ 改成刚才记录下来的,并在此行下面添加
extension = "memcache.so"
extension = "pdo_mysql.so"

再查找output_buffering = Off
修改为output_buffering = On
(保存退出)


配置eaccelerator加速PHP

mkdir -p /usr/local/eaccelerator_cache (建立cache_dir)
vi /usr/local/php/etc/php.ini
在末尾加入以下信息

 


[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-
20060613/eaccelerator.so"(之前记录下来的目录)
eaccelerator.shm_size="128"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="300"
eaccelerator.shm_prune_period="120"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
 

 


修改最大尺寸的共享内存段 提高性能
vi /etc/sysctl.conf
将字段修改为:
kernel.shmmax = 263213056(通常是物理内存的一半,我的是512的。)

使配置生效:
sysctl -p


建立网站和博客的目录

groupadd www -g 48
useradd -u 48 -g www www
mkdir -p /web/host/blog
mkdir -p /web/host/www
chmod +w /web/host/blog/
chmod +w /web/host/www
chown -R www:www /web/host/blog/
chown -R www:www /web/host/www


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

输入以下信息

<?xml version="1.0" ?> 
<configuration> 
 
  All relative paths in this config are relative to php's install prefix  
 
  <section name="global_options"> 
 
    Pid file  
    <value name="pid_file">/usr/local/php/logs/php-fpm.pid</value> 
 
    Error log file  
    <value name="error_log">/usr/local/php/logs/php-fpm.log</value> 
 
    Log level  
    <value name="log_level">notice</value> 
 
    When this amount of php processes exited with SIGSEGV or SIGBUS ...  
    <value name="emergency_restart_threshold">10</value> 
 
    ... 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.  
    <value name="emergency_restart_interval">1m</value> 
 
    Time limit on waiting child's reaction on signals from master  
    <value name="process_control_timeout">5s</value> 
 
    Set to 'no' to debug fpm  
    <value name="daemonize">yes</value> 
 
  </section> 
 
  <workers> 
 
    <section name="pool"> 
 
      Name of pool. Used in logs and stats.  
      <value name="name">default</value> 
 
      Address to accept fastcgi requests on.  
      Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'  
      <value name="listen_address">127.0.0.1:9000</value> 
 
      <value name="listen_options"> 
 
        Set listen(2) backlog  
        <value name="backlog">-1</value> 
 
        Set permissions for unix socket, if one used.  
        In Linux read/write permissions must be set in order to allow connections

from web server.  
        Many BSD-derrived systems allow connections regardless of permissions.  
        <value name="owner"></value> 
        <value name="group"></value> 
        <value name="mode">0666</value> 
      </value> 
 
      Additional php.ini defines, specific to this pool of workers.  
      <value name="php_defines"> 
        <value name="sendmail_path">/usr/sbin/sendmail -t -i</value> 
        <value name="display_errors">0</value>   #0改为1显示php错误信息,否则

nginx会报500空白页
      </value> 
 
      Unix user of processes  
        <value name="user">www</value> 
 
      Unix group of processes  
        <value name="group">www</value> 
 
      Process manager settings  
      <value name="pm"> 
 
        Sets style of controling worker process count.  
        Valid values are 'static' and 'apache-like'  
        <value name="style">static</value> 
 
        Sets the limit on the number of simultaneous requests that will be served.  
        Equivalent to Apache MaxClients directive.  
        Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi  
        Used with any pm_style.  
        <value name="max_children">128</value> 
 
        Settings group for 'apache-like' pm style  
        <value name="apache_like"> 
 
          Sets the number of server processes created on startup.  
          Used only when 'apache-like' pm_style is selected  
          <value name="StartServers">20</value> 
 
          Sets the desired minimum number of idle server processes.  
          Used only when 'apache-like' pm_style is selected  
          <value name="MinSpareServers">5</value> 
 
          Sets the desired maximum number of idle server processes.  
          Used only when 'apache-like' pm_style is selected  
          <value name="MaxSpareServers">35</value> 
 
        </value> 
 
      </value> 
 
      The timeout (in seconds) for serving a single request after which the worker

process will be terminated  
      Should be used when 'max_execution_time' ini option does not stop script

execution for some reason  
      '0s' means 'off'  
      <value name="request_terminate_timeout">0s</value> 
 
      The timeout (in seconds) for serving of single request after which a php

backtrace will be dumped to slow.log file  
      '0s' means 'off'  
      <value name="request_slowlog_timeout">0s</value> 
 
      The log file for slow requests  
      <value name="slowlog">logs/slow.log</value> 
 
      Set open file desc rlimit  
      <value name="rlimit_files">51200</value> 
 
      Set max core size rlimit  
      <value name="rlimit_core">0</value> 
 
      Chroot to this directory at the start, absolute path  
      <value name="chroot"></value> 
 
      Chdir to this directory at the start, absolute path  
      <value name="chdir"></value> 
 
      Redirect workers' stdout and stderr into main error log.  
      If not set, they will be redirected to /dev/null, according to FastCGI specs  
      <value name="catch_workers_output">yes</value> 
 
      How much requests each process should execute before respawn.  
      Useful to work around memory leaks in 3rd party libraries.  
      For endless request processing please specify 0  
      Equivalent to PHP_FCGI_MAX_REQUESTS  
      <value name="max_requests">10240</value> 
 
      Comma separated list of ipv4 addresses of FastCGI clients that allowed to

connect.  
      Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi

(5.2.2+)  
      Makes sense only with AF_INET listening socket.  
      <value name="allowed_clients">127.0.0.1</value> 
 
      Pass environment variables like LD_LIBRARY_PATH  
      All $VARIABLEs are taken from current environment  
      <value name="environment"> 
        <value name="HOSTNAME">$HOSTNAME</value> 
        <value name="PATH">/usr/local/bin:/usr/bin:/bin</value> 
        <value name="TMP">/tmp</value> 
        <value name="TMPDIR">/tmp</value> 
        <value name="TEMP">/tmp</value> 
        <value name="OSTYPE">$OSTYPE</value> 
        <value name="MACHTYPE">$MACHTYPE</value> 
        <value name="MALLOC_CHECK_">2</value> 
      </value> 
 
    </section> 
 
  </workers> 
 
</configuration> 


 

 


启动php-cgi进程
ulimit -SHn 10240(最大进程数)
/usr/local/php/sbin/php-fpm start

 

php-fpm还有其他参数:start|stop|quit|restart|reload|logrotate

 

安装Nginx所需的pcre库:
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make
make install
cd ../

 

安装Nginx
tar zxvf nginx-0.7.22.tar.gz
cd nginx-0.7.22/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-

http_stub_status_module --with-http_ssl_module
make
make install
cd ../


修改nginx 配置文件

#运行用户
user  www www;
#启动进程
worker_processes 8;
#全局错误日志及PID文件
error_log  /logs/nginx_error.log  crit;
pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this

process.
worker_rlimit_nofile 51200;
#工作模式及连接数上限
events
{
 use epoll;
 worker_connections 51200;
}
#设定http服务器
http
{
#设定mime类型

 include       mime.types;
 #设定日志格式

 default_type  application/octet-stream;

 #charset  gb2312;
    
 server_names_hash_bucket_size 128;
 #设定请求缓冲
 client_header_buffer_size 32k;
 large_client_header_buffers 4 32k;
 client_max_body_size 8m;
    
 sendfile on;
 tcp_nopush     on;

 keepalive_timeout 60;

 tcp_nodelay on;


 fastcgi_connect_timeout 500;
 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/css application/xml;
 gzip_vary on;

 #limit_zone  crawler  $binary_remote_addr  10m;

 #设定虚拟主机

 server
 {
   listen       80;
   server_name  bbs.i5zh.com;
   index index.html index.htm index.php;

#设定网站目录
   root  /data/host/blog;

#limit_conn   crawler  20;   
                           
   location ~ .*/.(php|php5)?$
   {     
#fastcgi_pass  unix:/tmp/php-cgi.sock;
     fastcgi_pass  127.0.0.1:9000;
     fastcgi_index index.php;
#设定fcgi文件,这里在nginx.conf 同一目录
     include fcgi.conf;
   }
  
   location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$
   {
     expires      30d;
   }

   location ~ .*/.(js|css)?$
   {
     expires      1h;
   }   

#设定本虚拟主机的访问日志
   log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
   access_log  /data/logs/access.log  access;
     }

 server
 {
   listen       80;
   server_name  www.i5zh.com;
   index index.html index.htm index.php;
   root  /data/host/www;

   location ~ .*/.(php|php5)?$
   {     
     #fastcgi_pass  unix:/tmp/php-cgi.sock;
     fastcgi_pass  127.0.0.1:9000;
     fastcgi_index index.php;
     include fcgi.conf;
   }

   log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
   access_log  /data/logs/wwwlogs.log  wwwlogs;
 }

 server
 {
   listen  80;
   server_name  status.i5zh.com;

   location / {
   stub_status on;
   access_log   off;
   }
 }
}
 

 


在/usr/local/nginx/conf 下建立fcgi.conf
输入以下内容

 

        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;

  # PHP only, required if PHP was built with --enable-force-cgi-redirect
  fastcgi_param  REDIRECT_STATUS    200;
 

 


启动nginx
ulimit -SHn 10240
/usr/local/webserver/nginx/sbin/nginx

 

开机自动启动nginx和phpfcgi
在/etc/rc.local下加入

 

  ulimit -SHn 10240
  /usr/local/php/sbin/php-fpm start
  /usr/local/nginx/sbin/nginx

 


优化Linux内核参数

在/etc/sysctl.conf末尾增加以下内容:

 

net.ipv4.tcp_fin_timeout = 30
  net.ipv4.tcp_keepalive_time = 300
  net.ipv4.tcp_syncookies = 1
  net.ipv4.tcp_tw_reuse = 1
  net.ipv4.tcp_tw_recycle = 1
  net.ipv4.ip_local_port_range = 5000    65000

 


使配置立即生效:
/sbin/sysctl -p

 

检查修改过nginx.conf配置文件后,配置文件是否正确:
/usr/local/nginx/sbin/nginx -t

 

如果屏幕显示以下两行信息,说明配置文件正确:
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
the configuration file /usr/local//nginx/conf/nginx.conf was tested successfully

 

nginx的pid为/usr/local/nginx/nginx.pid
使修改过的nginx生效
kill -HUP 'cat /usr/local/nginx/nginx.pid'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值