在整个互联网web架构中,最近比较流行的轻量级web服务器nginx,功能和性能上越来越受欢迎。最近比较空闲了,从网络上看了些资料,自己尝试安装部署,算是初次体验吧。。我这里主要参考张宴的文章,声明下。
一、lnmp环境的搭建
1、下载软件:
nginx-0.8.46.tar.gz
php-5.2.14.tar.gz
php-5.2.14-fpm-0.5.14.diff.gz
mysql-5.5.3-m3.tar.gz
libiconv-1.13.1.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-8.10.tar.gz
eaccelerator-0.9.6.1.tar.bz2
PDO_MYSQL-1.0.2.tgz
ImageMagick.tar.gz
imagick-2.3.0.tgz
软件大家可以到sourceforge.net或者到oschina.net去下载。
2、编译安装PHP 5.2.14所需的支持库:
- tar zxvf libiconv-1.13.1.tar.gz
- cd libiconv-1.13.1/
- ./configure –prefix=/usr/local
- make
- make install
- cd ../
- tar zxvf libmcrypt-2.5.8.tar.gz
- cd libmcrypt-2.5.8/
- ./configure
- make
- make install
- /sbin/ldconfig
- cd libltdl/
- ./configure –enable-ltdl-install
- make
- make install
- cd ../../
- tar zxvf mhash-0.9.9.9.tar.gz
- cd mhash-0.9.9.9/
- ./configure
- make
- make install
- cd ../
- ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
- ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
- ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
- ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
- ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
- ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
- ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
- ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
- ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
- ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
- tar zxvf mcrypt-2.6.8.tar.gz
- cd mcrypt-2.6.8/
- /sbin/ldconfig
- ./configure
- make
- make install
- cd ../
- tar xvzf freetype-2.4.4.tar.gz
- ./configure --prefix=/usr/local/freetype
- make && make install
- tar xvzf jpegsrc.v7.tar.gz
- ./configure --prefix=/usr/local/jpeg
- make && make install
- tar xvzf libpng-1.5.0.tar.gz
- ./configure --prefix=/usr/local/libpng
- make && make install
- tar xvzf GD-2.0.35.tar.gz
- 链接头文件
- ln -s /usr/local/libpng/include/pnglibconf.h /usr/include
- ln -s /usr/local/libpng/include/png.h /usr/include
- cd gd-2.0.35
- aclocal
- autoconf
- ./configure --prefix=/usr/local/gd2 --with-freetype=/usr/local/freetype --with-png=/usr/local/libpng --with-jpeg=/usr/local/jpeg
- make && make install
3、安装数据库mysql
- useradd -d /home/mysql -s /sbin/nologin mysql
- ./configure --prefix=/usr/local/mysql --sysconfdir=/etc --localstatedir=/data/mysql --with-charset=utf8 --with-extra-charsets=all --with-plugins=innobase,partition --enable-thread-safe-client
- make
- make install
- cp support-files/my-medium.cnf /etc/my.cnf
- bin/mysql_install_db -–user=mysql 初始化数据库
- /usr/local/mysql/bin/mysqld_safe &
备注:如遇到此错误提示:error: No curses/termcap library found,检查后发现,ncurses有安装,问题应该出在编译时无法找到库文件。
解决办法:./configure --with-named-curses-libs=/usr/lib/libncursesw.so.5 编译参数中加上这句,即可OK
4、安装php
- tar zxvf php-5.2.14.tar.gz
- gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1
- cd php-5.2.14/
- ./configure --prefix=/usr/local/php --with-gd=/usr/local/gd2 --with-zlib-dir --enable-mbstring=all --with-freetype-dir=/usr/local/freetype --with-mysql=/usr/local/mysql --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/png --enable-fastcgi --enable-fpm --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
- make ZEND_EXTRA_LIBS='-liconv'
- make install
- cp php.ini-dist /usr/local/php/etc/php.ini
5、编译安装PHP5扩展模块
- tar zxvf memcache-2.2.5.tgz
- cd memcache-2.2.5/
- /usr/local/php/bin/phpize
- ./configure –with-php-config=/usr/local/php/bin/php-config
- make
- make install
- cd ../
- tar jxvf eaccelerator-0.9.6.1.tar.bz2
- cd eaccelerator-0.9.6.1/
- /usr/local/php/bin/phpize
- ./configure -enable-eaccelerator=shared -with-php-config=/usr/local/php/bin/php-config
- make
- make install
- cd ../
- tar zxvf PDO_MYSQL-1.0.2.tgz
- cd PDO_MYSQL-1.0.2/
- /usr/local/php/bin/phpize
- ./configure –with-php-config=/usr/local/php/bin/php-config –with-pdo-mysql=/usr/local/mysql
- make
- make install
- cd ../
- tar zxvf ImageMagick.tar.gz
- cd ImageMagick-6.5.1-2/
- ./configure
- make
- make install
- cd ../
- tar zxvf imagick-2.3.0.tgz
- cd imagick-2.3.0/
- /usr/local/php/bin/phpize
- ./configure –with-php-config=/usr/local/php/bin/php-config
- make
- make install
- cd ../
6、修改php.ini文件
- sed -i ‘s#extension_dir = “./”#extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/”\nextension = “memcache.so”\nextension = “pdo_mysql.so”\nextension = “imagick.so”\n#’ /usr/local/webserver/php/etc/php.ini
- sed -i ‘s#output_buffering = Off#output_buffering = On#’ /usr/local/webserver/php/etc/php.ini
- sed -i “s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g” /usr/local/webserver/php/etc/php.ini
- sed -i “s#; cgi.fix_pathinfo=0#cgi.fix_pathinfo=0#g” /usr/local/webserver/php/etc/php.ini
7、配置eAccelerator加速PHP:
mkdir -p /usr/local/eaccelerator_cache
vi /usr/local/php/etc/php.ini
- [eaccelerator]
- zend_extension=”/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so”
- eaccelerator.shm_size=”64″
- eaccelerator.cache_dir=”/usr/local/php/eaccelerator_cache”
- 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″
8、创建www用户和组,以及供blog.abc.com和www.abc.com两个虚拟主机使用的目录:
- /usr/sbin/groupadd www
- /usr/sbin/useradd -g www www
- mkdir /data/webappsu/blog
- chown -R www:www /data/webappsu/blog
9、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
- cp -rp php-fpm.conf.default php-fpm.conf
这个目前暂时以默认文档创建模板,需要调优时要对其中的某些参数做修改。
10、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为www:Linode 512 VPS ,此数值设为 24-32是合适的。
- ulimit -SHn 65535
- /usr/local/webserver/php/sbin/php-fpm start
注:/usr/local/webserver/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。
11、安装nginx前先检查下pcre库,如果pcre版本太旧或者未安装,则安装nginx会提示错误。
- #rpm -qa |grep pcre
- #tar xvzf pcre-8.12.tar.gz
- #cd pcre-8.12
- #./configure
- #make && make install
- 下载nginx1.0.2源码包,并上传至服务器。
- #useradd nginx
- #tar xvzf nginx-1.0.2.tar.gz
- #cd nginx-1.0.2
- #./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
- #make
- #make install
12、vim /usr/local/nginx/conf/nginx.conf
- user www www;
- worker_processes 8;
- error_log /data/logs/error.log;
- pid /data/logs/nginx.pid;
- worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
- worker_rlimit_nofile 102400;
- events {
- use epoll;
- worker_connections 102400;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- include vhosts/*.conf;
- log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- '$status $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /data/logs/access.log main;
- sendfile on;
- userid_expires max;
- tcp_nopush on;
- tcp_nodelay on;
- server_names_hash_bucket_size 256;
- client_header_buffer_size 16k;
- large_client_header_buffers 4 256k;
- client_max_body_size 20m;
- client_header_timeout 3m;
- client_body_timeout 3m;
- send_timeout 3m;
- fastcgi_buffers 8 128k;
- fastcgi_connect_timeout 300;
- fastcgi_send_timeout 300;
- fastcgi_read_timeout 300;
- fastcgi_buffer_size 128k;
- fastcgi_busy_buffers_size 256k;
- fastcgi_temp_file_write_size 256k;
- fastcgi_intercept_errors on;
- output_buffers 1 32k;
- postpone_output 1460;
- keepalive_timeout 30;
- gzip on;
- gzip_types text/plain text/htm text/css application/ x-javascript;
- }
#mkdir vhosts
vim blog.conf
- server{
- listen 80;
- server_name www.backter.com;
- index index.php index.html;
- root /data/webappsu/blog;
- error_page 404 /404.htm;
- autoindex_exact_size on;
- access_log /data/logs/nginx-access/blog.log combined;
- location ~ .*\.(php|php5)?$
- {
- #fastcgi_pass unix:/tmp/php-cgi.sock;
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- include fastcgi.conf;
- }
- location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
- {
- expires 30d;
- }
- location ~ .*\.(js|css)?$
- {
- expires 1h;
- }
- }
13、测试http://ip打开正常即证明安装无误。
可以用webbench测试下负载压力,下面是我的测试性能:
至此,整个lnmp部署完成,不过上面这个部署参考了张宴的文章,所以其实中间有很多加速插件初次都可以不安装的。。