部署网站运行平台LNMP + Memcached缓存
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了部署网站运行平台LNMP + Memcached缓存相关的知识,希望对你有一定的参考价值。
部署网站运行平台LNMP + Memcached缓存
*本实验的LNMP和Memcached都安装在同一台服务器上
*nginx 提供网站服务
*mysql 提供数据库服务
*php 提供动态网站编程语言
*Memcached 提供数据缓存(通过降低对Database的访问来加速web应用程序)
一、搭建LNMP平台(源码包安装) Linux+Nginx+Mysql+PHP(perl)
1)基本环境准备
安装“开发工具”和“开发库”“Development libraries”“Development tools”“X Software Development ”pcre-devel ( zlib-devel /gcc/openssl-devel)
[[email protected]~]# service httpd stop && chkconfig httpd off //关闭自带的httpd
[[email protected]~]# service mysqld stop && chkconfig mysqld off //关闭自带的mysql
[[email protected]~]# mv /etc/my.cnf /etc/my.cnf.bak //移除mysql配置文件
[[email protected]~]# yum groupinstall "开发工具" "开发库" "X 软件开发"
[[email protected]~]# yum -y install pcre-devel zlib-developenssl-devel gcc gcc-c+++ make
2)源码包安装nginx
注:可用./configure --help 查看配置参数
[[email protected]]# groupadd -r nginx //创建nginx组
[[email protected]]# useradd -r -g nginx -M nginx //创建nginx进程拥有者用户
[[email protected]]# tar -zxf nginx-1.2.0.tar.gz
[[email protected]]# cd nginx-1.2.0
[[email protected]]# cat conf.sh //创建配置脚本
./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/nginx.pid \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi \
--with-pcre
[[email protected]]# sh conf.sh //执行脚本进行配置
[[email protected]]# make && make install
[[email protected]]# cd /usr/local/nginx/
[[email protected]]# ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf //启动nginx
[[email protected]]# netstat -anptu | grep :80
[[email protected]]# elinks --dump http://localhost //测试能否访问
3)源码包安装mysql-5.5.13.tar.gz 需要用cmake编译工具
[[email protected]]#groupadd mysql //添加mysql组
[[email protected]]#useradd -M -s /sbin/nologin -g mysql mysql //添加mysql用户
[[email protected]]# tar -zxf cmake-2.8.10.2.tar.gz
[[email protected]]# cd cmake-2.8.10.2
[[email protected]]# ./bootstrap --prefix=/usr/local/cmake //指定安装目录
[[email protected]]# make && make install
[[email protected]]# /usr/local/cmake/bin/cmake –version //验证查看是否成功安装cmake编译工具
cmakeversion 2.8.10.2
[[email protected]]# tar -zxf mysql-5.5.13.tar.gz
[[email protected]]# cd mysql-5.5.13
[[email protected]]# cat conf.sh //若/usr/local/mysql已存在,先删除mysql/
/usr/local/cmake/bin/cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/usr/local/mysql/data-DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_USER=mysql -DEXTRA_CHARSETS=all -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1
[[email protected]]# sh conf.sh
[[email protected]]# make && make install
[[email protected] mysql]# cd /usr/local/mysql/
[[email protected]]# chown -R mysql:mysql . //修改安装目录mysql权限,所有者和所属组都改为mysql,作用是让所有用户可以访问mysql
[[email protected]]# ./scripts/mysql_install_db --user=mysql //初始化授权库
[[email protected]]# ls /usr/local/mysql/data //查看数据库初始化是否成功
mysql performance_schema test
[[email protected]]# cp./support-files/my-medium.cnf /etc/my.cnf //生成配置文件
[[email protected]]# cp ./support-files/mysql.server /etc/init.d/mysqldd //生成启动脚本
[[email protected]]# chmod +x /etc/init.d/mysqldd //给文件加执行权限
[[email protected]]# chkconfig --add mysqldd
[[email protected]]# service mysqldd start
[[email protected]]# chkconfig --level 35 mysqldd on
[[email protected]]# ./bin/mysql -uroot -p //测试连接
把源码mysql提供命令的目录添加到PATH里:
[[email protected]~]# cat /etc/bashrc //添加命令搜索路径
export PATH=/usr/local/mysql/bin:$PATH //指定PATH为全局变量,添加到未尾
[[email protected]~]# source /etc/bashrc //重新加载该文件
[[email protected]~]# mysql -uroot -p //用此方式测试连接数据库
[[email protected]~]# cat /etc/ld.so.conf
includeld.so.conf.d/*.conf
/usr/local/mysql/lib //指定mysql库文件的位置
[[email protected]~]# ldconfig -v //重新加载系统内的头文件和库文件信息
[[email protected]~]# ldconfig -v | grep mysql --color //查看
4)源码包安装PHP
A.需安装PHP扩展包:mhash、libiconv 、libmcrypt
加密时需要libltdl的支持,因此libmcrypt中集成了安装libltdl的文件
[[email protected] Desktop]# tar-zxf mhash-0.9.9.9.tar.gz //安装哈稀函数库,做哈稀运算
[[email protected] Desktop]# cdmhash-0.9.9.9
[[email protected] mhash-0.9.9.9]#./configure
[[email protected] mhash-0.9.9.9]#make && make install
[[email protected]]#cd -
[[email protected] Desktop]# tar-zxf libiconv-1.13.tar.gz //处理中文各种编码之间的转换
[[email protected] Desktop]# cd libiconv-1.13
[[email protected] libiconv-1.13]#./configure
[[email protected] libiconv-1.13]#make && make install
[[email protected]]#cd –
[[email protected] Desktop]# tar-zxf libmcrypt-2.5.8.tar.gz //提供加密功能的库文件
[[email protected] Desktop]# cdlibmcrypt-2.5.8
[[email protected]]# ./configure
[[email protected]]#make && make install
[[email protected]]# ldconfig -v
[[email protected]]# cd libltdl/
[[email protected]]#./configure --with-gmetad --enable-gexec --enable-ltdl-install
//配置libltdl
[[email protected] libltdl]# make&& make install
B、指定扩展包的库文件位置(即对库文件夹做链接)
[[email protected]~]# ln -sv /usr/local/lib/libmcrypt* /usr/lib
[[email protected]~]# ln -sv /usr/local/lib/mhash* /usr/lib
[[email protected]~]# ln -sv /usr/local/lib/libiconv* /usr/lib
[[email protected]~]# ldconfig -v //更新库链接
C、安装PHP
配置--enable-fpm
编译make ZEND_EXTRA_LIBS=’-liconv’、安装
[[email protected] Desktop]# tar -zxf php-5.4.9.tar.gz
[[email protected]]# cd php-5.4.9
[[email protected]]# cat conf.sh
./configure --prefix=/usr/local/php5nginx --with-config-file-path=/usr/local/php5nginx/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
[[email protected]]# make ZEND_EXTRA_LIBS=‘-liconv‘
[[email protected]]# make install
[[email protected]]# ls /usr/local //确认支持php页面
--enable-fpm
D、创建php主配置文件
[[email protected] php-5.4.9]# cd php-5.4.9
[[email protected] php-5.4.9]# cp php.ini-production /usr/local/php5nginx/etc/php.ini
到此为止 nginx mysql php 安装完毕!
5)创建PHP文件并测试
# vim /usr/local/nginx/html/test.php
1 <?php
2 phpinfo();
3 ?>
http://localhost →正常情况能显示Welcome to nginx!
http://localhost/test.php →此时还不能翻译php页面,test.php会被当做文件来下载【接下来会解决这个问题】
二、整合Nginx和Fast-cgi
目标:能翻译php页面
A.什么是cgi
*(公共)cgi是通用网关接口CommonGateway Interface,是HTTP服务器与你的其他机器上的程序进行 交谈的一种工具;
* 程序须运行在网络服务器上;
* CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。
B.什么是fast-cgi
CGI的升级版,开启预先等待用户访问的进程,由fpm管理
配置fpm服务并启动
[[email protected]~]# cd /usr/local/php5nginx/etc/
[[email protected]]# cp php-fpm.conf.default php-fpm.conf //生成fpm的配置文件
[[email protected]]# vim php-fpm.conf
[global]
pid= run/php-fpm.pid
error_log= log/php-fpm.log
log_level= error
daemonize= yes
[www]
user= www //所属者
group= www //所属组
listen= 127.0.0.1:9000 //fast-cgi默认监听本机9000端口
pm= dynamic
pm.max_children= 32 //每个进程运行起来默认有32个子进程
pm.start_servers= 15 //15个进程
pm.min_spare_servers= 5 //最小空闲进程数是5
pm.max_spare_servers= 32 //最大空闲进程数是32
[[email protected]]# useradd -s /sbin/nologin -M www //添加进程用户
[[email protected]]# cd /root/Desktop/php-5.4.9/sapi/fpm/
[[email protected]]# cp init.d.php-fpm /etc/rc.d/init.d/php-fpm //生成fpm的启动文件
[[email protected]]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected]]# chkconfig --add php-fpm
[[email protected]]# chkconfig --level 35 php-fpm on
[[email protected]]# service php-fpm start //启动
Startingphp-fpm done
[[email protected]]# netstat -antup | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 5365/php-fpm
使nginx支持fpm服务
[[email protected]~]# vim /usr/local/nginx/conf/nginx.conf //去掉以下配置的注释符号开启
location~ \.php$ {
// ~是正则表达式匹配的意思当有人访问.php结尾的时候,就把请求发给下面的fastcgi
roothtml;
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params; //Fast-CGI参数文件
}
[[email protected]~]# vim /usr/local/nginx/conf/fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;//加上这句变量名变量值
[[email protected]~]# vim /usr/local/nginx/sbin/nginx -t //测试
重启nginx和fpm(fast-cgi)
[[email protected]~]# /usr/local/nginx/sbin/nginx -s stop
[[email protected]~]# /usr/local/nginx/sbin/nginx
[[email protected]~]# service php-fpm restart
验证nginx能否解释php页面
http://192.168.1.3/test.php //能显示php页面则成功
测试php程序能否连接mysql数据库
[[email protected]]# netstat -antup | grep :3306
[[email protected]~]# vim /usr/local/nginx/html/link.php //编写测试文件
<?php
$links=mysql_connect("localhost","root","");
if($links){
echo "link dbok!!!";
}
else{
echo "link dbno!!!";
}
?>
[[email protected]~]# elinks --dump http://localhost/link.php → link db ok!!!
[[email protected]]# service mysqldd stop //停止数据库,再测试
[[email protected]~]# elinks --dump http://localhost/link.php → link db no!!!
三、整合LNMP和memcached
1)安装memcached (memcached也可以装在单独的服务器上)
安装事件库、指定libevent事件库文件位置
[[email protected]]# tar -zxf libevent-2.0.21-stable.tar.gz //先安装事件库
[[email protected]]# cd libevent-2.0.21-stable
[[email protected]]# ./configure
[[email protected]]# make && make install
[[email protected]]# echo /usr/local/lib > /etc/ld.so.conf.d/libevent.conf
[[email protected]]# ldconfig
[[email protected]]# tar -zxf memcached-1.4.5.tar.gz
[[email protected]]# cd memcached-1.4.5
[[email protected]]# ./configure
[[email protected]]# make && make install
[[email protected] memcached-1.4.5]# /usr/local/bin/memcached -u root -m100 -c 200 -n 10 -f 2 -d
[[email protected]]# netstat -anptu | grep :11211
2)使php程序能够连接到memcached
在php(LNMP)服务器上必须装memcache软件包,php需要调用memcache.so模块才能连接memcached 服务器;
[[email protected] Desktop]# tar -zxf memcache-2.2.5.tgz
[[email protected]]# cd memcache-2.2.5
[[email protected]]# /usr/local/php5nginx/bin/phpize //生成配置configure
[[email protected]]# ./configure --with-php-config=/usr/local/php5nginx/bin/php-config--enable-memcache
[[email protected]]# make && make install
Installingshared extensions: /usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/ //模块目录路径,需要复制下来
3)编辑php的主配置文件,使之调用memcache.so模块
vim /usr/local/php5nginx/etc/php.ini
extension_dir= //模块文件的位置
extension=memcache.so //模块名
[[email protected]~]# vim /usr/local/php5nginx/etc/php.ini
extension_dir=“/usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/”//加上路径
extension=memcache.so
[[email protected]~]# service php-fpm restart
4)写测试文件并测试php能否使用memcached
[[email protected]~]# cat /usr/local/nginx/html/test2.php
<?php
$memcache=newMemcache; //创建memcache对象
$memcache->connect(‘localhost‘,11211)or die (‘could not connect!!‘); //写memcached服务器的IP和端口号
$memcache->set(‘key‘,‘test‘); //定义变量
$get_values=$memcache->get(‘key‘); //获取变量值
echo$get_values;
?>
[[email protected]~]# elinks --dump http://localhost/test.php
5)使nginx支持memcached
[[email protected]~]# vim /usr/local/nginx/conf/nginx.conf
Server{ //一个Server容器代表一个web网站,进去后在server容器里把没有的添加进去
server_namenginx.tarena.com; //此项需做DNS解析,可以去掉此行
location/ { //插入如下内容
......
set$memcached_key $uri; //用uri路径定义变量
memcached_pass127.0.0.1:11211; //把请求发给memcached服务器,写memcached服务器的IP和11211,这里把memcached和LNMP都装在一台服务器上,所以写本地环回IP127.0.0.1
default_typetext/html; //指定缓存类型
error_page404 @fallback; //请求跳转标记
}
proxy_pass http://127.0.0.1:80; //在这里指定访问memcached服务器失败后,可以直接访问源站服务器组或单个web服务器
}
}
[[email protected]~]# pkill -9 ngnix
[[email protected]~]# /usr/local/nginx/sbin/nginx -t
[[email protected]~]# /usr/local/nginx/sbin/nginx
[[email protected]~]# elinks --dump http://localhost/test.php //测试能否访问显示test
如果能访问说明memcached能正常工作了。到此所有的安装配置已经完成!谢谢。
本文出自 “Dave-技术博客” 博客,请务必保留此出处http://davewang.blog.51cto.com/6974997/1857042
以上是关于部署网站运行平台LNMP + Memcached缓存的主要内容,如果未能解决你的问题,请参考以下文章