设为首页 收藏本站
查看: 1419|回复: 0

部署网站运行平台LNMP + Memcached缓存

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-28 09:07:05 | 显示全部楼层 |阅读模式
*本实验的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)
[iyunv@ser1~]# service httpd stop && chkconfig httpd off  //关闭自带的httpd
[iyunv@ser1~]# service mysqld stop && chkconfig mysqld off //关闭自带的mysql
[iyunv@ser1~]# mv /etc/my.cnf /etc/my.cnf.bak //移除mysql配置文件
[iyunv@ser1~]# yum groupinstall "开发工具" "开发库" "X 软件开发"
[iyunv@ser1~]# yum -y install pcre-devel zlib-developenssl-devel gcc gcc-c+++ make
2)源码包安装nginx
注:可用./configure --help 查看配置参数
[iyunv@ser1]# groupadd -r nginx //创建nginx
[iyunv@ser1]# useradd -r -g nginx -M nginx //创建nginx进程拥有者用户
[iyunv@ser1]# tar -zxf nginx-1.2.0.tar.gz
[iyunv@ser1]# cd nginx-1.2.0
[root@ser1nginx-1.2.0]# 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
[root@ser1nginx-1.2.0]# sh conf.sh  //执行脚本进行配置
[root@ser1nginx-1.2.0]# make && make install
[root@ser1nginx-1.2.0]# cd /usr/local/nginx/
[iyunv@ser1nginx]# ./sbin/nginx -c /usr/local/nginx/conf/nginx.conf //启动nginx
[iyunv@ser1nginx]# netstat -anptu | grep :80
[iyunv@ser1nginx]# elinks --dump http://localhost //测试能否访问


3)源码包安装mysql-5.5.13.tar.gz  需要用cmake编译工具
[iyunv@ser1Desktop]#groupadd mysql //添加mysql组
[iyunv@ser1Desktop]#useradd -M -s /sbin/nologin -g mysql mysql //添加mysql用户
[iyunv@ser1Desktop]# tar -zxf cmake-2.8.10.2.tar.gz
[iyunv@ser1Desktop]# cd cmake-2.8.10.2
[root@ser25pxake-2.8.10.2]# ./bootstrap  --prefix=/usr/local/cmake //指定安装目录
[root@ser25pxake-2.8.10.2]# make && make install
[root@ser25pxake-2.8.10.2]# /usr/local/cmake/bin/cmake –version //验证查看是否成功安装cmake编译工具
cmakeversion 2.8.10.2
[iyunv@ser1Desktop]# tar -zxf mysql-5.5.13.tar.gz
[iyunv@ser1Desktop]# cd mysql-5.5.13
[root@ser1mysql-5.5.13]# 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
[root@ser1mysql-5.5.13]# sh conf.sh
[root@ser1mysql-5.5.13]# make && make install

[iyunv@ser1 mysql]# cd /usr/local/mysql/
[iyunv@ser1mysql]# chown -R mysql:mysql .   //修改安装目录mysql权限,所有者和所属组都改为mysql,作用是让所有用户可以访问mysql
[iyunv@ser1mysql]# ./scripts/mysql_install_db  --user=mysql  //初始化授权库
[iyunv@ser1mysql]# ls /usr/local/mysql/data  //查看数据库初始化是否成功
  mysql   performance_schema   test

[iyunv@ser1mysql]# cp./support-files/my-medium.cnf /etc/my.cnf  //生成配置文件
[iyunv@ser1mysql]# cp ./support-files/mysql.server /etc/init.d/mysqldd  //生成启动脚本
[iyunv@ser1mysql]# chmod +x /etc/init.d/mysqldd   //给文件加执行权限
[iyunv@ser1mysql]# chkconfig --add mysqldd     
[iyunv@ser1mysql]# service mysqldd start
[iyunv@ser1mysql]# chkconfig --level 35 mysqldd on
[iyunv@ser1mysql]# ./bin/mysql -uroot -p  //测试连接   
把源码mysql提供命令的目录添加到PATH里:
[iyunv@ser1~]# cat /etc/bashrc        //添加命令搜索路径
export  PATH=/usr/local/mysql/bin:$PATH //指定PATH为全局变量,添加到未尾
[iyunv@ser1~]# source /etc/bashrc      //重新加载该文件
[iyunv@ser1~]# mysql -uroot -p        //用此方式测试连接数据库

[iyunv@ser1~]# cat /etc/ld.so.conf
includeld.so.conf.d/*.conf
/usr/local/mysql/lib            //指定mysql库文件的位置
[iyunv@ser1~]# ldconfig -v       //重新加载系统内的头文件和库文件信息
[iyunv@ser1~]# ldconfig -v | grep mysql --color  //查看
4源码包安装PHP
A.需安装PHP扩展包:mhash、libiconv 、libmcrypt
加密时需要libltdl的支持,因此libmcrypt中集成了安装libltdl的文件
[iyunv@ser1 Desktop]# tar-zxf mhash-0.9.9.9.tar.gz   //安装哈稀函数库,做哈稀运算
[iyunv@ser1 Desktop]# cdmhash-0.9.9.9
[iyunv@ser1 mhash-0.9.9.9]#./configure
[iyunv@ser1 mhash-0.9.9.9]#make && make install
[root@ser1mhash-0.9.9.9]#cd -
[iyunv@ser1 Desktop]# tar-zxf libiconv-1.13.tar.gz   //处理中文各种编码之间的转换
[iyunv@ser1 Desktop]# cd libiconv-1.13
[iyunv@ser1 libiconv-1.13]#./configure
[iyunv@ser1 libiconv-1.13]#make && make install
[root@ser1libiconv-1.13]#cd –
[iyunv@ser1 Desktop]# tar-zxf libmcrypt-2.5.8.tar.gz  //提供加密功能的库文件
[iyunv@ser1 Desktop]# cdlibmcrypt-2.5.8
[root@ser1libmcrypt-2.5.8]# ./configure
[root@ser1libmcrypt-2.5.8]#make && make install
[root@ser1libmcrypt-2.5.8]# ldconfig -v
[root@ser1libmcrypt-2.5.8]# cd libltdl/
[iyunv@ser1libltdl]#./configure --with-gmetad --enable-gexec --enable-ltdl-install
//配置libltdl
[iyunv@ser1 libltdl]# make&& make install

B、指定扩展包的库文件位置(即对库文件夹做链接)
[iyunv@ser1~]# ln -sv /usr/local/lib/libmcrypt* /usr/lib
[iyunv@ser1~]# ln -sv /usr/local/lib/mhash* /usr/lib
[iyunv@ser1~]# ln -sv /usr/local/lib/libiconv* /usr/lib
[iyunv@ser1~]# ldconfig -v  //更新库链接
C、安装PHP
配置--enable-fpm
编译make ZEND_EXTRA_LIBS=’-liconv’、安装
[iyunv@ser1 Desktop]# tar -zxf php-5.4.9.tar.gz
[iyunv@ser1Desktop]# cd php-5.4.9
[root@ser1php-5.4.9]# 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
[root@ser1php-5.4.9]# make ZEND_EXTRA_LIBS='-liconv'
[root@ser1php-5.4.9]# make install
[root@ser1php-5.4.9]# ls /usr/local //确认支持php页面
--enable-fpm
D创建php主配置文件
[iyunv@ser1 php-5.4.9]# cd php-5.4.9
[iyunv@ser1 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会被当做文件来下载【接下来会解决这个问题】

二、整合NginxFast-cgi
目标:能翻php页面
A.什么是cgi
*(公共)cgi是通用网关接口CommonGateway Interface,是HTTP服务器与你的其他机器上的程序进行  交谈的一种工具;
* 程序须运行在网络服务器上;
* CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。
B.什么是fast-cgi
   CGI的升级版,开启预先等待用户访问的进程,由fpm管理

配置fpm服务并启动
[iyunv@ser1~]# cd /usr/local/php5nginx/etc/
[iyunv@ser1etc]# cp php-fpm.conf.default  php-fpm.conf  //生成fpm的配置文件
[iyunv@ser1etc]# 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
[iyunv@ser1etc]# useradd -s /sbin/nologin -M www   //添加进程用户

[iyunv@ser1etc]# cd /root/Desktop/php-5.4.9/sapi/fpm/
[iyunv@ser1fpm]# cp init.d.php-fpm /etc/rc.d/init.d/php-fpm  //生成fpm的启动文件
[iyunv@ser1fpm]# chmod +x /etc/rc.d/init.d/php-fpm
[iyunv@ser1fpm]# chkconfig --add php-fpm
[iyunv@ser1fpm]# chkconfig --level 35 php-fpm on
[iyunv@ser1fpm]# service php-fpm start   //启动
Startingphp-fpm  done
[iyunv@ser1fpm]# netstat -antup | grep :9000
tcp        0     0 127.0.0.1:9000      0.0.0.0:*    LISTEN      5365/php-fpm

使nginx支持fpm服务
[iyunv@ser1~]# 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参数文件
}
[iyunv@ser1~]# vim /usr/local/nginx/conf/fastcgi_params
fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;//加上这句变量名变量值
[iyunv@ser1~]# vim /usr/local/nginx/sbin/nginx -t   //测试

重启nginx和fpm(fast-cgi)
[iyunv@ser1~]# /usr/local/nginx/sbin/nginx -s stop
[iyunv@ser1~]# /usr/local/nginx/sbin/nginx
[iyunv@ser1~]# service php-fpm restart

验证nginx能否解释php页面
http://192.168.1.3/test.php   //能显示php页面则成功

测试php程序能否连接mysql数据
[iyunv@PEX-Servernginx]# netstat -antup | grep :3306
[iyunv@ser1~]# vim /usr/local/nginx/html/link.php //编写测试文件
<?php
$links=mysql_connect("localhost","root","");
if($links){
echo "link dbok!!!";
}
else{
echo "link dbno!!!";
}
?>
[iyunv@ser1~]# elinks --dump http://localhost/link.php   → link db ok!!!
[iyunv@PEX-Serverhtml]# service mysqldd stop          //停止数据库,再测试
[iyunv@ser1~]# elinks --dump http://localhost/link.php   → link db no!!!

三、整合LNMP和memcached
1)安装memcached (memcached也可以装在单独的服务器上)
安装事件库、指定libevent事件库文件位置
[iyunv@ser1Desktop]# tar -zxf libevent-2.0.21-stable.tar.gz //先安装事件库
[iyunv@ser1Desktop]# cd libevent-2.0.21-stable
[root@ser1libevent-2.0.21-stable]# ./configure
[root@ser1libevent-2.0.21-stable]# make && make install
[root@ser1libevent-2.0.21-stable]# echo /usr/local/lib > /etc/ld.so.conf.d/libevent.conf
[root@ser1libevent-2.0.21-stable]# ldconfig

[iyunv@ser1Desktop]# tar -zxf memcached-1.4.5.tar.gz
[iyunv@ser1Desktop]# cd memcached-1.4.5
[root@ser1memcached-1.4.5]# ./configure
[root@ser1memcached-1.4.5]# make && make install
[iyunv@ser1 memcached-1.4.5]# /usr/local/bin/memcached -u root -m100 -c 200 -n 10 -f 2 -d
[root@ser1memcached-1.4.5]# netstat -anptu | grep :11211
2)使php程序能够连接到memcached    
在php(LNMP)服务器上必须装memcache软件包,php需要调用memcache.so模块才能连接memcached  服务器;
[iyunv@ser1 Desktop]# tar -zxf memcache-2.2.5.tgz
[iyunv@ser1Desktop]# cd memcache-2.2.5
[root@ser1memcache-2.2.5]# /usr/local/php5nginx/bin/phpize    //生成配置configure
[root@ser1memcache-2.2.5]# ./configure  --with-php-config=/usr/local/php5nginx/bin/php-config--enable-memcache
[root@ser1memcache-2.2.5]# 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    //模块名
[iyunv@ser1~]# vim /usr/local/php5nginx/etc/php.ini
extension_dir=“/usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525///加上路径
extension=memcache.so
[iyunv@ser1~]# service php-fpm restart
4)写测试文件测试php能否使用memcached
[iyunv@ser1~]# 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;
?>
[iyunv@ser1~]# elinks --dump http://localhost/test.php
5)使nginx支持memcached
[iyunv@ser1~]# 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;       //请求跳转标记
}
location@fallback {
proxy_pass http://127.0.0.1:80;   //在这里指定访问memcached服务器失败后,可以直接访问源站服务器组或单个web服务器
}
}
[iyunv@ser1~]# pkill -9 ngnix
[iyunv@ser1~]# /usr/local/nginx/sbin/nginx -t
[iyunv@ser1~]# /usr/local/nginx/sbin/nginx  
[iyunv@ser1~]# elinks --dump http://localhost/test.php //测试能否访问显示test


如果能访问说明memcached能正常工作了。到此所有的安装配置已经完成!谢谢。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-278496-1-1.html 上篇帖子: LAMP环境搭建 下篇帖子: LAMP之mysql的安装 网站
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表