1、簡介javascript
Nginx是俄羅斯人編寫的十分輕量級的HTTP服務器,Nginx,它的發音爲 「engine X」, 是一個高性能的HTTP和反向代理服務器,同時也是一個IMAP/POP3/SMTP 代理服務器.Nginx是由俄羅斯人 Igor Sysoev爲俄羅斯訪問量第二的 Rambler.ru站點開發的,它已經在該站點運行超過三年了。Igor Sysoev在創建的項目時,使用基於BSD許可。
在高併發鏈接的狀況下,Nginx是Apache服務器不錯的替代品。Nginx同時也能夠做爲7層負載均衡服務器來使用。Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 能夠承受3萬以上的併發鏈接數,至關於同等環境下Apache的10倍。php
Nginx 超越 Apache 的高性能和穩定性,使得國內使用 Nginx 做爲 Web 服務器的網站也愈來愈多,其中包括新浪博客、新浪播客、網易新聞、騰訊網、搜狐博客等門戶網站頻道,六間房、56.com等視頻分享網站,Discuz!官方論壇、水木社區等知名論壇,盛大在線、金山逍遙網等網絡遊戲網站,豆瓣、人人網、YUPOO相冊、金山愛詞霸、迅雷在線等新興Web 2.0網站。css
爲何Nginx的性能要比Apache高得多?這得益於Nginx使用了最新的epoll(Linux 2.6內核)和kqueue(freebsd)網絡I/O模型,而Apache則使用的是傳統的select模型。目前Linux下可以承受高併發訪問的 Squid、Memcached都採用的是epoll網絡I/O模型。
處理大量的鏈接的讀寫,Apache所採用的select網絡I/O模型很是低效。下面用一個比喻來解析Apache採用的select模型和Nginx採用的epoll模型進行之間的區別:
假設你在大學讀書,住的宿舍樓有不少間房間,你的朋友要來找你。select版宿管大媽就會帶着你的朋友挨個房間去找,直到找到你爲止。而epoll版 宿管大媽會先記下每位同窗的房間號,你的朋友來時,只需告訴你的朋友你住在哪一個房間便可,不用親自帶着你的朋友滿大樓找人。若是來了10000我的,都要 找本身住這棟樓的同窗時,select版和epoll版宿管大媽,誰的效率更高,不言自明。同理,在高併發服務器中,輪詢I/O是最耗時間的操做之 一,select和epoll的性能誰的性能更高,一樣十分明瞭。html
2、LNMP搭建步奏java
php編譯安裝node
cd /usr/local/src/mysql
wget http://mirrors.sohu.com/php/php-5.4.37.tar.bz2nginx
tar -jxvf php-5.4.37.tar.bz2 sql
ls /usr/local/php/vim
刪除以前的php:rm -rf /usr/local/php/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif --disable-ipv6 --enable-ftp
make
make install
配置文件:cp php.ini-production /usr/local/php/etc/php.ini
啓動腳本:cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
加入到系統服務列表:chkconfig --add php-fpm
開機啓動:chkconfig php-fpm on
cd /usr/local/php/etc
重命名
mv php-fpm.conf.default php-fpm.conf
查看是否有錯 /usr/local/php/sbin/php-fpm -t
useradd -s /sbin/nologin php-fpm
service php-fpm start
2.nginx編譯安裝
cd /usr/local/src/
wget http://mirrors.sohu.com/nginx/nginx-1.6.2.tar.gz
tar -zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-pcre
make
make install
cd /usr/local/nginx/
啓動nginx:/usr/local/nginx/sbin/nginx
3.測試php解析
nginx配置文件:vim /usr/local/nginx/conf/nginx.conf
打開(72行)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;
include fastcgi_params;
}
cat /usr//local/nginx/html/index.html
cd /usr/local/nginx/html
vi info.php
<?php
phpinfo();
?>
從新加載:/usr/local/nginx/sbin/nginx -s reload
4.nginx啓動腳本和配置文件
vim /usr/local/nginx/conf/nginx.conf
清空裏面的內容加入下面的內容:
啓動腳本
# vi /etc/init.d/nginx 寫入
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/var/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
# chmod 755 /etc/init.d/nginx
配置文件
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
include vhosts/*.conf;
}
cd /usr/local/nginx/conf/
mkdir vhosts
cd vhosts/
vim defualt.conf
server{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/1233;
deny all;
}
mkdir /tmp/1233
/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx reload
curl -x127.0.0.1:80 www.baidu.com(403錯誤)
vim 111.conf
server
{
listen 80;
server_name 111.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
/usr/local/nginx/sbin/nginx -t
/etc/init.d/nginx reload
curl -x127.0.0.1:80 111.com/forum.php -I
5.php-fpm配置文件
vim /usr/local/php/etc/php-fpm.conf
(清空內容)> /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = www.sock
user = php-fpm
group = php-fpm
listen.owner = nobody //和後面的nginx的一致
listen.group = nobody // 同上
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
slowlog = /tmp/www_slow.log
request_slowlog_timeout = 1
php_admin_value[open_basedir] =/data/www:/tmp/
/usr/local/php/sbin/php-fpm -t
/etc/init.d/php-fpm restart
ps aux |grep php-fpm