说明:
此文章主要讲解lnmp的编译安装。这里的操作系统是CentOS release 5.8 (Final) 64位
实现:
一.获取相关开源程序,yum安装相应库文件
# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
二.下载程序源码包
# cd /usr/local/src //将以下内容添加到lnmp.txt。通过wget -i lnmp.txt批量下载文件,就不需要一个个下载 dl.gaingreat.com/nginx-1.2.4.tar.gz dl.gaingreat.com/php-5.3.6.tar.gz dl.gaingreat.com/mysql-5.1.38.tar.gz dl.gaingreat.com/libiconv-1.13.tar.gz dl.gaingreat.com/libmcrypt-2.5.8.tar.gz dl.gaingreat.com/mcrypt-2.6.8.tar.gz dl.gaingreat.com/memcache-2.2.5.tgz dl.gaingreat.com/mhash-0.9.9.9.tar.gz dl.gaingreat.com/pcre-8.10.tar.gz dl.gaingreat.com/PDO_MYSQL-1.0.2.tgz dl.gaingreat.com/eaccelerator-0.9.6.1.tar.bz2 dl.gaingreat.com/imagick-3.0.1.tgz
三.编译安装PHP5.3.6(FastCGI模式)所需的支持库
1.libiconv用于加强系统对支持字符编码转换的功能
# tar zxvf libiconv-1.13.1.tar.gz # cd libiconv-1.13.1/ # ./configure --prefix=/usr/local # make && make install # cd ../
2.libmcrypt是加密算法库,php扩展mcrypt功能对此库有依耐关系,要使用mcrypt必须先安装此库
# 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 ../../
3.mhash是hash加密算法库,mcrypt功能对此库有依耐关系
# tar zxvf mhash-0.9.9.9.tar.gz # cd mhash-0.9.9.9/ # ./configure # make && make install # cd ../
4.为库添加软链接
64位系统,系统是32位还是64位可以通过getconf LONG_BIT查看位数,32为23位,64则为64位
# ln -sf /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la # ln -sf /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so # ln -sf /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4 # ln -sf /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8 # ln -sf /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config # ln -sf /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2 # ln -sf /usr/local/lib/libmhash.a /usr/lib64/libmhash.a # ln -sf /usr/local/lib/libmhash.la /usr/lib64/libmhash.la # ln -sf /usr/local/lib/libmhash.so /usr/lib64/libmhash.so # ln -sf /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2 # ln -sf /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1 # /sbin/ldconfig
32位系统
# 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 # /sbin/ldconfig
5.mcrypt编译
# tar zxvf mcrypt-2.6.8.tar.gz # cd mcrypt-2.6.8/ # ./configure # make && make install # cd ../
6.pcre编译
# tar zxvf pcre-8.10.tar.gz # cd pcre-8.10 # ./configure # make && make install # cd ../
四.编译安装mysql 5.1.38
# groupadd mysql # useradd -s /sbin/nologin -M -g mysql mysql # mkdir -p /home/mysql //创建目录,存放data和pid # chown -R mysql:mysql /home/mysql/ # tar zxvf mysql-5.1.38.tar.gz # cd mysql-5.1.38/ # ./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg make && make install cd ../
mysql更多详细配置与使用,请移步:mysql 5.1 编译安装
五.1编译安装PHP(FastCGI模式)
# tar zxvf php-5.3.6.tar.gz # cd php-5.3.6 # ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --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-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-pdo-mysql=/usr/local/mysql/ --with-mysql-sock=/tmp/mysql.sock # make ZEND_EXTRA_LIBS= '-liconv ' # make install # cp php.ini-production /usr/local/php/etc/php.ini # cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #因为php5.3开始自带fpm,使用自带的管理脚本 # chmod +x /etc/init.d/php-fpm # chkconfig php-fpm on #设置开机自启动 # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 修改php-fpm.conf,将pid改成以下,并将user和group改成www pid = run/php-fpm.pid user = www group = www
五.2编译安装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 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-3.0.1.tgz # cd imagick-3.0.1 # /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 ../
五.3修改php.ini文件,添加扩展模块
# mkdir -p /usr/local/eaccelerator_cache //添加eAccelerator加速目录,为php加速 # vim /usr/local/php/etc/php.ini //修改其中的extension_dir = "./"为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/",并在此行后增加以下内容 extension = "memcache.so" extension = "pdo_mysql.so" extension = "imagick.so" 查找output_buffering = Off 修改为output_buffering = On 查找; cgi.fix_pathinfo=0 修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。 shift+g跳至文章末尾,在文章末尾添加eAccelerator配置 [eaccelerator] zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so" eaccelerator.shm_size="64" eaccelerator.cache_dir="/usr/local/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"
五.4修改php-fpm.conf
# vim /usr/local/php/etc/php-fpm.conf //去掉内容前面的; pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35
现在还不能通过/etc/init.d/php-fpm启动php,还缺少www用户
六.1编译安装nginx
# groupadd www # useradd -s /sbin/nologin -M -g www www #www用户不能login # mkdir -p /home/www/data #此路径是用于存放各域名的root路径 # chmod +w /home/www/data # mkdir -p /home/www/logs # chmod 755 /home/www/logs # chown -R www:www /home/www # tar zxvf nginx-1.2.4.tar.gz # cd nginx-1.2.4 # ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module # make && make install # mkdir /usr/local/nginx/conf/vhosts/ //用于配置各域名
六.2修改nginx的配置文件nginx.conf,vhosts/test.conf
# vim /usr/local/webserver/nginx/conf/nginx.conf //添加以下内容 user www www; worker_processes 8; error_log /home/www/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; 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 60; tcp_nodelay on; 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 256k; 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; #limit_zone crawler $binary_remote_addr 10m; #log format log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent $request_body "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for '; include vhosts/*.conf; //这里,记得跟六.1的mkdir /usr/local/nginx/conf/vhosts/一样 }
六.3修改域名配置文件test.conf
# vim /usr/local/nginx/conf/test.conf,添加以下内容 server { listen 80; server_name www.test.com test.com; index index.html index.htm index.php default.html default.htm default.php; root /home/www/data/test; location ~ .*.(php|php5)?$ { 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 12h; } access_log off; }
六.4新建nginx管理脚本,因此nginx默认没有管理脚本
# vi /etc/init.d/nginx //输入以下内容 #! /bin/sh # chkconfig: 2345 55 25 # Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and # run 'update-rc.d -f nginx defaults ', or use the appropriate command on your # distro. For CentOS/Redhat run: 'chkconfig --add nginx ' ### BEGIN INIT INFO # Provides: nginx # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts the nginx web server # Description: starts nginx using start-stop-daemon ### END INIT INFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DESC="nginx daemon" NAME=nginx DAEMON=/usr/local/nginx/sbin/$NAME CONFIGFILE=/usr/local/nginx/conf/$NAME.conf PIDFILE=/usr/local/nginx/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { kill -INT `cat $PIDFILE` || echo -n "nginx not running" } do_reload() { kill -HUP `cat $PIDFILE` || echo -n "nginx can 't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0 为该文件增加执行权限,并设置为自启动 chmod +x /etc/init.d/nginx chkconfig nginx on
七.启动nginx,mysql,php
# /etc/init.d/nginx start|stop|restart # /etc/init.d/mysqld start|stop|restart # /etc/init.d/php-fpm start|stop|restart
附录:
1.make install mysql时,一直停留在Leaving directory `/mysql-test’
make[2]: Entering directory `/usr/local/src/mysql-5.1.38/mysql-test’
make[3]: Entering directory `/usr/local/src/mysql-5.1.38/mysql-test’
make[3]: Nothing to be done for `install-exec-am’.
make INSTALL_TO_DIR=”/usr/local/mysql/mysql-test” install_test_files
make[4]: Entering directory `/usr/local/src/mysql-5.1.38/mysql-test’
make[4]: Leaving directory `/usr/local/src/mysql-5.1.38/mysql-test’
mysql安装到这里卡了很久,是不是你觉得认为死机,程序安装错误了?聪明的你可能发现了,实际mysql已经安装成功了,它这步过段时间就会好了,但你知道是什么原因吗?哈哈告诉你吧“这是mysql在自我编译测试造成的“。只能等mysql自己完成,但mysql自编译是什么?
我这边是先top,看install进程是不是有变动,确定不是进程卡了的问题后,我把虚拟机关了,将原来的内存512提高到1024,明显快了很多。试的时候,可用free -m查看可用内存是不是充足。虽然mysql卡在Leaving directory `/mysql-test只能等mysql自己完成,但内存设置大点,还是会减少等待时间。
2.PHP 5.3.*编译安装时出现undefined reference to `libiconv’
手动编译PHP安装时遇到如下错误
/usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:73: undefined reference to `libiconv_open’ /usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:81: undefined reference to `libiconv’ /usr/local/src/php-5.3.10/ext/xmlrpc/libxmlrpc/encodings.c:101: undefined reference to `libiconv_close’ collect2: ld returned 1 exit status make: *** [sapi/fpm/php-fpm] 错误
1 表面看,是libiconv安装问题,重装libiconv之后问题依旧,网上看有人舍弃libiconv,使用 –without-iconv,我觉的不可取,这样是回避问题。 找了n久,终于找到bug所在: 在执行完 ./configure … 之后,修改下 Makefile,找到其中的
EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lldap -llber -lfreetype -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lcurl -ldl -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lidn -lssl -lcrypto -lz -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt
在最后面添加 -liconv ,修改后如下
EXTRA_LIBS = -lcrypt -lz -lcrypt -lrt -lmysqlclient -lmcrypt -lldap -llber -lfreetype -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lrt -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lcurl -ldl -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lidn -lssl -lcrypto -lz -lxml2 -lz -lm -lssl -lcrypto -ldl -lz -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -liconv
原文参考
Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)[原创]
http://solf.me/compile-nginx-php-mysql-on-centos-lnmp/