构建php环境、安装、依赖、nginx配置、ab压力测试命令、添加php-fpm为系统服务

目录

php简介

官网php安装包

选择下载稳定版本

(建议使用此版本,文章以此版本为例)

安装php解析环境

准备工作

安装依赖

 zlib-devel 和 libxml2-devel包。 

安装扩展工具库 

安装 libmcrypt

安装 mhash

 安装mcrypt

安装php

选项含义: 

 php.ini配置调整

创建配置文件

配置文件部分信息

修改配置文件

添加php-fpm为系统服务: 

添加防火墙配置

添加nginx配置(server中添加)

完整nginx.conf配置文件(例子)

压力测试

安装httpd-tools软件包使用ab工具

ab命令


php简介

百度搜索php链接

PHP是一种流行的开源服务器端脚本语言,特别适用于网页开发,并且可以嵌入HTML中使用。它全称是“超文本预处理器”(Hypertext Preprocessor),由Rasmus Lerdorf于1994年创建。php的语法混合了C、java、perl及部分自创的新语法、拥有更好的网页执行速度、更重要的是php支持绝大多数流行的数据库、且支持linux、windows、unix等多种操作系统。PHP主要用于创建动态的网页或应用程序,可以生成动态内容、管理数据库、处理表单数据、会话跟踪等。由于其灵活性、可扩展性和广泛的社区支持,PHP被全球的网站广泛采用,从简单的个人博客到复杂的电子商务系统。

PHP脚本通常在服务器上执行,并将结果以HTML的形式发送给客户端浏览器。它支持多种数据库,如MySQL、PostgreSQL、SQLite等,并且可以与多种服务器如Apache、Nginx、IIS等协同工作。

官网php安装包

php官网下载链接建议使用稳定版5.X

选择下载稳定版本

(建议使用此版本,文章以此版本为例)

php.5.6.27版本官网下载链接 下载位置:php、downloads、Old archives

wget -O /usr/local/src/ https://www.php.net/distributions/php-5.6.27.tar.gz --no-check-certificate

php.8.3.7版本官网下载链接 下载位置:php、downloads、Current stable php .tar.gz   [19299kb]

php.8.3.7版本官网下载链接 下载位置:php、downloads、Current stable php tar.bz2  [15480kb]

php.8.3.7版本官网下载链接 下载位置:php、downloads、Current stable php tar.xg   [12164kb]

PHP For Windows: Binaries and sources Releases windows系统

安装php解析环境

准备工作

为了避免发生程序冲突等现象,建议先将RPM 方式安装的 php及相关依赖包(如果已存在)卸载。根据实际安装情况可卸载 php、php-cli、php-Idap、php-common、php-mysql等。

rpm -e php php-cli phpldap php-common php-mysql --nodeps
#!/bin/bash
# 列出所有PHP相关的包
php_packages=$(rpm -qa | grep 'php')
# 检查并卸载每个包
for package in $php_packages; do
    if [[ -n "$package" ]]; then
        echo "正在卸载: $package"
        yum -y remove "$package"
    fi
done

安装依赖

yum install -y gd gcc make autoconf automake libtool openssl-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel bzip2-devel libmcrypt-devel zlib-devel libxm12-devel

PHP 已经自带 FPM(FastCGl Process Manager,FastCGl 进程管理器)模块, 用来对PHP 解析实例进行管理、优化解析效率。单服务器的LNMP 架构通常使用这种方式,因此在配 PHP 编译选项时应添加“--enable-fpm 以启用此模块。为了提高 PHP 解析效率,建议将相应版本的ZendGuardLoader 也装上。

 zlib-devel 和 libxml2-devel包。 

zlib包

    • zlib-devel 包提供了 zlib 库的开发文件,包括头文件和静态库。
    • zlib 是一个广泛使用的压缩库,它提供了一种用于压缩和解压缩数据的方法,特别是在网络传输和文件存储中。
    • 在 PHP 中,zlib 库用于支持 gzencode()gzdecode()gzdeflate()gzinflate() 等压缩和解压缩功能。
    • 如果在构建 PHP 环境时没有安装 zlib-devel,那么 PHP 的 zlib 相关模块将无法编译,这可能会导致一些依赖于这些模块的功能无法正常工作。
  1. libxml2-devel:

    • libxml2-devel 包提供了 libxml2 库的开发文件,包括头文件和链接库。
    • libxml2 是一个用于解析和构建 XML 和 HTML 文档的库。
    • 在 PHP 中,libxml2 库用于支持各种 XML 处理函数,如 simplexml_load_string()dom_import_simplexml()xml_parse() 等。
    • 如果在构建 PHP 环境时没有安装 libxml2-devel,那么 PHP 的 XML 相关模块将无法编译,这会影响所有与 XML 处理相关的功能。

安装扩展工具库 

在实际企业应用中,一部分基于php开发的web应用系统会需要额外的扩展工具,、数据加密攻击libmcrypt、mhash、mcrypt等、  站点 http://sourceforge.net  下载

安装 libmcrypt

wget -O / https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar xf libmcrypt-2.5.8.tar.gz -C /usr/src
cd /usr/src/libmcrypt-2.5.8/
. /configure
make &&make install
ln -s   /usr/local/bin/libmcrypt_config   /usr/bin/libmcrypt_config
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

安装 mhash

wget -O / https://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
tar xf /mhash-0.9.9.9.tar.gz -C /usr/src/
cd /usr/src/mhash-0.9.9.9/
./configure
make  &&make  install
ln-s/usr/local/lib/libmhash*/usr/lib/

 安装mcrypt

wget -O / https://lcmp.googlecode.com/files/mcrypt-2.6.8.tar.gz
tar xf /mcrypt-2.6.8.tar.gz -C /usr/src/
export   LD_LIBRARY_PATH=/usr/local/lib
. /configure
make && make install

安装php

tar xf php-5.6.27.tar.gz -C /usr/src
cd /usr/src/php-5.6.27
./configure   --prefix=/usr/local/php5.6   \
--with-gd   \
--with-zlib   \
--with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config   \
--with-config-file-path=/usr/local/php5   \
--enable-mbstring   \
--enable-fpm   \
--with-jpeg-dir=/usr/lib && make && make install

选项含义: 

选项说明

--prefix:

指定将PHP程序安装到哪个目录下,如/usr/ local/php5.6

--with-mcrypt:

加载数据加密等扩展工具支持。

--with-apxs2:

设置 Apache HTTP Server提供的apxs模块支持程序的文件位置。

--with-mysql:

设置MySQL数据库服务程序的安装位置。

--with-mysqli:

添加 mysqli扩展支持。包含访问的mysql的一些命令

--with-config--file-path:

设置PHP的配置文件php.ini将要存放的位置。

--enable-mbstring:

启用多字节字符串功能,以便支持中文等代码。

–with-pdo-mysql使用 MySQL Native Driver 作为 PDO MySQL 支持库。
–with-openssl启用 OpenSSL 支持。
–enable-fpm启用 FastCGI 进程管理器(PHP-FPM)。
–enable-sockets启用套接字支持。
–enable-sysvshm启用 System V 共享内存支持。
–enable-mbstring启用多字节字符串支持。
–with-freetype-dir指定 FreeType2 安装路径(通常自动检测)。
–with-jpeg-dir指定 JPEG 安装路径(通常自动检测)。
–with-png-dir指定 PNG 安装路径(通常自动检测)。
–with-zlib启用 Zlib 支持(通常自动检测)。
–with-libxml-dir指定 libxml2 安装路径。
–enable-xml启用 XML 支持。
–with-mhash启用 mhash 支持。
–with-bz2启用 BZip2 支持。
–enable-maintainer-zts启用线程安全支持(仅适用于开发人员)。

 php.ini配置调整

在lnmp环境中设置,主要包括php的配置文件php.ini-development和php.ini-production

php.ini-development                开发版样例文件,用于学习、测试。

php.ini-production                    生产版样例文件,用于实际运营。

创建配置文件

cp php.ini-production /etc /php.ini
#提供php-fpm脚本
cp /usr/local/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm 
chkconfig php-fpmon

配置文件部分信息

default_charset = "UTF-8	            //设置默认字符集为utf-8
file_uploads = On	                    //允许通过PHP网页上传文件
upload_max_filesize = 2M	            // 允许上传的文件大小限制
max_file_uploads = 20	                //每个http最多允许请求上传的文件数量
post_max_size = 8M                    	//每次通过表单post提交的数据量限制
short_open_tag = On                    	//允许识别PHP短语法标记,即<?…?> 
extension=php_mysqli.dll                //添加mysql支持(修改或添加均可)

修改配置文件

pid = run/php-fpm.pid listen = 0.0.0.0 :9000     
pm.max_children = 50 
pm.start_servers = 5
pm.min_spare_servers = 5    
pm.max_spare_servers = 35 

添加php-fpm为系统服务: 

cat >> /lib/systemd/system/php-fpm.service << jingyu
[Unit]
Description=The PHP 7.4 FastCGI Process Manager
Documentation=man:php-fpm7.4
After=network.target

[Service]
Type=simple
PIDFile=/var/run/php74-fpm.pid
ExecStart=/usr/local/php74/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php74/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
jingyu


systemctl start php-fpm
netstat -anptl|grep php-fpm

添加防火墙配置

firewall-cmd --permanent --add-port=9000/tcp 
firewall-cmd --reload 

添加nginx配置(server中添加)

location ~ .*\.(php|php5)?$ { 
    root html;
    fastcgi_pass 127.0.0.1:9000; 
    fastcgi_index index.php;
    include fastcgi.conf;
    fastcgi_cache cache_fastcgi;
    fastcgi_cache_valid 200 302 1h; 
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m; 
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500; 
    fastcgi_cache_key                     
    http://$host$request_uri;
}

完整nginx.conf配置文件(例子)

user www www;
    worker_processes    4;
    worker_cpu_affinity 0001 0010 0100 1000; 
    error_log    logs/error.log;
    #error_log    logs/error.log    notice;
    #error_log    logs/error.log    info;

        pid                  logs/nginx.pid;
events {
use epoll;
worker_connections    65535;
multi_accept on; }

http {
include
default_type

mime.types;
application/octet-stream;

#log_format    main     '$remote_addr - $remote_user [$time_local] "$request" '
#                       '$status $body_bytes_sent "$http_referer" '
#                       '"$http_user_agent" "$http_x_forwarded_for"';

#access_log    logs/access.log    main;

sendfile                  on;
tcp_nopush           on;
keepalive_timeout    65; tcp_nodelay on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s; open_file_cache_valid 30s;
open_file_cache_min_uses 1; client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;  server_tokens off;
client_max_body_size 10m;

fastcgi_connect_timeout 600;
fastcgi_send_timeout 600; 
fastcgi_read_timeout 600; 
fastcgi_buffer_size 64k;
fastcgi_buffers   4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /usr/local/nginx1.10/nginx_tmp; 
fastcgi_intercept_errors on;
fastcgi_cache_path  /usr/local/nginx1.10/fastcgi_cache  levels=1:2
keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;

gzip on;
gzip_min_length    2k;
gzip_buffers  4 32k;
gzip_http_version 1.1; 
gzip_comp_level 6;
gzip_types  text/plain text/css  text/javascript  application/json   application/javascript application/x-javascript application/xml;
gzip_vary on;
gzip_proxiedany; 
server {
    listen     80;
    server_name    www.csdn-jingyu.com; 
#charset koi8-r;
#access_log logs/host.access.log main;

location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ { 
        valid_referers none blocked    
        www.benet.combenet.com;
        if ($invalid_referer) {
        #return 302    http://www.benet.com/img/nolink.jpg;
        return 404; break;
}
access_log off;
 }
location / {
    root      html;
    index    index.php index.html index.htm;
}
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ { 
    expires 30d;
    #log_not_found off; 
    access_log off;
}
location ~* \.(js|css)$ { 
    expires 7d;
    log_not_found off; 
    access_log off;
}

location = /(favicon.ico|roboots.txt) { 
    access_log off;
    log_not_found off; 
}
location /status {
stub_status on; 
}
location ~ .*\.(php|php5)?$ { 
root html;
fastcgi_pass 127.0.0.1:9000; 
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 1h; 
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m; 
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500; 
fastcgi_cache_key http://$host$request_uri;
}
#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;
        } 
    }
}

重启或重载nginx服务

压力测试

安装httpd-tools软件包使用ab工具

yum -y install httpd-tools
ab -c 5000 -n 50000 #地址或者域名

ab命令

选项参数说明
-n请求数执行的请求总数
-c并发数同时发送的请求个数
-t时间测试持续的秒数(与 -n 二选一)
-pPOST 文件包含 POST 请求的数据文件
-T内容类型POST 请求的内容类型(如 application/x-www-form-urlencoded
-H自定义头添加自定义 HTTP 头
-CCookie设置 Cookie 值
-k启用 HTTP KeepAlive在多次请求之间保持连接打开
-v详细输出显示更多的细节信息
-w以 HTML 表格形式输出将结果以 HTML 表格的形式输出到文件中
-x表格标签属性与 -w 一起使用,设置 HTML 表格的属性
-yCSV 输出将结果以 CSV 格式输出到文件中
-ZSSL 加密使用 SSL 加密(需要 Apache 2.2.12 或更高版本)
-zHTTP 压缩使用 HTTP 压缩
-i使用 HEAD 方法使用 HTTP HEAD 方法而不是 GET 方法
-X使用代理通过指定的代理服务器发送请求
-A认证使用基本认证(需要用户名和密码)
-PSSL 私钥文件SSL 私钥文件路径(用于客户端证书认证)
-FSSL 证书格式SSL 证书格式(PEM 或 DER)
-ESSL 证书文件SSL 证书文件路径(用于客户端证书认证)
  • 17
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jingyu飞鸟

醒来觉得甚是爱你。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值