系统环境为Centos6.5
1、配置防火墙,开启80端口、3306端口
vi /etc/sysconfig/iptables #编辑防火墙配置文件,将下面两条规则添加到22端口规则下面 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT(允许80端口通过防火墙) -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT(允许3306端口通过防火墙) /etc/init.d/iptables restart #最后重启防火墙使配置生效
2、关闭SELINUX
vi /etc/selinux/config #编辑 SELINUX=disabled #增加 shutdown -r now #重启系统
安装相关依赖包
yum install -y patch make cmake gcc gcc-c++gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel libjpeglibjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel freetypefreetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2bzip2-devel libevent libevent-devel ncurses ncurses-devel curl curl-devele2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel opensslopenssl-devel vim-minimal nano fonts-chinese gettext gettext-develncurses-devel gmp-devel pspell-devel unzip libcap
============================Install MySQL5.6.16==================================
安装mysql
yum install gcc gcc-c++ cmake ncurses ncurses-devel #安装相关的依赖包 groupadd mysql #创建mysql组 useradd -s /sbin/nologin -M -g mysqlmysql #创建mysql用户并加入mysql组禁止登陆系统 tar zxvf mysql-5.6.16.tar.gz cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DEXTRA_CHARSETS=all-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=1-DWITH_SSL=system -DWITH_ZLIB=system -DWITH_EMBEDDED_SERVER=1-DENABLED_LOCAL_INFILE=1 make && make install cp support-files/my-default.cnf /etc/my.cnf #复制my.cnf文件 vi /etc/my.cnf #编辑my.cnf文件 datadir = /usr/local/mysql/var #指定mysql数据存放目录 /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --user=mysql #指定mysql用户初始化数据库 chown -R mysql /usr/local/mysql/var chgrp -R mysql /usr/local/mysql/. cp support-files/mysql.server /etc/init.d/mysqld #复制mysql命令 chmod 755 /etc/init.d/mysqld #修改mysql 可执行权限 chkconfig --add mysqld #加入至系统服务 chkconfig mysqld on #开机启动
初始化mysql时肯能会报如下错误
错误日志:
tail /data/mysql/localhost.localdomain.err 120710 20:36:24 mysqld_safe Starting mysqld daemon with databases from /data/mysql 120710 20:36:24 [ERROR] COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'latin1' 120710 20:36:24 [ERROR] Aborting
解决:修改my.cnf后重新初始化
[client] default-character-set = utf8 [mysqld] character_set_server=utf8
注:如果设为gbk,虽然初始化成功,但是在其它服务器上的导入数据库时可能会报错。
输出mysql库文件
vi /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib ldconfig
将mysql相关命令添加至环境变量中
vim /etc/profile.d/mysql.sh export PATH=$PAHT:/usr/local/mysql/bin source /etc/profile
输出库文件
ln -sv /usr/local/mysql/include/ /usr/include/mysql
输出帮助文档
vim /etc/man.config MANPATH /usr/local/mysql/man /etc/init.d/mysql start #启动mysql服务 /usr/local/mysql/bin/mysqladmin -u rootpassword $mysqlrootpwd #给root用户设置密码
============================Install PHP5.5.12====================================
安装所需依赖的软件包(支持库)
yum install libcurl-devel libtool libtool-ltdl-devel
libiconv(对文本进行编码间的转换,用它来处理中文各种编码之间的转换)
tar zxvf libiconv-1.14.tar.gz cd libiconv-1.14/ ./configure make && make install
libmcrypt(实现加密功能的库)
tar zxvf libmcrypt-2.5.6.tar.gz cd libmcrypt-2.5.6/ ./configure make && make install /sbin/ldconfig cd libltdl/ ./configure --enable-ltdl-install make && make install
mhash(哈稀函数库)
tar zxvf mhash-0.9.9.9.tar.gz cd mhash-0.9.9.9/ ./configure make && make install
动态库文件链接
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.6/usr/lib/libmcrypt.so.4.4.6 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 ldconfig #不重新加模块,下面编译 mcrypt 时,检查编译环境会出错。
安装php的mcrypt扩展(动态加载编译)
tar zxvf mcrypt-2.6.8.tar.gz cd mcrypt-2.6.8/ ./configure make && make install
这里可能会出现configure:error: *** libmcrypt was not found的问题
configure: error: *** libmcrypt was not found
为了的到mcrypt.so库文件,先后安装编译了mhash和libmcrypt,但是到最后编译mcrypt时报错:
最后发现是因为环境变量的问题,gcc编译的时候根据自身定义的变量寻找相关函数库等文件,
libmcrypt也是刚安装的,在变量中没有定义出来,所以手动添加:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
再次编译即可。
输出库文件给系统库查找路径
echo "/lib" >>/etc/ld.so.conf echo "/usr/lib" >>/etc/ld.so.conf echo "/usr/lib64" >>/etc/ld.so.conf echo "/usr/local/lib" >>/etc/ld.so.conf ldconfig
控制资源限制
cat>>/etc/security/limits.conf<<eof * soft nproc 65535 * hard nproc 65535 * soft nofile 65535 * hard nofile 65535 eof
修改文件数量限制
cat >>/etc/sysctl.conf<<eof fs.file-max=65535 eof
安装PHP-5.4.28
tar zxvf php-5.4.28.tar.gz cd php-5.4.28 / ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysql=mysqlnd --with-mysqli=mysqlnd--with-pdo-mysql=mysqlnd --with-iconv-dir --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-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 --disable-fileinfo make ZEND_EXTRA_LIBS='-liconv' make install
为php提供配置文件:
cp php.ini-production /etc/php.ini
为php-fpm提供配置文件:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
为php-fpm提供Sysv init脚本,并将其添加至服务列表
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm chmod +x /etc/rc.d/init.d/php-fpm chkconfig --add php-fpm chkconfig php-fpm on service php-fpm start
修改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/;cgi.fix_pathinfo=1/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 sed -i 's/register_long_arrays =On/;register_long_arrays = On/g' /usr/local/php/etc/php.ini sed -i 's/magic_quotes_gpc =On/;magic_quotes_gpc = On/g' /usr/local/php/etc/php.ini sed -i 's/disable_functions=.*/disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g'/usr/local/php/etc/php.ini
安装ZendGuardLoader
tar zxvf ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386.tar.gz mkdir -p /usr/local/zend/ cp ZendGuardLoader-70429-PHP-5.4-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so /usr/local/zend/
将ZendGuardLoader写入到php.ini配置文件中
cat>>/usr/local/php/etc/php.ini<<EOF ;eaccelerator ;ionCube [Zend Optimizer] zend_extension=/usr/local/zend/ZendGuardLoader.so EOF
============================Install Nginx1.6.0==================================
创建nginx组和用户
groupadd nginx useradd –M –g nginx –s /sbin/nologin nginx
安装PCRE
,pcre是一个Perl库,包括 perl兼容的正规表达式库
tar jxvf pcre-8.35.tar.bz2 cd pcre-8.35 ./configure make && make install ldconfig
安装nginx
tar zxvf nginx-1.6.0.tar.gz cd nginx-1.6.0/ ./configure --user=nginx --group=nginx--prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module--with-http_gzip_static_module --with-ipv6 make && make install
将nginx命令链接到系统命令中
ln -s /usr/local/nginx/sbin/nginx/usr/bin/nginx
创建虚拟主机,打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在http
范围引入虚拟主机配置文件includevhost/*.conf;
mkdir -p /usr/local/nginx/conf/vhost
整合nginx和php
启用如下选项
location / { root html; #必须设置 web 根目录 index index.php index.html index.htm; #添加 php 格式的主页 } location ~ \.php$ { root html; #必须设置 web 根目录 fastcgi_passunix:/tmp/php-cgi.sock; fastcgi_pass127.0.0.1:9000; #定义 fastcgi 所监听的IP地址和端口号,也可以是 socket fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #PHP 使用 SCRIPT_FILENAME 参数决定需要执行哪个脚本;这里一定要设为 web 目录的绝对路径或是变量 $document_root。这里非常重要,配置错误有可能会出现php空白页,或找不到页面的错误。 fastcgi_param QUERY_STRING $query_string; #QUERY_STRING 包含请求中的某些参数。 include fastcgi_params; }
创建PHP测试文件
cat >/usr/local/nginx/html/phpinfo.php<<eof <? phpinfo(); ?> Eof
测试php与MySQL
可以使用下面的php代码用来测试php是否可以连接MySQL数据库:
cat >/usr/local/nginx/html/index.php<<eof <?php $link=mysql_connect('localhost','root','数据库密码'); if ($link) echo "successful"; else echo "Faile"; mysql_close(); ?> Eof
=============================PHP加速和扩展====================================
根据需要安装PHP加速和扩展(eAccelerator/ionCube/p_w_picpathMagick/memcached/xcache)
主要介绍eAccelerator,xcache,memcached,p_w_picpathMagick这几个PHP加速和php扩展的安装及使用,这些不一定对每个人都有用,自己按需要安装。
安装eAccelerator(PHP加速器,使PHP程序代码执行效率能提高1-10倍)
eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。它还有对脚本起优化作用,以加快其执行效率。
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
在php.ini文件末尾加上一下配置
[eaccelerator] zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so" eaccelerator.shm_size="1" 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" eaccelerator.keys = "disk_only" eaccelerator.sessions ="disk_only" eaccelerator.content ="disk_only"
改完重启php-fpm服务和nginx服务
==============================================================================
配置 xcache-3.0.4,整合php与xcache
XCache 是一个国人开发的又快又稳定的 PHPopcode 缓存器. 经过良好的测试并在大流量/高负载的生产机器上稳定运行. 经过(在 linux 上)测试并支持所有现行 PHP 分支的最新发布版本。
tar zxvf xcache-3.0.4.tar.gz cd xcache-3.0.4/ /usr/local/php/bin/phpize ./configure --enable-xcache--enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php/bin/php-config make && make install
在php.ini文件末尾添加以下内容
;xcache [xcache-common] extension = xcache.so ;[xcache.admin] ;xcache.admin.enable_auth = On ;xcache.admin.user = "admin" ;run: echo -n "yourpassword"|md5sum |awk '{print $1}' to get md5 password ;xcache.admin.pass = "md5password" [xcache] xcache.shm_scheme = "mmap" xcache.size = 20M ; set to cpu count (cat /proc/cpuinfo |grep-c processor) xcache.count = $cpu_count xcache.slots = 8K xcache.ttl = 0 xcache.gc_interval = 0 xcache.var_size = 4M xcache.var_count = 1 xcache.var_slots = 8K xcache.var_ttl = 0 xcache.var_maxttl = 0 xcache.var_gc_interval = 300 xcache.readonly_protection = Off ; for *nix, xcache.mmap_path is a filepath, not directory. (auto create/overwrite) ; Use something like"/tmp/xcache" instead of "/dev/*" if you want to turn onReadonlyProtection ; different process group of php won'tshare the same /tmp/xcache xcache.mmap_path = "/dev/zero" xcache.coredump_directory = "" xcache.experimental = Off xcache.cacher = On xcache.stat = On xcache.optimizer = Off [xcache.coverager] ; enabling this feature will impactperformance ; enable only if xcache.coverager == On&& xcache.coveragedump_directory == "non-empty-value" ; enable coverage data collecting andxcache_coverager_start/stop/get/clean() functions xcache.coverager = Off xcache.coveragedump_directory ="" ;xcache end
===============================================================================
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。
安装memcache
tar zxvf memcache-3.0.6.tgz cd memcache-3.0.6/ /usr/local/php/bin/phpize ./configure--with-php-config=/usr/local/php/bin/php-config make && make install vi /usr/local/php/etc/php.ini
将extension_dir = “./”修改为extension = “memcache.so”
; extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/" extension = "memcache.so"
或者
sed -i 's#extension_dir ="./"#extension_dir ="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"\nextension= "memcache.so"\n#' /usr/local/php/etc/php.ini
tar zxvf libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-stable/ ./configure --prefix=/usr/local/libevent make && make install echo "/usr/local/libevent/lib/">> /etc/ld.so.conf ln -s/usr/local/libevent/lib/libevent-2.0.so.5 /lib/libevent-2.0.so.5 ldconfig
安装memcached
tar zxvf memcached-1.4.20.tar.gz cd memcached-1.4.20/ ./configure --prefix=/usr/local/memcached make &&make install ln /usr/local/memcached/bin/memcached/usr/bin/memcached
启动memcached服务
/usr/bin/memcached -d -p 11211 -u root -m64 -c 1024 -P /var/run/memcached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是64MB,
-u运行Memcache的用户,这里是root,
-l监听服务器IP地址(不指定为本机)
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,按照服务器的负载量来设定
-P是设置保存Memcache的pid文件,这里保存在/var/run/memcached.pid
chmod +x /etc/init.d/memcached useradd -s /sbin/nologin nobody chkconfig --level 345 memcached on
可以编写memcached.php到网站根目录测试
<?php header("Content-Type:text/html;charset=utf-8"); //连接 $mem = new Memcache; $mem->connect("127.0.0.1", 11211)or die ("Could not connect"); //显示版本 $version = $mem->getVersion(); echo "Memcached Server version: ".$version."<br>"; //保存数据 $mem->set('key1', 'This is first value',0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val."<br>"; //替换数据 $mem->replace('key1', 'This is replacevalue', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."<br>"; //保存数组 $arr = array('aaa', 'bbb', 'ccc', 'ddd'); $mem->set('key2', $arr, 0, 60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //删除数据 $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."<br>"; //清除所有数据 $mem->flush(); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //关闭连接 $mem->close(); ?>
===============================================================================
安装ImageMagick-6.8.9-1(Linux下强大图像处理函数,与GD类似)
ImageMagick是一个用于查看、编辑位图文件以及进行图像格式转换的开放源代码软件套装。它可以读取、编辑超过100种图象格式。可用来替换GD库。
tar zxvf ImageMagick-6.8.9-1.tar.gz cd ImageMagick-6.8.9-1/ ./configure --prefix=/usr/local/p_w_picpathmagick make && make install
安装Imagick(连接PHP和ImageMagick的通道)
tar zxvf imagick-3.1.2.tgz cd imagick-3.1.2/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config--with-imagick=/usr/local/p_w_picpathmagick make && make install
编写/usr/local/php/etc/php.ini,添加以下内容
extension = "imagick.so"
==============================================================================
以上就是LNMP和PHP加速缓存扩展的安装。