LNMP安装与配置
发布日期:2025-04-11 01:45:53 浏览次数:8 分类:精选文章

本文共 3384 字,大约阅读时间需要 11 分钟。

CGI 与 FastCGI 原理及 LNMP 架构配置实践

CGI(公共网关接口,Common Gateway Interface)是 HTTP 服务器与本机或其他机器上的程序进行通信的一种工具。CGI 程序需要在网络服务器上运行,是实现 Web 服务器与处理程序通信的重要技术。CGI 可以使用任何支持标准输入、输出和环境变量的语言,如 PHP、Perl、Tcl 等。

FastCGI 是 CGI 的一种改进方案,用于解决 CGI 程序反复加载导致性能低下的问题。FastCGI 是常驻型的 CGI,它通过保持 CGI 解释器进程在内存中,提升了性能和可扩展性。FastCGI 是一种协议,PHP 使用 PHP-FPM(FastCGI 进程管理器)来实现。PHP-FPM 负责管理 FastCGI 进程,提供高性能的 CGI 解释服务。

LNMP 架构概述

LNMP(Nginx + PHP(FastCGI) + MySQL)是企业级 Web 服务架构的主流配置方案。以下是 LNMP 架构的详细配置方法:

1. Nginx 源码安装

Nginx 是 LNMP 架构的核心组件,负责处理 HTTP 和 HTTPS 请求。

cd /usr/srcwget -c http://nginx.org/download/nginx-1.12.0.tar.gztar -xzf nginx-1.12.0.tar.gzcd nginx-1.12.0useradd www./configure --user=www --group=www --prefix=/usr/local/nginx \  --with-http_stub_status_module --with-http_ssl_modulemakemake install

2. MySQL 源码安装

MySQL 是 LNMP 架构的数据库组件,负责数据存储和查询。

yum install cmake ncurses-devel ncurses gcc-c++ -ywget -c https://cdn.mysql.com/archives/mysql-5.5/mysql-5.5.20.tar.gztar -xzf mysql-5.5.20.tar.gzcd mysql-5.5.20cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \  -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \  -DMYSQL_DATADIR=/data/mysql \  -DSYSCONFDIR=/etc \  -DMYSQL_USER=mysql \  -DMYSQL_TCP_PORT=3306 \  -DWITH_XTRADB_STORAGE_ENGINE=1 \  -DWITH_INNOBASE_STORAGE_ENGINE=1 \  -DWITH_PARTITION_STORAGE_ENGINE=1 \  -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \  -DWITH_MYISAM_STORAGE_ENGINE=1 \  -DEXTRA_CHARSETS=1 \  -DDEFAULT_CHARSET=utf8 \  -DDEFAULT_COLLATION=utf8_general_cimakemake install

3. PHP 源码安装

PHP 是 LNMP 架构的脚本语言组件,负责处理动态网页和应用逻辑。

yum install -y libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel \  libjpeg libjpeg-devel libpng libpng-devel freetype-develwget -c http://cn2.php.net/distributions/php-5.6.38.tar.gztar -zxf php-5.6.38.tar.gzcd php-5.6.38./configure --prefix=/usr/local/php \  --with-config-file-path=/usr/local/php/etc \  --with-apxs2=/usr/local/apache/bin/apxs \  --with-bz2 --with-curl --enable-ftp --enable-sockets \  --disable-ipv6 --with-gd --with-jpeg-dir --with-png-dir \  --with-freetype-dir --enable-gd-native-ttf --with-iconv-dir \  --enable-mbstring --enable-calendar --with-gettext --with-ldap \  --with-libxml-dir --with-zlib --with-pdo-mysql=mysqlnd \  --with-mysqli=mysqlnd --with-mysql=mysqlnd \  --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 \  --enable-bcmathmakemake install

4. Nginx 与 PHP 整合

配置原理

  • Nginx worker 进程:每个请求由 Nginx worker 进程处理。
  • PHP 处理程序:PHP 作为 CGI 程序,由 PHP-FPM 进程管理。
  • FastCGI 协议:PHP-FPM 监听本地 9000 端口,处理 PHP 请求。
  • 配置文件:FastCGI 配置文件通常在 nginx.conf 同级目录下,常用文件为 fastcgi.conffastcgi_params
  • 配置示例

    server {    include port.conf;    server_name www.jfedu.net jfedu.net;    index index.php index.html index.php;    root /usr/local/nginx/html;    location ~ \.php$ {        fastcgi_pass 127.0.0.1:9000;        fastcgi_index index.php;        include fastcgi_params;        # 动态添加 SCRIPT_FILENAME        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;    }}

    或者:

    location ~ \.php$ {    fastcgi_pass 127.0.0.1:9000;    fastcgi_index index.php;    include fastcgi.conf;}

    PHP-FPM 监听状态

    netstat -nlpt | grep php-fpm

    配置文件说明

  • fastcgi_params:包含 FastCGI 参数,默认缺少 SCRIPT_FILENAME
  • 动态添加:在需要的位置添加 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
  • 请求处理流程
    • Nginx 将请求转发给 PHP-FPM worker 进程。
    • PHP-FPM 将请求交给对应的 CGI 程序(如 PHP)。
    • CGI 程序处理后返回结果,PHP-FPM 将结果发送给 Nginx。
    • Nginx 将最终结果返回给客户端。
  • 通过以上配置,Nginx 可以高效地与 PHP 进行通信,实现动态网页处理。LNMP 架构凭借其高性能、可扩展性和稳定性,成为企业级 Web 服务的主流选择。

    上一篇:LNMP安装了哪些软件?安装目录在哪?
    下一篇:【Flink】No key set. This method should not be called outside of a keyed context.

    发表评论

    最新留言

    路过,博主的博客真漂亮。。
    [***.116.15.85]2025年04月27日 22时15分44秒