本文共 3384 字,大约阅读时间需要 11 分钟。
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(Nginx + PHP(FastCGI) + MySQL)是企业级 Web 服务架构的主流配置方案。以下是 LNMP 架构的详细配置方法:
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
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
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
nginx.conf
同级目录下,常用文件为 fastcgi.conf
和 fastcgi_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;}
netstat -nlpt | grep php-fpm
SCRIPT_FILENAME
。fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
。通过以上配置,Nginx 可以高效地与 PHP 进行通信,实现动态网页处理。LNMP 架构凭借其高性能、可扩展性和稳定性,成为企业级 Web 服务的主流选择。
转载地址:http://lyufk.baihongyu.com/