LNMP動態網站php
徐亮偉, 江湖人稱標桿徐。多年互聯網運維工作經驗,曾負責過大規模集群架構自動化運維管理工作。擅長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 w_root@'192.168.56.%' identified by '123456';
1.5安裝ownCloud
ownCloud 是一個自由開源的個人雲存儲解決方案,可以自由獲取無需付費,但用戶需要自行架設服務器。
ownCloud分為服務器端和客戶端兩個部分,可通過瀏覽器訪問,也可以安裝專用的客戶端軟件來使用。客戶端軟件支持幾乎所有的主流平台:Windows、Linux、iOS、Android。
除雲存儲外,ownCloud也可用於同步日歷、聯系人、網頁書簽;可以實現多人在線文件同步協作功能(類似google documents或Duddle等等)。
官網:https://owncloud.org/
下載:https://owncloud.org/install/
幫助文檔:https://doc.owncloud.org/
http配置
[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配置
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
//目錄權限
[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安裝LNMP架構
安裝LNMP架構
yum安裝 nginx1.12 php7.2 Mriadb5.7
1.安裝Nginx
//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.使用第三方擴展epel源安裝php7.2
//移除舊版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.安裝Mariadb
//下載官方擴展源, 擴展源集成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!';
配置LNMP架構
1.配置Nginx
實現動態請求轉發至php
[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.添加php
測試頁面
//測試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();
}
?>