Nginx+php(FastCGI)+Memcached+MySQL+APC 是目前主流的高效能伺服器搭建方式!適合大中型網站,小型站長也可以採用這種組合!
Nginx 超越Apache 的高效能和穩定性,使得國內使用Nginx 作為Web 伺服器的網站也越來越多,其中包括國內最大的電子地圖MapBar、新浪部落格、新浪播客、網易新聞等門戶網站頻道,六間房、56.com等影片分享網站,Discuz!官方論壇、水木社群等知名論壇,豆瓣、YUPOO相簿、海內SNS、迅雷線上等新興Web 2.0網站,更多的網站都在使用Nginx設定。
下載所需的安裝套件:這裡採用原始碼包編譯安裝:本部落格整合下載
http://wgkgood.gicp.net/download/nginx-0.7.61.tar.gz
http://wgkgood.gicp.net/download/pcre-8.01.tar.gz
http://wgkgood.gicp.net/download/memcache-2.2.5.tgz
http://wgkgood.gicp.net/download/libevent-1.4.12-stable.tar.gz
http://wgkgood.gicp.net/download/APC-3.1.4.tgz
下載到/usr/src下
另外還有兩個包mysql-5.1.41.tar.gz、php-5.3.5.tar.gz 【其他類似版本也可以! 】可以在官網下載。
一、正式安裝Nginx、【安裝nginx之前需要安裝pcre包和zlib以支援重寫,正規以及網頁壓縮等等】
cd /usr/src
tar xzf pcre-8.01.tar.gz
cd pcre-8.01
./configure –PRefix=/usr/local/pcre
make
make install
(2)、然後再安裝nginx :
useradd www && cd /usr/src && tar xzf nginx-0.7.61.tar.gz &&cd nginx-0.7.61 && ./configure –prefix=/usr/local/nginx –with-http_stub_status_module – openusr/local/nginx –with-http_stub_status_module – openus_/ssl / –with-pcre=/usr/src/pcre-8.01 –user=www –group=www &&make &&make install
【nginx注意* –with-pcre=/usr/src/pcre-8.01指向的是原始碼包解壓縮的路徑,而不是安裝的路徑,否則會報
make[1]: *** [/usr/local/pcre/Makefile] Error 127 錯誤】
二、接下來安裝mysql
cd /usr/src && tar xzf mysql-5.1.41.tar.gz && cd mysql-5.1.41 && ./configure –prefix=/usr/local/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase && make &&make install
(2)、mysql安裝完畢,建立mysql使用者和群組並初始化資料庫,並啟動資料庫。
cd /usr/local/mysql && useradd mysql && chown -R mysql:mysql /usr/local/mysql && /usr/local/mysql/bin/mysql_install_db –user=mysql && chown -R my:mysql var/mysql_install_db –user=mysql && chown -R my:mysql var/ &sql& ./ bin/mysqld_safe –user=mysql &
即可。 【如果mysql啟動報錯,請檢查/usr/local/mysql/var 目錄,mysql是否有權限】
三、安裝php :
cd /usr/src &&tar xzf php-5.3.5.tar.gz && cd php-5.3.5 && ./configure –prefix=/usr/local/php –with-config-file-path=/usr/local/ php/etc –with-mysql=/usr/local/mysql –with-mysqli=/u
sr/local/mysql/bin/mysql_config –with-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-li
bxml-dir=/usr –enable-xml –disable-rpath –enable-discard-path –enable-safe-mode –enable-bcmath –enable-shmop –enable-sysvsem
–enable-inline-optimization –with-curl –with-curlwrappers –enable-mbregex –enable-fpm –enable-sockets && make &&make install
安裝完畢! 【注意這個參數在此可以不加–enable-fastcgi;其他之前版本需要加上,以上安裝根據自己的選擇添加,如果報錯,根據具體報錯找原因】
四、整合Nginx和php(FastCGI)安裝完php-5.3.5後支援fastCGI
user www www;
worker_processes 8;
error_log /usr/local/logs/nginx/error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/CSS application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
listen 80;
server_name wgkgood.gicp.net;
index index.php index.htm index.html;
root /usr/webapps/www;
#limit_conn crawler 20;
location ~ .*.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*.(js|css)?$
{
expires 1h;
}
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 /usr/local/logs/nginx/access.log access;
}
}
Nginx配置完畢!啟動nginx ;/usr/local/nginx/sbin/nginx 即可,重新啟動nginx指令如下/usr/local/nginx/sbin/nginx –s reload
此設定檔僅供參考,感謝張宴老師!
(2)、配置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;
五、配置php設定檔:
cd /usr/local/php5/etc/ && cp php-fpm.conf.default php-fpm.conf
然後根據提示修改php-fpm.conf裡面的選項。
配置完畢後,啟動php-fpm
以下cp /usr/src/php-5.3.5/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
然後啟動/etc/init.d/php-fpm start 即可。
六、安裝apc配置:
cd /usr/src && tar xzf APC-3.1.4.tgz &&cd APC-3.1.4
/usr/local/php5/bin/phpize
./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php5/bin/php-config
&&make&& make install
安裝完後會產生一個apc.so在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/裡面。
七、安裝memcached,使fastcGI支持memcached
先安裝libevent,
cd /usr/src && tar xzf libevent-1.4.12-stable.tar.gz && cd libevent-1.4.12-stable && ./configure –prefix=/usr/local/libevent &&make && make install
然後安裝memcached
tar xzf memcache-2.2.5.tar.gz && cd memcache-2.2.5 && /usr/local/php5/bin/phpize && ./configure –prefix=/usr/local/memcached –with-libevent=/usr/ local/libevent –with-php-config=/usr/local/php5/bin/php-config &&make &&make install
安裝完後,會在/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/裡產生一個memcache.so 這個模組:
八、接下來修改php.ini
預設的php.ini在/usr/local/php5/lib/php.ini 你也可以指定:
extension_dir = “./”
修改為
extension_dir=”/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626″
把下面這些加到最後:
extension = apc.so
extension=memcache.so //這裡引用快取模組
[APC]
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64M
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl=7200
apc.user_ttl=7200
apc.gc_ttl = 3600
apc.cache_by_default = on
安裝到此已經完成!
重新啟動nginx和php-fpm ,用測試頁面存取。
此文章僅供參考!有不妥之處歡迎指正!共同學習!
文章來源:http: //blog.mgcrazy.com/ ?p=627 轉載請註明出處鏈接