Discuz教程网

让Discuz X2 使用 Nginx 的 sendfile 功能

[复制链接]
authicon dly 发表于 2011-9-4 17:24:59 | 显示全部楼层 |阅读模式
传统的Web服务器在处理文件下载的时候,总是先读入文件内容到应用程序内存,然后再把内存当中的内容发送给客户端浏览器。这种方式在应付当今大负载网站会消耗更多的服务器资源。sendfile是现代操作系统支持的一种高性能网络IO方式,操作系统内核的sendfile调用可以将文件内容直接推送到网卡的buffer当中,从而避免了Web服务器读写文件的开销,实现了“零拷贝”模式。

        Nginx 对 sendfile 有着良好的支持,Discuz! X2 也支持把附件通过 sendfile 发给客户,对于资源下载类的论坛,能明显减少PHP读取附件的资源占用。

         1,先在Nginx的配置文件里面检查是否有 sendfile off 配置,如果有,那么把sendfile 改为on

         2,打开论坛的配置文件config/config_global.php ,找到$_config['download']['xsendfile']['type'] =,把0改为1
              找到$_config['download']['xsendfile']['dir'] = '/down/';
              如果论坛放在根目录,那么把/down/改为/data/attachment/forum/
              如果论坛放在bbs目录,那么把/down/改为/bbs/data/attachment/forum/
              以此类推。

本教程基于用户手册的Nginx配置部分编写,实际测试有效。地址 https://www.discuz.1314study.com/forum.php?mod=redirect&goto=findpost&ptid=17177&pid=56305&fromuid=1

如果有用其他安装教程安装的,配置不成功的,可以在下面跟帖说明,并留下联系方式。无关帖子一律删除。






上一篇:最牛叉的幻灯片特效,需要的人太多了
下一篇:Discuz 大流量站点推荐使用稳定性更好的APC加速缓存
authicon  楼主| dly 发表于 2011-9-4 17:25:22 | 显示全部楼层

安装 —— CentOS 5 服务器 Nginx 环境推荐教程

一、系统约定
软件源代码包存放位置 /usr/local/src
源码包编译安装位置(prefix) /usr/local/software_name
脚本以及维护程序存放位置 /usr/local/sbin
MySQL 数据库位置 /var/lib/mysql(可按情况设置)
网站根目录 /home/www/wwwroot(可按情况设置)
虚拟主机日志根目录 /home/www/logs(可按情况设置)
运行账户 www:www
二、系统环境部署及调整
1、检查系统是否正常
# more /var/log/messages (检查有无系统级错误信息)
# dmesg (检查硬件设备是否有错误信息)
# ifconfig(检查网卡设置是否正确)
# ping www.163.com (检查网络是否正常)
# cat /proc/cpuinfo (检查CPU频率是否正常)
# top (按1检测CPU核数是否正常,内存大小是否正常)
2、关闭不需要的服务
# ntsysv
以下仅列出需要启动的服务,未列出的服务一律推荐关闭:
atd
crond
irqbalance
microcode_ctl
network
sendmail
sshd
syslog
关闭SElinux:修改/etc/selinux/config文件中的SELINUX= 为 disabled
3、更换yum国内源
# cd /etc/yum.repos.d
# mv CentOS-Base.repo CentOS-Base.repo.save
# wget http://centos.ustc.edu.cn/CentOS-Base.repo.5
# mv CentOS-Base.repo.5 CentOS-Base.repo
# yum clean all
4、服务器时间检查和设置
#data (检查时间是否正确,是否是中国时间CST)
#cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime (如果时区不对,则执行,时间正常的跳过)
#yum -y install ntp (安装ntp对时工具)
#chkconfig ntpd on (让对时服务开机启动)
5、使用 yum 对系统进行更新并且安装必要软件包
#yum update –y
#yum -y install make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel
6. 重新启动系统
# init 6
三、编译安装L.A.M.P环境
1、下载软件(截止到09年10月的最新版本)
# cd /usr/local/src
#wget http://sysoev.ru/nginx/nginx-0.7.63.tar.gz
#wget http://download.scientificlinux.net/nginx
#wget http://download.scientificlinux.net/php-fpm.conf
#wget http://download.scientificlinux.net/nginx.conf
#wget http://download.scientificlinux.net/fcgi.conf
#wget http://download.scientificlinux.net/php-5.2.10.tar.gz
#wget http://download.scientificlinux.net/php-5.2.10-fpm-0.5.13.diff.gz
#wget http://download.scientificlinux. ... glibc23-i386.tar.gz (32位系统)
#wget http://download.scientificlinux. ... ibc23-x86_64.tar.gz (64位系统)
#wget http://download.scientificlinux. ... i686-glibc23.tar.gz (32位系统)
#wget http://download.scientificlinux. ... 6_64-glibc23.tar.gz (64位系统)
2、安装MySQL
cd /usr/local/src
tar zxvf mysql-5.1.39-linux-i686-glibc23.tar.gz
mv mysql-5.1.39-linux-i686-glibc23 /usr/local/
ln -s /usr/local/mysql-5.1.39-linux-i686-glibc23/ /usr/local/mysql
groupadd mysql
useradd -g mysql mysql
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql-5.1.39-linux-i686-glibc23/
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 3 mysqld on
cp ./support-files/my-huge.cnf /etc/my.cnf
mv /usr/local/mysql/data /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
编辑/etc/my.cnf
在 [mysqld] 段增加
datadir = /var/lib/mysql
skip-innodb
wait-timeout = 10
max_connections = 512
max_connect_errors = 10000000
在 [mysqld] 段修改
max_allowed_packet = 16M
thread_cache_size = CPU个数*2
将 log-bin 注释
service mysqld start
bin/mysqladmin -u root password 'password_for_root'
其中引号内的password_for_root是要设置的root密码
3、安装Nginx
cd /usr/local/src/
tar zxvf nginx-0.7.63.tar.gz
cd nginx-0.7.63
./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --with-http_realip_module --with-http_addition_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --with-http_sub_module --with-http_dav_module
make
make install
cp /usr/local/src/nginx /etc/init.d/nginx
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
4、安装PHP和Zend
cd /usr/local/src
tar zxvf php-5.2.10.tar.gz
gzip -cd php-5.2.10-fpm-0.5.13.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/etc/cgi --enable-mbstring --enable-ftp --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-magic-quotes --with-mysql=/usr/local/mysql --with-pear --enable-sockets --with-ttf --with-freetype-dir=/usr --enable-gd-native-ttf --with-zlib --enable-sysvsem --enable-sysvshm --with-libxml-dir=/usr --enable-force-cgi-redirect --enable-fastcgi --with-xmlrpc --enable-zip --enable-fpm
make
make install
mkdir -p /usr/local/etc/cgi/
cp php.ini-dist /usr/local/etc/cgi/php.ini
编辑/usr/local/etc/cgi/php.ini
找到disable_functions =这行,在=号后面加上exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,proc_open,dl,popen,show_source
找到; cgi.fix_pathinfo=0这行去掉前面;号,如果这行=号后面不是0,则修改成0
mv -f /usr/local/src/php-fpm.conf /usr/local/php5/etc/php-fpm.conf
groupadd www
useradd -g www www
echo 'ulimit -SHn 65535' >> /etc/rc.local
echo '/usr/local/php5/sbin/php-fpm start' >> /etc/rc.local
cd /usr/local/src
tar zxvf ZendOptimizer-3.3.3-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.3-linux-glibc23-i386
./install
(注意第一个要填的路径是Zend安装路径,第二个是php.ini所在的路径,即/usr/local/etc/cgi)
(不要选重启apache)
5、启动Nginx和php
mv -f /usr/local/src/fcgi.conf /usr/local/nginx/conf/
cp -f /usr/local/src/nginx.conf /usr/local/nginx/conf/nginx.conf
mkdir -p /home/www/wwwroot
ulimit -SHn 65535
/usr/local/php5/sbin/php-fpm start
service nginx start
在/home/www/wwwroot放入一个index.php,内容为
打开浏览器访问,即可看到phpinfo页面
6、设置系统防火墙
编辑/usr/local/sbin/fw.sh
复制以下内容进去
#!/bin/bash
# Stop iptables service first
service iptables stop
# Load FTP Kernel modules
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
# Inital chains default policy
/sbin/iptables -F -t filter
/sbin/iptables -P INPUT DROP
/sbin/iptables -P OUTPUT ACCEPT
# Enable Native Network Transfer
/sbin/iptables -A INPUT -i lo -j ACCEPT
# Accept Established Connections
/sbin/iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# ICMP Control
/sbin/iptables -A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
# WWW Service
/sbin/iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# FTP Service
/sbin/iptables -A INPUT -p tcp --dport 21 -j ACCEPT
# SSH Service
/sbin/iptables -A INPUT -p tcp --dport 22 -j ACCEPT
退出编辑,执行以下命令
# chmod 755 /usr/local/sbin/fw.sh
# echo '/usr/local/sbin/fw.sh' >> /etc/rc.local
# /usr/local/sbin/fw.sh
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 05:59

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表