lnmp学习之php安装、nginx和php整合

来源:互联网 发布:java汉化版 编辑:程序博客网 时间:2024/05/01 15:38
php安装配置

1、lnmp下的php
apache====》libphp5.so
nginx php===》fcgi php-fpm port 9000

2、php安装准备
检查
[root@nginx db]# netstat -lntup | egrep "nginx|mysql"
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      51156/nginx         
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      65163/mysqld 
-------------------------------------------------------------
yum install –y zlib libxml zlib-devel libjpeg freetype libpng gd curl libiconv libxml2-devel freetype-devel libpng-devel gd-devel curl-devel gcc glibc glibc-common  xinetd openssl-devel  ncurses-devel gcc-c++ wget tree vim lsof apr*
-------------------------------------------------------------
tar -zxvf libiconv-1.14.tar.gz 
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make
make install
--------------------------------------------------------------
tar -zxvf libmcrypt-2.5.8.tar.gz 
cd libmcrypt-2.5.8
./configure
make
make install
sleep 2
/sbin/ldconfig
cd libltdl
./configure --enable-ltdl-install
make
make install
cd ../../

hp在make过程中出现“usr bin ld cannot find lltdl”错误,详细错误如下:

/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [libphp5.la] Error 1
原因:
在编辑php时添加的“–with-mcrypt”选项造成。
解决方法:
如果不需要mcrypt,那么编辑php时去掉该选项,然后再make、make install。
如果需要mcrypt,那么需要安装libltdl
libltdl在libmcrypt软件包中就有,具体过程:
#cd /software/libmcrypt-2.5.8/libltdl
#./configure –enable-ltdl-install
#make
#make install
这个时候再回到php的安装目录下进行重新编译php,一切ok
---------------------------------------------------------------
tar -zxvf mhash-0.9.9.9.tar.gz 
cd mhash-0.9.9.9
 ./configure
make && make install
vim /etc/ld.so.conf.d/mhash.conf  # 做添加mhash的库文件让系统找到,不然后面编译安装mcrypt会报错
    /usr/local/mhash/lib

rm -f /usr/lib64/libmcrypt.*
rm -f /usr/lib64/libmhash*
ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/
-----------------------------------------------------------------


tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig 
./configure LD_LIBRARY_PATH=/usr/local/lib
make
make install
cd ../
-----------------------------------------------------------------

3、php安装
yum -y install libxslt*
 tar -zxvf php-5.3.27.tar.gz 
 cd php-5.3.27
./configure
./configure \
--prefix=/data/soft/php-5.3.27 \
--with-mysql=/data/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-curlwrappers \
--enable-mbregex \
--enable-mbstring \
--enable-fpm \
--with-mcrypt \
--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


注意:在make前加上这两条命令:
ln -s /data/mysql/lib/libmysqlclient.so.18 /usr/lib64/ 
touch  ext/phar/phar.phar  

make  && make install   

ln -s /data/php-5.3.27/  /data/php/ 

配置php解析配置文件   (开发环境打开log,生产环境全部关闭)
[root@nginx php]# diff php.ini-*
521c521
< error_reporting = E_ALL | E_STRICT
---
> error_reporting = E_ALL & ~E_DEPRECATED
538c538
< display_errors = On
---
> display_errors = Off
549c549
< display_startup_errors = On
---
> display_startup_errors = Off
593c593
< track_errors = On
---
> track_errors = Off
611c611
< html_errors = On
---
> html_errors = Off
751c751
< ; removed in PHP 5.4.
---
> ; scheduled removed in PHP 5.4.
845c845
< ;cgi.redirect_status_env = 
---
> ;cgi.redirect_status_env =
1318c1318
< mysqlnd.collect_memory_statistics = On
---
> mysqlnd.collect_memory_statistics = Off
1587c1587
< session.bug_compat_42 = On
---
> session.bug_compat_42 = Off
1596c1596
< session.bug_compat_warn = On
---
> session.bug_compat_warn = Off

配置php配置文件
[root@nginx data]# ln -s soft/php-5.3.27 ./php
把生产环境copy到php下的lib
[root@nginx php]# ll *ini*
-rw-r--r--. 1 101 101 69606 Jul 11  2013 php.ini-development
-rw-r--r--. 1 101 101 69627 Jul 11  2013 php.ini-production
[root@nginx php]# cp php.ini-production   /data/php/lib/php.ini 

[root@nginx php]# cd etc/
[root@nginx etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default
[root@nginx etc]#cp php-fpm.conf.default php-fpm.conf 

[root@nginx php]# ./sbin/php-fpm 
[root@nginx php]# ps -ef | grep 9000
root     102639 102470  0 06:42 pts/1    00:00:00 grep 9000
[root@nginx php]# ps -ef | grep php
root     102635      1  0 06:42 ?        00:00:00 php-fpm: master process (/data/soft/php-5.3.27/etc/php-fpm.conf)
nginx    102636 102635  0 06:42 ?        00:00:00 php-fpm: pool www
nginx    102637 102635  0 06:42 ?        00:00:00 php-fpm: pool www
root     102641 102470  0 06:42 pts/1    00:00:00 grep php
[root@nginx php]# lsof -i :9000
COMMAND    PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 102635  root    7u  IPv4 462512      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 102636 nginx    0u  IPv4 462512      0t0  TCP localhost:cslistener (LISTEN)
php-fpm 102637 nginx    0u  IPv4 462512      0t0  TCP localhost:cslistener (LISTEN)

修改配置文件
[root@nginx data]# mkdir logs

pid = /data/logs/php-fpm.pid
error_log = /data/logs/php-fpm.log
log_level = error
rlimit_files = 32768(文件描述符)
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
pm.max_children = 1024   最大的子进程数
pm.start_servers = 16   启动的时候多少个子进程
pm.min_spare_servers = 5  最小剩余
pm.process_idle_timeout = 15s;
pm.max_requests = 2048        每个进程服务多少个进程后退出
;access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"  log格式(可改可补改)
slowlog = /data/logs/$pool.log.slow  慢查询
request_slowlog_timeout = 10
php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f 1098331428@qq.com  (发邮件)

vimdiff比对两个文件
[root@nginx etc]# vimdiff php-fpm.conf php-fpm.conf.default
检查语法
[root@nginx etc]# /data/php/sbin/php-fpm -t
[29-Nov-2016 07:58:37] NOTICE: configuration file /data/soft/php-5.3.27/etc/php-fpm.conf test is successful
[root@nginx php]# ps -ef | grep php
root     102917      1  1 08:14 ?        00:00:00 php-fpm: master process (/data/soft/php-5.3.27/etc/php-fpm.conf)
nginx    102918 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102919 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102920 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102921 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102922 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102923 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102924 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102925 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102926 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102927 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102928 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102929 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102930 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102931 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102932 102917  0 08:14 ?        00:00:00 php-fpm: pool www
nginx    102933 102917  0 08:14 ?        00:00:00 php-fpm: pool www

编辑开机自动启动脚本
[root@nginx etc]# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/etc/init.d/mysqld start
/data/php/sbin/php-fpm
/data/nginx/sbin/nginx

[root@nginx etc]# netstat -lntup | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      102635/php-fpm

[root@nginx php]# ./sbin/php-fpm --hlep
Usage: php-fpm [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F]
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -h               This help
  -i               PHP information
  -m               Show compiled in modules
  -v               Version number
  -p, --prefix <dir>
                   Specify alternative prefix path to FastCGI process manager (default: /data/soft/php-5.3.27).
  -g, --pid <file>
                   Specify the PID file location.
  -y, --fpm-config <file>
                   Specify alternative path to FastCGI process manager config file.
  -t, --test       Test FPM configuration and exit
  -D, --daemonize  force to run in background, and ignore daemonize option from config file
  -F, --nodaemonize
                   force to stay in foreground, and ignore daemonize option from config file
  -R, --allow-to-run-as-root
                   Allow pool to run as root (disabled by default)

3、整合php和nginx以下两种方法
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
粘贴到配置bbs配置文件 
[root@nginx extra]# sed -n '8,19p' bbs.conf  
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
[root@nginx extra]# sed -i '8,14s/#//g' bbs.conf     替换
[root@nginx extra]# cat bbs.conf 
        server {
        listen       80;
        server_name  bbs.wolf.com;
        location / {
            root   html/bbs;
            index  index.html index.htm;
            access_log logs/bbs_access.log;
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }
        }
    }
[root@nginx extra]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx1.8.1/conf/nginx.conf test is successful
[root@nginx extra]# /data/nginx/sbin/nginx -s reload

bbs首页加入index.php
 
[root@nginx bbs]# vi /data/nginx/conf/extra/bbs.conf 
        server {
        listen       80;
        server_name  bbs.wolf.com;
        location / {
            root   html/bbs;
            index  index.php index.html index.htm;
            access_log logs/bbs_access.log;
        #location ~ .* \. (php|php5)?$
        location ~ .*\.(php|php5)?$
        {
        #    root           html/bbs;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
         #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        #}
        }
    }
}


在nginx站点目录下写一个index.php代码测试php
<?php
    phpinfo();
?>

输入http://bbs.wolf.com/访问


需要连接数据库
[root@nginx bbs]# vi wolf_mysql.php  
<?php
$link=mysql_connect('localhost','root','123') or mysql_error();
if(!$link)  echo "failed!";
else echo "ok! \n";
mysql_close();
?>

测试
http://bbs.wolf.com/wolf_mysql.php
服务器上测试
[root@nginx bbs]# /data/php/bin/php wolf_mysql.php 
ok! 
[root@nginx bbs]#
=====================================================
资料
找到以下配置段
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
默认是匹配php与php5结尾的,去掉php5后缀的匹配,只匹配php的。
location ~ .*\.php$
{
try_files $uri =404;
fastcgi_pass  unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
==============================================================
 

0 0