CentOS 5.9 下lnmp的安装与配置

记得那时是你第一次听到lnmp,别人说这东西很好,你就跃跃欲试,可是当漫天的安装文档在你的眼前跳来跳去,你犹豫了,我该相信那一个呢?于是,再一打听,你知道了大神张宴,接着就有了这篇抄袭的文档。

一:安装lnmp

1 安装前的准备工作

a 解决包的依赖性

#!/bin/sh
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libX11-devel 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 cmake

b 添加 www 用户

#!/bin/sh
is_www=`grep '^www' /etc/passwd | wc -l`
if [ $is_www -eq 0 ];then 
    useradd -s /sbin/nologin www 
fi

c add ld.so.conf

#!/bin/sh
is_lib=`grep /usr/local/lib /etc/ld.so.conf|wc -l`
if [ $is_lib -eq 0 ];then 
    echo  "/usr/local/lib">>/etc/ld.so.conf
fi

d 解决编码转换问题

#!/bin/sh
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14/
./configure --prefix=/usr/local
make
make install
cd ../

e 添加加密算法扩展库,支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法

#!/bin/sh
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 mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../

f Mhash为PHP提供了多种哈希算法,如MD5,SHA1,GOST 等

#!/bin/sh
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

2 安装mysql-5.5.29(php需支持mysql客户端,必须)

#!/bin/sh
if [ ! -d mysql-5.5.29 ];then
    mkdir -p /usr/local/mysql/
    tar zxvf mysql-5.5.29.tar.gz
    cd mysql-5.5.29/
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
    -DMYSQL_DATADIR=/data/mysql_db \
    -DSYSCONFDIR=/etc \
    -DDEFAULT_CHARSET=utf8 \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk \
    -DWITH_MYISAM_STORAGE_ENGINE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 \
    -DWITH_MEMORY_STORAGE_ENGINE=1 \
    -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
    -DWITH_READLINE=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DMYSQL_USER=mysql \
    -DENABLE_DTRACE=OFF \
    -DMYSQL_TCP_PORT=3306
    make
    make install
    cd ..
    echo "/usr/local/mysql/lib">>/etc/ld.so.conf
    ldconfig
fi

3 安装php-5.3.21

#!/bin/sh
if [ ! -d php-5.3.21 ];then
cp -frp /usr/lib64/libldap* /usr/lib/
tar zxvf php-5.3.21.tar.gz
cd php-5.3.21/
./configure --prefix=/usr/local/php_fpm --with-config-file-path=/usr/local/php_fpm/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-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-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-ftp
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php_fpm/etc/php.ini
echo "php is ok!"
sleep 2
cd ..
fi

4 安装php的常用拓展

#!/bin/sh
#memcache 拓展
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php_fpm/bin/phpize
./configure --with-php-config=/usr/local/php_fpm/bin/php-config
make
make install
cd ..

#memcached 拓展
cd memcached/
tar zxf libmemcached-0.48.tar.gz
cd libmemcached-0.48
./configure --prefix=/usr/local/libmemcached --with-memcached
make
make install
cd ..

tar zxf memcached-1.0.2.tgz
cd memcached-1.0.2
/usr/local/php_fpm/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php_fpm/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached
make
make install
cd ../../

tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php_fpm/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php_fpm/bin/php-config
make
make install
cd ..

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php_fpm/bin/phpize
./configure --with-php-config=/usr/local/php_fpm/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install
cd ..

tar zxvf phpredis.tar.gz
cd owlient-phpredis-90ecd17/
/usr/local/php_fpm/bin/phpize
./configure -with-php-config=/usr/local/php_fpm/bin/php-config
make && make install
cd ..

#解决perl 兼容问题
yum -y install perl-CPAN

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_fpm/bin/phpize
./configure --with-php-config=/usr/local/php_fpm/bin/php-config
make
make install
cd ..

5 安装nginx

#!/bin/sh
#ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
tar zxvf pcre-8.32.tar.gz
mv pcre-8.32 /usr/local/src/pcre

#--with-pcre=DIR 是指 pcre 的源码目录,而不是 pcre 的安装目录
tar zxvf nginx-1.2.7.tar.gz
cd nginx-1.2.7/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre/
make && make install
cd ../

二:配置lnmp

1 配置mysql

a mysql作为服务器的一些后续工作
#!/bin/sh

#创建数据存放目录
mkdir -p /data/mysql_db/

#创建用户和用户组与赋予数据存放目录权限
useradd -s /sbin/nologin mysql
chown mysql.mysql -R /data/mysql_db/

#初始化数据库
cd /usr/local/mysql/
scripts/mysql_install_db  --user=mysql  --basedir=/usr/local/mysql --datadir=/data/mysql_db/

#设置mysqld的开机启动:
cd /usr/local/mysql/
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql

b 复制配置文件到/etc/my.cnf,根据自己需要做适当修改即可
cd /usr/local/mysql
cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf

2 配置php

a 手工修改:查找/usr/local/php_fpm/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/php_fpm/lib/php/extensions/no-debug-non-zts-20090626/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "memcached.so"
extension = "pdo_mysql.so"
extension = "imagick.so"
extension = "ftp.so"
extension = "redis.so"

再查找output_buffering = Off
修改为output_buffering = On

再查找; cgi.fix_pathinfo=1
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。

修改php上传文件、变量、执行时间、内存等设置:
upload_max_filesize = 500M
post_max_size = 80M
max_execution_time = 600
max_input_time = 600
max_input_nesting_level = 64
memory_limit = 256M

开启php短标签:
short_open_tag = On

关闭php的错误提示:
error_reporting = E_ERROR
display_errors = Off

b 编辑/usr/local/php_fpm/etc,添加如下内容
pid = run/php-fpm.pid

c 编辑启动脚本
cp lnmp-install-2013/php-5.3.21/sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm

编辑 /etc/init.d/php-fpm相关设置,如下
php_fpm_BIN=/usr/local/php_fpm/sbin/php-fpm
php_fpm_CONF=/usr/local/php_fpm/etc/php-fpm.conf
php_fpm_PID=/usr/local/php_fpm/var/run/php-fpm.pid

3 配置nginx

a 编辑/usr/local/nginx/conf/fastcgi_params,添加以下内容

fastcgi_pass   127.0.0.1:9000;
#fastcgi_pass   unix:/tmp/php-cgi.sock;
fastcgi_index  index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

b 修改nginx.conf,如下
user  www www;
worker_processes  6;

worker_rlimit_nofile 51200;

error_log  /data/logs/nginx_error.log  crit;
pid        /usr/local/nginx/nginx.pid;

events {
 use epoll;
 #use kqueue;   #FreeBSD system
 worker_connections 51200;
}

http {
 include       mime.types;
 default_type  application/octet-stream;

 #charset  gb2312;

 server_names_hash_bucket_size 256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 500m;

 client_header_timeout 3m;
 client_body_timeout 3m;
 send_timeout 3m;

 keepalive_timeout 75 20;
 server_tokens off;
 sendfile on;
 #tcp_nopush on;
 #tcp_nodelay on;

 # support ssi shtml
 #ssi on;
 #ssi_silent_errors on;
 #ssi_types text/shtml;

 fastcgi_intercept_errors 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 128k;

 gzip on;
 gzip_proxied expired no-cache no-store private auth;
 gzip_min_length  1k;
 gzip_buffers     4 16k;
 #gzip_http_version 1.0;
 gzip_comp_level 3;
 gzip_types       text/plain application/x-javascript text/css application/xml;
 gzip_disable  "MSIE [1-6]\.";
 gzip_vary on;

 #log_format access '$remote_addr - $remote_user [$time_local]  '
 #                    '"$request" $status $body_bytes_sent '
 #                    '"$http_referer" "$http_user_agent" '
 #                    '$host $request_time $http_x_forwarded_for';
 #access_log /data/logs/http.a.log;
 #error_log  /data/logs/http.e.log;

include vhosts/budong.com.conf;
}

c 编辑/usr/local/nginx/conf/vhosts/budong.com.conf,如下

server
  {
    listen       80;
    server_name  budong.com;
    index index.html index.htm index.php;
    root  /data/html/budong;

    location ~ .*\.(php|php5)?$
    {
      include fastcgi_params;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }

    location = /sitemap.xml {
        root  /data/html/budong;
    }

    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  /data/logs/budong.a.log  access;
    error_log  /data/logs/budong.e.log  access;
    error_page  502 http://budong.com;
    error_page  404 http://budong.com;
    }

d 将nginx的执行文件加入到系统环境变量
vim /etc/profile
PATH=$PATH:/usr/local/nginx/sbin/

三:是时候结束了

自言自语:

未完待续...

抄袭资料:

张宴的博客:http://www.s135.com/nginx_php_v6