[转] Centos下的LNMP架构

huhuhuemail 2014-07-05

 前提:安装Centos (略)

 安装LNMP,附件为nginx,php,php-fpm配置文件

 1. 安装wget工具包

yum install -y wget

 2. 安装相关软件包/库 

  1> 一些软件依赖包

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>安装libevent,memcached需要

yum -y install libevent libevent-devel

   3> 下载软件包

wget http://php-fpm.org/downloads/php-5.2.9-fpm-0.5.10-unofficial.diff.gz
	wget http://nginx.org/download/nginx-1.7.2.tar.gz
	wget http://cn2.php.net/get/php-5.3.28.tar.gz
	wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.6-rc.tar.gz
	wget http://www.memcached.org/files/memcached-1.4.20.tar.gz
	wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz   
	wget http://downloads.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
	wget http://downloads.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
	wget http://downloads.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz 
	wget http://downloads.sourceforge.net/project/pcre/pcre/8.10/pcre-8.10.tar.gz  
	wget http://sourceforge.net/projects/imagemagick/files/6.8.9-sources/ImageMagick-6.8.9-5.tar.gz
	wget http://pecl.php.net/get/APC-3.1.4.tgz  
	wget http://pecl.php.net/get/memcache-2.2.6.tgz 
	wget http://pecl.php.net/get/imagick-3.0.1RC2.tgz

  3.安装

  1> 安装libiconv

cd /usr/src/  
 tar zxvf libiconv-1.13.1.tar.gz 
 cd libiconv-1.13.1/  
 ./configure --prefix=/usr/local 
 make  
 make install

   2> 修改动态链接载入的目录查找文件

vi /etc/ld.so.conf
 #在文件最后添加一行内容/usr/local/lib,然后运行以下命令/sbin/ldconfig

   3> 安装libmcrypt  

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 ../../

   4> 安装mhash

tar zxvf mhash-0.9.9.9.tar.gz 
  cd mhash-0.9.9.9/ 
  ./configure 
  make && make install 
  cd ../  
  ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

   5> 安装mcrypt

tar zxvf mcrypt-2.6.8.tar.gz 
 cd mcrypt-2.6.8/ 
 /sbin/ldconfig 
 ./configure 
 make && make install 
 cd ../

   6> 安装memcached 

tar xvf memcached-1.4.5.tar.gz cd memcached-1.4.5  
 ./configure --prefix=/usr/local/memcached 
 make && make install 
 cd ../

   7> 安装pcre

tar zxvf pcre-8.10.tar.gz 
 cd pcre-8.10/ 
 ./configure 
 make && make install 
 cd ../

   8> 安装Nginx

/usr/sbin/groupadd www  
 /usr/sbin/useradd -g www www   
 tar zxvf nginx-0.8.52.tar.gz 
 cd nginx-0.8.52/  
 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 
 make && make install 
 cd ../

   9> 安装PHP

tar zxvf php-5.3.3.tar.gz 
 cd php-5.3.3   
 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-curl --with-curlwrappers --with-freetype-dir --with-jpeg-dir --with-png-dir --with-gd --enable-gd-native-ttf --with-iconv-dir=/usr/local/libiconv --with-libxml-dir=/usr/local --with-mhash --with-mcrypt  --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --with-xmlrpc --with-zlib --disable-debug --disable-rpath --enable-bcmath --enable-fpm   --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-pcntl --enable-safe-mode --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip --without-pear --enable-ftp --with-libdir=lib64 ←64位操作系统需要添加此项,否则去除此项
 make ZEND_EXTRA_LIBS='-liconv' 
 make install   
 cp ./php.ini-production /usr/local/php/etc/php.ini 
 cd ../

   10> 安装ImageMagick

tar zxvf ImageMagick-6.6.5-0.tar.gz 
 cd ImageMagick-6.6.5-0/ 
 ./configure 
 make && make install 
 cd ../

   11> 安装PHP扩展imagick

tar zxvf imagick-3.0.1RC2.tgz 
 cd imagick-3.0.1RC2/   
 ln -s /usr/local/include/ImageMagick-6 /usr/local/include/ImageMagick
 /usr/local/php/bin/phpize  
 ./configure --with-php-config=/usr/local/php/bin/php-config 
 make && make install 
 cd ../

   12> 安装PHP扩展memcache

tar zxvf memcache-2.2.6.tgz 
 cd memcache-2.2.6/   
 /usr/local/php/bin/phpize  
 ./configure --with-php-config=/usr/local/php/bin/php-config 
 make && make install 
 cd ../

   13> 安装PHP扩展APC

tar zxvf APC-3.1.4.tgz 
 cd APC-3.1.4/   
 /usr/local/php/bin/phpize  
 ./configure --with-php-config=/usr/local/php/bin/php-config 
 make && make install 
 cd ../

   14> 安装MySQL

/usr/sbin/groupadd mysql  
 /usr/sbin/useradd -g mysql mysql   
 tar xvf mysql-5.5.6-rc.tar.gz 
 cd mysql-5.5.6-rc/   
 ./configure --prefix=/usr/local/mysql/ --with-unix-socket-path=/tmp/mysql.sock --with-big-tables --with-charset=utf8  --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312,utf8 --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static  --with-plugins=partition,innobase,myisammrg --with-pthread --with-readline --without-debug --without-isam --enable-assembler --enable-local-infile  --enable-thread-safe-client   
 make 
 make install   
 chmod +w /usr/local/mysql  
 chown -R mysql:mysql /usr/local/mysql   
 cd ../

 4.配置

  1> MYSQL配置

mkdir -p /var/mysql/  
mkdir -p /var/mysql/data/ 
mkdir -p /var/mysql/log/  
chown -R mysql:mysql /var/mysql/   
/usr/local/mysql/bin/mysql_install_db --defaults-file=/var/mysql/my.cnf --basedir=/usr/local/mysql --datadir=/var/mysql/data --user=mysql   
vi /var/mysql/my.cnf

   2> PHP配置

#[1]# vi /usr/local/php/etc/php.ini 

#[2]查找 extension_dir = "ext"(809行左右),在后面添加以下几行:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" 
extension=apc.so  
extension=imagick.so 
extension=memcache.so

#[3] 按shift+g键跳到配置文件的末尾,添加以下配置信息:
[APC]  
apc.enabled=1 
apc.ttl=7200 
apc.shm_size=32

#3> PHP-FPM配置 
cd /usr/local/php/etc/  
cp php-fpm.conf.default php-fpm.conf 
vi php-fpm.conf
 
#4> Nginx配置
cd /usr/local/nginx/conf/  
cp nginx.conf.default nginx.conf 
vi nginx.conf

 5.启动

 1> 启动MySQL 

##cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/ 
##chkconfig --add mysql.server  #加入service和开机启动   
##/etc/rc.d/init.d/mysql.server start  #手动启动,下面设置root密码 
##上面方法启动不成功,下面的可以,不知道为什么,找了半天,找不到答案

/usr/local/mysql/bin/mysqld_safe --defaults-file=/var/mysql/my.cnf & #加入/etc/rc.d/rc.local中去,系统启动自动启动
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'   
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown  #手动关闭

  2> 启动memcached

chmod 0777 /var/run  #允许普通用户程序写入pid文件   
/usr/local/memcached/bin/memcached -p 11211 -d -u www -P /var/run/memcached.pid -m 64M -c 1024

参数含义:
-p memcached监听的TCP端口  
-l监听的ip地址,127.0.0.1是本机,当然也可以写上你的服务器IP,如:61.150.91.26,这是我服务器的IP地址,如果你需要多个服务器都能够读取这台memcached的缓存数据,那么就必须设定这个ip 
-d 以daemon方式运行,将程序放入后台  
-u memcached的运行用户,我设定的是nobody 
-P memcached的pid文件路径  
-m memcached可以使用的最大内存数量 
-c memcached同时可以接受的最大的连接数 如果你希望以socket方式来访问memcached,那么在启动的时候就必须去掉 -l和-p参数,并加上-s参数:     
-s memcached的socket文件路径

  3> 启动mephp-fpm和nginx

ulimit -SHn 65535  
/usr/local/php/sbin/php-fpm 
/usr/local/nginx/sbin/nginx
#以将这3条语句写入/etc/rc.local文件,这样每次开机就可以启动php-fpm和nginx了。

  4> 优化Linux内核参数

vi /etc/sysctl.conf

#在文件末尾增加以下内容:
# append 
net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog =  32768 net.core.somaxconn = 32768  
net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216  
net.ipv4.tcp_timestamps = 0 
net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2  
net.ipv4.tcp_tw_recycle = 1 #net.ipv4.tcp_tw_len = 1 net.ipv4.tcp_tw_reuse = 1  
net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800  
#net.ipv4.tcp_fin_timeout = 30 
#net.ipv4.tcp_keepalive_time = 120 
net.ipv4.ip_local_port_range = 1024  65535

使配置立即生效:
/sbin/sysctl -p

  

  

相关推荐