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
如何在 RHEL 9 上配置 DHCP 服务器如何在 RHEL 9 上配置 DHCP 服务器Jun 08, 2023 pm 07:02 PM

DHCP是“动态主机配置协议DynamicHostConfigurationProtocol”的首字母缩写词,它是一种网络协议,可自动为计算机网络中的客户端系统分配IP地址。它从DHCP池或在其配置中指定的IP地址范围分配客户端。虽然你可以手动为客户端系统分配静态IP,但DHCP服务器简化了这一过程,并为网络上的客户端系统动态分配IP地址。在本文中,我们将演示如何在RHEL9/RockyLinux9上安装和配置DHCP服务器。先决条件预装RHEL9或RockyLinux9具有sudo管理权限的普

在容器中怎么使用nginx搭建上传下载的文件服务器在容器中怎么使用nginx搭建上传下载的文件服务器May 15, 2023 pm 11:49 PM

一、安装nginx容器为了让nginx支持文件上传,需要下载并运行带有nginx-upload-module模块的容器:sudopodmanpulldocker.io/dimka2014/nginx-upload-with-progress-modules:latestsudopodman-d--namenginx-p83:80docker.io/dimka2014/nginx-upload-with-progress-modules该容器同时带有nginx-upload-module模块和ng

vue3项目打包发布到服务器后访问页面显示空白怎么解决vue3项目打包发布到服务器后访问页面显示空白怎么解决May 17, 2023 am 08:19 AM

vue3项目打包发布到服务器后访问页面显示空白1、处理vue.config.js文件中的publicPath处理如下:const{defineConfig}=require('@vue/cli-service')module.exports=defineConfig({publicPath:process.env.NODE_ENV==='production'?'./':'/&

服务器怎么使用Nginx部署Springboot项目服务器怎么使用Nginx部署Springboot项目May 14, 2023 pm 01:55 PM

1,将java项目打成jar包这里我用到的是maven工具这里有两个项目,打包完成后一个为demo.jar,另一个为jst.jar2.准备工具1.服务器2.域名(注:经过备案)3.xshell用于连接服务器4.winscp(注:视图工具,用于传输jar)3.将jar包传入服务器直接拖动即可3.使用xshell运行jar包注:(服务器的java环境以及maven环境,各位请自行配置,这里不做描述。)cd到jar包路径下执行:nohupjava-jardemo.jar>temp.txt&

python中怎么使用TCP实现对话客户端和服务器python中怎么使用TCP实现对话客户端和服务器May 17, 2023 pm 03:40 PM

TCP客户端一个使用TCP协议实现可连续对话的客户端示例代码:importsocket#客户端配置HOST='localhost'PORT=12345#创建TCP套接字并连接服务器client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)client_socket.connect((HOST,PORT))whileTrue:#获取用户输入message=input("请输入要发送的消息:&

Linux怎么在两个服务器直接传文件Linux怎么在两个服务器直接传文件May 14, 2023 am 09:46 AM

scp是securecopy的简写,是linux系统下基于ssh登陆进行安全的远程文件拷贝命令。scp是加密的,rcp是不加密的,scp是rcp的加强版。因为scp传输是加密的,可能会稍微影响一下速度。另外,scp还非常不占资源,不会提高多少系统负荷,在这一点上,rsync就远远不及它了。虽然rsync比scp会快一点,但当小文件众多的情况下,rsync会导致硬盘I/O非常高,而scp基本不影响系统正常使用。场景:假设我现在有两台服务器(这里的公网ip和内网ip相互传都可以,当然用内网ip相互传

如何使用psutil模块获取服务器的CPU、内存和磁盘使用率?如何使用psutil模块获取服务器的CPU、内存和磁盘使用率?May 07, 2023 pm 10:28 PM

psutil是一个跨平台的Python库,它允许你获取有关系统进程和系统资源使用情况的信息。它支持Windows、Linux、OSX、FreeBSD、OpenBSD和NetBSD等操作系统,并提供了一些非常有用的功能,如:获取系统CPU使用率、内存使用率、磁盘使用率等信息。获取进程列表、进程状态、进程CPU使用率、进程内存使用率、进程IO信息等。杀死进程、发送信号给进程、挂起进程、恢复进程等操作。使用psutil,可以很方便地监控系统的运行状况,诊断问题和优化性能。以下是一个简单的示例,演示如何

怎么在同一台服务器上安装多个MySQL怎么在同一台服务器上安装多个MySQLMay 29, 2023 pm 12:10 PM

一、安装前的准备工作在进行MySQL多实例的安装前,需要进行以下准备工作:准备多个MySQL的安装包,可以从MySQL官网下载适合自己环境的版本进行下载:https://dev.mysql.com/downloads/准备多个MySQL数据目录,可以通过创建不同的目录来支持不同的MySQL实例,例如:/data/mysql1、/data/mysql2等。针对每个MySQL实例,配置一个独立的MySQL用户,该用户拥有对应的MySQL安装路径和数据目录的权限。二、基于二进制包安装多个MySQL实例

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment