linux服务器下LNMP安装与配置方法

Nginx与apache、lighttp性能综合对比,如下图:

linux服务器下LNMP安装与配置方法 Linux 第1张

注意:关闭rpm默认安装的apache和mysql

1.准备php函数的rpm包

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

2.准备lnmp其他的源代码包

wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz

3.安装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 ../

(libiconv库为需要做转换的应用提供了一个iconv()的函数,以实现一个字符编码到另一个字符编码的转换)

代码如下:
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

(libmcrypt是加密算法扩展库。支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法。)

代码如下:
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/
./configure
make
make install
cd ../

4. 编译安装MySQL 5.5.3-m3

代码如下:
groupadd mysql
useradd -g mysql mysql

tar zxvf mysql-5.5.3-m3.tar.gz
cd mysql-5.5.3-m3
./configure --prefix=/usr/local/mysql --without-debug --enable-thread-safe-client --with-pthread --enable-assembler --enable-profiling --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-extra-charsets=all --with-plugins=all --with-mysqld-user=mysql --without-embedded-server --with-server-suffix=-community --with-unix-socket-path=/tmp/mysql.sock
Make
#编译
Make install
#安装
Cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
#准备mysql配置文件
Vi /etc/my.cnf
[client]
default-character-set=utf8
#修改客户端和连接字符集
[mysqld]
character-set-server=utf8
#修改服务器和数据库字符集
collation-server = utf8_general_ci
#修改服务器校验字符集                   登陆mysql后可以s查看字符集

Setfacl -m u:mysql:rwx -R /usr/local/mysql
Setfacl -m d:u:mysql:rwx -R /usr/local/mysql
#设置权限
/usr/local/mysql/bin/mysql_install_db --user=mysql
#安装mysql和test数据库
/usr/local/mysql/bin/mysqld_safe --user=mysql &
#启动mysql服务
/usr/local/mysql/bin/mysqladmin -uroot password  123
#修改mysql登录密码为123
/usr/local/mysql/bin/mysql -uroot -p123
#用mysql登录

5. 编译安装PHP(FastCGI模式。使用fastCGI管理php,加快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
#-p  1    是数字
#解压并打补丁,让php支持fpm来方便管理php-cgi进程(使用php-fpm管理fastCGI)
# gzip   -c  保留源文件   -d  解压
cd php-5.2.14/
./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-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --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 /lnmp/php-5.2.14/php.ini-dist  /usr/local/php/etc/php.ini
cd ../

6.准备编译安装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 ../
(eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍)
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 ../
(ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过89种基本格式的图片文件,包括流行的TIFF、JPEG、GIF、 PNG、PDF以及PhotoCD等格式)
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 ../

7. 修改php.ini文件,让php模块生效

代码如下:
vi /usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

手工添加
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文件类型错误解析漏洞

8. 在php.ini中配置eAccelerator加速PHP

代码如下:
mkdir -p /usr/local/eaccelerator_cache
#准备eaccelerator缓存目录

vi /usr/local/php/etc/php.ini
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/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"

9.准备php-cgi和nginx进程执行者用户

useradd nginx

10. 创建php-fpm配置文件- php-fpm.conf

vi /usr/local/php/etc/php-fpm.conf
<value name="display_errors">0</value>
#0改成1,页面上会输出错误日志.   取消注释
unix user of processes
      <value name="user">nginx</value>
      Unix group of processes
      <value name="group">nginx</value>                        取消注释

 <value name="max_children">128</value>
#最大子进程数128,如果内存小于2G,则64个最佳
<value name="rlimit_files">65535</value>
# Set open file desc rlimit,同时打开的文件数,linux系统允许同时打开的文件数为1024,修改linux系统中允许同时打开的文件,ulimit -SHn 65535,而且这个参数重启后还能生效,加到 /etc/profile全局配置文件的最后,开机就会生效,ulimit -a查看open files 65535
       ulimit  用户控制shell启动进程所占用的资源
              -H   设定硬性资源限制,也就是管理员设定的限制
              -S    设定软性资源限制,弹性限制
              -n    设定可同时打开的最大文件个数
              -f     设定单个文件最大大小
              -a    查看目前的限制
<value name="max_requests">1024</value>
#最大请求数, How much requests each process should execute before respawn.一个子进程能够回应1042个请求

11. 启动php-cgi(fastcgi)进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为nginx:

代码如下:
/usr/local/php/sbin/php-fpm start
#启动php-cgi
/usr/local/php/sbin/php-fpm reload
#重新加载配置文件
/usr/local/php/sbin/php-fpm stop
#关闭php-fpm,此时nginx肯定连不上php

12. 安装Nginx所需的pcre库

代码如下:
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../

13. 安装Nginx

代码如下:
tar zxvf nginx-0.8.46.tar.gz
cd nginx-0.8.46/
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

14. 修改Nginx配置文件

代码如下:
vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes 1;
#相当于cpu个数
error_log  logs/nginx_error.log;
#错误日志
pid        /usr/local/nginx/nginx.pid;
#主进程PID保存文件
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
#文件描述符数量
events
{
  use epoll;
#网络I/O模型,建议linux使用epoll,FreeBSD使用kqueue
  worker_connections 65535;
#最大允许连接数
}
http
{
  include       mime.types;
  default_type  application/octet-stream;
 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  logs/access.log  main;
#调用格式的日志

  sendfile on;
  tcp_nopush     on;
#tcp延迟
  keepalive_timeout 65;
#保持连接时间

  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;
#fastcgi设置
  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;
  server
  {
    listen       80;
#监听端口
    server_name  192.168.150.253;
#主机名,或IP。如果是主机名,要能够DNS解析

location / {
root  html;
#网站主目录。/usr/local/nginx/html/
index index.html index.htm index.php;
#默认网页顺序
}

    #limit_conn   crawler  20;   

    location ~ .*.(php|php5)?$
#~:匹配       后面正则表达式:.*任意字符  .点 php或php5结尾。碰到网页文
件名是.php或.php5结尾
    {    
root  html;
      #fastcgi_pass  unix:/tmp/php-cgi.sock;
      fastcgi_pass  127.0.0.1:9000;
#连接fastcgi,用来解析php语句
      fastcgi_index index.php;
#首页为index.php
   #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
#启动fast-cgi,可以在每个服务中启动,也可以放入/usr/local/nginx/conf/fastcgi_params,每个server都可以享用
      include fastcgi_params;
#包括fastcgi_params中参数
    }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
#图片格式缓存30天
    }
    location ~ .*.(js|css)?$
    {
      expires      1h;
#js/css缓存2小时
    }   
    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /data1/logs/access.log  access;
      }
}

参与评论