<服务>LNMP下的PHP和LNMP

时间:Nov. 24, 2016 分类:

目录:

LNMP下的PHP编译安装

基础环境

[root@why-1 nginx]#  netstat -lntup | egrep "mysql|nginx"
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7946/nginx          
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      5470/mysqld  

依赖包安装

[root@why-1 nginx]# yum install -y zlib libxml libjpeg freetype libpng gd  curl libiconv  zlib-devel libxml2-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel

可选项:字符集转换

[root@why-1 local]# tar xf libiconv-1.14.tar.gz
[root@why-1 local]# cd libiconv-1.14
[root@why-1 libiconv-1.14]# ./configure --prefix=/usr/local/libiconv-1.14 && make && make install

可选项:加密

加密扩展库

[root@why-1 local]# tar xf libmcrypt-2.5.8.tar.gz 
[root@why-1 local]# cd libmcrypt-2.5.8
[root@why-1 libmcrypt-2.5.8]# ./configure 
[root@why-1 libmcrypt-2.5.8]# make && make install
[root@why-1 libmcrypt-2.5.8]# ldconfig
[root@why-1 libmcrypt-2.5.8]# cd libltdl/
[root@why-1 libltdl]# ./configure --enable-ltdl-install
[root@why-1 libltdl]# make && make install

hash加密扩展

[root@why-1 local]# tar xf mhash-0.9.9.9.tar.gz 
[root@why-1 local]# cd mhash-0.9.9.9
[root@why-1 mhash-0.9.9.9]# ./configure 
[root@why-1 mhash-0.9.9.9]# make && make install
[root@why-1 mhash-0.9.9.9]# mkdir /usr/lib64old
[root@why-1 mhash-0.9.9.9]# mv /usr/lib64/libmcrypt.* /usr/lib64old
[root@why-1 mhash-0.9.9.9]# mv /usr/lib64/libmhash.* /usr/lib64old
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
[root@why-1 mhash-0.9.9.9]# ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

php支持的加密扩展库

[root@why-1 local]# tar xf mcrypt-2.6.8.tar.gz
[root@why-1 local]# cd mcrypt-2.6.8
[root@why-1 mcrypt-2.6.8]# ./configure LD_LIBRARY_PATH=/usr/local/bin && make && make install

php编译安装

[root@why-1 local]# tar xf php-5.3.27.tar.gz
[root@why-1 php-5.3.27]# yum install -y libxslt*
[root@why-1 local]# cd php-5.3.27


./configure \
--prefix=/usr/local/php5.3.27 \
--with-mysql=/usr/local/mysql \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp


[root@why-1 php-5.3.27]# ./configure --prefix=/usr/local/php5.3.27 --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp
[root@why-1 php-5.3.27]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64

不创建软链的话,make会报

Generating phar.php
/usr/local/php-5.3.27/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
make: *** [ext/phar/phar.php] 错误 127

php编译安装

[root@why-1 php-5.3.27]# make
[root@why-1 php-5.3.27]# make install

php配置

[root@why-1 php-5.3.27]# cp php.ini-production /usr/local/php/lib/php.ini
 [root@why-1 php-5.3.27]# cd /usr/local/php/etc/
[root@why-1 etc]# ll
总用量 28
-rw-r--r-- 1 root root  1212 11月  3 01:47 pear.conf
-rw-r--r-- 1 root root 21669 11月  3 01:47 php-fpm.conf.default
[root@why-1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@why-1 etc]# vi php-fpm.conf
取消:注释
pid = run/php-fpm.pid           #pid,可以修改路径
error_log = log/php-fpm.log     #error log
log_level = error               #日志级别,默认为notice
rlimit_files = 32768                #文件描述符,默认为32768
listen.owner = nginx
listen.group = nginx
pm.max_children = 1024          #最大子进程的个数,默认为5
pm.start_servers = 16           #启动的时候子进程的个数,默认为2
pm.min_spare_servers = 5        #最少保留的子进程的个数,默认为1
pm.max_spare_servers = 20       #最多保留的子进程的个数,默认为3
pm.process_idle_timeout = 15s;  #进程无任务的保留时间,默认我10s
pm.max_requests = 2048      #子进程服务处理的请求数,处理超过这个数量就重新启动一个
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%" #日志格式
slowlog = log/$pool.log.slow        #慢查询,慢的请求网址会记录在这里,可以修改路径
request_terminate_timeout = 10  #超时时间

启动php-fpm

[root@why-1 etc]# ../sbin/php-fpm -t
[03-Nov-2016 05:25:41] ERROR: Unable to create or open slowlog(/usr/local/php5.3.27/log/www.log.slow): No such file or directory (2)
[03-Nov-2016 05:25:41] ERROR: failed to post process the configuration
[03-Nov-2016 05:25:41] ERROR: FPM initialization failed
[root@why-1 etc]# mkdir /usr/local/php5.3.27/log/
[root@why-1 etc]# ../sbin/php-fpm -t
[03-Nov-2016 05:26:24] NOTICE: configuration file /usr/local/php5.3.27/etc/php-fpm.conf test is successful

[root@why-1 etc]# ../sbin/php-fpm 
[root@why-1 etc]# netstat -nlptu | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      26299/php-fpm     
[root@why-1 etc]# ps -ef | grep php-fpm
root     25508  2583  0 02:42 pts/2    00:00:00 vi php-fpm.conf
root     26149 26138  0 04:43 pts/0    00:00:00 vi /usr/local/php/etc/php-fpm.conf
root     26299     1  0 05:26 ?        00:00:00 php-fpm: master process (/usr/local/php5.3.27/etc/php-fpm.conf)
nginx    26300 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26301 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26302 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26303 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26304 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26305 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26306 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26307 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26308 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26309 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26310 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26311 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26312 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26313 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26314 26299  0 05:26 ?        00:00:00 php-fpm: pool www
nginx    26315 26299  0 05:26 ?        00:00:00 php-fpm: pool www
root     26325 26187  0 05:28 pts/4    00:00:00 grep php-fpm

共计16个php-fpm进程,和配置文件中一样。

LNMP

在Nginx和MySQL分离的情况下,编译安装MySQL只需要编译到make install结束即可,二进制包只需要解压即可,因为PHP需要MySQL的一些文件以及MySQL的客户端。 与LAMP不同的是, apache中的php是以一个模块的形式存在,libphp5.so, 而在nginx中php是以守护进程的形式存在,fcgi php-fpm port 9000

配置Nginx

[root@why-1 etc]# vi /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.why.com;
        index  index.php;
        location ~ .*\.(php|php5)?$
        {
        root  html;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_index  index.php;
        include        fastcgi.conf;

        }

创建主页php文件

[root@why-1 etc]# vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

[root@why-1 etc]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx1.6.3/conf/nginx.conf test is successful
[root@why-1 etc]# /usr/local/nginx/sbin/nginx -s reload

php主页连接数据库

修改/usr/local/nginx/html/index.php

<?php
  $link_id=mysql_connect('localhost','root','123456') or mysql_error();
  if($link_id){
    echo "mysql successful with php\n";
  }else{
    echo mysql_error;
  }
?>

重新刷新界面。

LNMP完成

CGI

CGI全称为公共网关接口,即http服务器与机器其他程序进行交互的一种工具,必须运行在网络服务器上,不过一般的传统CGI的性能会比较差,因为每次http服务器遇到动态程序是都需要重启脚本解析器来进行执行让后将结果返回给http服务器,在处理高并发的访问时是无法进行支撑的,然后诞生了FastCGI。

FastCGI

与CGI一样的模式,是一个可伸缩,高速的HTTP服务和动态脚本语言间通信的接口,在linux底层以socket的形式存在,把动态语言和http服务进行分离,http服务只需要抛出动态请求,由服务端进行对请求的处理再返回给http服务,再将结果返回给浏览器。 nginx与php之间就通过FastCGI进行的,类似CS架构,Nginx的fastcgi_pass为客户端,PHP端启动的多个php-fpm为server端。 php-fpm就是php FastCGI的管理器,只用于php,我们看到的是php-fpm,但是实际上进行处理的为FastCGI,FastCGI也是启动了一个wrapper线程抛给php解析器。 不过php编译的时候需要制定--enable-fpm参数。