企业级 PHP 高并发解决方案 0x01 LNMP环境搭建
企业级 PHP 高并发解决方案 0x01 LNMP 环境搭建
环境设置
在拿到新的服务器或者虚拟机的时候,我们首先需要保证我们的环境是被正确配置的,这样可以减少生产环境出现问题的概率,这篇文章主要是用来讲解 PHP 的环境搭建。
FQDN 设置(重要)
FQDN(Fully Qualified Domain Name 全限定域名) 是用来识别我们的主机身份,这一点非常重要。
例如:主机名是 server01 , 域名是 maksim.website ,那么FQDN就是 server01.maksim.website。
step 1. 修改 /etc/hosts
$ > vim /etc/hosts
# 在文件里追加一行
192.168.17.101 server01.maksim.website server01
step 2. 配置网卡
$ > vim /etc/sysconfig/network
# 修改 HOSTNAME 的值为 server01
HOSTNAME=server01
关闭 selinux
SELinux 是 2.6 版本的 Linux 内核中提供的强制访问控制(MAC)系统,对于目前的linux系统来说基本都内置开启了该安全增强的 selinux 内核模块。但是它虽然安全,但是对于一般的用户来说,过于复杂,而且如果开启 selinux 有的时候回出现各种各样的问题。
# 临时关闭
$ > setenforce 0 &>> /dev/null
# 永久关闭
$ > sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
网络校时 (重要)
服务器时间需要校准的主要原因如下:
- 计划任务要执行,需要提供一个可靠的时间
- 服务期间进行通信,需要统一的一致时间
# 安装时命令和服务
$ > yum install ntp -y
# 开启 ntpd start
$ > service netpd start
# 开启自启动 ntpd
$ > chkconfig ntpd on
国内第三方 NTP : https://opsx.alibaba.com/service?lang=zh-cn&activeKey=2
源码编译安装 LNMP
在生产环境 yum 安装的一般是依赖文件(小文件),例如 vim ,gcc,make, 等命令,而像 PHP、 Nginx、MySQL 这些都需要使用源码进行编译安装,这是因为可以定制化配置,尤其是有软件允许我们开发一些二次开发,我们需要在安装的时候编译到可执行文件中去。
软件名称 | 预计编译时间 |
---|---|
MySQL | 852s ~ 15m |
Nginx | 27s ~ 1m |
PHP | 564s ~ 10m6 |
这是我在工作中总结的一个编译耗时。
编译MySQL
相关参数介绍
编译参数的说明
-DCMAKE_INSTALL_PREFIX | 安装到的软件目录 |
---|---|
-DMYSQL_DATADIR | 数据文件存储的路径 |
-DSYSCONFDIR | 配置文件路径 (my.cnf) |
-DENABLED_LOCAL_INFILE=1 | 使用localmysql客户端的配置 |
-DWITH_PARTITION_STORAGE_ENGINE | 使mysql支持分表 |
-DEXTRA_CHARSETS | 安装支持的字符集 |
-DDEFAULT_CHARSET | 默认字符集使用 这里配置为utf8mb4 |
-DDEFAULT_COLLATION | 连接字符集 |
-DWITH_SSL | 开启mysql的ssl使用 |
初始化参数说明
--basedir | 安装到的软件目录 |
---|---|
--datadir | 数据文件存储路径 |
--user | mysql使用的用户 |
脚本实现安装及其初始化
#!/bin/bash
#源码编译安装MySQL
mysql_install() {
#1、创建用户
$(id mysql) &>/dev/null
[ $? -ne 0 ] && useradd -s /sbin/nologin -M mysql
#2、解决依赖
yum install -y cmake
yum install -y ncurses-devel
#3、编译安装
cd /root/soft
tar zxvf mysql-5.6.33.tar.gz
cd mysql-5.6.33
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci-DWITH_SSL=bundled
make && make install
#配置文件
rm -rf /etc/my.cnf
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
#授权并初始化数据库
chown -R mysql:mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#配置服务、自启动和环境变量
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
service mysqld start
chkconfig --add mysqld
echo 'PATH=/usr/local/mysql/bin:$PATH' >>/etc/profile
#删除匿名用户
#设置root域名的密码
rpm -qa | grep expect
if [ $? -ne 0 ]; then
yum -y install expect
fi
#导入环境变量PATH
export PATH=/usr/local/mysql/bin:$PATH
#初始化root密码 删除匿名用户
echo '#!/usr/bin/expect
set timeout 60
spawn mysql_secure_installation
expect {
"enter for none" { send "\r"; exp_continue}
"Y/n" { send "Y\r" ; exp_continue}
"password" { send "123456\r"; exp_continue}
"Cleaning up" { send "\r"}
}
interact ' >mysql_secure_installation.exp
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp
}
#脚本开始时间
start_time=$(date +%s)
#执行的脚本代码
mysql_install
#脚本结束时间
end_time=$(date +%s)
#脚本执行花费时间
const_time=$((end_time - start_time))
echo 'Take time is: '$const_time's'
编译安装 Nginx
常见用法:
- web服务器软件 httpd http协议,同类的web服务器软件:apache nginx(俄罗斯) IIS(微软 fastcgi) lighttpd(德国)
- 代理服务器 ,反向代理
- 邮箱代理服务器 IMAP POP3 SMTP
- 负载均衡功能 LB loadblance
Nginx架构的特点:
- 高可靠:稳定性 master进程 管理调度请求分发到哪一个worker=> worker进程 响应请求 一master多worker
- 热部署 :(1)平滑升级 (2)可以快速重载配置
- 高并发:可以同时响应更多的请求 事件 epoll模型 几万
- 响应快:尤其在处理静态文件上,响应速度很快 sendfile
- 低消耗:cpu和内存 1w个请求 内存2-3MB
- 分布式支持 :反向代理 七层负载均衡
安装
安装依赖
shell > yum -y install pcre-devel zlib-devel openssl-devel
自动化脚本
#!/bin/bash
#编译安装Nginx
nginx_install(){
#创建软件运行用户
`id www` &>>/dev/null
if [ $? -ne 0 ];then
useradd -s/sbin/nologin -M www
fi
#安装依赖
yum -y install pcre-devel zlib-devel openssl-devel
#编译安装
cd /root/soft
tar xvf nginx-1.14.2.tar.gz
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module && make && make install
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
nginx_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'
编译参数说明
参数 | 作用 |
---|---|
--prefix | 编译安装到的软件目录 |
--user | worker进程运行用户 |
--group | worker进程运行用户组 |
--with-http_ssl_module | 支持https 需要==pcel-devel==依赖 |
--with-http_stub_status_module | 基本状态信息显示 查看请求数、连接数等 |
--with-http_realip_module | 定义客户端地址和端口为header头信息 常用于反向代理后的真实IP获取 |
目录介绍
查看安装目录/usr/local/nginx
目录 | 作用 |
---|---|
conf | 配置文件 |
html | 网站默认目录 |
logs | 日志 |
sbin | 可执行文件 [软件的启动 停止 重启等] |
软件操作参数
查看nginx的二进制可执行文件的相关参数
shell > cd /usr/local/nginx/sbin
shell > ./nginx -h
执行后显示
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
#查看帮助
-?,-h : this help
#查看版本并退出
-v : show version and exit
#查看版本和配置选项并退出
-V : show version and configure options then exit
#检测配置文件语法并退出
-t : test configuration and exit
#检测配置文件语法打印它并退出
-T : test configuration, dump it and exit
#在配置测试期间禁止显示非错误信息
-q : suppress non-error messages during configuration testing
#发送信号给主进程 stop强制退出 quit优雅的退出 reopen重开日志 reload重载配置
-s signal : send signal to a master process: stop, quit, reopen, reload
#设置nginx目录 $prefix路径
-p prefix : set prefix path (default: /usr/local/nginx/)
#指定启动使用的配置文件
-c filename : set configuration file (default: conf/nginx.conf)
#在配置文件之外设置全局指令
-g directives : set global directives out of configuration file
一般主要使用:
-s参数控制管理nginx服务
-V参数查看nginx开启的模块和编译参数
-t参数检测配置文件是否有错误
服务配置
① 使用社区的服务配置文件
nginx编译包里默认没有服务启动脚本模板,可以通过社区获得
https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
上传脚本到/etc/init.d目录下
shell > vim /etc/init.d/nginx
修改软件和配置路径
#执行文件路径 第22行
nginx="/usr/local/nginx/sbin/nginx"
#配置文件路径 第25行
NGINIX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
②添加自启动
shell > chmod +x /etc/init.d/nginx
shell > chkconfig --add nginx
shell > chkconfig nginx on
注意在服务脚本中,有chkconfig配置开启模式、开启顺序、关闭顺序设置
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
# 开启模式(0-6) 开启顺序 关闭顺序
# chkconfig: - 85 15
PHP
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域。
PHP 独特的语法混合了C、Java、Perl以及PHP自创的语法。它可以比CGI或者Perl更快速地执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序嵌入到HTML(标准通用标记语言下的一个应用)文档中去执行,执行效率比完全生成HTML标记的CGI要高许多;PHP还可以执行编译后代码,编译可以达到加密和优化代码运行,使代码运行更快。
PHP-FPM(FastCGI Process Manager:FastCGI进程管理器**,对于PHP 5.3.3之前的php来说,是一个补丁包 ,旨在将FastCGI进程管理整合进PHP包中。
相对Spawn-FCGI,PHP-FPM在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab定时进行监控,而PHP-FPM则没有这种烦恼。 PHP5.3.3已经集成php-fpm了,不再是第三方的包了。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM**。
页面分类:
- 静态页面 一般普通访问到的页面
- 动态页面 用户可以和服务器进行交互页面
执行动态页面,需要和服务器进行交互,使用后端语言进行开发
LNMP 使用php进行开发交互
LAMP和LNMP在使用和配置PHP的区别:
安装
shell > yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
解压进入目录
shell > tar zxf php-7.2.12.tar.gz
shell > cd php-7.2.12
编译参数配置
shell > ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
--with 代表需要手动开启 可能需要加载第三方模块 第三方模块没有,就会error
--enable 代表开启php的默认功能
--without 关闭默认加载的模块
编译并安装到目录
shell > make && make install
查看PHP的安装目录
shell > cd /usr/local/php
shell > ls
目录名称 | 作用 |
---|---|
bin | php相关命令目录 php phpize、php-config在源码编译扩展时用 |
etc | 配置文件目录 |
include | php默认类库 |
lib | php第三方扩展类库 |
php | man文档文件 |
sbin | php-fpm执行文件 |
var | log日志目录 run运行目录 保存pid文件 |
配置
使用php-fpm进行管理php服务,有两个配置文件:
①php.ini #默认php配置文件
②php-fpm.conf #php-fpm相关的配置
复制配置文件
shell > cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
shell > cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
shell > cp /root/soft/php-7.2.12/php.ini-development /usr/local/php/etc/php.ini
添加启动服务
shell > cp /root/soft/php-7.2.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
shell > chmod +x /etc/init.d/php-fpm
shell > chkconfig --add php-fpm
添加环境变量(方便php、phpize、phpconfig查找使用)
shell > echo 'PATH=/usr/local/php/bin:$PATH' >> /etc/profile
shell > source /etc/profile
php安装脚本及其初始化配置
以下脚本,作为编译安装和配置php的参考
#!/bin/bash
php_install(){
#php编译安装
#和nginx使用相同的用户,如果没有就创建
`id www` &> /dev/null
[ $? -ne 0 ] && useradd -s /sbin/nologin -M www
#解决依赖
yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
#解压
tar xvf php-7.2.12.tar.gz
cd php-7.2.12
#编译安装php
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts && make && make install
#配置文件初始化
cp php.ini-development /usr/local/php/etc/php.ini
#php-fpm服务配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
#php-fpm服务子配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
#配置服务及其环境变量
cp /root/soft/php-7.2.12/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
service php-fpm start
chkconfig --add php-fpm
echo 'PATH=/usr/local/php/bin:$PATH' >> /etc/profile
}
#脚本开始时间
start_time=`date +%s`
#执行的脚本代码
php_install
#脚本结束时间
end_time=`date +%s`
#脚本执行花费时间
const_time=$((end_time-start_time))
echo 'Take time is: '$const_time's'
Nginx+php-fpm配置
①编写测试文件
shell > vim /usr/local/nginx/html/index.php
文件内容
<?php
phpinfo();
②在nginx.conf中配置
修改配置文件,告知nginx如果接收到.php结尾的请求,交由给php-fpm进行处理
shell > vim /usr/local/nginx/conf/nginx.conf
打开location .php 结尾那一段注释,并修改script为$document_root
#1、把root变量提升上层
root html;
location / {
#root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#2、默认使用上层的root变量
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#3、把script修改为$document_root $document_root 就是上面的root
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}