部署lnnp环境
案例要求:在服务器上用源码包部署lnmp环境,
准备工作:配置固定ip地址,安装开发工具开发库软件包组, 构建yum源
准备需要的源码包 将当前主机中rpm包提供的mysql、httpd停止并设置开机不启动。
安装源码的nginx软件包[root@localhost ~]# useradd -M -s /sbin/nologin www //生成nginx所有者www是源码包默认或nginx
[root@localhost ~]# cd /root/Desktop/lnmp+memcached/
[root@localhost nginx-1.2.0]# yum -y install pcre-devel pcre
[root@localhost lnmp+memcached]# tar -zxvf nginx-1.2.0.tar.gz
[root@localhost nginx-1.2.0]# ./configure \
> --prefix=/usr/local/nginx \
> --pid-path=/usr/local/nginx/nginx.pid \
> --user=www --group=www \
> --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@localhost nginx-1.2.0]# make && make install
2、安装源码mysql
(1) 安装编译工具
[root@localhost lnmp+memcached]# tar -zxvf cmake-2.8.10.2.tar.gz
[root@localhost cmake-2.8.10.2]# ./bootstrap --prefix=/usr/local/cmake
[root@localhost cmake-2.8.10.2]#make && make install
[root@localhost bin]# pwd
/usr/local/cmake/bin
[root@localhost bin]# ./cmake --version
cmake version 2.8.10.2 //出现版本号说明安装成功
(2) 安装源码mysql
[root@localhost lnmp+memcached]# useradd -M -s /sbin/nologin mysql //添加进程的所有者
[root@localhost lnmp+memcached]# tar -zxvf mysql-5.5.13.tar.gz
[root@localhost lnmp+memcached]# cd mysql-5.5.13
[root@localhost mysql-5.5.13]# /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@localhost mysql-5.5.13]# make && make install
[root@localhost data]# pwd
/usr/local/mysql/data
[root@localhost data]# ls
mysql test
[root@localhost mysql]# pwd
/usr/local/mysql
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql //初始化授权库
[root@localhost mysql]# chown -R mysql.mysql .
[root@localhost mysql]# cd /root/Desktop/lnmp+memcached/mysql-5.5.13
[root@localhost mysql-5.5.13]# cp support-files/my-medium.cnf /etc/my.cnf //创建主配置文件
[root@localhost mysql-5.5.13]# cp support-files/mysql.server /etc/init.d/mysldd //复制启动文件
[root@localhost mysql-5.5.13]# chmod +x /etc/init.d/mysldd
[root@localhost mysql-5.5.13]# /etc/init.d/mysldd start //启动服务
Starting MySQL.. [确定]
[root@localhost ~]# /usr/local/mysql/bin/mysql -hlocalhost -uroot -p //登录服务
[root@localhost ~]# vim /etc/profile //将命令的路径添加到系统环境PATH变量中。
export PATH=/usr/local/mysql/bin/:$PATH
[root@localhost ~]# vim /etc/ld.so.conf //指定mysql库文件头文件的位置
/usr/local/mysql/lib
[root@localhost ~]# ldconfig -v
3、安装PHP
(1): 安装php扩展包
[root@localhost lnmp+memcached]# tar -zxvf mhash-0.9.9.9.tar.gz // 函数库
[root@localhost mhash-0.9.9.9]# ./configure && make && make install
[root@localhost libiconv-1.13]# tar -zxvf libiconv-1.13.tar.g z//编码转换
[root@localhost libiconv-1.13]# ./configure && make && make install
[root@localhost lnmp+memcached]# tar -zxvf libmcrypt-2.5.8.tar.gz // 加密
[root@localhost lnmp+memcached]# cd libmcrypt-2.5.8
[root@localhost libmcrypt-2.5.8]# ./configure && make && make install
[root@localhost libmcrypt-2.5.8]# ldconfig –v
[root@localhost libmcrypt-2.5.8]# cd libltdl/
[root@localhost libmcrypt-2.5.8]# cd libltdl/
[root@localhost libltdl]# ./configure --with-gmetad --enable-gexec --enable-ltdl-install
[root@localhost libltdl]# make && make install
[root@localhost ~]# ln -sv /usr/local/lib/libmcrypt* /usr/local/
[root@localhost ~]# ln -sv /usr/local/lib/libmhash.* /usr/lib/
ln -sv /usr/local/lib/libiconv* /usr/lib/
[root@localhost ~]# ldconfig –v
(2) 安装PHP
[root@localhost lnmp+memcached]# tar -zxvf php-5.4.9.tar.gz
[root@localhost lnmp+memcached]# cd php-5.4.9
./configure \
--prefix=/usr/local/php5nginx \
--with-config-file-path=/usr/local/php5nginx/etc \
--with-mysql=/usr/local/mysql \
--with-myqsli=/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 \
:%s/\n// :%s/\\//
[root@localhost ~]# rpm -qa | grep -i ldap 编译前查看是否安装着两个包
[root@localhost ~]# rpm -qa | grep -i ssl
[root@localhost php-5.4.9]# make ZEND_EXTRA_LIBS='-liconv' && make install
[root@localhost php-5.4.9]# cp php.ini-production /usr/local/php5nginx/etc/php.ini //配置文件
[root@localhost ~]# vim /usr/local/nginx/html/test.php
<?php
phpinfo();
?>
http://nginx_ip/test.php
整合nginx和fast-cgi什么是cgi: 公共网关接口common gateway interface
HTTP服务器与你的或其他机器上的程序进行交谈的一种工具
程序必须运行在网络服务器上
CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出的环境变量如php perl…
Fast-cgi:fastCGI像是一个常驻(long-live)型的cgi,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次
支持分布式的运算,可以在网站服务器以外的主机上执行并且接收来自其他网站服务器的qingqiu
将CGI解释器进程保持在内存中,进行维护与调度
FastCGI技术目前支持语言有 PHP、C/C++ 、JAVA、 。。。。。。。。。。
配置管理fast-cgi进程的服务fpm
[root@localhost ~]# cd /usr/local/php5nginx/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf //生成主配置文件
[root@localhost etc]# grep -vE '^;|^$' php-fpm.conf
[global]
[www]
user = nobody
group = nobody
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5 //最大子进程数
pm.start_servers = 2//启动进程数
pm.min_spare_servers = 1 //最小空闲进程数
pm.max_spare_servers = 3 //最大空闲进程数
启动fpm服务
[root@localhost fpm]# pwd
/root/Desktop/lnmp+memcached/php-5.4.9/sapi/fpm
[root@localhost fpm]# cp init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@localhost fpm]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost fpm]# /etc/rc.d/init.d/php-fpm start | stop 启动脚本、关闭脚本
整合nginx胡fast-cgi :
当nginx服务器访问到.php结尾的文件时,把这样的请求,发给本机的9000端口。
Location 匹配用户访问url路径
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在配置文件中搜索9000(:/9000) 将那一段注释(#) 去掉
重启nginx、php-fpm
[root@localhost ~]# pkill -9 nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# /etc/init.d/php-fpm stop
[root@localhost ~]# /etc/init.d/php-fpm start
测试php能否连接到mysql数据库服务
[root@localhost ~]# vim /usr/local/nginx/html/link.php
<?php
$links=mysql_connect("localhost","root","");
if($links){
echo "links ok!!!";
}
else{
echo "links no!!!";
}
?>
[root@localhost ~]# elinks -dump http://nginx_ip /link.php
LNMP+MEMCACHED在LNMP服务器上部署memcached服务器 (libevent memcached)
[root@localhost ~]# cd /root/Desktop/lnmp+memcached/
[root@localhost lnmp+memcached]# tar -zxvf libevent-2.0.21-stable.tar.gz
[root@localhost lnmp+memcached]# cd libevent-2.0.21-stable
[root@localhost libevent-2.0.21-stable]# ./configure && make && make install
[root@localhost libevent-2.0.21-stable]# echo /usr/local/lib/ > /etc/ld.so.conf.d/libevent.conf
[root@localhost ~]# ldconfig
[root@localhost ~]# cd /root/Desktop/lnmp+memcached/
[root@localhost lnmp+memcached]# tar -zxvf memcached-1.4.5.tar.gz
[root@localhost lnmp+memcached]# cd memcached-1.4.5
[root@localhost memcached-1.4.5]# ./configure
[root@localhost memcached-1.4.5]# make && make install
[root@localhost ~]# /usr/local/bin/memcached -u root -m 100 -c 200 -n 10 -f 2 -d –vvv //启动服务
安装php程序连接memcached服务的软件包
[root@localhost ~]# cd /root/Desktop/lnmp+memcached/
[root@localhost lnmp+memcached]# tar -zxvf memcache-2.2.5.tgz
[root@localhost lnmp+memcached]# cd memcache-2.2.5
[root@localhost memcache-2.2.5]# /usr/local/php5nginx/bin/phpize //查看当前安装php的信息
[root@localhostmemcache-2.2.5]#./configure --with-php-config=/usr/local/php5nginx/bin/php-config --enable-memcached
[root@localhost memcache-2.2.5]# make && make install
/usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/ //显示模块安装路径
[root@localhost ~]# vim /usr/local/php5nginx/etc/php.ini
; extension_dir = "./"
/usr/local/php5nginx/lib/php/extensions/no-debug-non-zts-20100525/
extension=memcache.so
[root@localhost ~]# /etc/init.d/php-fpm restart //重启php-fpm服务 [root@localhost ~]# /usr/local/php5nginx/bin/php –m //查看加载的模块
[root@localhost ~]# /usr/local/php5nginx/bin/php -m | grep memcache
Memcache
测试php程序能否使用memcached服务器存储数据
[root@localhost ~]# vim /usr/local/nginx/html/lxg.php
<?php
$memcache=new Memcache;//创建memcache对象
$memcache->connect( 'localhost',11211) or die ( 'could not connect!!' );
$memcache->set('key','test'); //定义变量
$get_values=$memcache->get( 'key' ); //获取变量
echo $get_values;
?>
让nginx支持memcached服务
http {
upstream webgrp{
server 192.168.1.50:80;
}
server {
location / {
……………………………
set $memcached_key $uri; //用uri路径定义变量
memcached_pass 127.0.0.1:11211; //把请求发给memcached服务器
default_type text/html;
error_page 404 @fallback; //请求跳转标记
}
location @fallback{ //upstarem服务器
proxy_pass http://webgrp;
}
}
}