search
HomeBackend DevelopmentPHP Tutoriallinux后台服务器开发环境部署配置和验证(nginx+apache+php-fpm+FASTCGI)




linux后台服务器开发环境部署配置

引言
背景
随着公司互联网业务的不断增多,开发环境变得越来越复杂,为了便于统一服务器端的开发部署环境,特制定本配置文档。
使用软件
CentOS 6.3(Linux version 2.6.32-279.el6.x86_64)
gcc (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)
本次配置
Nginx 1.5.8
Apache 2.4.7
php 5.3.26

目的
  构造WEB前端技术架构,web前端的部署结构技术完全完成。
  完整描述 nginx + apache + FASTCGI(C/C++) 和nginx + php-fpm +FASTCGI(PHP)两种FASTCGI架构。
nginx 安装
软件准备
http://nginx.org/en/download.html
官网下载版本,nginx-1.5.8.tar.gz

http://www.pcre.org/
官网下载 pcre-8.34.tar.gz

http://zlib.net/
官网下载 zlib-1.2.5.tar.gz

http://www.openssl.org/source/
官网下载 openssl-1.0.1d.tar.gz

工作目录准备
建立工作目录 nginx_make,把上面下载的文件都放到该目录下。
解压所有的 .tar.gz 文件,对应出现个目录。
安装 pcre
cd pcre-8.34
./configure --prefix=/usr/local/pcre-8.34
make
make install
安装zlib
如果本地已经安装了 zlib ,可以不安装了。
安装步骤类似上面的,但是强烈建议编译安装的时候指定目录和安装版本。
安装openssl
如果本地已经安装 openssl,可以不安装了。
安装步骤类似上面的,但是强烈建议编译安装的时候指定目录和安装版本
安装nginx
cd nginx-1.5.8
./configure --user=root --group=root --prefix=/usr/local/nginx-1.5.8 --pid-path=/usr/local/nginx-1.5.8/nginx.pid --error-log-path=/data/logs/nginx/err/nginx --http-log-path=/data/logs/nginx/acc/nginx --with-http_gzip_static_module --with-poll_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-pcre=../pcre-8.34 --with-cc-opt=-O2
make
make install

apache 安装
软件准备
http://httpd.apache.org/
官网下载版本,httpd-2.4.7.tar.gz

http://apr.apache.org/
官网下载 apr-1.5.0.tar.gz

http://apr.apache.org/
官网下载 apr-util-1.5.3.tar.gz

http://www.pcre.org/
官网下载 pcre-8.34.tar.gz
工作目录准备
建立工作目录 apache_make,把上面下载的文件都放到该目录下。
解压所有的 .tar.gz 文件,对应出现个目录。
安装 apr
cd apr-1.5.0
./configure --prefix=/usr/local/apr-1.5.0
make
make install
安装 apr-util
cd apr-1.5.0
./configure --prefix=/usr/local/apr-util-1.5.3/ --with-apr=/usr/local/apr-1.5.3/bin/apr-1-config
安装 pcre
cd pcre-8.34
./configure --prefix=/usr/local/pcre-8.34/ --with-apr=/usr/local/apr-1.5.3/bin/apr-1-config
参考上面已经安装好的 pcre 。
安装apache
cd httpd-2.4.7
 ./configure --prefix=/usr/local/apache2.4.7/ --with-pcre=/usr/local/pcre-8.34/ --with-apr=/usr/local/apr-1.5.0/ --with-apr-util=/usr/local/apr-util-1.5.3/  
make
make install

php 安装
软件准备
http://www.php.net/releases/
官网下载版本,php-5.3.26.tar.gz
工作目录准备
建立工作目录 php_make,把上面下载的文件都放到该目录下。
解压所有的 .tar.gz 文件,对应出现个目录。
构造 libphp5.so
编译 php,构造 libphp5.so,用于 apache 解析 php。
cd php-5.3.26
make clean;
./configure --prefix=/usr/local/php-5.3.26 -with-apxs2=/usr/local/apache2.4.7/bin/apxs --with-curl --with-curlwrappers
make
make install
安装 php-fpm
编译 php,支持 php-fpm和 socket,并启用curl模块,用于 nginx 以 FASTCGI 模式解析执行 php。

cd php-5.3.26
make clean;
./configure --prefix=/usr/local/php-5.3.26 --enable-fpm --enable-sockets --with-curl --with-curlwrappers
make
make install

安装 php
cd php-5.3.26
make install
php.ini安装
cd php-5.3.26
cp php.ini-development /usr/local/php-5.3.26/lib/php.ini
nginx+php-fpm 配置
启动php-fpm
/usr/local/php-5.3.26/sbin/php-fpm -y /usr/local/php-5.3.26/etc/php-fpm.conf -c /usr/local/php-5.3.26/lib/php.ini
配置 nginx.conf
修改 /usr/local/nginx-1.5.8/conf/nginx.conf。
listen       8090;
charset utf-8;

location ~ \.php$ {
     /usr/local/web_umsa/
     FASTCGI_pass   127.0.0.1:9000;
     FASTCGI_index  index.php;
#FASTCGI_param  SCRIPT_FILENAME  $document_root$FASTCGI_script_name;
include        FASTCGI_params;
}

配置FASTCGI_params
修改 /usr/local/nginx-1.5.8/conf/FASTCGI_params。

# PHP only, required if PHP was built with --enable-force-cgi-redirect
FASTCGI_param  REDIRECT_STATUS    200;
FASTCGI_param  SCRIPT_FILENAME  $document_root$FASTCGI_script_name;

测试 phpinfo.php
按下面内容建立 php 测试文件。

 cat /usr/local/web_umsa/phpinfo.php
echo phpinfo();
?>
验证FASTCGI模式php
tcpdump -ilo  -XAvs0 port 9000
抓包,然后浏览器执行
http://169.254.10.12:8090/phpinfo.php
浏览器输出PHP 的安装配置信息。
能看到抓包,9000 端口有数据发送,可以确认,传输的协议是 FASTCGI协议。
apache 配置
php5_module模块引导
启用 /usr/local/apache2.4.7/conf/httpd.conf 里面的。
LoadModule php5_module        modules/libphp5.so
配置 Directory /
修改 /usr/local/apache2.4.7/conf/httpd.conf 里面的。


    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all


配置 DocumentRoot
修改 /usr/local/apache2.4.7/conf/httpd.conf 里面的。

DocumentRoot "/usr/local/web_umsa"
配置
修改 /usr/local/apache2.4.7/conf/httpd.conf 里面的。

#支持cgi
ScriptAlias /cgi-bin/ "/usr/local/web_umsa/cgi-bin/"
#支持 FASTCGI
ScriptAlias /fcg-bin/ "/usr/local/web_umsa/fcgi-bin/"

配置
修改 /usr/local/apache2.4.7/conf/httpd.conf 里面的。

AddType application/x-httpd-php .php .phtml .php3 .inc
AddType application/x-httpd-php-source .phps
配置
修改 /usr/local/apache2.4.7/conf/httpd.conf 里面的。


    AddHandler cgi-script .cgi .ums
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
    Options +ExecCGI


启动 apache
/usr/local/apache2.4.7/bin/apachectl start

验证 apache
http://169.254.10.12/phpinfo.php
http://169.254.10.12/

两个访问都能出来,证明安装成功。

apache-FASTCGI(C/C++) 配置
mod_fcgid加载
文件下载
官网 http://httpd.apache.org/mod_fcgid/ 下载
得到文件 mod_fcgid-2.3.9.tar.gz

编译安装
把 mod_fcgid-2.3.9.tar.gz 文件解压到 apache 原代码目录 httpd-2.4.7,得到目录
mod_fcgid-2.3.9,完整结构应该是  httpd-2.4.7/mod_fcgid-2.3.9。
执行apache 命令../bin/apachectl -k restart,把httpd 服务起来。
设置环境变量 APXS,设置后可查看效果
# echo $APXS
/usr/local/apache2.4.7/bin/apxs
其中的 /usr/local/apache2.4.7/bin/apxs 是刚才 apache 的安装目录。
执行目录下面的  
./configure.apxs;make;make install
查看安装结果
# grep "mod_fcgid.so" /usr/local/apache2.4.7/conf/httpd.conf
LoadModule fcgid_module modules/mod_fcgid.so
能查看到已经启用了 mod_fcgid 模块。
配置FASTCGI支持
配置
修改 /usr/local/apache2.4.7/conf/httpd.conf 里面的。



    SetHandler fcgid-script
    AddHandler fcgid-script .fcgi .ums
    Options FollowSymLinks
    AllowOverride All
    Order deny,allow
    Allow from all
    Options +ExecCGI


重启 apache,../bin/apachectl -k restart。
测试FASTCGI(C/C++)
下载FASTCGI库
官网http://www.FASTCGI.com/的http://www.FASTCGI.com/drupal/node/5位置(Current: download | docs | browse)下载 C/C++的开发包。最新版本是 2.4.1,得到文件fcgi-2.4.1-SNAP-0910052249.tar.gz。

编译库
解压文件,并进入目录。执行传统命令
 ./configure;make
看到
ranlib .libs/libfcgi.a
表示库已经生产,其他的编译错误不理会(是C++ 的错误,版本太老了)。
执行命令ls libfcgi/.libs/ -al,可以查询到 libfcgi.a和 libfcgi.so.0.0.0
动态库和静态库都有了。
构造例子
进入目录 exapmle,执行命令(编译cgi并部署到指定目录)
# gcc echo.c -o echo.ums ../libfcgi/.libs/libfcgi.a
#cp echo.ums /usr/local/web_umsa/fcgi-bin/
查看效果
http://169.254.10.12/fcgi-bin/echo.ums
可以看到页面输出:
Request number 8, Process ID: 28256
不断刷新页面,进程编号不变,序号不断增加。同时,服务器端查看发现
# ps aux|grep echo.ums
该进程一直存在,进程编号和页面一致。

开发环境验证(仅限于内部验证)
需要配置 nginx 转发,支持 .ums 解析。

#TCP长连接,nginx 作为  proxy
upstream tcp_keepalive {
    server 127.0.0.1:8080;
    keepalive 256;
}

#以php-fpm 实现的 fastcgi 部署方式
location ~ \.php$ {
     root           /data/php_project;
     fastcgi_pass   127.0.0.1:9000;
     fastcgi_index  index.php;
     #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
     include        fastcgi_params;
     #记得修改  fastcgi_params,增加配置
     # PHP only, required if PHP was built with --enable-force-cgi-redirect
     #fastcgi_param  REDIRECT_STATUS    200;
     #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;     
 }


#以为 .fcgi,.ums 结尾的请求使用http协议转发给 tcpend
location ~* \.(fcgi|ums)$ {
    #root           root
    # 向后端服务器发起请求时添加指定的header头信息
    #proxy_set_header Host $http_host;
    # 向后端服务器发送真实 IP
    #proxy_set_header X-Real-IP $remote_addr;
    # 让后端如php能直接通过变量获取真实IP
    #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass   http://tcpend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-By $server_addr:$server_port;
    proxy_set_header X-Forwarded-For $remote_addr;
    #proxy_set_header Connection "";
    proxy_connect_timeout 5s;
    proxy_read_timeout 10s;
    proxy_send_timeout 5s;
}    


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment