安裝文件爲:
Nginx: nginx-1.4.2 穩定版
Php:php-5.5.4 其中已經內置支持fastcgi了,不用在打補丁
首先配置防火牆
不在敘述
安裝nginx
安裝必要補丁
# yum -y install pcre-devel opensslopenssl-devel gcc-c++
安裝前優化
減小Nginx編譯後的文件大小
在編譯Nginx時,默認以debug模式進行,而在debug模式下會插入很多跟蹤和ASSERT之類的信息,編譯完成後,一個Nginx要有好幾兆字節。在編譯前取消Nginx的debug模式,編譯完成後Nginx只有幾百千字節,因此可以在編譯之前,修改相關源碼,取消debug模式,具體方法如下:
在Nginx源碼文件被解壓後,找到源碼目錄下的auto/cc/gcc文件,在其中找到如下幾行:
1. # debug
2. CFLAGS=”$CFLAGS -g”
註釋掉或刪掉這兩行,即可取消debug模式。
利用TCMalloc優化Nginx的性能
TCMalloc的全稱爲Thread-CachingMalloc,是谷歌開發的開源工具“google-perftools”中的一個成員。與標準的glibc庫的malloc相比,TCMalloc庫在內存分配效率和速度上要高很多,這在很大程度上提高了服務器在高併發情況下的性能,從而降低系統負載。下面簡單介紹如何爲Nginx添加TCMalloc庫支持。
要安裝TCMalloc庫,需要安裝libunwind(32位操作系統不需要安裝)和google-perftools兩個軟件包,libunwind庫爲基於64位CPU和操作系統的程序提供了基本函數調用鏈和函數調用寄存器功能。下面介紹利用TCMalloc優化Nginx的具體操作過程:
1.安裝libunwind庫
可以從http://download.savannah.gnu.org/releases/libunwind下載相應的libunwind版本,這裏下載的是libunwind-0.99-alpha.tar.gz,安裝過程如下:
1. [root@localhost home]#tar zxvf libunwind-0.99-alpha.tar.gz
2. [root@localhost home]# cd libunwind-0.99-alpha/
3. [root@localhost libunwind-0.99-alpha]#CFLAGS=-fPIC ./configure
4. [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC
5. [root@localhost libunwind-0.99-alpha]#make CFLAGS=-fPIC install
2.安裝google-perftools
可以從http://google-perftools.googlecode.com下載相應的google-perftools版本,這裏下載的是google-perftools-1.8.tar.gz,安裝過程如下:
1. [root@localhost home]#tar zxvf google-perftools-1.8.tar.gz
2. [root@localhost home]#cd google-perftools-1.8/
3. [root@localhost google-perftools-1.8]# ./configure
4. [root@localhost google-perftools-1.8]#make && make install
5. [root@localhost google-perftools-1.8]#echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
6. [root@localhost google-perftools-1.8]# ldconfig
至此,google-perftools安裝完成。
3.重新編譯Nginx
爲了使Nginx支持google-perftools,需要在安裝過程中添加“–with-google_perftools_module”選項重新編譯Nginx,安裝代碼如下:
1. [[email protected]]#./configure \
2. >--with-google_perftools_module --with-http_stub_status_module --with-http_mp4_module --with-http_flv_module --prefix=/usr/local/nginx
3. [root@localhost nginx-0.7.65]#make
4. [root@localhost nginx-0.7.65]#make install
到這裏Nginx安裝完成。
4.爲google-perftools添加線程目錄
創建一個線程目錄,這裏將文件放在/tmp/tcmalloc下,操作如下:
1. [root@localhost home]#mkdir /tmp/tcmalloc
2. [root@localhost home]#chmod 0777 /tmp/tcmalloc
5.修改Nginx主配置文件
修改nginx.conf文件,在pid這行的下面添加如下代碼:
1. #pid logs/nginx.pid;
2. google_perftools_profiles /tmp/tcmalloc;
接着,重啓Nginx,完成google-perftools的加載。
6.驗證運行狀態
爲了驗證google-perftools已經正常加載,通過如下命令查看:
1. [root@ localhost home]# lsof -n | grep tcmalloc
2. nginx 2395 nobody 9w REG 8,8 0 1599440 /tmp/tcmalloc.2395
3. nginx 2396 nobody 11w REG 8,8 0 1599443 /tmp/tcmalloc.2396
4. nginx 2397 nobody 13w REG 8,8 0 1599441 /tmp/tcmalloc.2397
5. nginx 2398 nobody 15w REG 8,8 0 1599442 /tmp/tcmalloc.2398
由於在Nginx配置文件中,設置worker_processes的值爲4,因此開啓了4個Nginx線程,每個線程會有一行記錄。每個線程文件後面的數字值就是啓動的Nginx的PID值。
至此,利用TCMalloc優化Nginx的操作完成。
內核參數的優化,主要是在Linux系統中針對Nginx應用而進行的系統內核參數優化,常見的優化參數值如下。
下面給出一個優化實例以供參考:
1. net.ipv4.tcp_max_tw_buckets = 6000
2. net.ipv4.ip_local_port_range = 1024 65000
3. net.ipv4.tcp_tw_recycle = 1
4. net.ipv4.tcp_tw_reuse = 1
5. net.ipv4.tcp_syncookies = 1
6. net.core.somaxconn = 262144
7. net.core.netdev_max_backlog = 262144
8. net.ipv4.tcp_max_orphans = 262144
9. net.ipv4.tcp_max_syn_backlog = 262144
10. net.ipv4.tcp_synack_retries = 1
11. net.ipv4.tcp_syn_retries = 1
12. net.ipv4.tcp_fin_timeout = 1
13. net.ipv4.tcp_keepalive_time = 30
將上面的內核參數值加入/etc/sysctl.conf文件中,然後執行如下命令使之生效:
[root@ localhost home]#/sbin/sysctl -p
詳細解釋參考高性能Linux 書
修改配置文件
Vi /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65536;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent'
'"$http_referer""$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent'
'"$http_referer""$http_user_agent" '
'"$http_range""$sent_http_content_range"';
access_log logs/access.log main;
client_max_body_size 20m;
client_header_buffer_size 32K;
large_client_header_buffers 432k;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 60;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #web路徑
index index.html index.htmindex.php;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #/usr/local/nginx/htmlweb路徑
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#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 HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
詳細說明參考高性能linux 書
啓動nginx
#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
停止nginx
查詢nginx主進程號
#ps -ef | grep nginx
從容停止Nginx:
kill -QUIT 主進程號
快速停止Nginx:
kill -TERM 主進程號
強制停止Nginx:
pkill -9 nginx
測試頁面,直接輸入服務器ip
安裝php
需要安裝需要的模塊,
yum -y install libjpeg libjpeg-devel libpnglibpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel curl-devel libxslt-devel
# tarzxvf php-5.5.4.tar.gz
# cdphp-5.5.4
複雜配置
#./configure--prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-jpeg-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip
簡易配置
# ./configure--prefix=/usr/local/php --with-mysql--enable-fpm--enable-opcache --with-freetype-dir --with-jpeg-dir--with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug--enable-safe-mode --enable-mbstring
如果需要GD庫還要
./configure--prefix=/usr/local/php --with-mysql --enable-fpm--enable-opcache --with-freetype-dir--with-jpeg-dir --with-png-dir --with-gd --with-zlib --with-libxml-dir--enable-xml --disable-debug --enable-safe-mode --enable-mbstring
# make&& make install
php-5.4.11已經支持fastcgi了,不用在打補丁了。
1.在編譯安裝時加上'--enable-fpm',注意修改php/etc/php-fpm.conf去掉pid前的註釋;
2.複製/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm還可以做成服務
複製php-fpm文件到php安裝目錄
# cp-R ./sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf
拷貝php.ini文件
安裝目錄有2個文件:php.ini-development和php.ini-production
注意php.ini-production擁有較高的安全性設定,則適合上線當產品使用
# cpphp.ini-development /usr/local/php/lib/php.ini
或者
# cp php.ini-production/usr/local/php/lib/php.ini
如果操作了這一步以後使用phpinfo()就會看到Loaded Configuration File:
php-fpm啓動
拷貝啓用文件
# cp -R ./sapi/fpm/php-fpm/etc/init.d/php-fpm
啓動
# /etc/init.d/php-fpm
重啓
# killallphp-fpm
# /etc/init.d/php-fpm
重啓nginx
#/usr/local/nginx/sbin/nginx -s reload
php安裝成功查看進程
配置php加速插件opcace
php.ini配置opcace
;opcache
zend_extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1
opcache.enable=1
/no-debug-non-zts-20121212 根據實際文件名稱配置
128意思是給它分配128M內存,然後重啓apache,用phpinfo查看是否生效,顯示下面的信息就說明生效了
配置與優化PHP-FPM
標籤max_children用於設置FastCGI的進程數。根據官方建議,小於2GB內存的服務器,可以只開啓64個進程,4GB以上內存的服務器可以開啓200個進程。
<valuename="max_children">5</value>
標籤rlimit_files用於設置PHP-FPM對打開文件描述符的限制,默認值爲1024。這個標籤的值必須和Linux內核打開文件數關聯起來,例如要將此值設置爲65535,就必須在Linux命令行執行'ulimit -HSn 65536'。
<valuename="rlimit_files">1024</value>
在配置完成Nginx+FastCGI之後,爲了保證Nginx下PHP環境的高速穩定運行,需要添加一些FastCGI優化指令。下面給出一個優化實例,將下面代碼添加到Nginx主配置文件中的HTTP層級。
1. fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
2. fastcgi_connect_timeout 300;
3. fastcgi_send_timeout 300;
4. fastcgi_read_timeout 300;
5. fastcgi_buffer_size 64k;
6. fastcgi_buffers 4 64k;
7. fastcgi_busy_buffers_size 128k;
8. fastcgi_temp_file_write_size 128k;
9. fastcgi_cache TEST;
10. fastcgi_cache_valid 200 302 1h;
11. fastcgi_cache_valid 301 1d;
12. fastcgi_cache_valid any 1m;
啓動nginx
ulimit –n 65535
ulimit –n 65536
# /usr/local/nginx/sbin/nginx -c/usr/local/nginx/conf/nginx.conf
重啓php-fpm
# killall php-fpm
ulimit –HSn 65536
# /etc/init.d/php-fpm
重啓nginx
# /usr/local/nginx/sbin/nginx -s reload
解決安裝完成後Mysql 連接不了問題(Nosuch file or directory 提示)
首先確定是mysql_connect()和mysql_pconnect()的問題,故障現象就是函數返回空,而mysql_error()返回“No such file or directory”。
寫個phpinfo頁面,找到mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket。
啓動mysql,執行命令 STATUS; 記下UNIX socket的值
如果2和3的值不一樣,則打開php.ini(可以從phpinfo頁面中找到php.ini的位置)
一共修改三處,分別爲:mysql.default_socket、mysqli.default_socket、pdo_mysql.default_socket
將UNIX socket的值賦予上面三次
重啓nginx 和 php-fpm
OK