lnmp architecture implements dynamic php

LNMP dynamic website php

[TOC]

Xu Liangwei, is known as the benchmark Xu in all corners of the world. Years of Internet operation and maintenance work experience, once responsible for the automatic operation and maintenance management of large-scale cluster architecture. He is good at Web cluster architecture and automated operation and maintenance. He was responsible for the operation and maintenance of a large domestic e-commerce business.
Personal blog " Xu Liangwei Architect Road " has benefited tens of thousands of people.
Author Q: 552408925, 572891887
Architect group: 471443208

1. Overview of PHP-FastCGI

How Nginx FastCGI works

nginx fastcgi accesses php
1. The user sends an http request message to the nginx server
2. nginx will judge the request according to the file url and suffix
3. If the request is for static content, nginx will return the result directly to the user
4. If the request is For dynamic content, nginx will send the request to the fastcgi client, and send the request to php-fpm through fastcgi_pass
5. After php-fpm receives the request, it will pass it to the wrapper through the local listening socket
. 6. The wrapper will generate a new request after receiving the request. The thread calls the php dynamic program parsing server
7. If the user requests blog posts or content, PHP will request MySQL query results
8. If the user requests pictures, attachments, PHP will request nfs to store the query results
9, PHP will query The result is given to Nginx
10, and nginx will generate a response message and return it to the user

PHP-FPM installation configuration

//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

Configure PHP to connect to the database

#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 

Configure PHP to add extension modules

加载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

Configure PHP-FPM main configuration

//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

Detailed explanation of PHP5-FPM configuration

[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使用

Configure PHP-FPM error log

修改 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. Compile and install the LNMP architecture

1.1 Compile 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 Compile 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.3 Nginx supports 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 Install 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 w_root@'192.168.56.%' identified by '123456';

1.5 Install ownCloud

ownCloud is a free and open source personal cloud storage solution that can be freely obtained without payment, but users need to set up their own servers.

ownCloud is divided into two parts, the server side and the client side, which can be accessed through a browser, or can be used by installing special client software. The client software supports almost all mainstream platforms: Windows, Linux, iOS, Android.

In addition to cloud storage, ownCloud can also be used to synchronize calendars, contacts, and webpage bookmarks; it can realize multi-person online file synchronization and collaboration (similar to google documents or Duddle, etc.).

Official website: https://owncloud.org/
Download: https://owncloud.org/install/
Help document: https://doc.owncloud.org/

http configuration

[root@xuliangwei 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 configuration

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;
  }
}

configure owncloud

//目录权限
[root@xuliangwei 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  #→网盘根目录

[root@xuliangwei owncloud]# chown -R nginx.nginx apps/ config/ data/ themes/
[root@xuliangwei owncloud]# chmod 755 apps/ config/


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

[root@xuliangwei ~]# 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批量创建用户。


[root@xuliangwei ~]# 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"


[root@xuliangwei ~]# export OC_PASS=123456
[root@xuliangwei ~]# 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"


//重置密码
[root@xuliangwei ~]# 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'


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


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


//查看用户统计
[root@xuliangwei ~]# sudo -u www /soft/php/bin/php /soft/nginx/html/owncloud/occ user:report
[root@xuliangwei ~]# 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 |
+------------------+---+


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


//安装php的redis模块略
[root@KVM ~]# /application/php/bin/php -m|grep redis
redis

[root@KVM ~]# yum -y install redis
[root@KVM ~]# vim /etc/redis.conf
port 0
unixsocket /tmp/redis.sock
[root@KVM ~]# /etc/init.d/redis start
[root@KVM ~]# 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); //不超时

[root@KVM ~]# redis-cli
redis 127.0.0.1:6379> keys *


//tmpfs存储session
[root@KVM www]# mkdir session
[root@KVM 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

[root@KVM www]# echo "tmpfs /data/www/session/ tmpfs defaults,noatime,mode=1777 0 0" >> /etc/fstab
[root@KVM www]# mount -a
[root@KVM 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
[root@KVM 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


[root@KVM www]# vim /application/php/lib/php.ini
session.save_path = "/data/www/session"

[root@KVM www]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

2.Yum install LNMP architecture

Install LNMP schema

yum install nginx1.12 php7.2 Mriadb5.7

1. InstallationNginx

//1.使用Nginx官方提供的rpm包
[root@nginx ~]# 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安装
[root@nginx ~]# yum install nginx -y
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx

2. Install php7.2 using the third-party extension epel source

//移除旧版php
[root@nginx ~]# yum remove php-mysql-5.4 php php-fpm php-common

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

//安装php72版本
[root@nginx ~]# 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
[root@nginx ~]# systemctl start php-fpm
[root@nginx ~]# systemctl enable php-fpm

3. InstallationMariadb

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

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

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

Configure LNMP Architecture

1. Configure Nginxto achieve dynamic request forwarding tophp

[root@nginx ~]# 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. Add a phptest page

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

//使用mysqli模块测试连接mysql
[root@nginx ~]# 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
[root@nginx ~]# 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();
        }
        ?>

Detect LNMP architecture

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324768699&siteId=291194637