linux下搭建nginx+php(FastCGI)+mysql运行环境

时间:2021-08-21 11:59:40

一、安装环境

1、CentOS5.5

2、php5.4

3、MySQL5.5.19

二、安装程序依赖库和开发环境

为了省事把所需要的库文件全都安装上,可以使用rpm包安装,也可以用yum命令安装,

1 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

#nginx所需要的库文件件

yum -y install pcre-devel zlib-devel

#PHP依赖的库文件

1 yum -y install gd-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel curl-devel freetype-devel

三、安装步骤

1、安装mysql5 参考之前的文章:

http://www.num123.com/post/108

2、安装PHP

注:php在5.3.3以后的版本都已经集成了php-fpm,不需要在打php的fastcgi里程管理器补丁了。

#安装库文件

#1

1 tar zxvf libiconv-1.14.tar.gz
2 cd libiconv-1.14
3 ./configure --prefix=/usr/local
4 make && make install

#2

1 tar zxvf libmcrypt-2.5.8.tar.gz
2 cd libmcrypt-2.5.8
3 ./configure
4 make && make install
5 /sbin/ldconfig
6 cd libltdl
7 ./configure --enable-ltdl-install
8 make && make install

#3

1 tar zxvf mhash-0.9.9.9.tar.gz
2 cd mhash-0.9.9.9/
3 ./configure
4 make && make install

#4

01 ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
02 ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
03 ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
04 ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
05 ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
06 ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
07 ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
08 ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
09 ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
10 ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

#5

1 tar -zxvf mcrypt-2.6.8.tar.gz
2 cd mcrypt-2.6.8/
3 /sbin/ldconfig
4 ./configure
5 make && make install

#6

01 mkdir -p /home/webserver/php5.4
02 tar zxvf php-5.4.0.tar.gz
03 cd php-5.4.0.tar.gz
04 ./configure --prefix=/home/webserver/php5.4 \
05 --with-config-file-path=/etc \
06 --with-iconv-dir=/usr/local/ --with-freetype-dir \
07 --with-mysql=/usr/local/mysql \
08 --with-mysqli=/usr/local/mysql/bin/mysql_config \
09 --with-jpeg-dir --with-png-dir --with-zlib \
10 --with-mhash --enable-sockets --enable-ftp \
11 --with-libxml-dir --enable-xml --disable-rpath \
12 --enable-bcmath \
13 --enable-shmop --enable-sysvsem \
14 --enable-inline-optimization --with-curl \
15 --with-curlwrappers \
16 --enable-mbregex \
17 --enable-mbstring --with-mcrypt --with-gd \
18 --enable-gd-native-ttf --with-openssl --with-mhash \
19 --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl \
20 --enable-fpm \
21 --with-xmlrpc --enable-zip --enable-soap \
22 --without-pear
23 make ZEND_EXTRA_LIBS='-liconv'
24 make install
25 cp php.ini-production /etc/php.ini

vi /etc/php.ini

修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。

#在编译的时候有可能会出去mysqlclient库找不到之类的问题,加上下面两行即可

ln -s /usr/local/mysql/lib/libmysqlclient.so /usr/lib/

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18

#7 更改php-pfm配置文件

创建用户nginx且属于www组

groupadd www

useradd -s /sbin/nologin -M nginx -g www

cd /home/webserver/php5.4/etc/

mv php-fpm.conf.default  php-fpm.conf

vi php-fpm.conf

根据需要调整参数:

pid = run/php-fpm.pid

user = nginx
group = www
pm.max_children = 300
pm.start_servers = 20
pm.min_spare_servers = 40
pm.max_spare_servers = 60
pm.max_requests = 3000

#检查php-fpm配置语法是否正确
/home/webserver/php5.4/sbin/php-fpm -t
#出现NOTICE: configuration file /home/webserver/php5.4/etc/php-fpm.conf test is successful 测试成功
#启动php-fpm并在后台运行
/home/webserver/php5.4/sbin/php-fpm &
#设置开机启动
vi /etc/rc.d/rc.local
#在行末加入
/home/webserver/php5.4/sbin/php-fpm &

PHP5.3.3以上版本的php-fpm重启、停止命令已经不是php-fpm start|stop|reload了,需要使用信号控制:

master进程可以理解以下信号
INT, TERM 立刻终止
QUIT 平滑终止
USR1 重新打开日志文件
USR2 平滑重载所有worker进程并重新载入配置和二进制模块
示例:
php-fpm 关闭:
kill -INT `cat /home/webserver/php5.4/var/run/php-fpm.pid`
php-fpm 重启:
kill -USR2 `cat /home/webserver/php5.4/var/run/php-fpm.pid`
我们也可以不用cat /home/webserver/php5.4/var/run/php-fpm.pid来查看主进程号,面通过ps -eaf命令来查看主进程号
查看php-fpm进程总数:
ps -A | grep php-fpm |wc -l

3、安装nginx

#安装PCRE,perl兼容的正则表达式

tar zxvf pcre-8.21.tar.gz

cd pcre-8.21

./configure

make && make install

#安装nginx

tar zxvf nginx-1.0.14.tar.gz

cd nginx-1.0.14

./configure --user=nginx --group=www \
--prefix=/home/webserver/nginx1.0.14 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
--with-http_flv_module

#检测配置文件
/home/webserver/nginx1.0.14/sbin/nginx -t
#出现以下提示表示成功
#nginx: the configuration file /home/webserver/nginx1.0.14/conf/nginx.conf syntax is ok
#nginx: configuration file /home/webserver/nginx1.0.14/conf/nginx.conf test is successful
#开启Nginx
/home/webserver/nginx1.0.14/sbin/nginx
#平滑重启Nginx
/home/webserver/nginx1.0.14/sbin/nginx -s reload
#添加开机启动
vi /etc/rc.d/rc.local
#最后移行加入
/home/webserver/nginx1.0.14/sbin/nginx

 

#####nginx.conf文件配置示例###########

user  nginx www;
worker_processes 10;

worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 3000;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;
  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 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;

server
 {
    listen    80;
    #server_name  www.num123.com;
    index index.html index.htm index.php;
    root  html;


    location ~ .*\.(php|php5)?$
    {     
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      1d;
    }
    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }   
    access_log  off;
}

}