nginx是一款俄罗斯的免费开源web软件,nginx服务特点是负载均衡和高迸发量逐渐受到人们青睐,并逐步在日常工作中替换apache web服务,成为主流web服务软件之一,搭建LNMP环境需下载php、mysql、nginx等(还有一些支持包做调整) Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪博客、新浪播客、网易新闻、腾讯网、搜狐博客等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,盛大在线、金山逍遥网等网络游戏网站,豆瓣、人人网、YUPOO相册、金山爱词霸、迅雷在线等新兴Web 2.0网站。
为什么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的性能谁的性能更高,同样十分明了。
前往php官网下载稳定版本http://php.net/
mysql官网下载稳定版 http://www.mysql.com/
nginx官网下载 http://nginx.org/
系统环境
系统平台:centos6.6 (2.6.32内核,32位版本)
php版本:php-5.3.27
mysql版本: mysql-5.5.42
nginx版本: nginx-1.8.0
准备工作:
安装编译、运行支持的组件
yum install gcc cpp glibc glibc-devel gcc-c++
yum -y install gcc make libtool automake autoconf rrdtool-devel libpcap-devel gdbm-devel zlib-devel geoip-devel
Nginx所需要的软件包记住名字搜索。
nginx-0.7.61.tar.gz
php-5.2.10.tar.gz
php-5.2.10-fpm-0.5.11.diff.gz
mysql-5.1.35.tar.gz
libiconv-1.13.tar.gz
libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
memcache-2.2.5.tgz
mhash-0.9.9.9.tar.gz
pcre-7.9.tar.gz
eaccelerator-0.9.5.3.tar.bz2
PDO_MYSQL-1.0.2.tgz
ImageMagick.tar.gz
imagick-2.2.2.tgz
安装mysql服务
安装php支持
进入下载目录下
cd /usr/local/src
tar zxf php-5.3.27.tar.gz -C .
cd php-5.3.27
编译成可安装的二进制文件
./configure --prefix=/usr/local/nginx_php --with-config-file-path=/usr/local/nginx_php/etc/ --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/usr/local/mysql-5.5.42tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --enable-zend-multibyte --disable-ipv6 --with-pear --with-curl
安装报错请根据报错提示安装对应源码包
第一次./configure可能会报错
configure:error :Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
用yum查找红色标记的软件包
yum list |grep curl
安装libcurl-devel.i386的rpm包
yum -y install libcurl-devel.i386
之后再次编译可安装文件
成功结果示例
+---------------------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------------------+
Thank you for using PHP.
make;make install 编译二进制文件并安装
make报错gcc: Internal error: Killed (program cc1)
在编译参数时加上--disable-fileinfo
报错
Please submit a full bug report.
See <http://bugzilla.redhat.com/bugzilla> for instructions.
make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
搜索下载安装libiconv,再次编译
安装完成示例
ln -s -f /usr/local/nginx_php/bin/phar.phar /usr/local/nginx_php/bin/phar
Installing PDO headers: /usr/local/nginx_php/include/php/ext/pdo/
安装完成后进入解压源码包目录拷贝配置文件
cp php.ini-production /usr/local/nginx_php/etc/php.ini 拷贝php启动服务脚本
cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
service php-fpm start
chkconfig php-fpm on 启动报错【1】
Starting php-fpm [30-Sep-2015 12:29:06] ERROR: failed to open configuration file '/usr/local/nginx_php/etc/php-fpm.conf': No such file or directory (2)[30-Sep-2015 12:29:06] ERROR: failed to load configuration file '/usr/local/nginx_php/etc/php-fpm.conf'
[30-Sep-2015 12:29:06] ERROR: FPM initialization failed
failed
拷贝php配置文件
cp /usr/local/src/php-5.3.27/sapi/fpm/php-fpm.conf /usr/local/nginx_php/etc/
报错【2】
Starting php-fpm [30-Sep-2015 12:29:06] ERROR:[pool www] caonnt get uid for user 'php-fpm'
[30-Sep-2015 12:29:06] ERROR: FPM initialization failed
failed
useradd -s /sbin/nologin php-fpm
再次启动,并查看php进程
ps -aux |grep php
完成后网站可能会有数据库连接不上的报错信息,删除已经解压和安装的php目录,重新编译安装即可,原因是编译参数出错,请仔细检查
安装nginx
tar zxf nginx-1.8.0 -C .
cd /usr/local/src/nginx-1.8.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module--with-http_ssl_module --with-prce;make;make install
停止Apache服务避免服务端口冲突/etc/init.d/httpd stop
临时启动nginx服务
/usr/local/nginx/sbin/nginx
修改nginx配置文件内容
vim /usr/local/nginx/conf/nginx.conf
worker_processes 1; #进程数
events {
worker_connections 4096; #最大连接数
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/www/bbs;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /usr/local/www/bbs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/bbs$fastcgi_script_name;
include fastcgi_params;
}
}
}
查看配置是否正确
/usr/local/nginx/sbin/nginx -t
暂时这样
网站进入后台管理中心会报错
Can not write to cache files, please check directory ./data/ and ./data/sysdata/ .
主要是权限没有给对,暂时不清楚是哪个目录,先放这搁着
转载于:https://blog.51cto.com/xiangcc/1699753