Nginx+PHP5搭建高效PHP应用服务器
文件位置 php /usr/local/php php.ini /etc/php.ini Nginx /usr/l...
扫描右侧二维码阅读全文
11
2008/04

Nginx+PHP5搭建高效PHP应用服务器

文件位置
php /usr/local/php
php.ini /etc/php.ini
Nginx /usr/local/nginx
mysql /usr/bin/
web目录 /var/www

准备PHP环境

yum -y install gd
yum -y install gd-devel
yum -y install libmcrypt
yum -y install libmcrypt-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install mysql
yum -y install mysql-devel
yum -y install libtool-ltdl
yum -y install libtool-ltdl-devel
yum -y install libjpeg-devel
yum -y install libpng-devel
yum -y install libmcrypt
yum -y install libmcrypt-devel

安装PHP模块
[coolcode linenum="off"]
./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-gd --enable-gd-native-ttf --with-mysql --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --with-mcrypt
[/coolcode]
make
make install

编译lighthttpd得到spawn-fcgi,用来运行FastCGI

./configure
make
cp ./src/spawn-fcgi /usr/local/php/bin

运行FastCGI,-C参数为开启进程数,如果内存大于3GB,可以开至64

/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 25 -u www -f /usr/local/php/bin/php-cgi

添加用户以及web发布目录

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
mkdir -p /var/www
chmod +w /var/www
chown -R www:www /var/www

创建ngnix日志

mkdir -p /var/log/nginx
chmod +w /var/log/nginx
chown -R www:www /var/log/nginx

编译安装Nginx

yum -y install pcre-devel
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install

配置运行Nginx

vi /usr/local/nginx/conf/ngnix.conf

view plaincopy to clipboardprint?
#user nobody;
worker_processes 10;
events {
? use epoll;
? worker_connections 1024;
}

http {
? include conf/mime.types;
? default_type 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 /var/log/nginx_access.log;

sendfile on;
? #tcp_nopush on;
? #keepalive_timeout 0;
? keepalive_timeout 65;
? gzip on;
? server {
? listen 80;
? server_name localhost;
? charset gb2312;
? #access_log logs/host.access.log main;
? root /var/www;
? #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;
? }
? location ~ \.php?$ {
? include conf/fcgi.conf;
? fastcgi_pass 127.0.0.1:9000;
? fastcgi_index index.php;
? }
? #location ~ /\.ht {
? # deny all;
? #}
? }
}

#user nobody;
worker_processes 10;
events {
? use epoll;
? worker_connections 1024;
}

http {
? include conf/mime.types;
? default_type 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 /var/log/nginx_access.log;

sendfile on;
? #tcp_nopush on;
? #keepalive_timeout 0;
? keepalive_timeout 65;
? gzip on;
? server {
? listen 80;
? server_name localhost;
? charset gb2312;
? #access_log logs/host.access.log main;
? root /var/www;
? #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;
? }
? location ~ \.php?$ {
? include conf/fcgi.conf;
? fastcgi_pass 127.0.0.1:9000;
? fastcgi_index index.php;
? }
? #location ~ /\.ht {
? # deny all;
? #}
? }
}

vi /usr/local/nginx/conf/fcgi.conf

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param REDIRECT_STATUS 200;

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

启动脚本

vi nginx.sh

!/bin/sh
ulimit -SHn 51200
/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 25 -u www -f /usr/local/php/bin/php-cgi
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

!/bin/sh
ulimit -SHn 51200
/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 25 -u www -f /usr/local/php/bin/php-cgi
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

chmod 755 nginx.sh

Last modification:November 26th, 2018 at 04:16 pm
If you think my article is useful to you, please feel free to appreciate

2 comments

  1. Johnny Woo

    一般是配置里面的fastcgi部分没有配置好.

  2. jspshiwo

    你好 :
    我的系统是debian 我刚新安装的 ngnix 就提示 502 Bad Gateway 错误 请问为什么啊 ?

    我用 netstat -anpo | grep "php-cgi" | wc -l 查看你 显示 1

    netstat -tlunp 显示如下

    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    tcp 0 0 127.0.0.1:10080 0.0.0.0:* LISTEN 2745/php-cgi
    tcp 0 0 127.0.0.1:2208 0.0.0.0:* LISTEN 2298/hpiod
    tcp 0 0 0.0.0.0:993 0.0.0.0:* LISTEN 2561/inetd
    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2810/mysqld
    tcp 0 0 0.0.0.0:110 0.0.0.0:* LISTEN 2561/inetd
    tcp 0 0 0.0.0.0:143 0.0.0.0:* LISTEN 2561/inetd
    tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2076/portmap
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2760/nginx
    tcp 0 0 127.0.0.1:52880 0.0.0.0:* LISTEN 2301/python
    tcp 0 0 0.0.0.0:113 0.0.0.0:* LISTEN 2561/inetd
    tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2416/cupsd
    tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2544/exim4

Leave a Comment