Chinaunix首页 | 论坛 | 博客
  • 博客访问: 120319
  • 博文数量: 22
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 187
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-03 21:11
文章分类
文章存档

2015年(5)

2014年(17)

分类: LINUX

2014-04-08 18:07:46

原文地址:LNMP环境搭建 作者:xiaozhenggang

LNMP

 

|| 安装所需各种依赖包

yum –y install

gcc gcc-c++ autoconf automake bison flex freetype freetype-devel fontconfig-devel gettext-devel libjpeg libjpeg-devel libpng libpng-devel libxml2 libxml2-devel libtool libtool-ltdl libtool-ltdl-devel libtiff-devel libXpm-devel libicu libicu-devel libidn libidn-devel libxslt libxslt-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 openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre pcre-devel pam-devel vim-enhanced mysql-devel

 

| 其它需要编译源码安装的依赖包

libivonv

wget

tar -xzvf libiconv-1.14.tar.gz

./configure

make && make install

 

libmcrypt

wget

tar -xzvf libmcrypt-2.5.7.tar.gz

./configure

make&&make install

/sbin/ldconfig

cd libltdl/

./configure --enable-ltdl-install

make

make install

 

mhash

wget http://ncu.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2

tar –xjvf mhash-0.9.9.9.tar.bz2

./configure

make

make install

cd ../

ln -s /usr/local/lib/libmcrypt.[la|so|so.4|so.4.4.8] /usr/lib/libmcrypt.[la|so|so.4|so.4.4.8]

ln -s /usr/local/lib/libmhash.[a|la|so|so.2|so.2.0.1] /usr/lib/libmhash .[a|la|so|so.2|so.2.0.1]

/sbin/ldconfconfig

 

mcrypt

wget http://ncu.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz

tar –xzvf mcrypt-2.6.8.tar.gz

./configure

make

make install

 

 

||编译安装mysql

 

||编译安装php Fast-CGI 模式

wget

tar -xjvf  php-5.2.17.tar.bz2

wget

gzip -cd php-5.2.17-fpm-0.5.14.diff.gz|patch -d php-5.2.17 -p1(此处为数字1,非L的小写)

cd php-5.2.17/

./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/ --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-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap=/usr --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear

 

#make的时候加参数ZEND_EXTRA_LIBS='-liconv'是因为编译时需要iconv, 但是configure的时候没有写到Makefile, 还有一种方法是直接修改Makefile, 在链接库的地方(应该在100行左右)加上-liconv

make ZEND_EXTRA_LIBS='-liconv'

make install

cp php.ini-dist /usr/local/php/etc/php.ini

 

||编译安装php扩展

|memcache

|安装依赖包libevent

1.yum install libevent

2.wget

  tar –xzvf libevent-1.4.14b-stable.tar.gz

  cd libevent-1.4.14b-stable

  ./configure –prefix=/usr/local/libevent

  make && make install

 

|wget    

tar –xzvf memcached-1.4.10.tar.gz

cd memcached-1.4.10

./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

make

make install

 

|wget

tar –xzvf memcache-2.2.6.tgz

cd memcache-2.2.6

/usr/local/php/bin/phpize

./configure --prefix=/usr/local/memcache --with-php-config=/usr/local/php/bin/php-config --enable-memcache

make

make install  

安装成功后提示如下:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/


成功后修改php.ini文件

vim /usr/local/php/etc/php.ini

extension_dir=”./” èextension_dir=”/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613”

extension = “memcache.so”

 

|安装eaccelerator

| eaccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。能使PHP程序代码执效率能提高1-10

eAccelerator 通过把经过编译后的PHP代码缓存到共享内存中,并在用户访问的时候直接调用从而起到高效的加速作用。它的效率非常高,从创建共享内存到查找编译后的代码都在非常短的时间内完成,对于不能缓存到共享内存中的文件和代码,eAccelerator还可以把他们缓存到系统磁盘上。

eAccelerator 只支持使用 mod_php 或者 fastcgi mode 安装的PHP

wget

unzip eaccelerator-0.9.6.1.zip

cd eaccelerator-0.9.6.1

/usr/local/php/bin/phpize

./configure --prefix=/usr/local/eaccelerator --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config

make

make install

安装成功后提示如下:

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/

之后,修改php.ini文件:

vim /usr/local/php/etc/php.ini

找到extension_dir,在下面添加

extension = “eaccelerator.so”

配置eaccelerator加速php

[eaccelerator]

zend_extension=” /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so”

eaccelerator.shm_size=”16”   #共享内存设置,以M为单位,默认为0

eaccelerator.cache_dir = "/tmp/eaccelerator" #缓存目录

eaccelerator.enable = "1"       #开启或关闭eaccelerator,1==,0==关;默认为1

eaccelerator.optimizer = "1"  #启动或关闭内部优化器,默认为1

eaccelerator.check_mtime = "1"    #启动或关闭PHP的文件修改检查;默认为1

eaccelerator.debug = "0"              #启动或关闭高度日志记录

eaccelerator.filter = ""    #判断哪些 PHP 文件必须缓存。可以指定缓存和不缓存的文件类型(如 "*.php *.phtml"等),如果参数以 "!" 开头,则匹配这些参数的文件被忽略缓存。默认值为 "";即,所有 PHP 文件都将被缓存。

eaccelerator.shm_max = "0"  #当使用 " eaccelerator_put() " 函数时禁止其向共享内存中存储过大的文件。该参数指定允许存储的最大值,单位:字节 (10240, 10K, 1M)"0" 为不限制。默认值为 "0"

eaccelerator.shm_ttl = "0"     # eAccelerator 获取新脚本的共享内存大小失败时,它将从共享内存中删除所有在最后 "shm_ttl" 秒内没有存取的脚本缓存。默认值为 "0",即:不从共享内春中删除任何缓存文件。

eaccelerator.prune_period = "0"    # eAccelerator 获取新脚本的共享内存大小失败时,他将试图从共享内存中删除早于"shm_prune_period" 秒的缓存脚本。默认值为 "0",即:不从共享内春中删除任何缓存文件。

eaccelerator.shm_only = "0"         #允许或禁止将已编译脚本缓存在磁盘上。该选项对 session 数据和内容缓存无效。默认值为 "0",即:使用磁盘和共享内存进行缓存

eaccelerator.compress = "1"          #允许或禁止压缩内容缓存。默认值为 "1",即:允许压缩。

eaccelerator.compress_level = "9"        #指定内容缓存的压缩等级。默认值为 "9",为最高等级。

eaccelerator.keys = "disk_only"

eaccelerator.session = "disk_only"

eaccelerator.content = "disk_only"

###设置内容缓存的存放的地方,可以设置为:

shm_and_disk 在共享缓存和硬盘(默认值)

shm 默认存在共享内存,如果共享内存已满或大小超过 "eaccelerator.shm_max" 的值,就存到硬盘

shm_only 只存放在共享内存

disk_only 只存放在硬盘

none 不缓存数据

 

eaccelerator.allowed_admin_path = "/var/www/html/21andy.com/eaccelerator"

###这是控制面板的地址 安装包里有个control.php,你把它复制到网站的任意目录,可以用它查看和管理,这个必须指定,否则查看缓存内容的时候会出错


||配置php-fpm

Nginx+PHP需要    PHP生成可执行文件才可以,所以要利用fastcgi技术来实现NginxPHP的整合,这个只要我们安装是启用FastCGI即可.这里,我们用php-fpm来管理FastCGI(php-fpm已以补丁的形式加入到PHP).

编辑配置文件:

vim php-fpm.conf

大约63行,去掉相应注释,为FastCGI指定用户,可使用默认的nobody,也可另行指定。

Unix user of processes

nobody


Unix group of processes

nobody

 

 

|启动php-cgi进程

Ulimit –SHn 65535

/usr/local/php/sbin/php-fpm start

 

####

|ulimit [-acdfHlmnpsStvw] [size]
参数介绍:
-H
设置硬件资源限制.
-S
设置软件资源限制.
-a
显示当前所有的资源限制.
-c size:
设置core文件的最大值.单位:blocks
-d size:
设置数据段的最大值.单位:kbytes
-f size:
设置创建文件的最大值.单位:blocks
-l size:
设置在内存中锁定进程的最大值.单位:kbytes
-m size:
设置可以使用的常驻内存的最大值.单位:kbytes
-n size:
设置内核可以同时打开的文件描述符的最大值.单位:n
-p size:
设置管道缓冲区的最大值.单位:kbytes
-s size:
设置堆栈的最大值.单位:kbytes
-t size:
设置CPU使用时间的最大上限.单位:seconds
-v size:
设置虚拟内存的最大值.单位:kbytes 5,简单实例: 

 

 

 

||编译安装 Nginx

wget

tar –xzvf nginx-1.0.10.tar.gz

cd nginx-1.0.10

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

make install

 

|nginx.conf 配置

#user  nobody;

worker_processes  1;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

worker_rlimit_nofile 65535;

events {

    use epoll;

    worker_connections  1024;

}

 

 

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;

    #charset   utf8;

    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;

    tcp_nodelay           on;

    #keepalive_timeout  0;

    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;

 

    gzip  on;

    gzip_min_length 1K;

    gzip_buffers   4 16K;

    gzip_http_version   1.0;

    gzip_comp_level    9;

    gzip_types text/plain application/x-javascript text/css application/xml;

    gzip_vary              on;

 

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        access_log  logs/localhost.access.log  main;

        root html;

        index index.html index.htm index.php;

      #配置nginx支持PHP

  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;

        }

       #缓存js/css相关信息

        location ~ .*\.(js|css)?$

        {

            expires 1h;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

 

|Fastcgi.conf 配置 (该文件内容基本不要修改)

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param  QUERY_STRING       $query_string;

fastcgi_param  REQUEST_METHOD     $request_method;

fastcgi_param  CONTENT_TYPE       $content_type;

fastcgi_param  CONTENT_LENGTH     $content_length;

 

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param  REQUEST_URI        $request_uri;

fastcgi_param  DOCUMENT_URI       $document_uri;

fastcgi_param  DOCUMENT_ROOT      $document_root;

fastcgi_param  SERVER_PROTOCOL    $server_protocol;

 

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;

fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

 

fastcgi_param  REMOTE_ADDR        $remote_addr;

fastcgi_param  REMOTE_PORT        $remote_port;

fastcgi_param  SERVER_ADDR        $server_addr;

fastcgi_param  SERVER_PORT        $server_port;

fastcgi_param  SERVER_NAME        $server_name;

 

|启动nginx

/usr/local/nginx/sbin/nginx

 

|配置Nginx+php-fpm开机启动

vim /etc/rc.local

在末尾添加:

ulimit -SHn 65535

/usr/local/php/sbin/php-fpm start

/usr/local/nginx/sbin/nginx

保存退出

 

||在不停止Nginx服务的情况下平滑变更Nginx配置

在修改过配置文件nginx.conf后可用如下命令检查配置文件是否正确

/usr/local/nginx/sbin/nginx –t

如提示如下所示则说明修改成功:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful


输入如下命令查看Nginx主进程号:

ps aux|grep "nginx: master process"|grep -v "grep"|awk '{print $2}'

或者

找到主进程号后可通过如下命令使配置文件生效:

kill –HUP 主进程号

或者可以这样

kill –HUP  ` ps aux|grep "nginx: master process"|grep -v "grep"|awk '{print $2}'`

kill –HUP  ` cat /usr/local/nginx/logs/nginx.pid `

 

############

nginx可接受的信号如下:

TERM, INT Terminate the server immediately
QUIT Stop the server
HUP Configuration changes, start new workers, graceful stop of old workers
USR1 Reopen log files
USR2 Upgrade the server executable
WINCH Graceful Stop (parent process advise the children to exit)
kill -HUP pid
重新应用配置文件
kill -USR1 pid
重新刷新log

 

 

||日志轮询

cat log_rotation.sh

#!/bin/bash

LOG_DIR=”/usr/local/nginx/logs”

DATA_DIR=`date +%Y%m%d`

/bin/mkdir –p $LOG_DIR/$DATA_DIR > /dev/null 2>&1

/bin/mv $LOG_DIR/access.log $LOG_DIR/$DATA_DIR/access.log

kill –USR1 `cat /usr/local/nginx/logs/nginx.pid`

 

设定log_rotation.sh在每天的000000运行:

crontab –e

59 23 * * * /bin/bash log_rotation.sh

阅读(1390) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~