Nginx ("engine x") 是一个高
性能的
HTTP 和反向代理
服务器,也是一个 IMAP/POP3/SMTP 代理
服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点
开发的,它已经在该站点运行超过两年半了。Igor 将源
代码以类BSD许可证的形式发布。
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为
Web 服务器的网站也越来越多,但是网上吹嘘胜于apche10倍实在比较夸张,
apache经过优化之后处理PHP
页面速度还是不错的,比fastcgi方式要稳定的多。
本文就是整合了
nginx和apache,由nginx处理静态页,apache处理
php页面
安装步骤
系统要求:Linux 2.6+ 内核,本文中的Linux操作系统为CentOS 4.8,另在RedHat AS4上也安装成功
一、获取相关
开源
程序:
所需
软件:
eaccelerator-0.9.5.3.tar.bz2
gd-2.0.35.tar.gz
httpd-2.2.14.tar.bz2
mysql-5.1.42.tar.gz
php-5.2.12.tar.bz2
ZendOptimizer-3.3.9-
linux-glibc21-i386.tar.gz
nginx-0.7.64.tar.gz
pcre-8.0.tar.gz
-------------------------------------------------------------------------------------------------
安装之前要确保系统中安装了以下这些包。
我们可以用rpm -qa |grep 来查看系统是否装
bzip2-devel
zlib-devel
libjpeg-devel
libpng-devel
libtiff-devel
freetype-devel
openssl-devel
libxml2-devel
gettext-devel
这些包一般都安了,除了最后一个,我们可以在光盘里找到用rpm -ivh 来安装上.
二、安装MY
SQL
- # tar -zxvf mysql-5.1.42.tar.gz
- # cd mysql-5.1.42
- # groupadd mysql
- # useradd -g mysql -s /sbin/nologin -M mysql
- # ./configure --prefix=/usr/local/www/mysql --with-charset=gbk --with-extra-charsets=all --enable-thread-safe-client --localstatedir=/var/data
- # make && make install
- # cp support-files/my-large.cnf /etc/my.cnf
- # chown -R mysql.mysql /usr/local/www/mysql/
- # /usr/local/www/mysql/bin/mysql_install_db --user=mysql
- # chown -R root.root /usr/local/www/mysql/
- # chown -R mysql.mysql /var/data
复制代码
启动
数据库服务,并添加到自启动
- # /usr/local/www/mysql/bin/mysqld_safe --user=mysql &
- # netstat -ant
复制代码
查看3306端口
加入
自动启动服务队列:
- # cp support-files/mysql.server /etc/rc.d/init.d/mysqld
- # chmod 755 /etc/rc.d/init.d/mysqld
- # chkconfig --add mysqld
- # chkconfig mysqld on
复制代码
添加root密码
- # /usr/local/www/mysql/bin/mysqladmin -u root password "123456"
复制代码
测试一下:
- # /usr/local/www/mysql/bin/mysql -u root -p
复制代码
输入密码:123456,看能不能进入到
数据库
配置库
文件搜索路径
- # echo "/usr/local/www/mysql/lib/mysql">>/etc/ld.so.conf
- # ldconfig
- # ldconfig -v
- # echo "export PATH=$PATH:/usr/local/www/mysql/bin">>/etc/profile
- # source /etc/profile
复制代码
三、安装gd
- # tar -jxvf gd-2.0.35.tar.bz2
- # cd gd-2.0.35
- # ./configure --prefix=/usr/local/www/gd --with-png --with-freetype --with-jpeg --with-zlib --with-fontconfig
- # make
- #如果GD报错:configure.ac:64: warning: macro `AM_ICONV' not found in library
- 你就make clean一下,然后再make
- # make install
复制代码
----------------------------------------------
四、安装Apache
- # groupadd www
- # useradd -g www -s /sbin/nologin -M www
- # tar -jxvf httpd-2.2.14.tar.bz2
- # cd httpd-2.2.14
- # cd srclib/apr
- # ./configure --prefix=/usr/local/www/apr --enable-threads --enable-other-child --enable-static
- # make && make install
- # cd ../apr-util
- # ./configure --prefix=/usr/local/www/apr-util --with-apr=/usr/local/www/apr
- # make && make install
- # cd ../..
- # ./configure --prefix=/usr/local/www/apache --enable-so --enable-rewrite --with-apr=/usr/local/www/apr --with-apr-util=/usr/local/www/apr-util
- # make
- # make install
- #vi /usr/local/www/apache/conf/httpd.conf
复制代码
(1)找到DocumentRoot "/usr/local/www/apache/htdocs"
修改为:DocumentRoot "/var/www"
(2)找到
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all //这句改为Allow from all
</Directory>
(3)找到<Directory "/usr/local/www/apache/htdocs">
修改为:<Directory "/var/www">
(4)找到
Options Indexes FollowSymLinks //这行注释掉,禁止目录列表
(5)找到
<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
User daemon //改为www
Group daemon //改为www
</IfModule>
</IfModule>
添加到自启动
# cp /usr/local/www/apache/bin/apachectl /etc/rc.d/init.d/httpd
然后 vi /etc/rc.d/init.d/httpd 添加(#!/bin/sh下面)
# chkconfig: 2345 10 90
# description: Activates/Deactivates Apache Web
Server
最后,运行chkconfig把Apache添加到系统的启动服务组里面:
# chkconfig --add httpd
# chkconfig httpd on
五、安装PHP
- # tar -jxvf php-5.2.12.tar.bz2
- # cd php-5.2.12
- # ./configure --prefix=/usr/local/www/php --with-apxs2=/usr/local/www/apache/bin/apxs --with-mysql=/usr/local/www/mysql --with-gd=/usr/local/www/gd --with-config-file-path=/usr/local/www/php --enable-mbstring=all --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --with-zlib --enable-sockets --enable-soap --enable-exif --with-zlib-dir --with-bz2 --with-libxml-dir
- # make
- # make install
- # cp php.ini-dist /usr/local/www/php/php.ini
复制代码
整合apache与php
# vi /usr/local/www/apache/conf/httpd.conf
找到AddType application/x-gzip .gz .tgz在其下加以下内容
AddType application/x-httpd-php .php
查找
设置WEB默认文件)
DirectoryIndex index.html
改成:
DirectoryIndex index.php index.html index.htm
保存退出
service httpd restart
然后在目录中建一个文件用来测试php情况
在htdocs目录下建一个测试页
vi index.php
<?php
phpinfo();
?>
保存退出
重启apache
六、安装PHP扩展
1、安装eaccelerator加速软件
eaccelerator是php的加速软件,使用后php的执行效率会有很大幅度的提升。
- # tar jxvf eaccelerator-0.9.5.3.tar.bz2
- # cd eaccelerator-0.9.5.3
- # /usr/local/www/php/bin/phpize
- # ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/www/php/bin/php-config
- # make
- # make install
复制代码
编译安装后我们会看到屏幕提示的eaccelerator.so所在的目录,php5.2.x系列是在/usr/local/www/php/lib /php/extensions/no-debug-non-zts-20060613,记住这个路径,待会要用到
修改php.ini
# vi /usr/local/www/php/php.ini
安装php扩展
在文件最后,[zend]之前,注意,这部分内容务必放在[zend]之前,不然可能会出现不可预期的服务器问题。添加下列信息:
[eaccelerator]
zend_extension="/usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
# mkdir /tmp/eaccelerator
# chmod 777 /tmp/eaccelerator
2、安装Zend
- # tar -zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
- # cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/
- # vi /usr/local/www/php/php.ini
- [Zend Optimizer]
- zend_optimizer.optimization_level=1
- zend_optimizer.encoder_loader=0
- zend_extension="/usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/ZendOptimizer.so"
复制代码
在
浏览器中打开phpinfo()那个测试页,如果出现以下内容,证明安装成功!
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
with eAccelerator v0.9.5.3, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
with Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies
3、安装PDO_MYSQL
- # tar zxvf PDO_MYSQL-1.0.2.tgz
- # cd PDO_MYSQL-1.0.2/
- # /usr/local/www/php/bin/phpize
- # ./configure --with-php-config=/usr/local/www/php/bin/php-config --with-pdo-mysql=/usr/local/www/mysql
- # make
- # make install
复制代码
# vi /usr/local/www/php/php.ini
添加
extension_dir = "/usr/local/www/php/lib/php/extensions/no-debug-non-zts-20060613/"
extension="pdo_mysql.so"
七、nginx反向代理
1.Tcmalloc 优化Nginx性能
64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解
功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理
机制的API。
- # tar zxvf libunwind-0.99-alpha.tar.gz
- # cd libunwind-0.99-alpha/
- # CFLAGS=-fPIC ./configure
- # make CFLAGS=-fPIC
- # make CFLAGS=-fPIC install
复制代码
2、安装google-perftools:
- # tar zxvf google-perftools-0.97.tar.gz
- # cd google-perftools-0.97/
- # ./configure
- # make && make install
- # echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
- # /sbin/ldconfig
复制代码
查看是否好用,启动nginx
# lsof -n|grep tcmalloc
如果出现下面的就表示成功了
----------------------------------------
nginx 4322 www 10w REG 8,2 0 682436 /var/tmp/tcmalloc.4322
nginx 4323 www 12w REG 8,2 0 682438 /var/tmp/tcmalloc.4323
nginx 4324 www 14w REG 8,2 0 682439 /var/tmp/tcmalloc.4324
nginx 4325 www 16w REG 8,2 0 682440 /var/tmp/tcmalloc.4325
nginx 4326 www 18w REG 8,2 0 682441 /var/tmp/tcmalloc.4326
nginx 4327 www 20w REG 8,2 0 682442 /var/tmp/tcmalloc.4327
nginx 4328 www 22w REG 8,2 0 682443 /var/tmp/tcmalloc.4328
nginx 4329 www 24w REG 8,2 0 682444 /var/tmp/tcmalloc.4329
mysqld_sa 5284 root mem REG 8,2 1388088 62997 /usr/local/lib/libtcmalloc.so.0.0.0
mysqld 5397 mysql mem REG 8,2 1388088 62997 /usr/local/lib/libtcmalloc.so.0.0.0
---------------------------------------------
3.安装 pcre
- #tar zxvf pcre-8.0.tar.gz
- #cd pcre-8.0
- #./configure
- #make && make install
复制代码
4、安装nginx
- # tar zxvf nginx-0.7.64.tar.gz
- # cd nginx-0.7.64
- # ./configure --with-http_stub_status_module --with-google_perftools_module --prefix=/usr/local/www/nginx --user=www --group=www
- # make && make install
复制代码
修改nginx的配置文件
我这里是把原先的重命名然后新建了一个nginx.conf
---------------------------------------------------
- user www;
- worker_processes 8;
- pid logs/nginx.pid;
- google_perftools_profiles /var/tmp/tcmalloc;
- worker_rlimit_nofile 51200;
- events
- {
- use epoll;
- worker_connections 51200;
- }
- http{
- include mime.types;
- default_type application/octet-stream;
- access_log off;
- error_log /dev/null;
- 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 120;
- #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;
- tcp_nodelay on;
- 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;
- include vhost/*.conf;
- }
复制代码
---------------------------------------------
vhost/www.test.com.conf的内容
---------------------------------------------
- server
- {
- listen 80;
- server_name www.yahunet.com;
- index index.html index.htm index.php;
- root /var/www/;
- location /nginx {
- stub_status on;
- auth_basic "NginxStatus";
- # auth_basic_user_file conf/htpasswd;
- #密码由apache的htpasswd工具来产生
- access_log off;
- }
- location / {
- location ~ .*\.(php|php5)?$ {
- index index.php;
- root /var/www/;
- proxy_pass http://127.0.0.1:81;
- }
- include proxy.conf;
- if ( !-e $request_filename) {
- proxy_pass http://127.0.0.1:81;
- }
- location ~* \.(jpg|jpeg|gif|png|swf)$ {
- if (-f $request_filename) {
- root /var/www/;
- expires 30d;
- break;
- }
- }
- location ~* \.(js|css)$ {
- if (-f $request_filename) {
- root /var/www/;
- expires 1d;
- break;
- }
- }
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- #如果需要记录把下面的注释去掉
- # log_format access '$http_x_forwarded_for - $remote_user [$time_local] "$request"'
- # '$status $body_bytes_sent "$http_referer"'
- # '"$http_user_agent" $remote_addr';
- # access_log logs/www.yahunet.com_access.log access;
- }
复制代码
-------------------------------------------------
proxy.conf内容
--------------------------------------------
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 50m;
- client_body_buffer_size 256k;
- proxy_connect_timeout 30;
- proxy_send_timeout 30;
- proxy_read_timeout 60;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
- proxy_max_temp_file_size 128m;
- #Nginx cache
- client_body_temp_path client_body 1 2;
- proxy_temp_path proxy_temp 1 2;
- #client_body_temp_path /tmpfs/client_body_temp 1 2;
- #proxy_temp_path /tmpfs/proxy_temp 1 2;
- #fastcgi_temp_path /tmpfs/fastcgi_temp 1 2;
复制代码
----------------------------------------------
修改apache的端口为81
启动 nginx
#/usr/local/www/nginx/sbin/nginx
然后我们用IE测试一下 http://IP地址或域名 就可以看到nginx的默认的页面证明nginx已经启动
关闭 nginx
# kill -QUIT `cat /usr/local/www/nginx/logs/nignx.pid`
重启 nginx
# kill -HUP `cat /usr/local/www/nginx/logs/nignx.pid`
把nginx加入到系统服务
- # vi /etc/rc.d/init.d/nginx
- #!/bin/bash
- # Startup script for the nginx Web Server
- # chkconfig: - 85 15
- # description: nginx is a World Wide Web server. It is used to serve
- # HTML files and CGI.
- # processname: nginx
- # pidfile: /usr/local/www/nginx/logs/nginx.pid
- # config: /usr/local/www/nginx/conf/nginx.conf
- PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
- export PATH
- NGINX_HOME=/usr/local/www/nginx/sbin
- NGINX_CONF=/usr/local/www/nginx/conf
- if [ ! -f "$NGINX_HOME/nginx" ]
- then
- echo "nginxserver startup: cannot start"
- exit
- fi
- case "$1" in
- 'start')
- $NGINX_HOME/nginx -c $NGINX_CONF/nginx.conf
- echo "nginx start successful"
- ;;
- 'stop')
- killall -TERM nginx
- ;;
- esac
- # chmod a+x /etc/rc.d/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on
复制代码
八、用tmpfs加速nginx和php
1、加速nginx
# mkdir /tmpfs
编辑/etc/fstab,加入
tmpfs /tmpfs tmpfs size=1024m,mode=1777 0 0
编辑/usr/local/nginx/conf/proxy.conf,将
缓存目录均指向/tmpfs
client_body_temp_path /tmpfs/client_body_temp 1 2;
proxy_temp_path /tmpfs/proxy_temp 1 2;
fastcgi_temp_path /tmpfs/fastcgi_temp 1 2;
此分区可视情况适当调整大小,此处设置为1G
2、加速php
# mkdir /tmpfs/eaccelerator
# chmod 777 /tmpfs/eaccelerator
# vi /usr/local/www/php/php.ini
eaccelerator.cache_dir="/tmp/eaccelerator"
改为:
eaccelerator.cache_dir="/tmpfs/eaccelerator"
注:/tmpfs重启后就没有了,eaccelerator需要手工创建
可以写一个创建eaccelerator目录的
脚本
- # vi /usr/local/www/eacc.sh
- #!/bin/sh
- /bin/mkdir /tmpfs/eaccelerator
- /bin/chmod 777 /tmpfs/eaccelerator
- /etc/init.d/httpd restart
- # chmod a+x /usr/local/www/eacc.sh
- # echo "/usr/local/www/eacc.sh">>/etc/rc.local
复制代码
九、优化Linux内核参数
- # vi /etc/sysctl.conf
- 在末尾增加以下内容:
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 300
- net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 1
- net.ipv4.ip_local_port_range = 5000 65000
- net.ipv4.tcp_max_syn_backlog = 8192
- net.ipv4.tcp_max_tw_buckets = 5000
- net.ipv4.tcp_synack_retries= 1
- net.ipv4.tcp_syn_retries = 1
- 使配置立即生效:
- # /sbin/sysctl -p
- # vi /etc/security/limits.conf
- * soft nofile 51200
- * hard nofile 102400
- * soft noproc 51200
- * hard noproc 102400
- # ulimit -SHn 51200
复制代码
十、Apache优化
1 apache-mpm.conf
httpd.conf去注释
- Include conf/extra/httpd-mpm.conf
- # vi /usr/local/www/apache/conf/extra/httpd-mpm.conf
- <IfModule mpm_prefork_module>
- ServerLimit 1024
- StartServers 15
- MinSpareServers 15
- MaxSpareServers 30
- MaxClients 1024
- MaxRequestsPerChild 0
- </IfModule>
复制代码
-------------------------------
ServerLimit
默认的MaxClient最大是256个线程,如果想设置更大的值,就的加上ServerLimit这个参数。20000是ServerLimit这个参数的最大值。如果需要更大,则必须编译apache,此前都是不需要重新编译Apache。
-------------------------------
2 apache-keepalive
httpd.conf去注释
- # Include conf/extra/httpd-default.conf
- # vi /usr/local/www/apache/conf/extra/httpd-default.conf
- Timeout 120 #与nginx的保持一至
- KeepAlive On
- MaxKeepAliveRequests 1000
- KeepAliveTimeout 5
复制代码
十一、MYSQL 的优化
1、Tcmalloc 优化mysql性能
# vi /etc/init.d/mysqld
添加一行export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
重新启动mysql
# lsof -n|grep tcmalloc
如果出现下面的就表示成功了
----------------------------------------
nginx 4322 www 10w REG 8,2 0 682436 /var/tmp/tcmalloc.4322
nginx 4323 www 12w REG 8,2 0 682438 /var/tmp/tcmalloc.4323
nginx 4324 www 14w REG 8,2 0 682439 /var/tmp/tcmalloc.4324
nginx 4325 www 16w REG 8,2 0 682440 /var/tmp/tcmalloc.4325
nginx 4326 www 18w REG 8,2 0 682441 /var/tmp/tcmalloc.4326
nginx 4327 www 20w REG 8,2 0 682442 /var/tmp/tcmalloc.4327
nginx 4328 www 22w REG 8,2 0 682443 /var/tmp/tcmalloc.4328
nginx 4329 www 24w REG 8,2 0 682444 /var/tmp/tcmalloc.4329
mysqld_sa 5284 root mem REG 8,2 1388088 62997 /usr/local/lib/libtcmalloc.so.0.0.0
mysqld 5397 mysql mem REG 8,2 1388088 62997 /usr/local/lib/libtcmalloc.so.0.0.0
---------------------------------------------
2、优化/etc/my.cnf
1)确认在“[mysqld]”部分加入了“skip-innodb”和“skip-bdb”参数;
2)确认在“[mysqld]”部分加入了“skip-name-resolve”和“skip-locking”参数;
3)如果不需要的话,可以将二进制
日志(binlog)停掉,方法是将“log-bin”注释掉;
4)在内存允许的情况下,对一些参数进行重新配置,目标在于将大部分操作集中于内存中,尽量不进行磁盘操作,对于我的 MYSQL 服务器我是如下修改的,基于 2G 内存情况:
- [mysqld]
- key_buffer=512M
- max_allowed_packet=4M
- table_cache=1024
- thread_cache=64
- join_buffer_size=32M
- sort_buffer=32M
- record_buffer=32M
- max_connections=512
- wait_timeout=120
- interactive_timeout=120
- max_connect_errors=30000
- long_query_time=1
- max_heap_table_size=256M
- tmp_table_size=128M
- thread_concurrency=8
- myisam_sort_buffer_size=128M
复制代码
你可以根据“show status”
命令返回的状态进行微调。我主要注意以下变量的数值,越小越好,最好为零:)
Created_tmp_disk_tables
Created_tmp_tables
Created_tmp_files
Slow_queries
在启动脚本中使用“--log-slow-queries=/home/logs/mysql_slow.log”参数,以便进行 SQL 语句的优化工作,这个其实是很很重要的工作。记得一定要在 my.cnf 中设置“long_query_time=1”才行。