centos5.5系统的php+mysql+nginx配置

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

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

二、安装PHP 5.2.6(FastCGI模式)


  1、编译安装PHP 5.2.6所需的支持库:
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/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
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/
./configure
make
make install
cd ../

 

-----------------------------------------------------------------

  2、编译安装MySQL 5.1.56
  useradd -g mysql
 ./configure --with-mysqld-user=mysql --prefix=/usr/local/webserver/mysql5 --with-charset=gbk --with-extra-charset=all --without-isam --exec-prefix=/usr/local/webserver/mysql5 --with-unix-socket-path=/usr/local/webserver/mysql5/var/mysql.sock
make;make install
 /usr/local/webserver/mysql5/bin/mysql_install_db --user=mysql
 cp /usr/local/webserver/mysql5/share/mysql/my-medium.cnf /usr/local/webserver/mysql5/var/my.cnf
cp /usr/local/webserver/mysql5/share/mysql/mysql.server /etc/init.d/mysqld
chmod 700 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2345 mysqld on
chown mysql:mysql -R /usr/local/webserver/mysql5/
vi /usr/local/webserver/mysql5/var/my.cnf
# 添加 max_connections=1000
service mysqld start
ln -s /usr/local/webserver/mysql5/bin/mysql_config /usr/bin/mysql_config
ln -s /usr/local/webserver/mysql5/bin/mysql /sbin/mysql
ln -s /usr/local/webserver/mysql5/bin/mysqladmin /sbin/mysqladmin
#配置库文件搜索路径
echo "/usr/local/webserver/mysql5/lib/mysql" >> /etc/ld.so.conf
ldconfig -v
#添加/usr/local/mysql/bin到环境变量PATH中
export PATH=$PATH:/usr/local/webserver/mysql5/bin

-----------------------------------------------------------------

3、编译安装PHP(FastCGI模式)
yum install openldap-devel
tar zxvf php-5.3.0.tar.gz
gzip -cd /mnt1/php-5.3.0-fpm-0.5.12.diff.gz | patch -d php-5.3.0 -p1
cd php-5.2.6/
./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-liconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-debug --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/webserver/php/etc/php.ini
cd ../
tar jxvf eaccelerator-0.9.6.tar.bz2
/usr/local/webserver/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install

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.2.2.tgz
cd imagick-2.2.2/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../

----------------------------------------------------------------

 

 

 若看到相关的php信息页面,则证明php也配置成功!!

 


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

----------------------------------------------------------------

     5、修改php.ini文件
  手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
  修改为extension_dir = extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/
  并在此行后增加以下几行,然后保存:
  extension = "memcache.so"
  extension = "pdo_mysql.so"
  extension = "imagick.so"

  再查找output_buffering = Off
  修改为output_buffering = On

-----------------------------------------------------------------

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-20090626/eaccelerator.so"
eaccelerator.shm_size="128"
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="300"
eaccelerator.shm_prune_period="120"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

 修改配置文件:
vi /etc/sysctl.conf
输入以下内容:
kernel.shmmax = 134217728
然后执行以下命令使配置生效:
/sbin/sysctl -p

-----------------------------------------------------------------

7、创建www用户和组
useradd www
chmod +w /var/www
chown -R www:www /var/www

----------------------------------------------------------------

 8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
  在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件:
mv /usr/local/webserver/php/etc/php-fpm.conf  /usr/local/webserver/php/etc/php-fpm.conf.bak
vi /usr/local/webserver/php/etc/php-fpm.conf

输入一下内容 
 <?xml version="1.0" ?>
 <configuration>
  <section name="global_options">
    <value name="pid_file">/usr/local/webserver/php/logs/php-fpm.pid</value>
    <value name="error_log">/usr/local/webserver/php/logs/php-fpm.log</value>
     <value name="log_level">notice</value>
      <value name="emergency_restart_threshold">10</value>
       <value name="emergency_restart_interval">1m</value>
         <value name="process_control_timeout">5s</value>
         <value name="daemonize">yes</value>
          </section>
           <workers>
            <section name="pool">
              <value name="name">default</value>
              <value name="listen_address">127.0.0.1:9000</value>
              <value name="listen_options">
              <value name="backlog">-1</value>
              <value name="owner"></value>
              <value name="group"></value>
              <value name="mode">0666</value>
              </value>   <value name="php_defines">
              <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
              <value name="display_errors">1</value>
              </value>   <value name="user">www</value>
              <value name="group">www</value>
              <value name="pm">
              <value name="style">static</value>
              <value name="max_children">128</value>
              <value name="apache_like">
              <value name="StartServers">20</value>
              <value name="MinSpareServers">5</value>
              <value name="MaxSpareServers">35</value>
              </value>
              </value>
                  <value name="request_terminate_timeout">0s</value>
                  <value name="request_slowlog_timeout">0s</value>
                  <value name="slowlog">logs/slow.log</value>
                  <value name="rlimit_files">51200</value>
                  <value name="rlimit_core">0</value>
                  <value name="chroot"></value>
                  <value name="chdir"></value>
                  <value name="catch_workers_output">yes</value>
                  <value name="max_requests">500</value>
                   <value name="allowed_clients">127.0.0.1</value>
                   <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>

----------------------------------------------------------------
9、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启25个进程),用户为www:
ulimit -SHn 51200
/usr/local/webserver/php/sbin/php-fpm start

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

-----------------------------------------------------------------

10、优化Linux内核参数
vi /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 0.8.35
  1、安装Nginx所需的pcre库:
tar zxvf pcre-7.7.tar.gz
cd pcre-7.7/
./configure
make && make install
cd ../

----------------------------------------------------------------
  2、安装Nginx
# tar -zxvf /mnt1/nginx-0.8.35.tar.gz
cd nginx-0.8.35/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

----------------------------------------------------------------

  3、创建Nginx日志目录
mkdir /var/www/logs
chmod +w /var/www/logs
chown -R www:www /var/www/logs/

ln -s /var/www ../webserver/nginx/www
ln -s /var/www/logs ../webserver/nginx/logs

----------------------------------------------------------------
 

4、创建Nginx配置文件
在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:

rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf

user  www www;

worker_processes 8;

error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

http
{
  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;
     tcp_nodelay on;


  keepalive_timeout 60;

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

  #limit_zone  crawler  $binary_remote_addr  10m;

  server
  {
    listen       80;
    server_name  blog.s135.com;
    index index.html index.htm index.php;
    root  /var/www/htdoc;

    #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;
      include fcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      1d;
    }

    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }
        location /NginxStatus {
                        stub_status             on;
                        access_log              on;
                        auth_basic              "NginxStatus";
                        auth_basic_user_file  conf/htpasswd;
                        allow 192.168.186.53;
                        deny all;
                }
             
    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  /var/www/logs/access.log  access;

        }
}
在上面有设置查看Nginx状态的地址,需要apache的htpasswd 来生成一个登录验证文件,这样生成一个htpasswd 文件:

[root@oracle132 vhosts]# /usr/local/bin/htpasswd  -c htpasswd xh_l

New password:   (此处输入您的密码)
Re-type new password:   (再次输入您的密码)
Adding password for user   xh_l


上面 /usr/local/bin/htpasswd 是htpasswd 文件的执行路径,如果没有这个文件,可以从apache的bin目录拷贝一个过来即可!

-c是创建一个文件

-c后面的httpasswd是创建验证文件的名字.

xh_l是创建的用户

5、 在/usr/local/webserver/nginx/conf/目录中创建fcgi.conf文件:

vi /usr/local/webserver/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;

----------------------------------------------------------------

6、启动Nginx

ulimit -SHn 65535
/usr/local/webserver/nginx/sbin/nginx


--------------------------------------------------------------------------------

  四、配置开机自动启动Nginx + PHP

vi /etc/rc.local

  在末尾增加以下内容:

引用
ulimit -SHn 65535
/usr/local/webserver/php/sbin/php-fpm start
/usr/local/webserver/nginx/sbin/nginx


--------------------------------------------------------------------------------

  五、优化Linux内核参数

vi /etc/sysctl.conf

  在末尾增加以下内容:
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

net.ipv4.ip_local_port_range = 1024  65535


  使配置立即生效:

/sbin/sysctl -p


--------------------------------------------------------------------------------

  六、在不停止Nginx服务的情况下平滑变更Nginx配置
  1、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

/usr/local/webserver/nginx/sbin/nginx -t

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

  2、平滑重启:
  对于Nginx 0.8.x版本,现在平滑重启Nginx配置非常简单,执行以下命令即可:

/usr/local/webserver/nginx/sbin/nginx -s reload
 

----------------------------------OK,配置完成-------------------------------

测试:

1.启动nginx:/usr/local/webserver/nginx/sbin/nginx

2.启动php-fpm:/usr/local/webserver/php/sbin/php-fpm start

3.通过ip访问服务器,出现:Welcome to Nginx!说明Nginx服务已开启!

4.php测试,在Nginx的web根下建立:info.php

    <?php 

   phpinfo();

    ?>

你可能感兴趣的:(PHP,nginx,数据库,mysql,centos)