虽然很多人建议使用一键安装包来安装WEB套件LEMP(又称LNMP),但在我看来,既然大家买了VPS,就要有折腾的决心,否则,和普通的性能强点的虚拟主机又有何区别呢?所以,个人建议大家还是自己亲自折腾一下这个配置过程,一来可以学习Linux,二来下次碰到问题也不会手忙脚乱不知所措. 本教程使用的Linux环境为centos 6.0和Fedora 15,如果你使用其它的Linux发行版本,请确认你的登陆用户权限是否为root (对于root无法登陆的用户,在执行命令时候,请加上sudo), 其次请确定你的安装软件的管理包是什么,比如CentOS/Fedora/Redhat使用的是yum,而ubuntu使用的是apt-get. 教程中使用的Nginx是最新的稳定版本1.0.15,Mysql使用5.1.68,PHP则是5.2.17,以及PHP-FPM的版本是php-5.2.17-fpm-0.5.14,以及用于加速PHP的eaccelerator-0.9.6.1 一.首先建议用以下命令检查系统中是否有装过或者残留的php/mysql/apache等相关的服务,以免影响了安装. 用rpm命令来检查相关软件 代码: rpm -qa|grep httpd rpm -e httpd rpm -qa|grep mysql rpm -e mysql rpm -qa|grep php rpm -e php 上面的命令中rpm -qa表示列出所有安装好的软件,grep则是过滤只列出有关键词httpd的.参数-e表示erase,也就是卸载的意思. 不过rpm卸载软件可能不能解决好软件依赖问题,所以再用yum remove卸载一次,参数-y表示不再询问你yes/no,直接执行. 代码: yum -y remove httpd yum -y remove php yum -y remove mysql-server mysql yum -y remove php-mysql 二.接下来,安装一些需要的依赖库. 代码: #这个命令非常长 #下面许多./configure命令也存在这种情况 #复制时注意下面滚动条,或者同时复制多行执行 yum -y install patch make cmake perl gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel vim-minimal nano fonts-chinese gettext gettext-devel ncurses-devel gmp-devel pspell-devel unzip 三.下载、编译、安装、配置过程 开始下载并安装需要的软件,对于国内的主机或者VPS建议使用国内镜像下载,比较著名的有sohu/163以及各大学的镜像,对于国外的VPS或者独立主机用户,选则相应国家的镜像下载速度会非常快,毕竟资本主义的网速比我们快. 因为我的主机为Linode的东京机房,所以我尽量使用日本的镜像,大家在官方网站下载时,对于有多个镜像供选择的官方下载,我会给你提供镜像选择链接,替换为你的下载链接即可. 首先建立一个目录用于下载安装的文件吧. 代码: mkdir lemp cd lemp/ 接下来下载并解压需要的软件 1.编译依赖库autoconf、libiconv等 稍微解释一下这几步,一般在Linux中编译程序,都是configure/make/make install三步,configure的作用是检查系统环境并生成Makefile文件,因为每个系统环境参数可能有所不同.make则是编译并生成可执行文件,make install则是把生成的部署到系统里面. 代码: ############编译autoconf-2.13###################### wget -c http://mirrors.ustc.edu.cn/gnu/autoconf/autoconf-2.13.tar.gz tar xvfz autoconf-2.13.tar.gz cd autoconf-2.13/ ./configure --prefix=/usr/local/autoconf-2.13 make make install cd ../ ##################编译libiconv-1.14################# wget -c http://mirrors.ustc.edu.cn/gnu/libiconv/libiconv-1.14.tar.gz tar xvfz libiconv-1.14.tar.gz cd libiconv-1.14/ ./configure make make install cd ../ #############编译Libmcrypt###################### wget -c wget -c http://ncu.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.bz2 tar xvfj libmcrypt-2.5.8.tar.bz2 cd libmcrypt-2.5.8 ./configure make make install /sbin/ldconfig cd libltdl ./configure --enable-ltdl-install make make install cd ../ cd ../ ############编译mhash-0.9.9.9#################### wget -c http://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2 tar xvfj mhash-0.9.9.9.tar.bz2 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/lib/libltdl.a /usr/lib/libltdl.a ln -s /usr/local/lib/llibltdl.la /usr/lib/libltdl.la ln -s /usr/local/lib/libltdl.so /usr/lib/libltdl.so ln -s /usr/local/lib/libltdl.so.3 /usr/lib/libltdl.so.3 ln -s /usr/local/lib/libltdl.so.3.1.0 /usr/lib/libltdl.so.3.1.0 ############编译Mcrypt########## wget -c http://ncu.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz tar zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8 ./configure make make install cd ../ 2.编译安装mysql 代码: #镜像选择:http://84.113.22.230:8980/mysql/downloads_html/mirrors.html wget -c ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.1/mysql-5.1.68.tar.gz tar xvfz mysql-5.1.68.tar.gz cd mysql-5.1.68/ ./configure --prefix=/usr/local/mysql --with-extra-charsets=all --enable-thread-safe-client --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-extra-charsets=all --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile make make install cd ../ ##########添加专门的mysql用户和组,控制权限####### groupadd mysql useradd -s /sbin/nologin -M -g mysql mysql cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf /usr/local/mysql/bin/mysql_install_db --user=mysql chown -R mysql /usr/local/mysql/var chgrp -R mysql /usr/local/mysql/. ##########设置开机启动项########## cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql chmod 755 /etc/init.d/mysql cat > /etc/ld.so.conf.d/mysql.conf<<EOF /usr/local/mysql/lib/mysql /usr/local/lib EOF ldconfig ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql ln -s /usr/local/mysql/include/mysql /usr/include/mysql /etc/init.d/mysql start ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql ln -s /usr/local/mysql/bin/mysqldump /usr/bin/mysqldump ln -s /usr/local/mysql/bin/myisamchk /usr/bin/myisamchk #############设置mysql root用户的密码 /usr/local/mysql/bin/mysqladmin -u root password 你的密码 /etc/init.d/mysql restart /etc/init.d/mysql stop #################添加mysql为系统服务########### chkconfig --level 345 mysql on service mysql on 3.编译安装PHP 代码: #http://cn.php.net/get/php-5.2.17.tar.gz/from/a/mirror wget -c http://cn.php.net/get/php-5.2.17.tar.gz/from/cn2.php.net/mirror wget -c http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz wget -c http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz wget -c http://pecl.php.net/get/memcache-3.0.5.tgz wget -c http://ncu.dl.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.zip ############PHP配置环境变量########### export PHP_AUTOCONF=/usr/local/autoconf-2.13/bin/autoconf export PHP_AUTOHEADER=/usr/local/autoconf-2.13/bin/autoheader ###########编译PHP######## tar xvfz php-5.2.17.tar.gz gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1 cd php-5.2.17/ ./buildconf --force ./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 --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic make ZEND_EXTRA_LIBS='-liconv' make install ########创建PHP配置文件 mkdir -p /usr/local/php/etc cp php.ini-dist /usr/local/php/etc/php.ini cd ../ ########创建PHP符号链接########### ln -s /usr/local/php/bin/php /usr/bin/php ln -s /usr/local/php/bin/phpize /usr/bin/phpize ln -s /usr/local/php/sbin/php-fpm /usr/bin/php-fpm #########编译memcache-3.0.5扩展##### tar xvfz memcache-3.0.5.tgz cd memcache-3.0.5/ /usr/local/php/bin/phpize ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir make make install cd ../ #########编译PDO_MYSQL扩展########### tar xvfz 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 ../ ########编译PHP加速扩展############### unzip eaccelerator-0.9.6.1.zip 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 ../ ########设置PHP配置文件############ 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"\n#' /usr/local/php/etc/php.ini sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/php/etc/php.ini sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /usr/local/php/etc/php.ini sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/etc/php.ini sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/php/etc/php.ini sed -i 's/; cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini sed -i 's/; cgi.fix_pathinfo=0/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini sed -i 's/max_execution_time = 30/max_execution_time = 300/g' /usr/local/php/etc/php.ini cat >>/usr/local/php/etc/php.ini<<EOF extension="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="0" eaccelerator.shm_prune_period="0" eaccelerator.shm_only="0" eaccelerator.compress="1" eaccelerator.compress_level="9" EOF ###创建eAccelerator缓存目录## mkdir /tmp/eaccelerator chmod 777 /tmp/eaccelerator ###获取已经配置好的php-fpm等配置文件,你也可自行配置## wget -c http://www.11ing.com/tool/conf.zip unzip conf.zip rm -rf conf.zip rm -f /usr/local/php/etc/php-fpm.conf cp conf/php-fpm.conf /usr/local/php/etc/php-fpm.conf ###设置PHP解释器开机启动以及作为系统服务###### cp conf/init.d.php-fpm5.2 /etc/init.d/php-fpm chmod +x /etc/init.d/php-fpm chkconfig --level 345 php-fpm on
4.编译安装nginx 代码: wget -c http://sourceforge.net/projects/pcre/files/pcre/8.30/pcre-8.30.tar.gz wget -c http://nginx.org/download/nginx-1.2.8.tar.gz ###########编译 pcre 库############# tar xvfz pcre-8.30.tar.gz cd pcre-8.30/ ./configure make make install cd ../ ln -s /usr/local/lib/libpcre.so.1 /usr/lib/libpcre.so.1 ###############创建WEB专用用户####### groupadd www useradd -s /sbin/nologin -g www www mkdir -p /home/wwwroot chmod +w /home/wwwroot mkdir -p /home/wwwlogs chmod 777 /home/wwwlogs chown -R www:www /home/wwwroot ###########编译nginx############# tar xvfz nginx-1.2.8.tar.gz cd nginx-1.2.8/ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 make make install cd ../ rm -f /usr/local/nginx/conf/nginx.conf ##########拷贝配置文件到nginx##### cp conf/nginx.conf /usr/local/nginx/conf/nginx.conf cp conf/dabr.conf /usr/local/nginx/conf/dabr.conf cp conf/discuz.conf /usr/local/nginx/conf/discuz.conf cp conf/sablog.conf /usr/local/nginx/conf/sablog.conf cp conf/typecho.conf /usr/local/nginx/conf/typecho.conf cp conf/wordpress.conf /usr/local/nginx/conf/wordpress.conf cp conf/discuzx.conf /usr/local/nginx/conf/discuzx.conf cp conf/none.conf /usr/local/nginx/conf/none.conf cp conf/wp2.conf /usr/local/nginx/conf/wp2.conf rm -f /usr/local/nginx/conf/fcgi.conf cp conf/fastcgi.conf /usr/local/nginx/conf/fastcgi.conf /etc/init.d/mysql start ######获取nginx自动启动脚本###### cp conf/init-rpm-nginx.sh /etc/init.d/nginx chmod +x /etc/init.d/nginx #######添加为系统开机启动服务##### chkconfig --level 345 nginx on /etc/init.d/nginx start /etc/init.d/mysql start /etc/init.d/php-fpm start 到此为止,你已经安装好了LEMP,至于优化的文章,大家可以自行搜过吧,呵呵~ 重要的根目录: 代码: mysql: /usr/local/mysql php: /usr/local/php nginx: /usr/local/nginx web: /home/wwwroot 如果要添加虚拟主机,则搜索nginx添加虚拟主机的一般用法即可。 本文参考了lnmp.org的一键安装包,实际上更多地是对其代码进行了解析,并改成更符合人工部署逻辑和代码阅读的习惯,十分感谢.其中去掉了zend_optimize组件,替代为eAccelerator,另外不建议关闭SELinux服务,这是redhat发行版的精华,也是安全保障所在。