[服务器] 体验 Nginx + PHP FastCGI + MySQL
来源:互联网 发布:钢铁少女数据 编辑:程序博客网 时间:2024/04/28 08:05
最近 Nginx 很火,主要得益于它对并发请求的强大处理能力,这也是 Apache 以及 iis 的软肋所在,所以在一个比较大型的网络应用中,加入 Nginx 这个新元素就变得必不可少了,这里主要记录下一些主要的安装步骤,以及在安装中出现的问题。
软件版本:Nginx 0.6.32, PHP 5.2.6, MySQL 5.1.26.
安装过程的主要操作记录如下:
(Install required packages ..)
# wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
# wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.26-rc.tar.gz/from/http://mirror.x10.com/mirror/mysql/
# wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.8.diff.gz
# wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
# wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz
# wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
...
# tar -zxvf mysql-5.1.26-rc.tar.gz
# cd mysql-5.1.26-rc
# ./configure --prefix=/usr/local/mysql --without-debug --with-unix-socket-path=/usr/local/mysql/mysql.sock --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client
# make install clean
# chmod +w /usr/local/mysql/
# cd /usr/local/mysql/
# cp share/mysql/my-large.cnf /etc/my.cnf
(Replace port = 3306 , socket = /tmp/mysql.sock in /etc/my.cnf first.)
# bin/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
# cp share/mysql/mysql.server /usr/local/bin/mysql.server
# mysql.server start|status|stop (ok.)
...
# yum install (or apt-get -y install) libxml2-devel (Fix configure error: checking whether libxml build works)
# yum install (or apt-get -y install) curl-devel (Fix configure error: Please reinstall the libcurl distribution)
# yum install (or apt-get -y install) freetype-devel
# yum install (or apt-get -y install) libjpeg-devel
# yum install (or apt-get -y install) libpng-devel
# yum install (or apt-get -y install) mysql-devel
...
# tar -zxvf libiconv-1.12.tar.gz
# cd libiconv-1.12
# ./configure
# make install clean
...
# tar -zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make install clean
...
# tar -zxvf php-5.2.6.tar.gz
# gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1
# cd php-5.2.6
# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-libxml-dir=/usr --enable-xml --with-gd --enable-gd-native-ttf --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-discard-path --enable-safe-mode --enable-calendar --enable-bcmath --enable-shmop --enable-sysvsem --enable-pcntl --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --with-openssl --disable-debug --disable-rpath
# cp php.ini-dist /etc/php.ini
# make install clean
...
# tar -zxvf xcache-1.2.2.tar.gz
# cd xcache-1.2.2
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache
(Install `apt-get -y install autoconf` if phpize failed.)
# make install clean
...
(Config php.ini ..)
# sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"#' /etc/php.ini
# sed -i 's#display_errors = On#display_errors = Off#' /etc/php.ini (if production envionment)
# vi /etc/php.ini (Add follow config to the end of the file)
... ...
[xcache-common]
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"
[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo | grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo | grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"
(Config php-fpm.conf)
... ...
<value name="user">nginx</value> (add user nginx:nginx first for nginx server)
<value name="group">nginx</value>
... ...
<value name="max_children">128</value>
... ...
<value name="rlimit_files">51200</value>
... ...
<value name="max_requests">51200</value>
... ...
# ulimit -SHn 51200
# /usr/local/php/sbin/php-fpm start|stop|restart (ok.)
以上安装过程与传统的 php 安装用做 apache 的 mod_php 的方式有一定的区别,特别要注意的是:如果你想同时把 php 安装成为 mod_php 和 fast_cgi 模式的话,是不能成功的,也就是说 --enable-fastcgi 与 --with-apxs2=xxx 选项是不能同时选中的,否则安装会没有效果。
(Intergrate with Nginx ..)
# tar zxvf pcre-7.7.tar.gz
# cd pcre-7.7/
# ./configure
# make install clean
# tar zxvf nginx-0.6.32.tar.gz
# cd nginx-0.6.32/
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
# make install clean
(Set your own conf/nginx.conf ..)
另附上 nginx.server 脚本,用于管理 nginx 服务:
以上是我在自己的测试服务器上面配置的 Nginx Virtual Host,大家可以参考一下。个人习惯把 mysql.server, nginx.server, php-fpm 这些常用的控制工具 copy 到 /usr/local/bin 下以方便操作。要注意的是,在配置文件中必须写明 fastcgi_param SCRIPT_FILENAME /path/to/your/scripts/$fastcgi_script_name; 此项,否则在运行的时候会出现 No input file specified. 错误。
到这里你运行 php-fpm start 和 nginx.server start 就可以正常编写 php 服务器端程序了。至于 php error log 的配置你必须在 php.ini 中单独指定存放文件,这个和 php 作为 apache 的 mod_php 模式时也是有区别的,特此提醒,全文完。
软件版本:Nginx 0.6.32, PHP 5.2.6, MySQL 5.1.26.
安装过程的主要操作记录如下:
(Install required packages ..)
# wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
# wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.26-rc.tar.gz/from/http://mirror.x10.com/mirror/mysql/
# wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.8.diff.gz
# wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
# wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz
# wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
...
# tar -zxvf mysql-5.1.26-rc.tar.gz
# cd mysql-5.1.26-rc
# ./configure --prefix=/usr/local/mysql --without-debug --with-unix-socket-path=/usr/local/mysql/mysql.sock --with-extra-charsets=gbk,gb2312,utf8 --with-pthread --enable-thread-safe-client
# make install clean
# chmod +w /usr/local/mysql/
# cd /usr/local/mysql/
# cp share/mysql/my-large.cnf /etc/my.cnf
(Replace port = 3306 , socket = /tmp/mysql.sock in /etc/my.cnf first.)
# bin/mysql_install_db --defaults-file=/etc/my.cnf --user=mysql
# cp share/mysql/mysql.server /usr/local/bin/mysql.server
# mysql.server start|status|stop (ok.)
...
# yum install (or apt-get -y install) libxml2-devel (Fix configure error: checking whether libxml build works)
# yum install (or apt-get -y install) curl-devel (Fix configure error: Please reinstall the libcurl distribution)
# yum install (or apt-get -y install) freetype-devel
# yum install (or apt-get -y install) libjpeg-devel
# yum install (or apt-get -y install) libpng-devel
# yum install (or apt-get -y install) mysql-devel
...
# tar -zxvf libiconv-1.12.tar.gz
# cd libiconv-1.12
# ./configure
# make install clean
...
# tar -zxvf libmcrypt-2.5.8.tar.gz
# cd libmcrypt-2.5.8
# ./configure
# make install clean
...
# tar -zxvf php-5.2.6.tar.gz
# gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1
# cd php-5.2.6
# ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-libxml-dir=/usr --enable-xml --with-gd --enable-gd-native-ttf --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-discard-path --enable-safe-mode --enable-calendar --enable-bcmath --enable-shmop --enable-sysvsem --enable-pcntl --with-curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --with-openssl --disable-debug --disable-rpath
# cp php.ini-dist /etc/php.ini
# make install clean
...
# tar -zxvf xcache-1.2.2.tar.gz
# cd xcache-1.2.2
# /usr/local/php/bin/phpize
# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-xcache
(Install `apt-get -y install autoconf` if phpize failed.)
# make install clean
...
(Config php.ini ..)
# sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"#' /etc/php.ini
# sed -i 's#display_errors = On#display_errors = Off#' /etc/php.ini (if production envionment)
# vi /etc/php.ini (Add follow config to the end of the file)
... ...
[xcache-common]
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so
[xcache.admin]
xcache.admin.user = "xcache"
; xcache.admin.pass = md5($yourpasswd)
xcache.admin.pass = "e10adc3949ba59abbe56e057f20f883e"
[xcache]
xcache.cacher = On
xcache.shm_scheme = "mmap"
xcache.size = 32M
; cpu number (cat /proc/cpuinfo | grep -c processor)
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 2M
; cpu number (cat /proc/cpuinfo | grep -c processor)
xcache.var_count = 2
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.readonly_protection = Off
xcache.mmap_path = "/dev/zero"
(Config php-fpm.conf)
... ...
<value name="user">nginx</value> (add user nginx:nginx first for nginx server)
<value name="group">nginx</value>
... ...
<value name="max_children">128</value>
... ...
<value name="rlimit_files">51200</value>
... ...
<value name="max_requests">51200</value>
... ...
# ulimit -SHn 51200
# /usr/local/php/sbin/php-fpm start|stop|restart (ok.)
以上安装过程与传统的 php 安装用做 apache 的 mod_php 的方式有一定的区别,特别要注意的是:如果你想同时把 php 安装成为 mod_php 和 fast_cgi 模式的话,是不能成功的,也就是说 --enable-fastcgi 与 --with-apxs2=xxx 选项是不能同时选中的,否则安装会没有效果。
(Intergrate with Nginx ..)
# tar zxvf pcre-7.7.tar.gz
# cd pcre-7.7/
# ./configure
# make install clean
# tar zxvf nginx-0.6.32.tar.gz
# cd nginx-0.6.32/
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
# make install clean
(Set your own conf/nginx.conf ..)
- user nginx nginx;
- worker_processes 4;
- # [ debug | info | notice | warn | error | crit ]
- error_log logs/error.log crit;
- pid logs/nginx.pid;
- events {
- # use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
- use epoll;
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 60;
- fastcgi_connect_timeout 60;
- fastcgi_send_timeout 180;
- fastcgi_read_timeout 180;
- fastcgi_buffer_size 128k;
- fastcgi_buffers 4 128k;
- fastcgi_busy_buffers_size 128k;
- fastcgi_temp_file_write_size 128k;
- fastcgi_temp_path /dev/shm;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 8k;
- gzip_http_version 1.1;
- gzip_types text/plain application/x-javascript text/css text/html application/xml;
- charset utf-8;
- server {
- listen 8000;
- server_name localhost;
- index index.html index.htm index.php;
- root /home/nginx/web/www;
- log_format access '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" $http_x_forwarded_for';
- access_log /home/nginx/web/logs/access.log access;
- error_log /home/nginx/web/logs/error.log crit;
- if (-d $request_filename)
- {
- rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
- }
- location ~ .*/.phpx?$
- {
- include fastcgi_params;
- # Add for fastcgi
- fastcgi_param SCRIPT_FILENAME /home/nginx/web/www/phps/$fastcgi_script_name;
- # Add End
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- }
- error_page 404 /404.html;
- error_page 500 502 503 504 /50x.html;
- location ~ .*/.html?$
- {
- root /home/nginx/web/www/html;
- }
- location ~ ^/(images|img|javascript|js|css|flash|media|static)/
- {
- root /home/nginx/web/www/htdocs;
- access_log off;
- expires 30d;
- }
- location /NginxStatus {
- stub_status on;
- access_log on;
- auth_basic "NginxStatus";
- auth_basic_user_file htpasswd;
- }
- }
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
- #!/bin/sh
- #
- # Nginx daemon control script.
- #
- # This is an init script for the nginx daemon.
- # To use nginx, you must first set up the config file(s).
- #
- # Written for Slackware Linux by Cherife Li <cherife@dotimes.com>.
- ROOTPATH="/usr/local/nginx"
- DAEMON="$ROOTPATH/sbin/nginx"
- CONF="$ROOTPATH/conf/nginx.conf"
- PID="$ROOTPATH/logs/nginx.pid"
- nginx_start() {
- # Sanity checks.
- if [ ! -r $CONF ]; then # no config file, exit:
- echo "Please check the nginx config file, exiting..."
- exit 1
- fi
- if [ -s $PID ]; then
- echo "Nging is already running?"
- exit 1
- fi
- echo "Starting Nginx server daemon:"
- if [ -x $DAEMON ]; then
- $DAEMON -c $CONF
- fi
- }
- nginx_test_conf() {
- echo "Checking configuration for correct syntax and"
- echo "then trying to open files referenced in configuration..."
- $DAEMON -t -c $CONF
- }
- nginx_term() {
- echo "Shutdown Nginx quickly..."
- kill -TERM $(cat $PID)
- }
- nginx_stop() {
- echo "Shutdown Nginx gracefully..."
- kill -QUIT $(cat $PID)
- }
- nginx_reload() {
- echo "Reloading Nginx configuration..."
- kill -HUP $(cat $PID)
- }
- nginx_upgrade() {
- echo "Upgrading to the new Nginx binary."
- echo "Make sure the Nginx binary has been replaced with new one"
- echo "or Nginx server modules were added/removed."
- kill -USR2 $(cat $PID)
- sleep 3
- kill -QUIT $(cat $PID.oldbin)
- }
- nginx_restart() {
- nginx_stop
- sleep 5
- nginx_start
- }
- case "$1" in
- check)
- nginx_test_conf
- ;;
- start)
- nginx_start
- ;;
- term)
- nginx_term
- ;;
- stop)
- nginx_stop
- ;;
- reload)
- nginx_reload
- ;;
- restart)
- nginx_restart
- ;;
- upgrade)
- nginx_upgrade
- ;;
- *)
- echo "usage: $0 {check|start|term|stop|reload|restart|upgrade}"
- esac
到这里你运行 php-fpm start 和 nginx.server start 就可以正常编写 php 服务器端程序了。至于 php error log 的配置你必须在 php.ini 中单独指定存放文件,这个和 php 作为 apache 的 mod_php 模式时也是有区别的,特此提醒,全文完。
- [服务器] 体验 Nginx + PHP FastCGI + MySQL
- yum安装nginx-mysql-php-fastcgi构建LNMP服务器
- Debian+Nginx+PHP(FastCGI)+MySQL搭建LNMP服务器(最新版)
- Nginx + PHP(FastCGI)”Web服务器
- Windows+Nginx+PHP(FastCGI)+MYSQL 整理
- Ubuntu下安装Nginx+PHP+MySQL+FastCGI
- Debian+Nginx+PHP(FastCGI)+MySQL搭建LNMP服务器(三版)
- Debian+Nginx+PHP(FastCGI)+MySQL搭建LNMP服务器(三版)
- CentOS上yum安装nginx+mysql+php+php-fastcgi
- Nginx+FastCGI+PHP-2
- nginx php fastcgi
- nginx php fastCGI配置
- nginx与php(fastcgi)
- linux下nginx+php(fastcgi)web服务器搭建
- Ubuntu 配置 NginX + PHP + FastCGI + MySQL 原来如此简单
- CentOS5下安装配置nginx+fastcgi php+mysql
- 通过yum安装nginx-mysql-php-fastcgi配置LNMP
- centos安装nginx+mysql+php+fastcgi+memcache最简单方法
- Jira一些零散看法
- GNU Lesser General Public License v3
- vc++访问javascript(4)--原来函数也是对象
- 带时分秒日历控件
- 进程地址空间的共享(可以实现一个程序只有一个实例运行)
- [服务器] 体验 Nginx + PHP FastCGI + MySQL
- HP RAMS介绍
- 基金也有抄底英雄
- Where can I download the Windows 2000 Resource Kit tools?
- 长线和价值的坚守者--OFII
- 经典SQL语句集锦
- 数学知识的小小应用在C语言中
- WSACreateEvent(),WSAEventSelect,WSAWaitForMultipleEvents(),WSAEnumNetworkEvents()
- Rootkit Unhooker LE v3.8.340.551