為什么使用nginx+php(fastcgi)作為生產環境?我提出我的看法.

  1. php(fastcgi)不夠穩定,經常出現502錯誤,生成相對復雜的頁面沒有優勢,反而會使php-cgi進程變為僵尸進程.
  2. 安全性,多用戶多站點權限問題.php(fastcgi)在應對多用戶多站點往往捉襟見肘,不易于實施.
  3. 整合其他開發語言,apache表現得游刃有余.資源利用恰到好處.

為什么采用nginx做前端,apache作為后端的方案?nginx在處理靜態內容上較apache是幾倍或幾十倍的差異,因而放在前面過濾靜態內容是最為恰當的.同時nginx也是一個負載均衡器,低資源消耗,高性能轉發是它的特點.經過nginx在前面的過濾,后端的apache需要處理的內容相對就比較少了.只需負責處理動態內容就可以了.在性能與穩定性的權衡下,使用nginx+apache搭配會讓它們在各自擅長的領域展現自身的價值.

本教程以CentOS 5.4 32bit為環境.其他Linux發行版本暫未測試.nginx,php,apache,mysql,pureftpd均為最新穩定版.

獲取操作系統源更新.

yum update

yum -y install gcc gcc-c++ bison patch unzip mlocate flex wget automake autoconf gd cpp gettext readline-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel libidn libidn-devel openldap openldap-devel openldap-clients openldap-servers nss_ldap expat-devel libtool libtool-ltdl-devel

如果系統默認安裝了apache,請先卸載.執行:

yum remove httpd

下載最新穩定版的程序源碼包,以下都是到官方網站或sourceforge下載的源碼包.

cd /usr/local/src

wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.45.tar.gz/from/http://mysql.he.net/

wget http://www.apache.org/dist/httpd/httpd-2.2.15.tar.gz

wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz

wget http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2/download

wget http://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download

wget http://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2/download

wget http://www.php.net/get/php-5.2.13.tar.gz/from/this/mirror

wget http://php-fpm.org/downloads/php-5.2.13-fpm-0.5.13.diff.gz

wget http://www.lancs.ac.uk/~steveb/patches/php-mail-header-patch/php5-mail-header.patch

wget http://pecl.php.net/get/memcache-2.2.5.tgz

wget http://bart.eaccelerator.net/source/0.9.6/eaccelerator-0.9.6.tar.bz2

wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz

wget http://pecl.php.net/get/imagick-2.3.0.tgz

wget http://download.suhosin.org/suhosin-0.9.29.tgz

wget http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_lin_x86.tar.gz

wget http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz

wget http://monkey.org/~provos/libevent-1.4.13-stable.tar.gz

wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz

wget http://sourceforge.net/projects/pcre/files/pcre/8.01/pcre-8.01.tar.gz/download

wget http://nginx.org/download/nginx-0.7.65.tar.gz

wget http://download.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.28.tar.gz

一.安裝Mysql.安裝最新穩定版5.1.45版本,并沒有采用最新開發版.

cd /usr/local/src

tar -zxf mysql-5.1.45.tar.gz

cd mysql-5.1.45

./configure --prefix=/usr/local/mysql --enable-assembler --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innodb_plugin,myisam,myisammrg

make && make install

cd ../

groupadd mysql -g 27

useradd mysql -u 27 -g 27 -c "MySQL Server" -d /var/lib/mysql -M

cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

/usr/local/mysql/bin/mysql_install_db --user=mysql

chown -R mysql /usr/local/mysql/var

chgrp -R mysql /usr/local/mysql/.

cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql

chmod u+x /etc/init.d/mysql

chkconfig --level 345 mysql on

echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf

echo "/usr/local/lib" >>/etc/ld.so.conf

ldconfig

ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

ln -s /usr/local/mysql/include/mysql /usr/include/mysql

ln -s /usr/local/mysql/bin/mysql_config /usr/bin/mysql_config

service mysql start

/usr/local/mysql/bin/mysqladmin -u root password root

service mysql restart

service mysql stop

二.編譯安裝apache(httpd).apache的執行用戶為nobody.

cd /usr/local/src

tar -zxf httpd-2.2.15.tar.gz

cd httpd-2.2.15

./configure --prefix=/usr/local/apache --enable-headers --enable-mime-magic --enable-proxy --enable-rewrite --enable-ssl --enable-suexec --disable-userdir --with-included-apr --with-mpm=prefork --with-ssl=/usr --with-suexec-caller=nobody --with-suexec-docroot=/ --with-suexec-gidmin=100 --with-suexec-logfile=/usr/local/apache/logs/suexec_log --with-suexec-uidmin=100 --with-suexec-userdir=public_html

make

make install

mkdir /usr/local/apache/domlogs

cp /usr/local/apache/bin/apachectl /etc/init.d/httpd

1.編輯/etc/init.d/httpd,在首行#!/bin/sh下添加:

# Startup script for the Apache Web Server

#

# chkconfig: - 85 15

# description: Apache is a World Wide Web server. It is used to serve \

# HTML files and CGI.

# processname: httpd

# pidfile: /usr/local/apache/logs/httpd.pid

# config: /usr/local/apache/conf/httpd.conf

ulimit -n 1024

ulimit -n 4096

ulimit -n 8192

ulimit -n 16384

ulimit -n 32768

ulimit -n 65535

保存退出.

2.配置apache配置參數文件httpd.conf,位于/usr/local/apache/conf/目錄

cd /usr/local/apache/conf/

mv httpd.conf httpd.conf.bak

mkdir vhosts

vi httpd.conf

輸入以下內容:

PidFile logs/httpd.pid
LockFile logs/accept.lock
ServerRoot "/usr/local/apache"
Listen 0.0.0.0:81
User nobody
Group nobody
ServerAdmin admin@evlit.com
ServerName host.evlit.com

Timeout 300
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 5
UseCanonicalName Off
AccessFileName .htaccess
TraceEnable Off
ServerTokens ProductOnly
FileETag None
ServerSignature Off
HostnameLookups Off

# LoadModule perl_module modules/mod_perl.so

DocumentRoot "/usr/local/apache/htdocs"

<Directory "/">
 Options ExecCGI FollowSymLinks Includes IncludesNOEXEC -Indexes -MultiViews SymLinksIfOwnerMatch
 Order allow,deny
 Allow from all
 AllowOverride All
</Directory>

<Directory "/usr/local/apache/htdocs">
 Options Includes -Indexes FollowSymLinks
 AllowOverride None
 Order allow,deny
 Allow from all
</Directory>

DefaultType text/plain
RewriteEngine on
AddType text/html .shtml
AddHandler cgi-script .cgi .pl .plx .ppl .perl
AddHandler server-parsed .shtml
<IfModule mime_module>

    TypesConfig conf/mime.types
    AddType application/perl .pl .plx .ppl .perl
    AddType application/x-img .img
    AddType application/x-httpd-php .php .php3 .php4 .php5 .php6
    AddType application/x-httpd-php-source .phps
    AddType application/cgi .cgi
    AddType text/x-sql .sql
    AddType text/x-log .log
    AddType text/x-config .cnf conf
    AddType text/x-registry .reg
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddType application/x-tar .tgz
    AddType application/rar .rar
    AddType application/x-compressed .rar
    AddType application/x-rar .rar
    AddType application/x-rar-compressed .rar
    AddType text/vnd.wap.wml .wml
    AddType image/vnd.wap.wbmp .wbmp
    AddType text/vnd.wap.wmlscript .wmls
    AddType application/vnd.wap.wmlc .wmlc
    AddType application/vnd.wap.wmlscriptc .wmlsc
</IfModule>

<IfModule dir_module>
 DirectoryIndex index.html index.htm index.shtml index.php index.perl index.pl index.cgi
</IfModule>

<Files ~ "^error_log$">
 Order allow,deny
 Deny from all

 Satisfy All
</Files>

<FilesMatch "^\.ht">
 Order allow,deny
 Deny from all
 Satisfy All
</FilesMatch>

ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
 LogFormat "%h %l %u %t \"%r\" %>s %b" common

 <IfModule logio_module>

 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
 </IfModule>
 CustomLog "logs/access_log" common
</IfModule>

<IfModule alias_module>
 ScriptAlias /cgi-bin/ "/usr/local/apache/cgi-bin/"
</IfModule>

<Directory "/usr/local/apache/cgi-bin">
 AllowOverride None
 Options None
 Order allow,deny
 Allow from all
</Directory>

<IfModule mpm_prefork_module>
 StartServers          3
 MinSpareServers       3
 MaxSpareServers       5
 MaxClients          150
 MaxRequestsPerChild   1024
</IfModule>

<IfModule mod_headers.c>
<FilesMatch "\.(html|htm|shtml)$">

Header set Cache-Control "max-age=3600, must-revalidate"
</FilesMatch>
</IfModule>

ReadmeName README.html
HeaderName HEADER.html

IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t

Include conf/extra/httpd-languages.conf

<Location /server-status>
 SetHandler server-status
 Order deny,allow
 Deny from all
 Allow from 127.0.0.1
</Location>
ExtendedStatus On

<Location /server-info>
 SetHandler server-info
 Order deny,allow
 Deny from all
 Allow from 127.0.0.1

</Location>

<IfModule ssl_module>
Listen 0.0.0.0:443
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
SSLCipherSuite ALL:!ADH:+HIGH:+MEDIUM:-LOW:-SSLv2:-EXP
SSLPassPhraseDialog  builtin
SSLSessionCache         dbm:/usr/local/apache/logs/ssl_scache
SSLSessionCacheTimeout  300
SSLMutex  file:/usr/local/apache/logs/ssl_mutex
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

#Vhosts
NameVirtualHost 127.0.0.1:81
NameVirtualHost *

<VirtualHost 127.0.0.1:81 *>
 ServerName host.evlit.com
 DocumentRoot /var/www/html
 ServerAdmin admin@evlit.com
</VirtualHost>

Include conf/vhosts/*

上述出現的127.0.0.1請改為你本機公網IP.

三.編譯安裝php(mod_php)

1.編譯安裝相關支持庫

cd /usr/local/src

tar -zxf libiconv-1.13.1.tar.gz

cd libiconv-1.13.1/

./configure

make

make install

cd /usr/local/src

tar -jxf libmcrypt-2.5.8.tar.bz2

cd libmcrypt-2.5.8/

./configure

make

make install

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make

make install

cd /usr/local/src

tar -jxf mhash-0.9.9.9.tar.bz2

cd mhash-0.9.9.9/

./configure

make

make install

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la

ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so

ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4

ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8

ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

cd /usr/local/src

tar -zxf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8/

/sbin/ldconfig

./configure

make

make install

2.編譯php,這里我們為php打入補丁.有助于防止郵件發送被濫用(多用戶)以及在郵件中提供有價值的信息.補丁介紹信息請點擊:http://www.lancs.ac.uk/~steveb/patches/php-mail-header-patch/

cd /usr/local/src

tar -zxf php-5.2.13.tar.gz

patch -d php-5.2.13 -p1 < php5-mail-header.patch

cd php-5.2.13

./configure --prefix=/usr/local --with-config-file-path=/etc --with-apxs2=/usr/local/apache/bin/apxs --enable-bcmath --enable-calendar --enable-exif --enable-ftp --enable-gd-native-ttf --enable-libxml --enable-magic-quotes --enable-mbstring --enable-pdo=shared --enable-soap --enable-sockets --enable-zip --with-bz2 --with-curl --with-curlwrappers --with-freetype-dir --with-gd --with-gettext --with-jpeg-dir --with-kerberos --with-libexpat-dir=/usr --with-libxml-dir=/usr --with-mcrypt=/usr --with-mhash=/usr --with-mysql=/usr --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mysqli=/usr/bin/mysql_config --with-openssl=/usr --with-openssl-dir=/usr --with-pdo-mysql=shared --with-pdo-sqlite=shared --with-png-dir=/usr --with-sqlite=shared --with-ttf --with-xmlrpc --with-zlib -with-zlib-dir=/usr

make ZEND_EXTRA_LIBS='-liconv'

make install

cp php.ini-dist /etc/php.ini

3.安裝php擴展模塊

cd /usr/local/src

tar -zxf memcache-2.2.5.tgz

cd memcache-2.2.5/

phpize

./configure --with-php-config=/usr/local/bin/php-config --with-zlib-dir --enable-memcache

make

make install

cd /usr/local/src

tar -jxf eaccelerator-0.9.6.tar.bz2

cd eaccelerator-0.9.6/

phpize

./configure --enable-eaccelerator=shared --with-php-config=/usr/local/bin/php-config

make

make install

mkdir -p /tmp/eaccelerator

chmod 777 /tmp/eaccelerator

echo "mkdir -p /tmp/eaccelerator" >> /etc/rc.local

echo "chmod 777 /tmp/eaccelerator" >> /etc/rc.local

cd /usr/local/src

tar -zxf ImageMagick.tar.gz

cd ImageMagick-*

./configure

make

make install

cd /usr/local/src

tar -zxf imagick-2.3.0.tgz

cd imagick-2.3.0/

phpize

./configure --with-php-config=/usr/local/bin/php-config

make

make install

cd /usr/local/src

tar -zxf suhosin-0.9.29.tgz

cd suhosin-0.9.29

phpize

./configure

make

make install

cd /usr/local/src

tar -zxf ioncube_loaders_lin_x86.tar.gz

cd ioncube

mkdir /usr/local/ioncube

mv ioncube_loader_lin_5.2.so /usr/local/ioncube/

cd /usr/local/src

tar -zxf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz

mkdir -p /usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x

cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so

3.1.修改php.ini.

查找/etc/php.ini中的extension_dir = "./".將其修改為extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"

查找;include_path = ".:/php/includes",刪除前面的分號,并修改為include_path = ".:/usr/lib/php:/usr/local/lib/php"

跳到最后一行,然后添加以下內容:

extension = "memcache.so"

extension = "pdo.so"

extension = "pdo_mysql.so"

extension = "pdo_sqlite.so"

extension = "sqlite.so"

extension = "eaccelerator.so"

eaccelerator.shm_size = 32

eaccelerator.cache_dir = "/tmp/eaccelerator"

eaccelerator.enable = 1

eaccelerator.optimizer = 0

eaccelerator.debug = 0

eaccelerator.name_space = ""

eaccelerator.check_mtime = 1

eaccelerator.filter = ""

eaccelerator.shm_max = 0

eaccelerator.shm_ttl = 7200

eaccelerator.shm_prune_period = 7200

eaccelerator.shm_only = 1

eaccelerator.compress = 0

eaccelerator.compress_level = 9

eaccelerator.keys = shm

eaccelerator.sessions = shm

eaccelerator.content = shm

zend_extension = "/usr/local/ioncube/ioncube_loader_lin_5.2.so"

zend_extension = "/usr/local/Zend/lib/Optimizer-3.3.9/php-5.2.x/ZendOptimizer.so"

4,安裝Memcached(可選)

cd /usr/local/src

tar -xzf libevent-1.4.13-stable.tar.gz

cd libevent-1.4.13-stable

./configure

make

make install

ln -s /usr/local/lib/libevent-1.4.so.2 /usr/lib

cd /usr/local/src

tar -xzf memcached-1.4.4.tar.gz

cd memcached-1.4.4

./configure --with-libevent=/usr

make

make install

基本使用方法:

啟動:/usr/local/bin/memcached -d -m 64 -p 11211 -u nobody -l localhost

關閉:killall -9 memcached

四.安裝nginx

1.安裝pcre庫

cd /usr/local/src

tar -zxf pcre-8.01.tar.gz

cd pcre-8.01

./configure

make

make install

2.安裝nginx

cd /usr/local/src

tar -zxf nginx-0.7.65.tar.gz

cd nginx-0.7.65

./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --pid-path=/usr/local/nginx/logs/nginx.pid --error-log-path=/usr/local/nginx/logs/error.log --http-log-path=/usr/local/nginx/logs/access.log --http-client-body-temp-path=/tmp/nginx_client --http-proxy-temp-path=/tmp/nginx_proxy --http-fastcgi-temp-path=/tmp/nginx_fastcgi --with-http_stub_status_module

make

make install

2.1.添加init控制腳本

#! /bin/sh
ulimit -n 65535
# Description: Startup script for nginx
# chkconfig: 2345 55 25

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/nginx.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
 $DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
 kill -QUIT `cat $PIDFILE` || echo -n "nginx not running"
}

do_reload() {
 kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
}

case "$1" in
 start)
 echo -n "Starting $DESC: $NAME"
 do_start
 echo "."
 /etc/init.d/httpd start
 ;;
 stop)
 echo -n "Stopping $DESC: $NAME"
 do_stop
 echo "."
 /etc/init.d/httpd stop
 ;;
 reload)
 echo -n "Reloading $DESC configuration..."
 do_reload
 echo "."
 /etc/init.d/httpd restart
 ;;
 restart)
 echo -n "Restarting $DESC: $NAME"
 do_stop
 sleep 1
 do_start
 echo "."
 /etc/init.d/httpd restart
 ;;
 *)
 echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
 exit 3
 ;;
esac

exit 0

保存退出,給該文件賦予執行權限并設置開機啟動

chmod u+x /etc/init.d/nginx

chkconfig --level 345 nginx on

2.2.修改nginx配置文件,位于:/usr/local/nginx/conf/目錄

cd /usr/local/nginx/conf/

mv nginx.conf nginx.conf.bak

mkdir vhosts

vi nginx.conf

輸入以下內容:

worker_processes  1;
worker_rlimit_nofile  65535;
events {
 worker_connections  65535;
 use epoll;
}
error_log  /usr/local/nginx/logs/error.log info;
http {
 include    mime.types;
 default_type  application/octet-stream;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  10;
 gzip on;
 gzip_http_version 1.0;
 gzip_min_length  1100;
 gzip_comp_level  3;
 gzip_buffers  4 32k;
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  3m;
 client_body_timeout 3m;
 send_timeout     3m;
 connection_pool_size  256;
 server_names_hash_max_size 2048;
 server_names_hash_bucket_size 256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 request_pool_size  32k;
 output_buffers   4 64k;
 postpone_output  1460;
 open_file_cache max=1000 inactive=300s;
 open_file_cache_valid    600s;
 open_file_cache_min_uses 2;
 open_file_cache_errors   off;
 include "/usr/local/nginx/conf/vhosts/*.conf";
 server {
 listen 80;
 server_name _;
 access_log off;
 location ~* \.(ftpquota|htaccess|asp|aspx|jsp|asa|mdb)$ {
 deny  all;
 }
 location / {
 client_max_body_size    100m;
 client_body_buffer_size 128k;
 proxy_send_timeout   300;
 proxy_read_timeout   300;
 proxy_buffer_size    4k;
 proxy_buffers     16 32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;
 proxy_connect_timeout 30s;
 proxy_pass http://127.0.0.1:81/;
 proxy_set_header   Host   $host;
 proxy_set_header   X-Real-IP  $remote_addr;
 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 }
}

保存退出.

五.為apache安裝rpaf模塊,該模塊用于apache做后端時獲取訪客真實的IP.

1.使用apxs安裝模塊.這里要使用此前apache編譯安裝后的apxs

cd /usr/local/src/

tar -zxf mod_rpaf-0.6.tar.gz

cd mod_rpaf-0.6

/usr/local/apache/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

2.編輯/usr/local/apache/conf/httpd.conf,添加模塊參數,查找LoadModule php5_module modules/libphp5.so,在下方添加:

LoadModule rpaf_module modules/mod_rpaf-2.0.so

#Mod_rpaf settings

RPAFenable On

RPAFproxy_ips 127.0.0.1 [your_ips]

RPAFsethostname On

RPAFheader X-Forwarded-For

上面出現的[your_ips]請修改為你本機所監聽web服務的ip.多個IP用空格空開.

六.安裝ftp服務器:pure-ftpd

1.編譯安裝

cd /usr/local/src/

tar -zxf pure-ftpd-1.0.28.tar.gz

cd pure-ftpd-1.0.28

./configure --prefix=/usr/local/pureftpd --with-language=simplified-chinese --with-everything

make

make install

chmod u+x configuration-file/pure-config.pl

cp configuration-file/pure-config.pl /usr/local/pureftpd/sbin/

mkdir /usr/local/pureftpd/etc/

cp configuration-file/pure-ftpd.conf /usr/local/pureftpd/etc/

ln -s /usr/local/pureftpd/bin/pure-pw /usr/local/bin/

2.配置pure-ftpd,這里采用PureDB的驗證方式.

vi /usr/local/pureftpd/etc/pure-ftpd.conf

查找 PureDB /etc/pureftpd.pdb 取消前面的#號并設置成PureDB/usr/local/pureftpd/etc/pureftpd.pdb

查找 PassivePortRange 取消前面的#號

其他參數根據需要進行修改

3.添加自啟動.這里不創建init腳本.直接放在/etc/rc.local啟動即可

echo "/usr/local/pureftpd/sbin/pure-config.pl /usr/local/pureftpd/etc/pure-ftpd.conf --daemonize" > /etc/rc.local

至此.所有安裝工作結束.

如何使用這套系統

一,做好必要的安全工作

設置用戶家目錄/home/user,相關配置參數文件,以及訪問日志等目錄的權限.

chmod 711 /home

chmod 711 /usr/local/pureftpd

chmod 711 /usr/local/apache/conf/vhosts

chmod 711 /usr/local/nginx/conf/vhosts

chmod 711 /usr/local/apache/domlogs

chmod 711 /usr/local/apache/logs

二,如何創建用戶

創建用戶分兩個步驟.第一步創建系統用戶.該命令直接創建用戶家目錄.第二步創建ftp用戶.創建該用戶依賴系統用戶的創建.步驟如下(以創建用戶名為admin為例):

useradd admin -m -s /sbin/nologin

pure-pw useradd admin -u admin -g admin -d /home/admin -m[第一次執行不可用]

pure-pw mkdb[僅限第一次執行]

注意.通過上述方法安裝的ftp服務器在第一次創建用戶的時候不可以在pure-pw useradd ...后直接添加參數-m更新ftp用戶數據庫.需要分兩步執行.以后可以直接在創建用戶時在后面添加參數-m,執行之后會提示讓你輸入密碼.需要重復輸入兩次.

三.如何綁定域名

由于采用前后端操作.因此需要修改兩個服務器軟件的虛擬主機參數.實例如下(以admin.com為例,用戶目錄承接上文的/home/admin):

1.創建nginx虛擬主機參數

cd /usr/local/nginx/conf/vhosts

touch admin.com.conf

vi admin.com.conf

輸入以下內容:

server {
 error_log /usr/local/nginx/logs/admin.com-error_log warn;
 listen 127.0.0.1:80;
 server_name admin.com www.admin.com;
 access_log off;
 location ~* \.(gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg \
|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|rar \
|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
 access_log /usr/local/apache/domlogs/admin.com combined;
 root /home/admin/public_html;
 expires 7d;
 try_files $uri @backend;
 }
 error_page 400 401 402 403 404 405 406 407 408 409 500 501 502 503 504 @backend;
 location @backend {
 internal;
 client_max_body_size    100m;
 client_body_buffer_size 128k;
 proxy_send_timeout   300;
 proxy_read_timeout   300;
 proxy_buffer_size    4k;
 proxy_buffers     16 32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;
 proxy_connect_timeout 30s;
 proxy_redirect http://admin.com:81 http://admin.com;
 proxy_redirect http://www.admin.com:81 http://www.admin.com;
 proxy_pass http://127.0.0.1:81;
 proxy_set_header   Host   $host;
 proxy_set_header   X-Real-IP  $remote_addr;
 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 location ~* \.(ftpquota|htaccess|asp|aspx|jsp|asa|mdb)$ {
 deny  all;
 }
 location / {
 client_max_body_size    100m;
 client_body_buffer_size 128k;
 proxy_send_timeout   300;
 proxy_read_timeout   300;
 proxy_buffer_size    4k;
 proxy_buffers     16 32k;
 proxy_busy_buffers_size 64k;
 proxy_temp_file_write_size 64k;
 proxy_connect_timeout 30s;
 proxy_redirect http://admin.com:81 http://admin.com;
 proxy_redirect http://www.admin.com:81 http://www.admin.com;
 proxy_pass http://127.0.0.1:81/;
 proxy_set_header   Host   $host;
 proxy_set_header   X-Real-IP  $remote_addr;
 proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}

保存退出,注意將上述出現的127.0.0.1替換本機監聽web服務的IP

2.創建apache虛擬主機配置文件

cd /usr/local/apache/conf/vhosts

touch admin.com.conf

vi admin.com.conf

輸入以下內容:

<VirtualHost 127.0.0.1:81>

 ServerName admin.com
 ServerAlias www.admin.com
 DocumentRoot /home/admin/public_html
 ServerAdmin admin@evlit.com
 UseCanonicalName Off
 CustomLog /usr/local/apache/domlogs/admin.com combined
 php_admin_value open_basedir "/home/admin:/usr/lib/php:/usr/local/lib/php:/tmp"
 <IfModule !mod_disable_suexec.c>
 SuexecUserGroup admin admin
 </IfModule>
 ScriptAlias /cgi-bin/ /home/admin/public_html/cgi-bin/
</VirtualHost>

保存退出,注意將上述出現的127.0.0.1替換本機監聽web服務的IP.

四.如何管理MySQL數據庫

1.下載最新版PhpMyAdmin源碼包

mkdir -p /var/www/html

chmod -R 711 /var/www

cd /var/www/html

wget http://sourceforge.net/projects/phpmyadmin/files%2FphpMyAdmin%2F3.3.0%2FphpMyAdmin-3.3.0-all-languages.tar.bz2/download

tar -jxf phpMyAdmin-3.3.0-all-languages.tar.bz2

mv phpMyAdmin-3.3.0-all-languages phpmyadmin

2.增加apache配置,編輯httpd.conf,轉到最后一行

cd /usr/local/apache/conf

vi httpd.conf /* shift+g轉到最后一行 */

#Managed Tools

<VirtualHost 127.0.0.1:81 *>

ServerName localhost

ServerAlias pma.*

DocumentRoot /var/www/html/phpmyadmin

ServerAdmin admin@localhost

UseCanonicalName Off

</VirtualHost>

同樣,修改上述出現的127.0.0.1為你提供web服務的IP.重啟apache后.我們打開綁定到服務器IP的pma.yourdomain.com即可訪問到phpmyadmin.第一次使用.需要進行配置.具體配置請善用Google.

其他沒有照顧到的地方自行添加即可.如perl,sendmail等.

為方便管理員添加用戶及綁定域名.我編寫了一個腳本.

wget http://icodex.org/vhosts

chmod u+x vhosts

./vhosts

本文轉載自:http://icodex.org/2010/03/nginx-apache-guide/