lnmp架构实现动态php

Posted oldxulinux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lnmp架构实现动态php相关的知识,希望对你有一定的参考价值。

LNMP动态网站php

[TOC]

徐亮伟, 江湖人称标杆徐。多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作。
个人博客"徐亮伟架构师之路"累计受益数万人。
笔者Q:552408925、572891887
架构师群:471443208

1.PHP-FastCGI概述

nginx FastCGI的运行原理

技术分享图片

nginx fastcgi访问php
1、用户发送http请求报文给nginx服务器
2、nginx会根据文件url和后缀来判断请求
3、如果请求的是静态内容,nginx会将结果直接返回给用户
4、如果请求的是动态内容,nginx会将请求交给fastcgi客户端,通过fastcgi_pass将这个请求发送给php-fpm
5、php-fpm收到请求后会通过本地监听的socket交给wrapper
6、wrapper收到请求会生成新的线程调用php动态程序解析服务器
7、如果用户请求的是博文、或者内容、PHP会请求mysql查询结果
8、如果用户请求的是图片、附件、PHP会请求nfs存储查询结果
9、php会将查询到的结果交给Nginx
10、nginx会生成一个响应报文返还给用户

PHP-FPM安装配置

//php7编译安装
useradd -M -s /sbin/nologin www
yum -y install openssl-devel bzip2-devel curl-devel db4-devel libjpeg-devel libpng-devel libXpm-devel gmp-devel libc-client-devel openldap-devel unixODBC-devel postgresql-devel sqlite-devel aspell-devel net-snmp-devel libxslt-devel pcre-devel mysql-devel  net-snmp-devel libxslt-devel libacl-devel systemtap kernel-devel yum-utils  systemtap-sdt-devel freetype freetype-devel mcrypt libmcrypt-devel mhash php-pgsql



##libiconv依赖
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar xf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && make install
cd ..


##下载php7进行编译安装

mkdir -p /soft/package/src/
cd /soft/package/src
wget http://cn.php.net/distributions/php-7.1.4.tar.gz
tar -xf php-7.1.4.tar.gz
cd php-7.1.4
./configure --prefix=/soft/php714 --with-config-file-path=/soft/php714/etc --with-iconv-dir=/usr/local/libiconv --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pdo-pgsql=pgsqlnd --with-pgsql=pgsqlnd --with-curl --with-gd --with-xpm-dir --with-jpeg-dir --with-png-dir --with-freetype-dir --with-xmlrpc --with-fpm-user=www --with-fpm-group=www --with-fpm-acl --with-mcrypt --with-tsrm-pthreads --with-gettext --with-libxml-dir --with-zlib --with-bz2 --with-openssl --with-mhash --enable-fpm --enable-opcache --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-calendar --enable-bcmath --enable-exif --enable-ftp --enable-mbstring --enable-shmop --enable-dtrace --enable-soap --enable-zip --enable-pdo --enable-xml --enable-pcntl --enable-mbregex --enable-opcache-file --enable-gd-native-ttf --enable-inline-optimization --enable-maintainer-zts --disable-rpath --disable-fileinfo

make ZEND_EXTRA_LIBS=‘-liconv -L/usr/local/libiconv/lib‘
make install

\cp php.ini-production /server/engine/php714/etc/php.ini
\cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
chmod +x /etc/init.d/php7-fpm


##centos7不支持libiconv

yum install libticonv libticonv-devel
--with-iconv=shared
make && make install

配置PHP与数据库连接

#php7测试MySQLi连接mysql
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
     
    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);
     
    // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    echo "连接成功";
    ?>


##测试pdo连接mysql
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
        echo "连接成功";
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    }
    ?>


##测试连接pgsql
yum install php-pgsql 

配置PHP新增扩展模块

加载redis扩展https://www.iamle.com/archives/1989.html 
    wget -c https://github.com/phpredis/phpredis/archive/php7.zip
    unzip php7.zip
    cd phpredis-php7
    /usr/local/php7/bin/phpize
    ./configure --with-php-config=/usr/local/php7/bin/php-config
    make
    make install
    cd ..

    /usr/local/php7/etc/php.ini
    中加入
    extension=redis.so


##加载memcache扩展
    https://github.com/websupport-sk/pecl-memcache/archive/php7.zip  # FTP上传
    cd pecl-memcache
    export PHP_PREFIX="/usr/local"
    $PHP_PREFIX/php70/bin/phpize
    ./configure --with-php-config=$PHP_PREFIX/php70/bin/php-config
    make && make install

配置PHP-FPM主要配置

//PHP5-FPM配置文件 4核16G、8核16G

[global]
pid = /var/run/php-fpm.pid
error_log = /soft/log/php/php-fpm.log
log_level = warning
rlimit_files = 655350
events.mechanism = epoll


[www]
user = www
group = www
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660
 
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 512
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.process_idle_timeout = 15s;
 
pm.max_requests = 2048
 
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www.log
php_admin_flag[log_errors] = on

request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log

PHP5-FPM配置详解释

[global]

//pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
pid = /var/run/php-fpm.pid

//错误日志,默认在安装目录中的/soft/log/php/php-fpm.log
error_log = /soft/log/php/php-fpm_error.log

//错误级别. 可用级别为: alert(必须立即处理),error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
log_level = warning

//设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认文件描述符1024可修改/etc/sysctl.conf调整文件描述符。 
rlimit_files = 655350

events.mechanism = epoll

//启动进程的用户和组
[www]
user = www
group = www

//fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: ‘ip:port‘, ‘/path/to/unix/socket‘. 
    每个进程池都需要设置.
listen = 127.0.0.1:9000


//unix socket设置选项,如果使用tcp方式访问,这里注释即可。
listen.owner = www
listen.group = www
   
//允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接
listen.allowed_clients = 127.0.0.1

//对于专用服务器,pm可以设置为static。
#如何控制子进程,选项有static和dynamic。如果选择static,则由pm.
pm = dynamic

//static模式下创建的子进程数或dynamic模式下同一时刻允许最大的php-fpm子进程数量
pm.max_children = 24

//动态方式下的起始php-fpm进程数量
pm.start_servers = 20
    
//动态方式下服务器空闲时最小php-fpm进程数量
pm.min_spare_servers = 10

//动态方式下服务器空闲时最大php-fpm进程数量
pm.max_spare_servers = 30

pm.max_requests = 1024
pm.process_idle_timeout = 15s;

//FPM状态页面.如果没有设置,则无法访问状态页面.监控php-fpm状态使用。
pm.status_path = /status

rlimit_files = 65535

php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www_error.log
php_admin_flag[log_errors] = on

request_slowlog_timeout = 5s
    #设置php超时时间,会将超时对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 ‘0‘ 表示 ‘Off‘

slowlog = /soft/log/php/php-slow.log
    #慢查询请求记录日志位置,配合request_slowlog_timeout使用

配置PHP-FPM错误日志

修改 php-fpm.conf 文件,添加(或修改)如下配置:
[global] 
error_log = /soft/log/php/php-fpm.log
[www] 
catch_workers_output = yes
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www.log
php_admin_flag[log_errors] = on

request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log

修改 php.ini 文件,添加(或修改)如下配置:
log_errors = On 
error_log = "/soft/log/php/php_error.log" 
error_reporting=E_ALL&~E_NOTICE
重启 php-fpm

1.编译安装LNMP架构

1.1编译Nginx

groupadd -g 888 www
useradd -u 888 -g 888 -s /sbin/nologin -M www
yum -y install pcre pcre-devel openssl-devel

mkdir -p /soft/package/src
cd /soft/package/src
wget http://nginx.org/download/nginx-1.12.2.tar.gz
tar xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/soft/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
make
make install
ln -s /soft/nginx-1.12.2/ /soft/nginx

1.2编译PHP

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel -y
yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel -y
yum -y install libmcrypt-devel mhash mcrypt


mkdir -p /soft/package/src
cd /soft/package/src
tar xf php-5.6.23.tar.gz 
cd php-5.6.23
./configure --prefix=/soft/php-fastcgi5.6.23 --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-jpeg-dir --with-png-dir --with-zlib --enable-xml  --with-libxml-dir --with-curl --enable-bcmath --enable-shmop --enable-sysvsem  --enable-inline-optimization --enable-mbregex --with-openssl --enable-mbstring --with-gd --enable-gd-native-ttf --with-freetype-dir=/usr/lib64 --with-gettext=/usr/lib64 --enable-sockets --with-xmlrpc --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --with-config-file-path=/usr/local/php-fastcgi/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www 
make && make install

ln -s /soft/php-fastcgi5.6.23/ /soft/php
cp /soft/package/src/php-5.6.23/php.ini-production /soft/php/etc/php.ini
cp  /soft/package/src/php-5.6.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp  /soft/php/etc/php-fpm.conf.default  /soft/php/etc/php-fpm.conf
chmod +x /etc/init.d/php-fpm


>  /soft/php/etc/php-fpm.conf

vim /soft/php/etc/php-fpm.conf
[global]
pid = /soft/php/var/run/php-fpm.pid
error_log = /soft/log/php/php-fpm.log
log_level = warning
rlimit_files = 655350
events.mechanism = epoll

[www]
user = www
group = www
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660

listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 512
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.process_idle_timeout = 15s;
pm.max_requests = 2048

catch_workers_output = yes
php_flag[display_errors] = off
php_admin_value[error_log] = /soft/log/php/php-www.log
php_admin_flag[log_errors] = on

request_slowlog_timeout = 5s
slowlog = /soft/log/php/php-slow.log


vim /soft/php/etc/php.ini
log_errors=On
error_log = "/soft/log/php/php_error.log"
error_reporting=E_ALL&~E_NOTICE


mkdir /soft/log/php -p

/etc/init.d/php-fpm start
netstat -lntup|grep php-fpm

1.3Nginx支持PHP

cd /soft/nginx/conf
mkdir default && mv  *.default  default/
egrep -v ‘#|^$‘ /soft/nginx/conf/default/nginx.conf.default > /soft/nginx/conf/nginx.conf


//在默认index选项添加 index.php
index  index.php index.html index.htm;

//添加nginx支持php的location

vim /soft/nginx/conf/nginx.conf
location ~ ^(.+\.php)(.*)$ {
   fastcgi_split_path_info       ^(.+\.php)(.*)$;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_pass  127.0.0.1:9000;
   fastcgi_index index.php;
   include fastcgi.conf;
}


/soft/nginx/sbin/nginx -t
/soft/nginx/sbin/nginx -s reload

vim /soft/nginx/html/index.php
<?php
    phpinfo();
?>

1.4安装MySQL

useradd mysql -s /sbin/nologin -M
cd /soft/package/src/
wget http://download.xuliangwei.com/mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
tar xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz
mv /soft/package/src/mysql-5.6.30-linux-glibc2.5-x86_64 /soft/mysql-5.6.30
ln -s /soft/mysql-5.6.30/ /soft/mysql
chown -R mysql.mysql /soft/mysql

mkdir /data/3306 -p
chown -R mysql.mysql /data/3306

/soft/mysql/scripts/mysql_install_db --basedir=/soft/mysql --datadir=/data/3306/ --user=mysql
cp /soft/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
sed -i ‘s#/usr/local/mysql#/soft/mysql#g‘ /soft/mysql/bin/mysqld_safe /etc/init.d/mysqld
cp /soft/mysql/support-files/my-default.cnf /etc/my.cnf

vim /etc/my.cnf
basedir = /soft/mysql
datadir = /data/3306
/etc/init.d/mysqld start

//将MySQL命令加入PATH变量, 记得退出终端重新登录生效
echo ‘export PATH=/soft/mysql/bin:$PATH‘ >/etc/profile.d/mysqld.sh

//设置mysql账号密码
mysqladmin password 123456

//登陆mysql
mysql -uroot -p123456

//安装wordpress
cd /soft/package/src/
wget https://cn.wordpress.org/wordpress-4.9.1-zh_CN.tar.gz
tar xf wordpress-4.9.1-zh_CN.tar.gz
mv /soft/package/src/wordpress /soft/nginx/html/
chown -R www.www /soft/nginx/html/wordpress/

//配置mysql远程访问账号及密码
mysql -uroot -p123456
create database wordpress;
grant all  on wordpress.* to [email protected]‘192.168.56.%‘ identified by ‘123456‘;

1.5安装ownCloud

ownCloud 是一个自由开源的个人云存储解决方案,可以自由获取无需付费,但用户需要自行架设服务器。

ownCloud分为服务器端和客户端两个部分,可通过浏览器访问,也可以安装专用的客户端软件来使用。客户端软件支持几乎所有的主流平台:Windows、Linux、iosandroid

除云存储外,ownCloud也可用于同步日历、联系人、网页书签;可以实现多人在线文件同步协作功能(类似google documents或Duddle等等)。

官网:https://owncloud.org/
下载:https://owncloud.org/install/
帮助文档:https://doc.owncloud.org/

http配置

[[email protected] onlien]# cat owncloud.conf
    server {
        listen       80;
        server_name  www.test.com;
        root   html/owncloud;
  rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
  rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
  rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
  index index.php;
  error_page 403 /core/templates/403.php;
  error_page 404 /core/templates/404.php;
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
    }
location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){
    deny all;
    }
  location / {
  # The following 2 rules are only needed with webfinger
  rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
  rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
  rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
  rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
  rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
  try_files $uri $uri/ /index.php;
  }
  location ~ \.php(?:$|/) {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_pass 127.0.0.1:9000;
  }
  # Optional: set long EXPIRES header on static assets
  location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
      expires 30d;
      # Optional: Don‘t log access to assets
        access_log off;
  }
}

https配置

upstream php-handler {
  server 127.0.0.1:9000;
  #server unix:/var/run/php5-fpm.sock;
}

server {
  listen 80;
  server_name cloud.example.com;
  # enforce https
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  server_name cloud.example.com;

  ssl_certificate /etc/ssl/nginx/cloud.example.com.crt;
  ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key;

  # Add headers to serve security related headers
  add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
  add_header X-Content-Type-Options nosniff;
  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";
  add_header X-Robots-Tag none;
  add_header X-Download-Options noopen;
  add_header X-Permitted-Cross-Domain-Policies none;

  # Path to the root of your installation
  root /var/www/owncloud/;
  # set max upload size
  client_max_body_size 10G;
  fastcgi_buffers 64 4K;

  # Disable gzip to avoid the removal of the ETag header
  gzip off;

  # Uncomment if your server is build with the ngx_pagespeed module
  # This module is currently not supported.
  #pagespeed off;

  index index.php;
  error_page 403 /core/templates/403.php;
  error_page 404 /core/templates/404.php;

  rewrite ^/.well-known/carddav /remote.php/dav/ permanent;
  rewrite ^/.well-known/caldav /remote.php/dav/ permanent;

  # The following 2 rules are only needed for the user_webfinger app.
  # Uncomment it if you‘re planning to use this app.
  #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
  #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }

  location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/ {
    deny all;
  }

  location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
    deny all;
  }

  location / {

    rewrite ^/remote/(.*) /remote.php last;

    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

    try_files $uri $uri/ =404;
  }

  location ~ \.php(?:$|/) {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS on;
    fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
    fastcgi_pass php-handler;
    fastcgi_intercept_errors on;
  }

  # Adding the cache control header for js and css files
  # Make sure it is BELOW the location ~ \.php(?:$|/) { block
  location ~* \.(?:css|js)$ {
    add_header Cache-Control "public, max-age=7200";
    # Add headers to serve security related headers
    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
    add_header X-Content-Type-Options nosniff;
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Robots-Tag none;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies none;
    # Optional: Don‘t log access to assets
    access_log off;
  }

  # Optional: Don‘t log access to other assets
  location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
    access_log off;
  }
}

配置owncloud

//目录权限
[[email protected] www]# ll -h
total 8.0K
drwxr-xr-x 15 root  root  4.0K Apr  5 12:00 owncloud
drwxrwx---  4 nginx nginx 4.0K Apr  5 12:04 owncloud_data  #→网盘根目录

[[email protected] owncloud]# chown -R nginx.nginx apps/ config/ data/ themes/
[[email protected] owncloud]# chmod 755 apps/ config/


//Nginx和php配置上传文件大小限制
[[email protected] ~]# grep 10m /application/nginx/conf/nginx.conf
    client_max_body_size 10m;

[[email protected] ~]# grep 10M /application/php/lib/php.ini
post_max_size = 10M
upload_max_filesize = 10M

----如果需要-----
max_input_time 3600
max_execution_time 3600


//命令行操作新增用户
user
 user:add            adds a user
 user:delete         deletes the specified user
 user:lastseen       shows when the user was logged it last
                     time
 user:report         shows how many users have access
 user:resetpassword  Resets the password of the named user


user:add [--password-from-env] [--display-name[="..."]] [-g|--group[="..."]] uid

display-name  web界面的全名
uid 登录用户名
group  没有会自动创建
password-from-env  从环境变量读取密码,使用这个参数需要在root权限下(su),sudo不会读取环境变量。这个参数可以作为shell批量创建用户。


[[email protected] ~]# sudo -u nginx /application/php/bin/php /data/www/owncloud/occ user:add --display-name="张耀" --group="users" --group="admin" zhangyao
Enter password: 
Confirm password: 
The user "zhangyao" was created successfully
Display name set to "张耀"
Created group "users"
User "zhangyao" added to group "users"
User "zhangyao" added to group "admin"


[[email protected] ~]# export OC_PASS=123456
[[email protected] ~]# su -s /bin/sh nginx -c ‘/application/php/bin/php /data/www/owncloud/occ user:add --password-from-env --display-name="张三" --group="users" --group="admin" zhangsan‘
The user "zhangsan" was created successfully
Display name set to "张三"
User "zhangsan" added to group "users"
User "zhangsan" added to group "admin"


//重置密码
[[email protected] ~]# sudo -u nginx /application/php/bin/php /data/www/owncloud/occ user:resetpassword zhangyao
Enter a new password: 
Confirm the new password: 
Successfully reset password for zhangyao


//也可以使用password-from-env重置密码
export OC_PASS=123456
su -s /bin/sh nginx -c ‘/application/php/bin/php /data/www/owncloud/occ user:resetpassword --password-from-env zhangsan‘


//删除用户
[[email protected] ~]# sudo -u nginx /application/php/bin/php /data/www/owncloud/occ user:delete zhangsan
The specified user was deleted


//查看用户最近登陆
[[email protected] ~]# sudo -u nginx /application/php/bin/php /data/www/owncloud/occ user:lastseen admin
admin`s last login: 06.04.2016 12:59


//查看用户统计
[[email protected] ~]# sudo -u www /soft/php/bin/php /soft/nginx/html/owncloud/occ user:report
[[email protected] ~]# sudo -u nginx /application/php/bin/php /data/www/owncloud/occ user:report
+------------------+---+
| User Report      |   |
+------------------+---+
| OC\User\Database | 2 |
|                  |   |
| total users      | 2 |
|                  |   |
| user directories | 3 |
+------------------+---+


//默认提供文件配置,只对新用户生效
[[email protected] skeleton]# pwd
/data/www/owncloud/core/skeleton
[[email protected] skeleton]# ls
Documents  Photos


//安装php的redis模块略
[[email protected] ~]# /application/php/bin/php -m|grep redis
redis

[[email protected] ~]# yum -y install redis
[[email protected] ~]# vim /etc/redis.conf
port 0
unixsocket /tmp/redis.sock
[[email protected] ~]# /etc/init.d/redis start
[[email protected] ~]# vim /data/www/owncloud/config/config.php
‘filelocking.enabled‘ => ‘true‘,
‘memcache.locking‘ => ‘\OC\Memcache\Redis‘,
‘memcache.local‘ => ‘\OC\Memcache\Redis‘,
‘redis‘ => array(
     ‘host‘ => ‘localhost‘,
     ‘port‘ => 6379,
      ),


//用socket模式会报错,代码有bug
  ‘memcache.local‘ => ‘\OC\Memcache\Redis‘,
  ‘redis‘ => array(
     ‘host‘ => ‘/tmp/redis.sock‘,
     ‘port‘ => 0,
  ),


//报错
2016/04/07 11:54:20 [error] 2067#0: *86 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught exception ‘RedisException‘ with message ‘Redis server went away‘ in /data/www/owncloud/lib/private/memcache/redis.php:78
Stack trace:
#0 /data/www/owncloud/lib/private/memcache/redis.php(78): Redis->get(‘8cee5d4894f3fbd...‘)
#1 /data/www/owncloud/lib/autoloader.php(164): OC\Memcache\Redis->get(‘OCP\\Util‘)
#2 [internal function]: OC\Autoloader->load(‘OCP\\Util‘)
#3 /data/www/owncloud/index.php(51): spl_autoload_call(‘OCP\\Util‘)
#4 {main}
  thrown in /data/www/owncloud/lib/private/memcache/redis.php on line 78" while reading response header from upstream, client: 192.168.0.61, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.0.200:8000"


经过查找各方资料,发现是php.ini文件中的一个配置项导致:
default_socket_timeout = 60
由于redis扩展也是基于php 的socket方式实现,因此该参数值同样会起作用。
找到了问题就比较好解决了:
1、直接修改php.ini,将其设置为我们想要的值(这个不推荐)
2、在我们的脚本中通过以下方式设置,这样就比较灵活,不对其他脚本产生影响
ini_set(‘default_socket_timeout‘, -1); //不超时

[[email protected] ~]# redis-cli
redis 127.0.0.1:6379> keys *


//tmpfs存储session
[[email protected] www]# mkdir session
[[email protected] www]# ll
total 12
drwxr-xr-x 15 root  root  4096 Apr  7 19:17 owncloud
drwxrwx---  7 nginx nginx 4096 Apr  8 14:01 owncloud_data
drwxr-xr-x  2 root  root  4096 Apr  8 14:12 session

[[email protected] www]# echo "tmpfs /data/www/session/ tmpfs defaults,noatime,mode=1777 0 0" >> /etc/fstab
[[email protected] www]# mount -a
[[email protected] www]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       684G  318G  332G  49% /
tmpfs           7.8G     0  7.8G   0% /dev/shm
/dev/sda1        93M   27M   61M  31% /boot
tmpfs           7.8G     0  7.8G   0% /data/www/session
[[email protected] www]# ll -h
total 8.0K
drwxr-xr-x 15 root  root  4.0K Apr  7 19:17 owncloud
drwxrwx---  7 nginx nginx 4.0K Apr  8 14:01 owncloud_data
drwxrwxrwt  2 root  root    40 Apr  8 14:14 session


[[email protected] www]# vim /application/php/lib/php.ini
session.save_path = "/data/www/session"

[[email protected] www]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

2.Yum安装LNMP架构

安装LNMP架构

yum安装 nginx1.12 php7.2 Mriadb5.7

1.安装Nginx

//1.使用Nginx官方提供的rpm包
[[email protected] ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

//2.执行yum安装
[[email protected] ~]# yum install nginx -y
[[email protected] ~]# systemctl start nginx
[[email protected] ~]# systemctl enable nginx

2.使用第三方扩展epel源安装php7.2

//移除旧版php
[[email protected] ~]# yum remove php-mysql-5.4 php php-fpm php-common

//安装扩展源
[[email protected] ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[[email protected] ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

//安装php72版本
[[email protected] ~]# yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-fpm php72w-mysqlnd php72w-opcache

//启动php
[[email protected] ~]# systemctl start php-fpm
[[email protected] ~]# systemctl enable php-fpm

3.安装Mariadb

//下载官方扩展源, 扩展源集成mysql5.6、5.7、8.0,仅5.7仓库是开启
[[email protected] ~]# rpm -ivh http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql57-community-release-el7-10.noarch.rpm
[[email protected] ~]# yum install mysql-community-server -y
[[email protected] ~]# systemctl start mysqld
[[email protected] ~]# systemctl enable mysqld

//如果mysql登陆需要密码,请查看该文件
[[email protected] ~]# grep ‘temporary password‘ /var/log/mysqld.log

//登陆mysql重新配置密码
[[email protected] ~]# mysql -uroot -p‘password‘
mysql> ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘MyNewPass4!‘;

配置LNMP架构

1.配置Nginx实现动态请求转发至php

[[email protected] ~]# cat /etc/nginx/conf.d/php.conf 
server {
        server_name _;
        listen 80;
        root /soft/code;
        index index.php index.html;

        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /soft/code$fastcgi_script_name;
                include        fastcgi_params;
        }
}

2.添加php测试页面

//测试phpinfo
[[email protected] ~]# cat /soft/code/info.php
<?php
        phpinfo();
?>

//使用mysqli模块测试连接mysql
[[email protected] ~]# cat /soft/code/mysqli.php
        <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
         
        // 创建连接
        $conn = mysqli_connect($servername, $username, $password);
         
        // 检测连接
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
        echo "连接成功";
        ?>

//使用pdo模块测试连接mysql
[[email protected] ~]# cat /soft/code/mysqlpdo.php
<?php
        $servername = "localhost";
        $username = "root";
        $password = "";

        try {
            $conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
            echo "连接成功";
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
        ?>

检测LNMP架构

技术分享图片

技术分享图片

技术分享图片
















以上是关于lnmp架构实现动态php的主要内容,如果未能解决你的问题,请参考以下文章

LNMP架构

LNMP架构的搭建

LNMP网站架构

Nginx架构篇LNMP动态网站环境部署

LAMP和LNMP的实现原理图

Lnmp架构部署动态网络环境