【Web 集群实战】13_LNMP 之 PHP(FastCGI方式)
【Web 集群实战】13_LNMP 之 PHP(FastCGI方式)
标签(空格分隔): Web集群实战
一、PHP 服务的安装准备
1. 检查 Nginx 及 MySQL 的安装情况
1)检查确认 Nginx 及 MySQL 的安装路径
[[email protected] mysql]# ls -ld /application/mysql/
drwxr-xr-x 13 root root 4096 Sep 21 20:10 /application/mysql/
[[email protected] mysql]# ls -ld /application/nginx/
drwxr-xr-x 11 root root 4096 Sep 17 15:16 /application/nginx/
2) 检查端口及启动情况
[[email protected] mysql]# netstat -lntup|grep -E "80|3306"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1157/nginx: master
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1258/mysqld
3)测试访问 Nginx 及 MySQL 是否 OK
# 测试 Nginx
[[email protected] ~]# wget 127.0.0.1
......
Saving to: ‘index.html’
100%[===================================================================================================================================================>] 29 --.-K/s in 0s
2018-09-27 13:14:18 (1.51 MB/s) - ‘index.html’ saved [29/29]
# 测试 MySQL
[[email protected] ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.61 MySQL Community Server (GPL)
......
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
2. 检查安装 PHP 所需的 lib 库
[[email protected] ~]# yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libconv-devel -y
[[email protected] ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libconv-devel
libxml2-devel-2.9.1-6.el7_2.3.x86_64
zlib-devel-1.2.7-17.el7.x86_64
libjpeg-turbo-devel-1.2.90-5.el7.x86_64
[[email protected] ~]# yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel -y
[[email protected] ~]# rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
gd-devel-2.0.35-26.el7.x86_64
freetype-devel-2.4.11-15.el7.x86_64
libpng-devel-1.5.13-7.el7_2.x86_64
libcurl-devel-7.29.0-46.el7.x86_64
libxslt-devel-1.1.28-5.el7.x86_64
- 由于 yum 源中没有 libconv-devel,所以需手工编译
[[email protected] ~]# cd /home/ylt/tools/
[[email protected] tools]# wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
[[email protected] tools]# tar zxf libiconv-1.15.tar.gz
[[email protected] tools]# cd libiconv-1.15
[[email protected] libiconv-1.15]# ./configure --prefix=/usr/local/libconv
[[email protected] libiconv-1.15]# make
[[email protected] libiconv-1.15]# make install
[[email protected] libiconv-1.15]# cd ../
3. 安装 libmcrypt 库
- 由于 yum 源中没有 libmcrypt-devel,因此需要配置 epel 第三方 yum 源
[[email protected] tools]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[[email protected] tools]# yum install libmcrypt-devel -y
4. 安装 mhash 加密扩展库
[[email protected] tools]# yum install mhash -y
5. 安装 mcrypt 加密扩展库
[[email protected] tools]# yum install mcrypt -y
二、开始安装 PHP 服务
1. 获取 PHP 软件包
[[email protected] tools]# wget http://cn2.php.net/get/php-7.1.22.tar.gz/from/this/mirror
2. 解压配置 PHP
[[email protected] tools]# tar xzf php-7.1.22.tar.gz
[[email protected] tools]# cd php-7.1.22
[[email protected] tools]# ./configure --prefix=/application/php-7.1.22 --eanble-mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --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
3. 编译 PHP
[[email protected] php-7.1.22]# ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
[[email protected] php-7.1.22]# touch ext/phar/phar.phar
[[email protected] php-7.1.22]# make
4. 安装 PHP 生成文件到系统
[[email protected] php-7.1.22]# make install
5. 配置 PHP 引擎配置文件 php.ini
- 设置软链接以便访问
[[email protected] php-7.1.22]# ln -s /application/php-7.1.22 /application/php
[[email protected] application]# ll /application/php
lrwxrwxrwx 1 root root 24 Sep 26 04:10 /application/php -> /application/php-7.1.22/
- 查看 PHP 配置默认模板文件
[[email protected] php-7.1.22]# ls php.ini*
php.ini-development php.ini-production
- 拷贝 PHP 配置文件到 PHP 默认目录,并更改文件名称为 php.ini
[[email protected] php-7.1.22]# cp php.ini-production /application/php/lib/php.ini
[[email protected] php-7.1.22]# vim /application/php/lib/php.ini
# 将 477 行改成 display_errors = On
6. 配置 PHP 服务的配置文件 php-fpm.conf
[[email protected] php-7.1.22]# cd /application/php/etc/
[[email protected] php-7.1.22]# ls
pear.conf php-fpm.conf.default php-fpm.d
[[email protected] etc]# cd php-fpm.d/
[[email protected] php-fpm.d]# ls
www.conf.default
[[email protected] php-fpm.d]# cp www.conf.default ../php-fpm.conf
7. 启动 PHP 服务
[[email protected] php-fpm.d]# /application/php/sbin/php-fpm
[[email protected] php-fpm.d]# ps -ef|grep php-fpm
root 1973 1 0 19:21 ? 00:00:00 php-fpm: master process (/application/php-7.1.22/etc/php-fpm.conf)
nginx 1974 1973 0 19:21 ? 00:00:00 php-fpm: pool www
nginx 1975 1973 0 19:21 ? 00:00:00 php-fpm: pool www
root 1981 1872 0 19:22 pts/0 00:00:00 grep --color=auto php-fpm
[[email protected] php-fpm.d]# lsof -i :9000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
php-fpm 1973 root 7u IPv4 24547 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 1974 nginx 5u IPv4 24547 0t0 TCP localhost:cslistener (LISTEN)
php-fpm 1975 nginx 5u IPv4 24547 0t0 TCP localhost:cslistener (LISTEN)
三、配置 Nginx 支持 PHP 程序访问
1. 修改 Nginx 配置文件
- 查看 Nginx 当前的配置
[[email protected] php-fpm.d]# cd /application/nginx/conf/
[[email protected] conf]# cp nginx.con
[[email protected] conf]# cat nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}
- PHP 解析,以 blog 为例
[[email protected] conf]# cat extra/blog.conf
server {
listen 80;
server_name blog.yangyangyang.org;
location / {
root html/blog;
index index.html index.htm;
}
location ~ .*\.(php|php7)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
2. 检查并启动 Nginx
[[email protected] conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[[email protected] conf]# ../sbin/nginx -s reload
3. 测试 LNMP 环境生效情况
1)测试 PHP 解析请求是否 OK
- 进入指定的默认站点目录后,编辑 index.php,添加如下内容
[[email protected] conf]# cd ../html/blog/
[[email protected] blog]# echo "<?php phpinfo(); ?>" >test_info.php
[[email protected] blog]# cat test_info.php
<?php phpinfo(); ?>
- 调整 Windows 下的 host 解析
192.168.2.133 www.yangyangyang.org bbs.yangyangyang.org blog.yangyangyang.org status.yangyangyang.org yangyangyang.org
2)针对 Nginx 请求访问 PHP,然后对 PHP 连接 MySQL 的情况进行测试
- 新建 phpdb 数据库
[[email protected] blog]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.61 MySQL Community Server (GPL)
......
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database phpdb;
mysql> show database;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ftpdb |
| mysql |
| performance_schema |
| phpdb |
+--------------------+
5 rows in set (0.01 sec)
mysql> quit
Bye
[[email protected] blog]#
- 编辑 test_mysqli.php,加入如下内容
[[email protected] blog]# cat test_mysqli.php
<?php
/* Database config */
$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'passwd';
$db_database = 'phpdb';
/* End config */
$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_database);
/* check connection */
if (mysqli_connect_error()) {printf("Connect failed: %s\n", mysqli_connect_error());}
else{echo "mysql successful by ylt !";}
?>
- 结果如下图所示