Linux System Environment
[root@nginx01 ~]# cat /etc/redhat-release #==》系统版本
CentOS release 6.7 (Final)
[root@nginx01 ~]# uname –r #==》内核版本
2.6.32-573.el6.x86_64
[root@nginx01 ~]# uname -m #==》系统架构
x86_64
[root@nginx01 ~]# echo $LANG #==》系统字符集
en_US.UTF-8
[root@lamp ~]# mysql -uroot -p123456 #==》mysql版本
Server version: 5.5.32 MySQL Community Server (GPL)
[root@lnmp ~]# /application/nginx/sbin/nginx -v #==》nginx版本
nginx version: nginx/1.16.0
[root@lnmp ~]# /application/php/bin/php –v #==》PHP版本
PHP 5.3.27 (cli) (built: Jul 9 2019 14:54:53)
PHP配置文件(LNMP)
提示:/application/php是 PHP安装目录
/application/php/lib/php.ini #==》PHP主配置文件php.ini
/application/php/etc/php-fpm.conf #==》PHP FastCGI进程管理器配置文件
/application/php/sbin/php-fpm #==》PHP的FastCGI二进制启动命令
/application/php/bin/phpize #==》编译PHP插件前需运行此命令扩建PHP模块
/application/php/bin/php-config #==》编译PHP插件需要指定的PHP编译文件
PHP简介
PHP是一种流行的通用脚本语言,特别适合web开发。LNMP架构上主要使用到PHP主要功能之一的FastCGI。
想要了解FastCGI先了解一下是CGI,CGI简称通用网关接口(Common Gateway Interface),为HTTP服务器与其它主机上的程序服务通信交流的一种工具,CGI程序须运行在网络服务器上。CGI因性能较差,后面就产生了FastCGI(可伸缩地、高速地在 HTTP服务器和动态脚本语言间通信的接口)主要优点是把动态语言和HTTP服务器分离开来,多数流行的HTTP服务器都支持FastCGI功能,包括Apache、Nginx、Lighttpd等。
PHP工作流程图(LNMP架构)

一、配置阿里云yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum makecache
二、PHP安装前检查Mysql和Nginx是否已安装
[root@nginx01 ~]# ls -ld /application/mysql/ #==》查看mysql安装目录
drwxr-xr-x 13 root root 4096 Jun 26 15:46 /application/mysql/
[root@nginx01 ~]# ls -ld /application/nginx/ #==》查看nginx安装目录
drwxr-xr-x 11 root root 4096 Jun 26 20:16 /application/nginx/
[root@nginx01 ~]# netstat -tlunp | grep -E "nginx|mysqld" #==》查看nginx和mysql是否启动
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4534/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4215/nginx
#==》测试nginx是否正常
[root@nginx01 ~]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Wed, 26 Jun 2019 12:45:48 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 26 Jun 2019 12:15:33 GMT
Connection: keep-alive
ETag: "5d1361e5-264"
Accept-Ranges: bytes
#==》测试mysql是否正常
[root@nginx01 ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
三、安装PHP所需要的lib库
标注:有部分lib库无法yum安装,只能到官网下载相应的软件包进行安装
1、使用yum安装PHP通用lib库
[root@nginx01 ~]# yum -y install zlib libxml libjpeg freetype libpng gd curl libiconv zlib-devel libxm12-devel libjpeg-devel freetype-devel libpng-devel gd-devel curl-devel libxml2-devel libxslt-devel
2、原码安装libiconv库
标注:**libiconv属于GNU项目的软件,可前往GNU官网软件列表查找libiconv库并进行下载 http://www.gnu.org/software/
[root@nginx01 tools]# pwd
/home/oldboy/tools
[root@nginx01 tools]# wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
[root@nginx01 tools]# ls -l libiconv-1.16.tar.gz
-rw-r--r-- 1 root root 5166734 Apr 27 03:37 libiconv-1.16.tar.gz
[root@nginx01 tools]# tar -zxvf libiconv-1.16.tar.gz
[root@nginx01 tools]# cd libiconv-1.16
[root@nginx01 libiconv-1.16]# ./configure --prefix=/usr/local/libiconv
[root@nginx01 libiconv-1.16]#make
[root@nginx01 libiconv-1.16]# make install
3、使用yum源的epel扩展源安装libmcrypt库(第二步已说明如何配置epel扩展源)
标注:**libmcrypt库也可以使用源码安装,下载地址 http://mcrypt.hellug.gr/lib/
[root@nginx01 tools]# yum -y install libmcrypt libmcrypt-devel
[root@nginx01 tools]# rpm -qa libmcrypt*
libmcrypt-devel-2.5.8-9.el6.x86_64
libmcrypt-2.5.8-9.el6.x86_64
4、使用yum源的epel扩展源安装mhash库(第二步已说明如何配置epel扩展源)
[root@nginx01 tools]# yum -y install mhash
[root@nginx01 tools]# rpm -qa mhash*
mhash-0.9.9.9-3.el6.x86_64
5、使用yum源的epel扩展源安装mcrypt库(第二步已说明如何配置epel扩展源)
[root@nginx01 tools]# yum -y install mcrypt
[root@nginx01 tools]# rpm -qa mcrypt*
mcrypt-2.6.8-10.el6.x86_64
四、下载PHP源码安装包并编译安装PHP
标注:**PHP源码安装包下载地址 https://www.php.net/releases/
[root@nginx01 tools]# wget https://www.php.net/distributions/php-5.3.27.tar.gz
[root@nginx01 tools]# ls -l php-5.3.27.tar.gz
-rw-r--r-- 1 root root 15008639 May 15 05:46 php-5.3.27.tar.gz
[root@nginx01 tools]# tar -zxvf php-5.3.27.tar.gz
[root@nginx01 tools]# cd php-5.3.27
[root@nginx01 php-5.3.27]# ./configure --prefix=/application/php5.3.27 --with-mysql=/application/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-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
#==》如果编译PHP最后提示出现如下就表示编译PHP成功
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
[root@nginx01 php-5.3.27]# echo $? #==》检查编译PHP是否正常
0
#==》./configure编译之后一定要执行此命令,不然执行make会报错
[root@nginx01 php-5.3.27]# ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[root@nginx01 php-5.3.27]# make
#==》如果make最后提示出现如下就表示编译PHP成功
Build complete.
Don't forget to run 'make test'.
[root@nginx01 php-5.3.27]# echo $?
0
[root@nginx01 php-5.3.27]# make install
五、优化配置PHP配置文件php-fpm.conf
[root@nginx01 ~]# ln -s /application/php5.3.27/ /application/php
[root@nginx01 php-5.3.27]# pwd
/home/oldboy/tools/php-5.3.27
[root@nginx01 php-5.3.27]# ls -l php.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@nginx01 php-5.3.27]# cp php.ini-production /application/php/lib/php.ini
[root@nginx01 php-5.3.27]# ls -l /application/php/lib/php.ini
-rw-r--r-- 1 root root 69627 Jun 27 13:02 /application/php/lib/php.ini
[root@nginx01 etc]# cd /application/php/etc/
[root@nginx01 etc]# cp php-fpm.conf.default php-fpm.conf
[root@nginx01 etc]# mkdir -p /app/log/
[root@nginx01 etc]# vim php-fpm.conf
25 pid = run/php-fpm.pid
32 error_log = /var/log/php-fpm.log
93 rlimit_files = 32768
108 events.mechanism = epoll
162 listen.owner = nginx
163 listen.group = nginx
217 pm.max_children = 1024
222 pm.start_servers = 16
232 pm.max_spare_servers = 20
237 ;pm.process_idle_timeout = 15s;
243 ;pm.max_requests = 2048
418 access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
429 request_slowlog_timeout = 0
506 php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i –f
[root@nginx01 etc]# /application/php/sbin/php-fpm -t
[27-Jun-2019 13:50:02] NOTICE: configuration file /application/php5.3.27/etc/php-fpm.conf test is successful
[root@nginx01 etc]# /application/php/sbin/php-fpm
六、配置Nginx添加FastCGI功能支持并检查
[root@nginx01 etc]# netstat -tlunp | grep -E "mysqld|nginx|php-fpm"
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 12385/php-fpm
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2139/mysqld
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4494/nginx
[root@nginx01 ~]# cd /application/nginx/html/
[root@nginx01 html]# vim index.php
<?php
phpinfo();
?>
[root@nginx01 html]# vim ../conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.testwebsite.com;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
[root@nginx01 html]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx1.6.2/conf/nginx.conf test is successful
[root@nginx01 html]# /application/nginx/sbin/nginx -s reload
[root@nginx01 html]# curl -I 127.0.0.1/index.php
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Thu, 27 Jun 2019 06:04:38 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.27
七、检查PHP连接mysql是否成功
[root@nginx01 html]# vim /application/nginx/html/mysql_tset.php
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('localhost','root','123456') or mysql_error();
if($link_id){
echo "mysqsl successful by oldboy!\n";
}else{
echo "mysql_error()";
}
?>
[root@nginx01 html]# /application/php/bin/php /application/nginx/html/mysql_test.php
mysqsl successful by oldboy!
网友评论