[关闭]
@yanglt7 2018-10-21T15:55:55.000000Z 字数 7443 阅读 743

【Web 集群实战】13_LNMP 之 PHP(FastCGI方式)

Web集群实战


一、PHP 服务的安装准备

1. 检查 Nginx 及 MySQL 的安装情况

1)检查确认 Nginx 及 MySQL 的安装路径

  1. [root@ylt001 mysql]# ls -ld /application/mysql/
  2. drwxr-xr-x 13 root root 4096 Sep 21 20:10 /application/mysql/
  3. [root@ylt001 mysql]# ls -ld /application/nginx/
  4. drwxr-xr-x 11 root root 4096 Sep 17 15:16 /application/nginx/

2) 检查端口及启动情况

  1. [root@ylt001 mysql]# netstat -lntup|grep -E "80|3306"
  2. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1157/nginx: master
  3. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1258/mysqld

3)测试访问 Nginx 及 MySQL 是否 OK

  1. # 测试 Nginx
  2. [root@ylt001 ~]# wget 127.0.0.1
  3. ......
  4. Saving to: index.html
  5. 100%[===================================================================================================================================================>] 29 --.-K/s in 0s
  6. 2018-09-27 13:14:18 (1.51 MB/s) - index.html saved [29/29]
  7. # 测试 MySQL
  8. [root@ylt001 ~]# mysql -u root -p
  9. Enter password:
  10. Welcome to the MySQL monitor. Commands end with ; or \g.
  11. Your MySQL connection id is 1
  12. Server version: 5.5.61 MySQL Community Server (GPL)
  13. ......
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. mysql>

2. 检查安装 PHP 所需的 lib 库

  1. [root@ylt001 ~]# yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libconv-devel -y
  2. [root@ylt001 ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libconv-devel
  3. libxml2-devel-2.9.1-6.el7_2.3.x86_64
  4. zlib-devel-1.2.7-17.el7.x86_64
  5. libjpeg-turbo-devel-1.2.90-5.el7.x86_64
  6. [root@ylt001 ~]# yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel -y
  7. [root@ylt001 ~]# rpm -qa freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
  8. gd-devel-2.0.35-26.el7.x86_64
  9. freetype-devel-2.4.11-15.el7.x86_64
  10. libpng-devel-1.5.13-7.el7_2.x86_64
  11. libcurl-devel-7.29.0-46.el7.x86_64
  12. libxslt-devel-1.1.28-5.el7.x86_64
  1. [root@ylt001 ~]# cd /home/ylt/tools/
  2. [root@ylt001 tools]# wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
  3. [root@ylt001 tools]# tar zxf libiconv-1.15.tar.gz
  4. [root@ylt001 tools]# cd libiconv-1.15
  5. [root@ylt001 libiconv-1.15]# ./configure --prefix=/usr/local/libconv
  6. [root@ylt001 libiconv-1.15]# make
  7. [root@ylt001 libiconv-1.15]# make install
  8. [root@ylt001 libiconv-1.15]# cd ../

3. 安装 libmcrypt 库

  1. [root@ylt001 tools]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  2. [root@ylt001 tools]# yum install libmcrypt-devel -y

4. 安装 mhash 加密扩展库

  1. [root@ylt001 tools]# yum install mhash -y

5. 安装 mcrypt 加密扩展库

  1. [root@ylt001 tools]# yum install mcrypt -y

二、开始安装 PHP 服务

1. 获取 PHP 软件包

  1. [root@ylt001 tools]# wget http://cn2.php.net/get/php-7.1.22.tar.gz/from/this/mirror

2. 解压配置 PHP

  1. [root@ylt001 tools]# tar xzf php-7.1.22.tar.gz
  2. [root@ylt001 tools]# cd php-7.1.22
  3. [root@ylt001 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

  1. [root@ylt001 php-7.1.22]# ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/
  2. [root@ylt001 php-7.1.22]# touch ext/phar/phar.phar
  3. [root@ylt001 php-7.1.22]# make

4. 安装 PHP 生成文件到系统

  1. [root@ylt001 php-7.1.22]# make install

5. 配置 PHP 引擎配置文件 php.ini

  1. [root@ylt001 php-7.1.22]# ln -s /application/php-7.1.22 /application/php
  2. [root@ylt001 application]# ll /application/php
  3. lrwxrwxrwx 1 root root 24 Sep 26 04:10 /application/php -> /application/php-7.1.22/
  1. [root@ylt001 php-7.1.22]# ls php.ini*
  2. php.ini-development php.ini-production
  1. [root@ylt001 php-7.1.22]# cp php.ini-production /application/php/lib/php.ini
  2. [root@ylt001 php-7.1.22]# vim /application/php/lib/php.ini
  3. # 将 477 行改成 display_errors = On

6. 配置 PHP 服务的配置文件 php-fpm.conf

  1. [root@ylt001 php-7.1.22]# cd /application/php/etc/
  2. [root@ylt001 php-7.1.22]# ls
  3. pear.conf php-fpm.conf.default php-fpm.d
  4. [root@ylt001 etc]# cd php-fpm.d/
  5. [root@ylt001 php-fpm.d]# ls
  6. www.conf.default
  7. [root@ylt001 php-fpm.d]# cp www.conf.default ../php-fpm.conf

7. 启动 PHP 服务

  1. [root@ylt001 php-fpm.d]# /application/php/sbin/php-fpm
  2. [root@ylt001 php-fpm.d]# ps -ef|grep php-fpm
  3. root 1973 1 0 19:21 ? 00:00:00 php-fpm: master process (/application/php-7.1.22/etc/php-fpm.conf)
  4. nginx 1974 1973 0 19:21 ? 00:00:00 php-fpm: pool www
  5. nginx 1975 1973 0 19:21 ? 00:00:00 php-fpm: pool www
  6. root 1981 1872 0 19:22 pts/0 00:00:00 grep --color=auto php-fpm
  7. [root@ylt001 php-fpm.d]# lsof -i :9000
  8. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  9. php-fpm 1973 root 7u IPv4 24547 0t0 TCP localhost:cslistener (LISTEN)
  10. php-fpm 1974 nginx 5u IPv4 24547 0t0 TCP localhost:cslistener (LISTEN)
  11. php-fpm 1975 nginx 5u IPv4 24547 0t0 TCP localhost:cslistener (LISTEN)

三、配置 Nginx 支持 PHP 程序访问

1. 修改 Nginx 配置文件

  1. [root@ylt001 php-fpm.d]# cd /application/nginx/conf/
  2. [root@ylt001 conf]# cp nginx.con
  3. [root@ylt001 conf]# cat nginx.conf
  4. worker_processes 1;
  5. error_log logs/error.log;
  6. events {
  7. worker_connections 1024;
  8. }
  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;
  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';
  15. sendfile on;
  16. keepalive_timeout 65;
  17. include extra/www.conf;
  18. include extra/bbs.conf;
  19. include extra/blog.conf;
  20. include extra/status.conf;
  21. }
  1. [root@ylt001 conf]# cat extra/blog.conf
  2. server {
  3. listen 80;
  4. server_name blog.yangyangyang.org;
  5. location / {
  6. root html/blog;
  7. index index.html index.htm;
  8. }
  9. location ~ .*\.(php|php7)?$ {
  10. root html/blog;
  11. fastcgi_pass 127.0.0.1:9000;
  12. fastcgi_index index.php;
  13. include fastcgi.conf;
  14. }
  15. }

2. 检查并启动 Nginx

  1. [root@ylt001 conf]# ../sbin/nginx -t
  2. nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
  3. nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
  4. [root@ylt001 conf]# ../sbin/nginx -s reload

3. 测试 LNMP 环境生效情况

1)测试 PHP 解析请求是否 OK

  1. [root@ylt001 conf]# cd ../html/blog/
  2. [root@ylt001 blog]# echo "<?php phpinfo(); ?>" >test_info.php
  3. [root@ylt001 blog]# cat test_info.php
  4. <?php phpinfo(); ?>
  1. 192.168.2.133 www.yangyangyang.org bbs.yangyangyang.org blog.yangyangyang.org status.yangyangyang.org yangyangyang.org

phpinfo

2)针对 Nginx 请求访问 PHP,然后对 PHP 连接 MySQL 的情况进行测试

  1. [root@ylt001 blog]# mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 3
  5. Server version: 5.5.61 MySQL Community Server (GPL)
  6. ......
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. mysql> create database phpdb;
  9. mysql> show database;
  10. +--------------------+
  11. | Database |
  12. +--------------------+
  13. | information_schema |
  14. | ftpdb |
  15. | mysql |
  16. | performance_schema |
  17. | phpdb |
  18. +--------------------+
  19. 5 rows in set (0.01 sec)
  20. mysql> quit
  21. Bye
  22. [root@ylt001 blog]#
  1. [root@ylt001 blog]# cat test_mysqli.php
  2. <?php
  3. /* Database config */
  4. $db_host = 'localhost';
  5. $db_user = 'root';
  6. $db_pass = 'passwd';
  7. $db_database = 'phpdb';
  8. /* End config */
  9. $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_database);
  10. /* check connection */
  11. if (mysqli_connect_error()) {printf("Connect failed: %s\n", mysqli_connect_error());}
  12. else{echo "mysql successful by ylt !";}
  13. ?>

test_mysqli

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注